x
x
查看: 7728|回复: 0

分享STM32 红外解码电视遥控器程序

[复制链接]
发表于 2012-11-20 13:52:42 | 显示全部楼层 |阅读模式
/**********************************************************************************
*       STM32F103VET6 红外驱动【遥控器解码实验程序】
*
*   遥控器 :42位编码
*
***********************************************************************************/
#include "stm32f10x.h"
#include "exti.h"
#include "beep.h"
#include "SysTick.h"
   
#define    LED1_0      GPIOD->BRR  = 0x00000100 //LED低电平
#define    LED2_0      GPIOD->BRR  = 0x00000200
#define    LED3_0      GPIOD->BRR  = 0x00000400
#define    LED4_0      GPIOD->BRR  = 0x00000800

#define    LED1_1      GPIOD->BSRR = 0x00000100     //LED高电平
#define    LED2_1      GPIOD->BSRR = 0x00000200
#define    LED3_1      GPIOD->BSRR = 0x00000400
#define    LED4_1      GPIOD->BSRR = 0x00000800   

#define    IR_Hongwai_0         GPIOE->BRR  = 0x00000004   //红外数据低电平
#define    IR_Hongwai_1         GPIOE->BSRR = 0x00000004   //红外数据高电平
#define    IR_Hongwai_x GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2)  //读取红外电平状态

unsigned char TimeByte;
unsigned int IR_Tireafg[4];
unsigned int IR_xidwrit[8] = {0, 0, 0, 0, 0, 0 ,0, 0};
  
/*
*   GPIO端口配置子函数
*/
void GPIO_InitStructReadtempCmd(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;  //配置GPIO管脚
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;  //配置管脚为输入上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;  //配置管脚速率50MHZ
GPIO_Init(GPIOE, &GPIO_InitStruct);  //初始化指定端口

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;  //配置管脚陪推挽式输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStruct);
}
   
/*
*   配置外设时钟子函数
*/
void RCC_APB2PeriphReadtempyCmd(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  //打开GPIOB外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);  //打开GPIOE外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);  //打开GPIOD外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);  //打开AFIO复用功能外设时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);   
}

/*
* 计算低脉宽持续时间Count1 * 10us  
*/
unsigned int IR_HongwaiRead_LSB_Cmd()
{
unsigned int Count1 = 0;    //定义时间变量
IR_Hongwai_0;    //红外数据低电平
do    //进入循环体
{
Count1++;    //时间变量加1
Delay_10us(1);    //延时10us
}  while(IR_Hongwai_x == 0);  //判断条件如果读出来的红外数据为高电平跳出循环体

return(Count1);    //返回时间变量积数
}

/*
* 计算高脉宽持续时间Count2 * 10us  
*/
unsigned int IR_HongwaiRead_MSB_Cmd()
{
unsigned int Count2 = 0;    //定义时间变量
IR_Hongwai_1;    //红外数据高电平
do    //进入循环体
{     
Count2++;    //时间变量加1
Delay_10us(1);    //延时10us
}  while(IR_Hongwai_x == 1);  //判断条件如果读出来的红外数据为低电平跳出循环体
return(Count2);
}


/*
*   函数主体
*/
int main(void)
{
SystemInit();   //初始化系统时钟进入72M主频
SYSTICK_InitStructReadTCmd();   //初始化SysTick配置器寄存器
RCC_APB2PeriphReadtempyCmd();   //初始化外设时钟配置寄存器
GPIO_InitStructReadtempCmd();   //初始化GPIO端口配置寄存器
EXTI_InitStructReadtempCmd();   //初始化EXTI外部线路寄存器
NVIC_InitStructReadtempCmd();   //初始化NVIC中断配置寄存器

while(1)
{

}   
}

