STM32视频教程

使用输出比较生成红外NEC方波

作者:陈广
日期:2022-1-9


视频播放地址

点击播放

tim.c代码(寄存器版):

#include "tim.h"

/* TIM2 init function */
void MX_TIM2_Init(void)
{
  LL_TIM_InitTypeDef TIM_InitStruct = {0};
  LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};

  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);

  /* TIM2 interrupt Init */
  NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
  NVIC_EnableIRQ(TIM2_IRQn);

  TIM_InitStruct.Prescaler = 71;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 65535;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM2, &TIM_InitStruct);
  LL_TIM_DisableARRPreload(TIM2);
  LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FORCED_ACTIVE;
  TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.CompareValue = 0;
  TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
  LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH1);
  LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
  LL_TIM_DisableMasterSlaveMode(TIM2);
  /* USER CODE BEGIN TIM2_Init 2 */

  /* USER CODE END TIM2_Init 2 */
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
    /**TIM2 GPIO Configuration
    PA0-WKUP     ------> TIM2_CH1
    */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

/* USER CODE BEGIN 1 */
volatile int cnt;
volatile uint16_t step[67] =
{
    9000, 4500, //前导码
    560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, //地址码,索引2~17
    560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, //地址反码,索引18~33
    560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, //命令码,索引34~49
    560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, //命令反码,索引50~65
    560
};

//将地址码和命令码转化为NEC编码
void EncodeStep(uint8_t addr, uint8_t cmd)
{
    int i;
    for(i = 3; i <= 17; i += 2)
    {
        step[i] = (addr & (1 << ((i - 3) / 2))) ? 1690 : 560;
    }
    addr = ~addr;
    for(i = 19; i <= 33; i += 2)
    {
        step[i] = (addr & (1 << ((i - 19) / 2))) ? 1690 : 560;
    }
    for(i = 35; i <= 49; i += 2)
    {
        step[i] = (cmd & (1 << ((i - 35) / 2))) ? 1690 : 560;
    }
    cmd = ~cmd;
    for(i = 51; i <= 65; i += 2)
    {
        step[i] = (cmd & (1 << ((i - 51) / 2))) ? 1690 : 560;
    }
}

//生成一个NEC方波
void General_Pulse(void)
{
    EncodeStep(0x56, 0x69);
    cnt = 0;
    TIM2->CCMR1 = 0x30;
    TIM2->CCER = 1;
    TIM2->DIER = 2;
    TIM2->CR1 = 1;
}

void TIM2_IRQHandler(void)
{
    if(TIM2->SR & 2) //通道1中断
    {
        TIM2->SR &= ~2; //清除中断标志位
        if(cnt > 66) //脉冲生成结束后,停止定时器的运行
        {
            TIM2->DIER = 0;
            TIM2->CR1 = 0;
            TIM2->CCMR1 = 0x50;
            return;
        }
        TIM2->CNT = 0;
        TIM2->CCR1 = step[cnt];
        cnt++;
    }
}
/* USER CODE END 1 */

tim.c代码(LL库函数版)

#include "tim.h"
/* TIM2 init function */
void MX_TIM2_Init(void)
{
  LL_TIM_InitTypeDef TIM_InitStruct = {0};
  LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};

  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);

  /* TIM2 interrupt Init */
  NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
  NVIC_EnableIRQ(TIM2_IRQn);

  TIM_InitStruct.Prescaler = 71;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 65535;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM2, &TIM_InitStruct);
  LL_TIM_DisableARRPreload(TIM2);
  LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FORCED_ACTIVE;
  TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.CompareValue = 0;
  TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
  LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH1);
  LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
  LL_TIM_DisableMasterSlaveMode(TIM2);

  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
    /**TIM2 GPIO Configuration
    PA0-WKUP     ------> TIM2_CH1
    */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE BEGIN 1 */
volatile int cnt;
volatile uint16_t step[67] =
{
    9000, 4500, //前导码
    560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, //地址码,索引2~17
    560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, //地址反码,索引18~33
    560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, //命令码,索引34~49
    560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, 560, 0, //命令反码,索引50~65
    560
};

//将地址码和命令码转化为NEC编码
void EncodeStep(uint8_t addr, uint8_t cmd)
{
    int i;
    for(i = 3; i <= 17; i += 2)
    {
        step[i] = (addr & (1 << ((i - 3) / 2))) ? 1690 : 560;
    }
    addr = ~addr;
    for(i = 19; i <= 33; i += 2)
    {
        step[i] = (addr & (1 << ((i - 19) / 2))) ? 1690 : 560;
    }
    for(i = 35; i <= 49; i += 2)
    {
        step[i] = (cmd & (1 << ((i - 35) / 2))) ? 1690 : 560;
    }
    cmd = ~cmd;
    for(i = 51; i <= 65; i += 2)
    {
        step[i] = (cmd & (1 << ((i - 51) / 2))) ? 1690 : 560;
    }
}

//生成一个NEC方波
void General_Pulse(void)
{
    EncodeStep(0x56, 0x69);
    cnt = 0;
    LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_TOGGLE); //将输入比较设置为toggle模式
    LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1); //使能通道1
    LL_TIM_EnableIT_CC1(TIM2); //使能通道1中断
    LL_TIM_EnableCounter(TIM2); //启动计数器
}
//定时器中断服务函数
void TIM2_IRQHandler(void)
{
    if(LL_TIM_IsActiveFlag_CC1(TIM2)) //通道1中断
    {
        LL_TIM_ClearFlag_CC1(TIM2); //清除中断标志位
        if(cnt > 66) //脉冲生成结束后,停止定时器的运行
        {
            LL_TIM_DisableIT_CC1(TIM2); //禁用通道1中断
            LL_TIM_DisableCounter(TIM2); //禁用计数器
            LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_ACTIVE); //切换为强制高电平模式
            return;
        }
        LL_TIM_SetCounter(TIM2, 0); //定时器计数器清零
        LL_TIM_OC_SetCompareCH1(TIM2, step[cnt]); //将数组中对应的值写入CCR1寄存器
        cnt++;
    }
}
/* USER CODE END 1 */

;

© 2018 - IOT小分队文章发布系统 v0.3