完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
之前做红外发射,在网上都找不到现成的资料,现在自己做出来了,拿出来给有需要的人 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); } |
|
相关推荐
|
|
1129 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1075 浏览 2 评论
2175 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1269 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1693 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-26 12:09 , Processed in 0.655316 second(s), Total 67, Slave 48 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号