基于STM32的红外接收(IRM-H638T)

硬件原理图:

HW到单片机控制引脚


#include "remote.h"
#include "delay.h"
#include "usart.h"
#include "stm32f10x.h"
#include "stm32f10x_exti.h"
u32 hwanjbz0,hw_jsm,A;
u8 CSCS,hw_jsbz;
void Remote_Init(void)    			  
{  

	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
//	TIM_ICInitTypeDef  TIM_ICInitStructure;  
 
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //使能PORTC时钟 
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);	//TIM3 时钟使能 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				 //PA7 输入 
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 		//上拉输入 
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_Init(GPIOC, &GPIO_InitStructure);
 	GPIO_SetBits(GPIOC,GPIO_Pin_5);	//初始化GPIOC7
	

//    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;				//定时器配置结构体定义
//	NVIC_InitTypeDef NVIC_InitStructure;						//中断配置结构体定义

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);        //时钟使能
	
	//定时器TIM2初始化
	TIM_TimeBaseStructure.TIM_Period = 9;    //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
	TIM_TimeBaseStructure.TIM_Prescaler =71;  //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;      //设置时钟分割:TDTS = Tck_tim	
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位

	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM2中断,允许更新中断	TIM_IT_Update

	//中断优先级NVIC设置
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM2中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;        //从优先级1级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;           //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器


	TIM_Cmd(TIM3, ENABLE);  //使能TIM2
}


u16 hwanjbz;//16 红外按键




u32  cszz,bianma0,bianma;
u8  numbz0,numbn,numbw,xjybz,xjybz0=0,jmbz,countb;
u16 count0,count1,ajys;

u16 timechk,countren,uchd,udch,udch0,countren=0,countldk,countldg;
u32 adcux,adcux0;
u8  chdbh=0,chdms=0,moshi,gdms,psys,tpys,yxms,rcys,shdnum,shdn,shdn1,shdn2,dcjz,
       shdn3,shdunum,shdunumx,coumtjishi;
u8  bgys=0,zhtai=0,keyxz=0,chukong,chukong0,countkz,countrcz,
       lundbz,lundbz2,time0,time00,yanshi,
       shdn0,shdn01,shdn02,shdn03;
extern u8 keytzjs,keytz;
extern u8 numssyebz,keybz,keyxy;

    
		
		
		
		
 void timer1 (void)
{
  // cszz++;
   
	
	
    if (RDATA == 0)//为低电平时
    {
		
    	if(xjybz==2){count0++;countb++;if(countb>25){xjybz=3;countb=0;}}
    	else{xjybz=1;count0++;count1=0;}
    }  
    else  //为高电平时
    { 
    	countb=0;
    	if((xjybz==1)||(xjybz==2)){xjybz=2;count0++;}
    	count1++;if(count1>10800){count1=0;count0=0;bianma=0;bianma0=0;numbz0=0;numbw=0;}
     }
		
		 
		 
     
    if(xjybz==3)
    {
    	if((count0>1330)&&(count0<1370)){ numbw=0;bianma=0;jmbz=1;}  //遇到引导码,就把numbw清0,bianma清0    
    	if((count0>100)&&(count0<130))  { numbz0=1;numbn=0;jmbz=1;}  //0码
      if((count0>200)&&(count0<245))  { numbz0=1;numbn=1;jmbz=1;}  //1码    
      if((count0>1100)&&(count0<1150)){ numbz0=0;jmbz=1;xjybz0=1;}	//重复码
      if(jmbz==0){count1=0;count0=0;bianma=0;numbz0=0;numbw=0;}	
			
    	count0=0;xjybz=0;jmbz=0;
			
			
    if(numbz0==1)
    {
    	numbz0=0;
    	numbw++;
    	bianma=bianma<<1;
    	bianma=bianma+numbn;
    	numbn=0;
    	if(numbw>31)
    	{//
    			bianma0=bianma;
    			bianma=0;
    		  numbw=0;
    		  xjybz0=1;
		
    	}
    }
		
		
    if(ajys<200){xjybz0=0;} //不要忘记在timer0isr()中,加 if(ajys<500){ajys++;}
    if(xjybz0==1)
    	{
    		  //功能 629D 上移 02FD 下移 A857 确定 9867 取消 18E7
    		  // 上移-3 下移-2 确认-1
   	      if(bianma0==0x00FF629D){keytz=1;keybz=1;   }  
    			if(bianma0==0x00FF02FD){keytz=3; }  //上移 
    	    if(bianma0==0x00FFA857){keytz=2;}	
    	    if(bianma0==0x00FF9867){keytz=1;keybz=1;   }   //确定 
   	      if(bianma0==0x00FF18E7){numssyebz=1;       }  		
				
				
    	    xjybz0=0;ajys=0;
     	}
	 }

	 
	 
}     





void TIM3_IRQHandler(void)
{ 		   
	timer1();
   TIM_ClearITPendingBit(TIM3, TIM_IT_Update); // 清除中断标志	
}



 



// 计算公式:
void TIM2_Int_Init(u16 arr,u16 psc)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;				//定时器配置结构体定义
	NVIC_InitTypeDef NVIC_InitStructure;						//中断配置结构体定义

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);        //时钟使能
	
	//定时器TIM2初始化
	TIM_TimeBaseStructure.TIM_Period = arr;    //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
	TIM_TimeBaseStructure.TIM_Prescaler =psc;  //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;      //设置时钟分割:TDTS = Tck_tim	
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位

	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM2中断,允许更新中断	TIM_IT_Update

	//中断优先级NVIC设置
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  //TIM2中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;        //从优先级1级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;           //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器


	TIM_Cmd(TIM2, ENABLE);  //使能TIM2
}



void TIM2_IRQHandler(void)   //TIM2中断
{

	
	if(ajys<500){ajys++;} 

		
	    TIM_ClearITPendingBit(TIM2, TIM_IT_Update); // 清除中断标志	
	
}



需要两个定时器

定时器3周期10us负责采集波形,接收头频率是38K的

定时器2周期是0.5ms 负责启动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值