STM8-利用TIM2的定時(shí)功能,實(shí)現(xiàn)燈每隔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); //清中斷標(biāo)志位
}
#include "stm8s_gpio.h"
#include "stm8s_tim2.h"
void CLK_Configuration();
void TIM2_Configuration_set_1ms(void);
void GPIO_Configuration();
void main()
{
enableInterrupts();//使能總中斷
}
void CLK_Configuration()
{
}
void TIM2_Configuration_set_1ms(void)
{
}
void GPIO_Configuration()
{
}
#pragma vector=13+2
__interrupt void TIM2_IRQ()
{
}
評(píng)論