找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2100|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

請教下STM32定時(shí)器中斷的使用

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:417546 發(fā)表于 2020-8-31 23:17 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
STM8L152K4  定時(shí)器中斷  初學(xué)者
本程序使用定時(shí)器中斷4,每5ms產(chǎn)生一個(gè)中斷,并在timecreat函數(shù)中進(jìn)行計(jì)時(shí),在主函數(shù)的while中,通過if語句判斷定時(shí)器中斷時(shí)間后,對gpio口進(jìn)行相應(yīng)操作,函數(shù)如下:
#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中斷計(jì)數(shù)一次
  {
    ramTime._5MS=0;
    //pKeyP.TIME_KEY++;  

    vTcount1++;
    if(vTcount1 >19)//有個(gè)邏輯問題不太理解,即在此處定義了時(shí)間,在后續(xù)執(zhí)行調(diào)用變量時(shí),是順序調(diào)用執(zhí)行還是直接調(diào)用執(zhí)行
    {
        vTcount1 = 0;
                vTcount2++;//到達(dá)500ms
                if(vTcount2==5)ramTime._500MS = 1;
                else if(vTcount2==7)ramTime._700MS = 1;//到達(dá)700ms
                else if(vTcount2==10)
                {
                        ramTime._1S= 1;//到達(dá)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個(gè)按鍵接口
    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上拉輸入
}
/*定時(shí)器初始化*/
void timer4_init()
{
    CLK_PeripheralClockConfig (CLK_Peripheral_TIM4,ENABLE); //使能外設(shè)時(shí)鐘,STM8L默認(rèn)所有外設(shè)時(shí)鐘初始時(shí)關(guān)閉,使用前徐開啟

    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);//向上溢出中斷,中斷函數(shù)向量號為25
    TIM4_Cmd(ENABLE);
}

/*void exti_init()
{
  EXTI_DeInit ();
  EXTI_SetPinSensitivity (EXTI_Pin_4,EXTI_Trigger_Falling);
}*/

/*按鍵操作函數(shù)*/
void Dokey()
{
  int key1;
  static unsigned int key_press_time = 0;  // ……請記得標(biāo)為靜態(tài)變量
    key1=GPIO_ReadInputDataBit(KEY1_GPIO_PORT,KEY1_GPIO_PINS);
    if(key1 == 0)
    {
        if(++key_press_time <=0)--key_press_time;//計(jì)量按鍵時(shí)間,并避免數(shù)據(jù)溢出
        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;
    }
}

/*主函數(shù)*/
int main(void)
{     
    //exti_init();
    timer4_init();
    gpio_init();

    enableInterrupts();//使能中斷總開關(guān)

    ramTime._5MS=0;//srtuct變量,在define.h函數(shù)中定義,用于時(shí)間的調(diào)用
    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函數(shù)
#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;


定時(shí)器4中斷函數(shù)
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);//翻轉(zhuǎn)GPD4輸出狀態(tài)
        i=0;
    }*/

    TIM4_ClearITPendingBit(TIM4_IT_Update);
    //flag16ms ++;
    cnt5ms++;
  if(cnt5ms>24)
  {
     cnt5ms=0;
     ramTime._5MS=1;        
  }
}

以上為函數(shù)部分,但該有的中斷效果沒出來,不太會調(diào),有大神指導(dǎo)下嗎!
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表