作者:陈广
日期:2022-1-18
#include "tim.h"
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);
LL_TIM_OC_EnablePreload(TIM2, LL_TIM_CHANNEL_CH1);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM2;
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_TIM_OC_DisablePreload(TIM2, LL_TIM_CHANNEL_CH1);
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 = 2;
TIM2->CCMR1 = 0x70;
TIM2->CCER = 1;
TIM2->DIER = 3;
TIM2->ARR = step[0] + step[1] - 1;
TIM2->CCR1 = step[0];
TIM2->CNT = 0;
TIM2->CR1 |= 1;
}
//定时器中断服务函数
void TIM2_IRQHandler(void)
{
if(TIM2->SR & 1) // 定时器中断
{
TIM2->SR &= ~1; //清除中断标志位
if(cnt <= 66)
{
if(cnt != 66)
{
TIM2->ARR = step[cnt] + step[cnt + 1] - 1;
}
TIM2->CCR1 = step[cnt];
}
cnt += 2;
}
if(TIM2->SR & 2) //通道1中断
{
TIM2->SR &= ~2; //清除中断标志位
if(cnt >= 68)
{
TIM2->DIER = 0;
TIM2->CCMR1 = 0x50;
TIM2->CR1 &= ~1;
return;
}
}
}
/* USER CODE END 1 */
#include "tim.h"
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);
LL_TIM_OC_EnablePreload(TIM2, LL_TIM_CHANNEL_CH1);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM2;
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_TIM_OC_DisablePreload(TIM2, LL_TIM_CHANNEL_CH1);
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 = 2;
LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_PWM2); //将通道1设置为PWM模式1
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1); //使能通道1
LL_TIM_EnableIT_UPDATE(TIM2); //使能定时器中断
LL_TIM_EnableIT_CC1(TIM2); //使能通道1中断
LL_TIM_SetAutoReload(TIM2, step[0] + step[1] - 1);
LL_TIM_OC_SetCompareCH1(TIM2, step[0]);
LL_TIM_SetCounter(TIM2, 0); //清空计数器
LL_TIM_EnableCounter(TIM2); //启动计数器
}
//定时器中断服务函数
void TIM2_IRQHandler(void)
{
if(LL_TIM_IsActiveFlag_UPDATE(TIM2)) // 定时器中断
{
LL_TIM_ClearFlag_UPDATE(TIM2); //清除中断标志位
if(cnt <= 66)
{
if(cnt != 66)
{
LL_TIM_SetAutoReload(TIM2, step[cnt] + step[cnt + 1] - 1);
}
LL_TIM_OC_SetCompareCH1(TIM2, step[cnt]);
}
cnt += 2;
}
if(LL_TIM_IsActiveFlag_CC1(TIM2)) //通道1中断
{
LL_TIM_ClearFlag_CC1(TIM2); //清除中断标志位
if(cnt >= 68)
{
LL_TIM_DisableIT_UPDATE(TIM2);
LL_TIM_DisableIT_CC1(TIM2);
LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_ACTIVE); //将通道1设置为强制高电平
LL_TIM_DisableCounter(TIM2);
return;
}
}
}
/* USER CODE END 1 */
;