本文的copyright归yuweixian4230@163.com 所有,使用GPL发布,可以自由拷贝,转载。
但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。
作者:yuweixian4230@163.com
博客:yuweixian4230.blog.chinaunix.net
附件: 18tim3外部计数器.rar
设置tim3 为外部计数器模式,从信号发生器中输入 100hz 的方波, tim3计数器中每100个方波产生一个中断
- void test(void)
-
{
-
GPIO_InitTypeDef GPIO_InitStructure;
-
NVIC_InitTypeDef NVIC_InitStructure;
-
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
-
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
-
GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
/************************************************/
-
-
-
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //TIM3时钟配置选择
-
//TIM3输出初始化
-
TIM_DeInit(TIM3); //TIM3 ch2 pin17 pa7
-
-
TIM_TimeBaseStructure.TIM_Period = 99; //计数值=100-1
-
TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频
-
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分割
-
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; //模式
-
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //基本初始化
-
-
TIM_SetCounter(TIM3, 99); //设置 TIM3 计数器寄存器值
-
TIM_ARRPreloadConfig(TIM3, ENABLE); //设置 TIMx 自动重装载寄存器值
-
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
-
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_External1);
-
-
//TIM3通道的捕捉初始化
-
TIM_SetCompare2(TIM3, 99);
-
TIM_Cmd(TIM3, ENABLE); //启动TIM3
-
TIM_ClearFlag(TIM3, TIM_IT_CC2);
-
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); //打开中断
-
-
-
//#if 0
-
#ifdef VECT_TAB_RAM
-
/* Set the Vector Table base location at 0x20000000 */
-
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
-
#else /* VECT_TAB_FLASH */
-
/* Set the Vector Table base location at 0x08000000 */
-
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
-
#endif
-
//#endif
-
-
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//抢占式优先级别, 无抢占优先级
-
-
-
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //
-
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级0
-
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级0
-
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //允许中断
-
NVIC_Init(&NVIC_InitStructure); //写入设置
-
}
阅读(2218) | 评论(0) | 转发(0) |