基于STM32的智能学习空调项目的定时器捕获驱动

本文介绍了一种使用STM32的TIM3定时器进行红外信号捕获和发射的方法。通过对GPIO端口进行初始化配置,并设置TIM3定时器的工作模式、中断和服务,实现了对红外信号的有效处理。
/*************************************************************************************************************************
* 函数 : TIM3.C
* 功能 : 红外信号捕获和发射
* 参数 :
* 返回 :
* 依赖 : 底层读写函数
* 作者 : li_qcxy@126.com
* 时间 : 2016-4-9
* 最后修改时间 : 
* 说明
* 参考

*************************************************************************************************************************/ 

voidIRDA_GPIO_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
 
//LED1 LED2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA,PD端口时钟
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PA,PD端口时钟

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12; //
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
 GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOA.8
 
 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_6;  //PA6 清除之前设置  
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure);

 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_5;  //PA6 清除之前设置  
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 GPIO_SetBits(GPIOA,GPIO_Pin_5); //PA.8 输出高
 
 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_10 | GPIO_Pin_11;  //PA6 清除之前设置  
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);
 
// 看门狗SP706 输入脚PA7
 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_7;  //PA7 清除之前设置  
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void TIM3_Int_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;


RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能


TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值计数到5000为500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值  10Khz的计数频率  
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
 
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0x0;
  TIM_ICInit(TIM3, &TIM_ICInitStructure);

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器


TIM_ITConfig(TIM3,TIM_IT_CC1,DISABLE);//不允许CC1IE捕获中断
TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE);//不允许更新中断


TIM_Cmd(TIM3,DISABLE); //使能定时器3
  
}




void TIM_Polarityset(u8 Polarity)
{
 if(Polarity == 0) TIM3->CCER &= ~(1<<1);  //上升
 else TIM3->CCER |= 1<<1;                 //下降

}



void TIM3_IRQHandler(void)   //TIM3中断
{
 if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
 {
TIM_ClearITPendingBit(TIM3,TIM_IT_Update );  //清除TIMx的中断待处理位:TIM 中断源 
  switch(Timer3)
  {
   case 0:
   {
    Timer3++;
    if(u8Lev == 0x0) GPIO_SetBits(GPIOA,GPIO_Pin_5);
    else GPIO_ResetBits(GPIOA,GPIO_Pin_5);
    break;
   }
   case 1:
   {
    Timer3++;
    GPIO_SetBits(GPIOA,GPIO_Pin_5);
    break;
   }
   case 2:
   {
    Timer3 = 0;
    Timer_PWM++;
    if(Timer_PWM >= Capture)
    {
     Capture =  u16P[u16Pcn++];
     if(Capture == 0xffff)  //输出结束
     {
      TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE);
      TIM_Cmd(TIM3,DISABLE);
      GPIO_SetBits(GPIOA,GPIO_Pin_5);
irad_launch = 1;
      u8FSMirda = IRDA_FSM_DILE;
     }
     else
     {
      u8Lev = ~u8Lev;
      Timer_PWM = 0;
     }
    }
    break;
   }
   default:
   {
    Timer3 = 0;
   }
  }
  
 }

 if(TIM_GetITStatus(TIM3, TIM_IT_CC1) == SET) 
  {
    //TIM_ICInitTypeDef  TIM_ICInitStructure;
    /* Clear TIM3 Capture compare interrupt pending bit */
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
    u8Timerirda = 0;
    if(CaptureNumber == 0)
    {
      /* Get the Input Capture value */
      IC3ReadValue1 = TIM_GetCapture1(TIM3);
      CaptureNumber = 1;
      u8Lev = 0xff;
    }
    else if(CaptureNumber == 1)
    {
      /* Get the Input Capture value */
      IC3ReadValue2 = TIM_GetCapture1(TIM3); 
      
      /* Capture computation */
      if (IC3ReadValue2 > IC3ReadValue1)
      {
        Capture = (IC3ReadValue2 - IC3ReadValue1); 
      }
      else
      {
        Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2); 
      }
      u16P[u16Pcn++] = Capture;
      IC3ReadValue1 = IC3ReadValue2;
      
      /* Frequency computation */ 
      //TIM3Freq = (uint32_t) SystemCoreClock / Capture;
      //CaptureNumber = 0;
    }
  u8Lev = ~u8Lev;  
  TIM_Polarityset(u8Lev);  
  }
}

按需引入<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Ant Design Vue 纯HTML项目示例</title> <!-- 引入Ant Design Vue的CSS --> <link rel="stylesheet" href="https://unpkg.com/ant-design-vue@3.2.21/dist/antd.css"> <!-- 引入Ant Design X Vue的CSS --> <link rel="stylesheet" href="https://unpkg.com/ant-design-x-vue@1.0.0/dist/antdx.css"> <!-- vue3引入 --> <script src="https://unpkg.com/dayjs/dayjs.min.js"></script> <script src="https://unpkg.com/dayjs/plugin/customParseFormat.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekday.js"></script> <script src="https://unpkg.com/dayjs/plugin/localeData.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekOfYear.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekYear.js"></script> <script src="https://unpkg.com/dayjs/plugin/advancedFormat.js"></script> <script src="https://unpkg.com/dayjs/plugin/quarterOfYear.js"></script> <!-- vue3 --> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> <!-- antdv --> <script src="https://cdn.jsdelivr.net/npm/ant-design-vue@4.2.6/dist/antd.min.js"></script> <!-- antdxv --> <script src="https://cdn.jsdelivr.net/npm/ant-design-x-vue@1.2.7/dist/index.umd.min.js"></script> </head> <body> <div id="app"></div> <script> const { createApp, ref, computed } = Vue; const { Button } = antd; const { Bubble, XProvider } = antdx; createApp({ template: ` <AXProvider :theme="{ algorithm: myThemeAlgorithm, }"> <div :style="{ padding: &#39;24px&#39;, backgroundColor: bgColor, }"> UMD <AXBubble content="hello bubble"></AXBubble> <AButton type="primary" @click="setLightTheme">Light</AButton> <AButton type="primary" @click="setDarkTheme">Dark</AButton> </div> </AXProvider> `, setup() { const { theme } = antd; const bgColor = ref("white"); const myThemeAlgorithm = ref(theme.defaultAlgorithm); const setLightTheme = () => { myThemeAlgorithm.value = theme.defaultAlgorithm; bgColor.value = "white"; }; const setDarkTheme = () => { myThemeAlgorithm.value = theme.darkAlgorithm; bgColor.value = "#141414"; }; return { myThemeAlgorithm, bgColor, setLightTheme, setDarkTheme }; } }) .use(XProvider) .use(Button) .use(Bubble) .mount("#app"); </script> <style> .container { max-width: 1200px; margin: 24px auto; padding: 0 16px; } .search-form { margin-bottom: 24px; padding: 16px; background-color: #f5f5f5; border-radius: 4px; } .user-table { margin-top: 16px; } .mb-6 { margin-bottom: 24px; } .mt-2 { margin-top: 8px; } </style> </body> </html>
07-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值