/*
*   EXTI外部中断线服务程序
*/
void EXTI2_IRQHandler(void)
{
unsigned char i = 0;  
unsigned char flag = 1;
// unsigned char flag1 = 1;  
unsigned int Countline2 = 0;
IR_Hongwai_1;
Countline2 = IR_HongwaiRead_LSB_Cmd();  //低电平引导码 9ms判断
if((Countline2 < 850) || (Countline2 > 950)) //小于8694us 大于9272us 一直循环否则跳出
{
return;   
}
Countline2 = IR_HongwaiRead_MSB_Cmd();  //高电平引导码 4.5ms判断
if((Countline2 < 400) || (Countline2 > 450)) //小于4195us 大于4712us 一直循环否则跳出
{
return;
}
TimeByte = 0;
for(i = 1; i < 14; i++)
{
TimeByte = TimeByte >> 1;
Countline2 = IR_HongwaiRead_LSB_Cmd();   //低电平延时0.56 判断
if((Countline2 < 40) || (Countline2 > 85))//小于425us 大于851us 一直循环否则跳出
{
return;
}

Countline2 = IR_HongwaiRead_MSB_Cmd();   //高电平延时0.56判断
if((Countline2 < 40) || (Countline2 > 200))//小于425us 大于1793us 一直循环否则跳出
{
return;
}
if( Countline2 > 130)  //高电平延时大于1300us写1否则写0
{
TimeByte |= 0x80;  //写1
}
}
IR_Tireafg[0] = TimeByte;
TimeByte = 0;

for(i = 14; i < 27; i++)
{
TimeByte = TimeByte >> 1;
Countline2 = IR_HongwaiRead_LSB_Cmd();
if((Countline2 < 40) || (Countline2 > 85))
{
return;
}

Countline2 = IR_HongwaiRead_MSB_Cmd();
if((Countline2 < 40) || (Countline2 > 200))
{
return;
}
if( Countline2 > 130)
{
TimeByte |= 0x80;
}
}
IR_Tireafg[1] = TimeByte;
TimeByte = 0;

for(i = 27; i < 35; i++)
{
TimeByte = TimeByte >> 1;
Countline2 = IR_HongwaiRead_LSB_Cmd();
if((Countline2 < 40) || (Countline2 > 85))
{
return;
}

Countline2 = IR_HongwaiRead_MSB_Cmd();
if((Countline2 < 40) && (Countline2 > 200))
{
return;
}
if( Countline2 > 130)
{
TimeByte |= 0x80;
}
}
IR_Tireafg[2] = TimeByte;
TimeByte = 0;

for(i = 35; i < 43; i++)
{
TimeByte = TimeByte >> 1;
Countline2 = IR_HongwaiRead_LSB_Cmd();
while((Countline2 < 40) || (Countline2 > 85))
{
return;
}

Countline2 = IR_HongwaiRead_MSB_Cmd();
while((Countline2 < 40) || (Countline2 > 200))
{
return;
}
if( Countline2 > 130)
{
TimeByte |= 0x80;
}
}
IR_Tireafg[3] = TimeByte;

//************************判断用户正码和反码***************************************//
do
{
if(IR_Tireafg[0] == 0x08 & IR_Tireafg[1] == 0xF7 )
{
flag = 0;   
}
} while(flag == 1);         
//************************判断按键正码和反码***************************************//
/* do
{
if(IR_Tireafg[2] == ~IR_Tireafg[3])
{
flag1 = 0;
}
} while(flag1 == 0);        */

//************************按下键码对应LED点亮**************************************//
switch  (IR_Tireafg[2])
{
case         0x00:   //按键 0
LED1_1; LED2_0; LED3_0; LED4_0;
break;
  
case         0x01:    //按键 1
LED1_0; LED2_1; LED3_0; LED4_0;
break;
  
case         0x02:   //按键 2
LED1_0; LED2_0; LED3_1; LED4_0;
break;
  
case         0x03:   //按键 3
LED1_0; LED2_0; LED3_0; LED4_1;
break;

case         0x04:   //按键 4
LED1_0; LED2_0; LED3_1; LED4_0;
break;

case         0x05:   //按键 5
LED1_0; LED2_1; LED3_0; LED4_0;
break;

case         0x06:   //按键 6
LED1_1; LED2_0; LED3_0; LED4_0;
break;

case         0x07:   //按键 7
LED1_1; LED2_0; LED3_1; LED4_0;
break;

case         0x08:   //按键 8
LED1_0; LED2_0; LED3_0; LED4_0;
break;
  
case         0x09:   //按键 9
LED1_0; LED2_1; LED3_0; LED4_1;
break;

case         0x15:   //静音键
LED1_0; LED2_1; LED3_1; LED4_0;
break;

case         0x1C:   //开机键
LED1_1; LED2_0; LED3_0; LED4_1;
break;
  
case         0x14:   //OSD键
LED1_1; LED2_1; LED3_0; LED4_0;
break;
  
case         0x0E:   //RECALL键
LED1_0; LED2_0; LED3_1; LED4_1;
break;
  
case         0x19:   //SLEEP键
LED1_1; LED2_1; LED3_1; LED4_0;
break;
  
case         0x0A:   //A/C键
LED1_0; LED2_1; LED3_1; LED4_1;
break;
  
case         0x0F:   //TV/AV键
LED1_1; LED2_1; LED3_1; LED4_1;
break;
  
case         0x13:   //PP键
LED1_1; LED2_0; LED3_1; LED4_0;
break;
  
case         0x0C:   //GAME键
LED1_0; LED2_1; LED3_1; LED4_1;
break;
  
case         0x1E:   //V-键
LED1_1; LED2_1; LED3_1; LED4_0;
break;
  
case         0x1F:   //V+键
LED1_0; LED2_0; LED3_1; LED4_0;
break;
  
case         0x1B:   //P+键
LED1_0; LED2_0; LED3_0; LED4_1;
break;
  
case         0x1A:   //P-键
LED1_1; LED2_0; LED3_0; LED4_0;
break;
  
case         0x10:   //MENU键
LED1_0; LED2_1; LED3_0; LED4_0;
break;
  
  default  :     break;

}     
// Beep_lookCmd();     //蜂鸣器按键音
EXTI_ClearITPendingBit(EXTI_Line2); //清除EXTI2外部线路挂起位

}
  
/*******************************************END****************************************/



                     本人以前是搞51单片机的刚接手STM32没有多久,本人喜欢结交单片机爱好者,共同学习有高手的也可以一起加入,QQ群100626882,我们可以在群里面交流和学习STM32

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表