搜索
bottom↓
回复: 1

STM32 38KHZ红外有问题 高手进来看看

[复制链接]

出0入0汤圆

发表于 2010-8-21 15:09:24 | 显示全部楼层 |阅读模式
今天想用STM32做个红外发射接收实验(38KHZ),发射模拟载波,38KHZ的频率用示波器测试后为38KHZ,试验方法:连续发射的载波(才开始玩红外),然后网上找资料说不能这样发,然后我发射一会,停止一会,一体化接收头开始的时候有一个明显的跳变后就一直高电平,档一下后又是有一个明显的跳变后就一直高电平,再挡又是有一个明显的跳变后就一直高电平。程序如下 :(QQ513246017,请高手给看下)
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
u16 CCR1_Val = 500;
u16 CCR2_Val = 375;
u16 CCR3_Val = 250;
u16 CCR4_Val = 125;
u16 CCR5_Val = 625;
ErrorStatus HSEStartUpStatus;
u32 tick;
u8 mode;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void EXTI_init(void);
/* Private functions ---------------------------------------------------------*/

void delay(unsigned int count)
{
        int i,j;
        for(i=0;i<count;i++)
        for(j=0;j<400;j++)        ;
}


int main(void)
{
       
       
        #ifdef DEBUG
          debug();
        #endif

          /* System Clocks Configuration */
          RCC_Configuration();
       
          /* NVIC Configuration */
          NVIC_Configuration();
       
          /* GPIO Configuration */
          GPIO_Configuration();
  /* -----------------------------------------------------------------------
    TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    TIM3CLK = 36 MHz, Prescaler = 0x0, TIM3 counter clock = 36 MHz
    TIM3 ARR Register = 946 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
    TIM3 Frequency = 38 KHz.
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
           TIM3 Channel4 duty cycle = (TIM3_CCR5/ TIM3_ARR)* 100 = 62.5%
  ----------------------------------------------------------------------- */

          /* Time base configuration */
          TIM_TimeBaseStructure.TIM_Period = 946;
          TIM_TimeBaseStructure.TIM_Prescaler = 0;
          TIM_TimeBaseStructure.TIM_ClockDivision = 0;
          TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
       
          TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
       
          /* PWM1 Mode configuration: Channel1 */
          TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
          TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
          
          /* PWM1 Mode configuration: Channel3 */
          TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
          TIM_OCInitStructure.TIM_Pulse = CCR5_Val;        // 62.5%
          TIM_OC3Init(TIM3, &TIM_OCInitStructure);
          TIM_ARRPreloadConfig(TIM3, ENABLE);
          TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);//载入值
          
       
          /* TIM3 enable counter */

          GPIO_ResetBits(GPIOA,GPIO_Pin_8);//蜂鸣器置0
          GPIO_SetBits(GPIOA,GPIO_Pin_3); //接收位置1
          while (1)
          {
                        mode = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3);
                        tick++;
                        if(tick < 1000000)
                        {
                       
                                 TIM_Cmd(TIM3, ENABLE);

                        }
                        if(tick >= 1000000 && tick < 2000000)
                        {
                                tick = 0;
                                TIM_Cmd(TIM3, DISABLE);
                        }
                        if(mode == 0)
                        {       
                                delay(100);
                                if(mode == 0)GPIO_SetBits(GPIOA,GPIO_Pin_8);
                        }
                        else
                        {
                                delay(100);
                                if(mode == 1)GPIO_ResetBits(GPIOA,GPIO_Pin_8);
                               
                        }  
          }
}


void RCC_Configuration(void)
{
          RCC_DeInit();
          RCC_HSEConfig(RCC_HSE_ON);
          HSEStartUpStatus = RCC_WaitForHSEStartUp();
          if (HSEStartUpStatus == SUCCESS)
          {
                    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
                    FLASH_SetLatency(FLASH_Latency_2);
                    RCC_HCLKConfig(RCC_SYSCLK_Div1);
                    RCC_PCLK2Config(RCC_HCLK_Div1);
                    RCC_PCLK1Config(RCC_HCLK_Div4);
                    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
                    RCC_PLLCmd(ENABLE);
                    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
                    {}
                    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
                    while (RCC_GetSYSCLKSource() != 0x08)
                    {}
          }
          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
          RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);
}
void GPIO_Configuration(void)
{
          GPIO_InitTypeDef GPIO_InitStructure;
       
          GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_3;//接收口
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
           GPIO_Init(GPIOA, &GPIO_InitStructure);
       
          GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8;   //蜂鸣器
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
       
          GPIO_Init(GPIOA, &GPIO_InitStructure);
         
          /*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);
}
void NVIC_Configuration(void)
{
        //  NVIC_InitTypeDef NVIC_InitStructure;
        #ifdef  VECT_TAB_RAM
          NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
        #else  /* VECT_TAB_FLASH  */
          NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
        #endif

}
#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
          /* User can add his own implementation to report the file name and line number,
             ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
       
          while (1)
          {}
}
#endif

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

阿莫论坛20周年了!感谢大家的支持与爱护!!

你熬了10碗粥,别人一桶水倒进去,淘走90碗,剩下10碗给你,你看似没亏,其实你那10碗已经没有之前的裹腹了,人家的一桶水换90碗,继续卖。说白了,通货膨胀就是,你的钱是挣来的,他的钱是印来的,掺和在一起,你的钱就贬值了。

出0入0汤圆

发表于 2010-8-26 14:25:50 | 显示全部楼层
红外接收头是连续编码的吗?
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-10-3 10:27

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表