学前班
最后登录1970-1-1
在线时间 小时
注册时间2015-1-1
|
我用51单片机调试38kHz可以驱动红外发射管,可以接收。 但是我用stm32调pwm调38KHz就驱动不了,或者说是接收不到。这问题出在哪里呢?手头上只有数字万用表,测出来的频率也是38kHz,
接收头是市面上的38KHz接收头
- #include "bsp_pwm_output.h"
- static void TIM3_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* ÉèÖÃTIM3CLK Ϊ 72MHZ */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* GPIOA and GPIOB clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
- /*GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸´ÓÃÍÆÍìÊä³ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /*GPIOB Configuration: TIM3 channel 3 and 4 as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- static void TIM3_Mode_Config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- /* PWMÐźŵçƽÌø±äÖµ */
- u16 CCR1_Val = 8;
- u16 CCR2_Val = 10;
- u16 CCR3_Val = 13;
- u16 CCR4_Val = 18;
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 25; //38kHZ
- TIM_TimeBaseStructure.TIM_Prescaler = 71; //ÉèÖÃÔ¤·ÖƵ
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ(ÕâÀïÓò»µ½)
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- /* PWM1 Mode configuration: Channel1 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃΪPWMģʽ1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪ¸ßµçƽ
- TIM_OC1Init(TIM3, &TIM_OCInitStructure); //ʹÄÜͨµÀ1
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel2 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //ÉèÖÃͨµÀ2µÄµçƽÌø±äÖµ£¬Êä³öÁíÍâÒ»¸öÕ¼¿Õ±ÈµÄPWM
- TIM_OC2Init(TIM3, &TIM_OCInitStructure); //ʹÄÜͨµÀ2
- TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel3 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //ÉèÖÃͨµÀ3µÄµçƽÌø±äÖµ£¬Êä³öÁíÍâÒ»¸öÕ¼¿Õ±ÈµÄPWM
- TIM_OC3Init(TIM3, &TIM_OCInitStructure); //ʹÄÜͨµÀ3
- TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel4 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //ÉèÖÃͨµÀ4µÄµçƽÌø±äÖµ£¬Êä³öÁíÍâÒ»¸öÕ¼¿Õ±ÈµÄPWM
- TIM_OC4Init(TIM3, &TIM_OCInitStructure); //ʹÄÜͨµÀ4
- TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM3, ENABLE); // ʹÄÜTIM3ÖØÔؼĴæÆ÷ARR
- /* TIM3 enable counter */
- TIM_Cmd(TIM3, ENABLE); //ʹÄܶ¨Ê±Æ÷3
- }
- void TIM3_PWM_Init(void)
- {
- TIM3_GPIO_Config();
- TIM3_Mode_Config();
- }
复制代码 |
-
-
|