[table][tr][td]
我用51单片机调试38kHz可以驱动红外发射管,可以接收。 但是我用stm32调pwm调38KHz就驱动不了,或者说是接收不到。这问题出在哪里呢?手头上只有数字万用表,测出来的频率也是38kHz,
接收头是市面上的38KHz接收头
<div class="blockcode"><div id="code_L7F">
- #include &quot;bsp_pwm_output.h&quot;
- static void TIM3_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* &Eacute;è&Ouml;&Atilde;TIM3CLK &Icirc;&ordf; 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; // &cedil;&acute;&Oacute;&Atilde;&Iacute;&AElig;&Iacute;ì&Ecirc;&auml;&sup3;&ouml;
- 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&ETH;&Aring;&ordm;&Aring;&micro;&ccedil;&AElig;&frac12;&Igrave;&oslash;±&auml;&Ouml;&micro; */
- 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; //&Eacute;è&Ouml;&Atilde;&Ocirc;¤·&Ouml;&AElig;&micro;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //&Eacute;è&Ouml;&Atilde;&Ecirc;±&Ouml;&Oacute;·&Ouml;&AElig;&micro;&Iuml;&micro;&Ecirc;&yacute;&pound;&ordm;&sup2;&raquo;·&Ouml;&AElig;&micro;(&Otilde;&acirc;&Agrave;&iuml;&Oacute;&Atilde;&sup2;&raquo;&micro;&frac12;)
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //&Iuml;ò&Eacute;&Iuml;&frac14;&AElig;&Ecirc;&yacute;&Auml;&pound;&Ecirc;&frac12;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- /* PWM1 Mode configuration: Channel1 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //&Aring;&auml;&Ouml;&Atilde;&Icirc;&ordf;PWM&Auml;&pound;&Ecirc;&frac12;1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //&Eacute;è&Ouml;&Atilde;&Igrave;&oslash;±&auml;&Ouml;&micro;&pound;&not;&micro;±&frac14;&AElig;&Ecirc;&yacute;&AElig;÷&frac14;&AElig;&Ecirc;&yacute;&micro;&frac12;&Otilde;&acirc;&cedil;&ouml;&Ouml;&micro;&Ecirc;±&pound;&not;&micro;&ccedil;&AElig;&frac12;·&cent;&Eacute;ú&Igrave;&oslash;±&auml;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //&micro;±&para;¨&Ecirc;±&AElig;÷&frac14;&AElig;&Ecirc;&yacute;&Ouml;&micro;&ETH;&iexcl;&Oacute;&Uacute;CCR1_Val&Ecirc;±&Icirc;&ordf;&cedil;&szlig;&micro;&ccedil;&AElig;&frac12;
- TIM_OC1Init(TIM3, &TIM_OCInitStructure); //&Ecirc;&sup1;&Auml;&Uuml;&Iacute;¨&micro;&Agrave;1
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel2 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //&Eacute;è&Ouml;&Atilde;&Iacute;¨&micro;&Agrave;2&micro;&Auml;&micro;&ccedil;&AElig;&frac12;&Igrave;&oslash;±&auml;&Ouml;&micro;&pound;&not;&Ecirc;&auml;&sup3;&ouml;&Aacute;í&Iacute;&acirc;&Ograve;&raquo;&cedil;&ouml;&Otilde;&frac14;&iquest;&Otilde;±&Egrave;&micro;&Auml;PWM
- TIM_OC2Init(TIM3, &TIM_OCInitStructure); //&Ecirc;&sup1;&Auml;&Uuml;&Iacute;¨&micro;&Agrave;2
- TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel3 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //&Eacute;è&Ouml;&Atilde;&Iacute;¨&micro;&Agrave;3&micro;&Auml;&micro;&ccedil;&AElig;&frac12;&Igrave;&oslash;±&auml;&Ouml;&micro;&pound;&not;&Ecirc;&auml;&sup3;&ouml;&Aacute;í&Iacute;&acirc;&Ograve;&raquo;&cedil;&ouml;&Otilde;&frac14;&iquest;&Otilde;±&Egrave;&micro;&Auml;PWM
- TIM_OC3Init(TIM3, &TIM_OCInitStructure); //&Ecirc;&sup1;&Auml;&Uuml;&Iacute;¨&micro;&Agrave;3
- TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel4 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //&Eacute;è&Ouml;&Atilde;&Iacute;¨&micro;&Agrave;4&micro;&Auml;&micro;&ccedil;&AElig;&frac12;&Igrave;&oslash;±&auml;&Ouml;&micro;&pound;&not;&Ecirc;&auml;&sup3;&ouml;&Aacute;í&Iacute;&acirc;&Ograve;&raquo;&cedil;&ouml;&Otilde;&frac14;&iquest;&Otilde;±&Egrave;&micro;&Auml;PWM
- TIM_OC4Init(TIM3, &TIM_OCInitStructure); //&Ecirc;&sup1;&Auml;&Uuml;&Iacute;¨&micro;&Agrave;4
- TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM3, ENABLE); // &Ecirc;&sup1;&Auml;&Uuml;TIM3&Ouml;&Oslash;&Ocirc;&Oslash;&frac14;&Auml;&acute;&aelig;&AElig;÷ARR
- /* TIM3 enable counter */
- TIM_Cmd(TIM3, ENABLE); //&Ecirc;&sup1;&Auml;&Uuml;&para;¨&Ecirc;±&AElig;÷3
- }
- void TIM3_PWM_Init(void)
- {
- TIM3_GPIO_Config();
- TIM3_Mode_Config(); }
|