新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM8-利用TIM2的定時(shí)功能,實(shí)現(xiàn)燈每隔1S閃爍

STM8-利用TIM2的定時(shí)功能,實(shí)現(xiàn)燈每隔1S閃爍

作者: 時(shí)間:2016-11-24 來(lái)源:網(wǎng)絡(luò) 收藏
目的:熟悉定時(shí)器的定時(shí)功能的使用(自動(dòng)重裝)
#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)志位

}


關(guān)鍵詞: STM8TIM2定時(shí)功

評(píng)論


技術(shù)專區(qū)

關(guān)閉