STM32 红外发射Timer1 ch1 PWM输出

2019-07-21 07:55发布

之前做红外发射,在网上都找不到现成的资料,现在自己做出来了,拿出来给有需要的人


void Timer1_PWM_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    //TIM1基本计数器设置(设置PWM频率)
    //频率=TIM1_CLK/(ARR+1)
    TIM_BaseInitStructure.TIM_Period = 1894;            //不分频。PWM频率=72000/1894=38Khz
    TIM_BaseInitStructure.TIM_Prescaler = 0;
    TIM_BaseInitStructure.TIM_ClockDivision = 0;
    TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
    //启用ARR的影子寄存器(直到产生更新事件才更改设置)
    TIM_ARRPreloadConfig(TIM1, ENABLE);
 //TIM1_OC1模块设置(设置1通道占空比)
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    //启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
    TIM_Cmd(TIM1, ENABLE);                //TIM1开启
    //TIM1_OC通道输出PWM(一定要加)
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
    TIM_SetCompare1(TIM1,625);         //设置占空比为1/3
}

下面是简单的发送
void IR_Send(void)
{
    //9ms
    TIM_Cmd(TIM1, ENABLE);
    Delay_nMS(9);
    //4.5ms
    TIM_Cmd(TIM1, DISABLE);
    Delay_nMS(4);
    Delay_nUS(500);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);
    //0
    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
    Delay_nUS(565);

    TIM_Cmd(TIM1, ENABLE);
    Delay_nUS(565);
    Delay_nUS(565);
    Delay_nUS(565);
    TIM_Cmd(TIM1, DISABLE);
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
12条回答
特工_500
1楼-- · 2019-07-21 13:47
上面的代码是当时测试用的,下面的做了些改进

void Timer1_PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
    //TIM1基本计数器设置(设置PWM频率)
    //频率=TIM1_CLK/(ARR+1)
    TIM_BaseInitStructure.TIM_Period = 1894; //不分频。PWM频率=72000/1894=380Khz
    TIM_BaseInitStructure.TIM_Prescaler = 0;
    TIM_BaseInitStructure.TIM_ClockDivision = 0;
    TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
    //启用ARR的影子寄存器(直到产生更新事件才更改设置)
    TIM_ARRPreloadConfig(TIM1, ENABLE);
 //TIM1_OC1模块设置(设置1通道占空比)
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    //启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
    TIM_Cmd(TIM1, ENABLE); //TIM1开启
    //TIM1_OC通道输出PWM(一定要加)
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

void IR_Send(void)
{
//9ms
TIM_Cmd(TIM1, ENABLE);
Delay_nMS(9);
//4.5ms
TIM_Cmd(TIM1, DISABLE);
Delay_nMS(4);
Delay_nUS(500);
IR_Send_Byte(0x00); //用户码
IR_Send_Byte(0xFF); //用户反码
IR_Send_Byte(0x00); //数据码
IR_Send_Byte(0xFF); //数据反码
//下面增加是为了避免在发送最后一位时出现错码
TIM_Cmd(TIM1, ENABLE);
Delay_nUS(565);
TIM_Cmd(TIM1, DISABLE);
Delay_nUS(565);
}

void IR_Send_Byte(u8 getdata)
{
u8 senddata,i;
senddata=getdata;
for(i=0;i<8;i++)
{
if(senddata&0x01)
{
//1
TIM_Cmd(TIM1, ENABLE);
Delay_nUS(565);
TIM_Cmd(TIM1, DISABLE);
Delay_nUS(565);
Delay_nUS(565);
Delay_nUS(565);
}
else
{
//0
TIM_Cmd(TIM1, ENABLE);
Delay_nUS(565);
TIM_Cmd(TIM1, DISABLE);
Delay_nUS(565);
}
senddata=senddata>>1;
}
}
exiul
2楼-- · 2019-07-21 15:18
LZ  :小弟才学习stm32,能不能贴个完整的工程代码呀。
Delay_nUS函数,和delay_us函数有什么区别?
xiaoyan
3楼-- · 2019-07-21 21:18
谢谢分享,代码感觉有点臃肿哦,能不能简化些呢
特工_500
4楼-- · 2019-07-21 23:15
回复【2楼】xiaoyan:
---------------------------------
行,再改改
xiaoyan
5楼-- · 2019-07-22 00:07
 精彩回答 2  元偷偷看……
正点原子
6楼-- · 2019-07-22 02:47
谢谢分享

一周热门 更多>