STM8L152K4 定時器中斷 初學者
本程序使用定時器中斷4,每5ms產生一個中斷,并在timecreat函數中進行計時,在主函數的while中,通過if語句判斷定時器中斷時間后,對gpio口進行相應操作,函數如下:
#include "stm8l15x.h"
#include "Define.h"
//以下定義LED與按鍵的接口
#define LED1_GPIO_PORT GPIOC //BLUE
#define LED1_GPIO_PINS GPIO_Pin_4
#define LED2_GPIO_PORT GPIOB //RED
#define LED2_GPIO_PINS GPIO_Pin_0
#define LED3_GPIO_PORT GPIOD //GREEN
#define LED3_GPIO_PINS GPIO_Pin_2
#define KEY1_GPIO_PORT GPIOD //K1
#define KEY1_GPIO_PINS GPIO_Pin_4
#define KEY2_GPIO_PORT GPIOA //K2
#define KEY2_GPIO_PINS GPIO_Pin_2
#define KEY3_GPIO_PORT GPIOB //K3
#define KEY3_GPIO_PINS GPIO_Pin_3
u8 vTcount1,vTcount2;
//unsigned char flag16ms = 0;
ramTime_srtuct ramTime;
void TimeCreate()
{
if(ramTime._5MS)//5ms中斷計數一次
{
ramTime._5MS=0;
//pKeyP.TIME_KEY++;
vTcount1++;
if(vTcount1 >19)//有個邏輯問題不太理解,即在此處定義了時間,在后續執行調用變量時,是順序調用執行還是直接調用執行
{
vTcount1 = 0;
vTcount2++;//到達500ms
if(vTcount2==5)ramTime._500MS = 1;
else if(vTcount2==7)ramTime._700MS = 1;//到達700ms
else if(vTcount2==10)
{
ramTime._1S= 1;//到達1s
vTcount2=0;
}
}
}
}
/*GPIO初始化*/
void gpio_init()
{
// GPIO_Init(LED1_GPIO_PORT, LED1_GPIO_PINS, GPIO_Mode_Out_PP_High_Fast);//初始化LED1,GPC4低速推挽輸出
//GPIO_Init(KEY1_GPIO_PORT, KEY1_GPIO_PINS, GPIO_Mode_In_PU_IT);//初始化按鍵1,上拉中斷輸入
GPIO_Init(LED1_GPIO_PORT, LED1_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);//帶上拉,推挽輸出低電平
GPIO_Init(LED2_GPIO_PORT, LED2_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);//帶上拉,推挽輸出低電平
GPIO_Init(LED3_GPIO_PORT, LED3_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);//帶上拉,推挽輸出低電平
//初始化3個按鍵接口
GPIO_Init(KEY1_GPIO_PORT, KEY1_GPIO_PINS, GPIO_Mode_In_PU_No_IT);//初始化按鍵,GPD4上拉輸入
GPIO_Init(KEY2_GPIO_PORT, KEY2_GPIO_PINS, GPIO_Mode_In_PU_No_IT);//初始化按鍵,GPA2上拉輸入
GPIO_Init(KEY3_GPIO_PORT, KEY3_GPIO_PINS, GPIO_Mode_In_PU_No_IT);//初始化按鍵,GPB3上拉輸入
}
/*定時器初始化*/
void timer4_init()
{
CLK_PeripheralClockConfig (CLK_Peripheral_TIM4,ENABLE); //使能外設時鐘,STM8L默認所有外設時鐘初始時關閉,使用前徐開啟
TIM4_TimeBaseInit(TIM4_Prescaler_128, 200);//..原為0xffff,,,,16M/8/128=15.625K,0xff=255,255*(1/15.625)=0.01632S,大約61次中斷是1S
TIM4_ITConfig(TIM4_IT_Update, ENABLE);//向上溢出中斷,中斷函數向量號為25
TIM4_Cmd(ENABLE);
}
/*void exti_init()
{
EXTI_DeInit ();
EXTI_SetPinSensitivity (EXTI_Pin_4,EXTI_Trigger_Falling);
}*/
/*按鍵操作函數*/
void Dokey()
{
int key1;
static unsigned int key_press_time = 0; // ……請記得標為靜態變量
key1=GPIO_ReadInputDataBit(KEY1_GPIO_PORT,KEY1_GPIO_PINS);
if(key1 == 0)
{
if(++key_press_time <=0)--key_press_time;//計量按鍵時間,并避免數據溢出
if(key_press_time==3000)//長按三秒
{
GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PINS);
}
}else
{
if(20<=key_press_time && key_press_time < 3000)//大于20ms小于3s
{
GPIO_SetBits(LED2_GPIO_PORT,LED2_GPIO_PINS);
}
key_press_time=0;
}
}
/*主函數*/
int main(void)
{
//exti_init();
timer4_init();
gpio_init();
enableInterrupts();//使能中斷總開關
ramTime._5MS=0;//srtuct變量,在define.h函數中定義,用于時間的調用
ramTime._100MS=0;
ramTime._500MS=0;
ramTime._700MS=0;
ramTime._1S=0;
// TimeCreate();
while(1)
{
// GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PINS);
// GPIO_SetBits(LED2_GPIO_PORT,LED2_GPIO_PINS);
// GPIO_SetBits(LED3_GPIO_PORT,LED3_GPIO_PINS);
if(ramTime._500MS)
{
ramTime._500MS = 0;
GPIO_ToggleBits(LED2_GPIO_PORT,LED2_GPIO_PINS);
//Dokey();
}
if(ramTime._1S)
{
ramTime._1S = 0;
GPIO_ToggleBits(LED2_GPIO_PORT,LED2_GPIO_PINS);
}
}
}
Define.h函數
#ifndef __DEFINE_H
#define __DEFINE_H
#include "stm8l15x.h"
typedef struct
{
u8 _5MS;
u8 _100MS;
u8 _500MS;
u8 _700MS;
u8 _1S;
u8 _1000mS;
} ramTime_srtuct;
定時器4中斷函數
extern ramTime_srtuct ramTime;
u8 cnt5ms;
INTERRUPT_HANDLER(TIM4_UPD_OVF_TRG_IRQHandler,25)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
/*i++;
if(i==61)
{
GPIO_ToggleBits(GPIOC, GPIO_Pin_4);//翻轉GPD4輸出狀態
i=0;
}*/
TIM4_ClearITPendingBit(TIM4_IT_Update);
//flag16ms ++;
cnt5ms++;
if(cnt5ms>24)
{
cnt5ms=0;
ramTime._5MS=1;
}
}
以上為函數部分,但該有的中斷效果沒出來,不太會調,有大神指導下嗎!!
|