1 #include "stm32f10x.h" 2 #include "stm32f10x_tim.h" 3 #include "misc.h" 4 #include "stm32f10x_gpio.h" 5 6 unsigned int CCR2_Val=45000; 7 extern void time_ini(void); 8 unsigned int capture1=0; 9 TIM_TimeBaseInitTypeDef TIM3_TimeBaseStructure; 10 TIM_OCInitTypeDef TIM3_OCInitStructure; 11 TIM_BDTRInitTypeDef TIM3_BDTRInitStructure; 12 13 void time_ini(void){ 14 GPIO_InitTypeDef GPIO_InitStructure; 15 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //定时器3 时钟使能 16 17 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PB5复用为TIM3的通道2 18 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 19 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 20 GPIO_Init(GPIOB, &GPIO_InitStructure); 21 22 /* TIM3局部复用功能开启 在TIM3的局部复用开启时,PB5会被复用为TIM3_CH2*/ 23 GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3 , ENABLE); 24 25 /* Time Base configuration */ 26 /*------------------------------------------------------------------- 27 TIM3CLK=72MHz 预分频系数Prescaler=63 经过分频 定时器时钟为1.125MHz 28 捕获/比较寄存器2 TIM3_CCR2= CCR2_Val 29 2通道产生的更新频率是=1.125MHz/CCR2_Val=25Hz 30 31 -------------------------------------------------------------------*/ 32 TIM3_TimeBaseStructure.TIM_Prescaler = 630; //预分频器TIM3_PSC=630 33 TIM3_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器向上计数模式 TIM3_CR1[4]=0 34 TIM3_TimeBaseStructure.TIM_Period =0xffff; //自动重装载寄存器TIM3_APR 35 TIM3_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分频因子 TIM3_CR1[9:8]=00 36 37 TIM_TimeBaseInit(TIM3,&TIM3_TimeBaseStructure); //写TIM3各寄存器参数 38 39 TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ; //TIM3_CCMR1[14:12]=011 翻转 当TIM3_CCR2=TIM3_CNT时,翻转OC2REF的电平 40 TIM3_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输入/捕获2输出允许 OC2信号输出到对应的输出引脚PB5 41 TIM3_OCInitStructure.TIM_Pulse =CCR2_Val; //若CC1通道配置为输出:CCR2是装入当前捕获/比较2 TIM3_CCR2寄存器的值(预装载值)。 42 //当前捕获/比较寄存器包含了与计数器TIM3_CNT比较的值,并且在OC端口上输出信号 43 TIM3_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性 低电平有效 TIM3_CCER[5]=1; 44 45 TIM_OC2Init(TIM3, &TIM3_OCInitStructure); 46 TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable); //TIM3_CCMR1[1]=0 禁止TIM3_CCR2寄存器的预装载功能,可随时写入TIM3_CCR2 47 //且新值马上起作用 48 49 TIM_Cmd(TIM3,ENABLE); //启动定时器3 TIM3_CR1[0]=1; 50 TIM_ITConfig(TIM3,TIM_IT_CC2,ENABLE); //TIM3_DIER[2]=1 允许捕获/比较2中断 51 } 52 53 void RCC_Configuration(void){ 54 55 SystemInit(); 56 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 57 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC 58 | RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE , ENABLE); 59 } 60 61 void NVIC_Configuration(void) 62 { 63 NVIC_InitTypeDef NVIC_InitStructure; 64 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); 65 66 NVIC_InitStructure.NVIC_IRQChannel =TIM3_IRQn ; //配置定时器中断 67 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 68 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 69 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 70 NVIC_Init(&NVIC_InitStructure); 71 } 72 73 int main(void) 74 { 75 RCC_Configuration(); //系统时钟设置及外设时钟使能 76 NVIC_Configuration(); //中断源配置 77 time_ini(); //定时器3的初始化 78 while(1); 79 } 80 81 void TIM3_IRQHandler(void){ 82 83 if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET) //判断状态寄存器 TIM3_SR[2] 是否发生了捕获/比较2 中断 84 { 85 TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); //软件清除状态寄存器 TIM3_SR[2] 捕获/比较2 中断中断标志 86 87 capture1 = TIM_GetCapture2(TIM3); //获取TIM3_CCR2的预装值 88 TIM_SetCompare2(TIM3, capture1+ CCR2_Val); //和CCR2_Val累加后装入TIM3_CCR2 这是为了配合计数器值的递增。以在下一次作出比较。 89 90 } 91 92 }