想写一个程序,每隔一秒,就进行中断,把LED灯打开,一秒之后,中断进来,熄灭LED灯,依次循环
main函数:
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
void RCC_Config(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
}
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_Config()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
TIM_TimeBaseStruct.TIM_Period=2000;
TIM_TimeBaseStruct.TIM_Prescaler=35999;
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
int main(void)
{
RCC_Config();
LED_GPIO_Config();
NVIC_Config();
TIM3_Config();
while(1) ;
}
中断服务程序:
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
if(GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_4)==Bit_RESET)
{
GPIO_SetBits(GPIOC,GPIO_Pin_4); //熄灭
}
else
{
GPIO_ResetBits(GPIOC,GPIO_Pin_4); //发光
}
} |