STM8-利用TIM2的定时功能,实现灯每隔1S闪烁
时间:01-22 09:38 阅读:1681次
*温馨提示:点击图片可以放大观看高清大图
简介:本文给大家分享了STM8-利用TIM2的定时功能,实现灯每隔1S闪烁的代码。
#include"stm8s.h"
#include "stm8s_gpio.h"
#include "stm8s_tim2.h"
void CLK_Configuration();
void TIM2_Configuration_set_1ms(void);
void GPIO_Configuration();
void main()
{
CLK_Configuration();
TIM2_Configuration_set_1ms();
GPIO_Configuration();
enableInterrupts();//使能总中断
while(1)
{
}
}
void CLK_Configuration()
{
CLK_DeInit();
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//HSI16MHZ,1分频
}
void TIM2_Configuration_set_1ms(void)
{
TIM2_DeInit();
TIM2_TimeBaseInit( TIM2_PRESCALER_16,1000);
TIM2_ITConfig( TIM2_IT_UPDATE , ENABLE);
TIM2_SetCounter(0x0000);
TIM2_Cmd(ENABLE);
}
void GPIO_Configuration()
{
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST);
}
#pragma vector=13+2
__interrupt void TIM2_IRQ()
{
static unsigned int j;
j++;
if(j==1000)
{
j=0;
GPIO_WriteReverse(GPIOD, GPIO_PIN_0);//1s取一次反
}
TIM2_ClearITPendingBit(TIM2_IT_UPDATE); //清中断标志位
}