Main ()
{
#include “stm32f10x.h”
#include “stdio.h”
float Frequency_value;
#define LED_ON GPIO_ResetBits(GPIOA,GPIO_Pin_12)
#define LED_OFF GPIO_SetBits(GPIOA,GPIO_Pin_12)
Void TIM2_Configuart(void)
{ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockClockCmd(RCClock/TIMB1Enable its clock/TIMB1)
TIM_TimeBaseStructure.TIM_Period = 400-1; //设置重装载寄存器的周期
TIM_TimeBaseStructure.TIM_Prescaler = 36000-1; //设置时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上的计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //TIM3的初始化
TIM_ETRClockMode2Config(TIM3,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0);
// TIM_ExtTRGPSC_OFF :ETRP 预分频 OFF
// TIM_ExtTRGPolarity_NonInverted : TIM外部触发极性非翻转:高电平或者上升沿有效
TIM_SetCounter(TIM3,0);
TIM_Cmd(TIM2, ENABLE); //使能TIM3
}
Void TIM3_Configuart(void)
{ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//Enable its clock
// TIM_Period | TIM_Prescaler 16位
TIM_TimeBaseStructure.TIM_Period = 0XFFFF - 1; //设置重装载寄存器的周期
TIM_TimeBaseStructure.TIM_Prescaler = 0X00; //设置时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上的计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //TIM3的初始化
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //TIM3更新中断使能
TIM_Cmd(TIM2, ENABLE); //使能TIM2
}
Void IO_Configuart(void)
{ GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//Enable the clock where it is GPIO_InitStructure.GPIO_Pin = GPIO_Pin_PP; GPIO_Speed_Out_Mhz = GPIO_Speed_Out //Set the speed GPIO_Init(GPIOA,&GPIO_InitStructure);
}
Void NVIC_ Configuart (void)
{ NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; // interrupt TIM2 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 0 preemptive priority NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // priority from the account. 3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel enable NVIC_Init(&NVIC_InitStructure); //Initialize NVIC register } void USART1_Configuart(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //Set the pin TX | RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //Set the serial communication mode
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//Set the speed
GPIO_Init ; InitStructure,&GPIO
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardWareFlowControl = USART_HardWareFlowControl_None; // 无 流 控
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 使 能 发送 和 接收
USART_Init (USART1, & USART_InitStructure);
USART_Cmd (USART1, ENABLE);
}
int fputc (int ch, FILE * f)
{ USART_SendData (USART1, (unsigned char) ch); while (USART_GetFlagStatus (USART1, USART_FLAG_TC) == RESET) {} return ch; } void Delay_ms (void) {
long int i,j;
for (i = 0; i < 10000; i++)
for (j = 0; j < 1000; j++);
}
int main(void)
{
TIM3_Configuart();
TIM2_Configuart();
IO_Configuart();
NVIC_ Configuart();
USART1_Configuart();
While(1)
{ LED_ON; printf("\nHello!\n"); printf("\n%f",Frequency_value); Delay_ms(); LED_OFF; Delay_ms(); } Return 0; } / stm32f10x_it.c * *******/ //Timer 3 interrupt service routine void TIM3_IRQHandler(void) //TIM3 interrupt { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //Check whether the TIM3 update interrupt occurs or not { TIM_ClearITPendingBit( TIM2, TIM_IT_Update ); //Clear TIMx update interrupt flag Frequency_value = TIM_GetCounter(TIM3)/0.2; //Period 200ms TIM_SetCounter(TIM3,0);//Count numbers from 0 } / stm32f10x_it.c ****** **/ / stm32f10x_it.h ********/
Extern float Frequency_value;
void TIM2_IRQHandler(void);
/stm32f10x_it.h********/