定时中断
#include "stm32f10x.h" static GPIO_InitTypeDef GPIO_InitStructure; static NVIC_InitTypeDef NVIC_InitStructure; static TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; #define PB5OUT(n) *(volatile uint32_t *)(0x42000000+((uint32_t)&GPIOB->ODR-0x40000000)*32+n*4) void led0(void){ //led0-->PB5 //使能端口 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //初始化GPIO引脚 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;//高速响应 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB,GPIO_Pin_5); } void TIM2init(void){ //使能定时器2硬件时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //配置定时器2 分配值,计数值等 TIM_TimeBaseStructure.TIM_Period = 10000-1;//计数值 TIM_TimeBaseStructure.TIM_Prescaler = 7200-1;//预分频器7100-1+1,隐形着一个+1操作,所以要减一 TIM_TimeBaseStructure.TIM_ClockDivision = 0;//这个用不到 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //配置定时器2中断的触发方式 TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //配置定时器2的中断优先级 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //使能定时器2 TIM_Cmd(TIM2, ENABLE); } void TIM2_IRQHandler(void){ if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET) {PB5OUT(5)^=1 ; TIM_ClearITPendingBit(TIM2,TIM_IT_Update); } } int main(){ led0(); TIM2init(); while(1){ } }
微信扫码关注
更新实时通知