《红外遥控STM32程序(10页).doc》由会员分享,可在线阅读,更多相关《红外遥控STM32程序(10页).doc(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-红外遥控STM32程序-第 10 页/* F103VET6 红外驱动【遥控器解码实验程序】* 遥控器 :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 GPIO
2、D-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
3、_Pin_2)/读取红外电平状态unsigned char TimeByte;unsigned int IR_Tireafg4;unsigned int IR_xidwrit8 = 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;/配置管脚为输入上拉G
4、PIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;/配置管脚速率50MHZGPIO_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
5、= 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
6、_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_HongwaiR
7、ead_LSB_Cmd()unsigned int Count1 = 0; /定义时间变量IR_Hongwai_0; /红外数据低电平do /进入循环体Count1+; /时间变量加1Delay_10us(1); /延时10uswhile(IR_Hongwai_x = 0);/判断条件如果读出来的红外数据为高电平跳出循环体return(Count1); /返回时间变量积数/* 计算高脉宽持续时间Count2 * 10us*/unsigned int IR_HongwaiRead_MSB_Cmd()unsigned int Count2 = 0; /定义时间变量IR_Hongwai_1; /红外
8、数据高电平do /进入循环体 Count2+; /时间变量加1Delay_10us(1); /延时10uswhile(IR_Hongwai_x = 1);/判断条件如果读出来的红外数据为低电平跳出循环体return(Count2);/* 函数主体*/int main(void)SystemInit(); /初始化系统时钟进入72M主频SYSTICK_InitStructReadTCmd(); /初始化SysTick配置器寄存器RCC_APB2PeriphReadtempyCmd(); /初始化外设时钟配置寄存器GPIO_InitStructReadtempCmd(); /初始化GPIO端口配置
9、寄存器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();/低电平引
10、导码 9ms判断if(Countline2 950) /小于8694us 大于9272us 一直循环否则跳出return; Countline2 = IR_HongwaiRead_MSB_Cmd();/高电平引导码 4.5ms判断if(Countline2 450) /小于4195us 大于4712us 一直循环否则跳出return;TimeByte = 0;for(i = 1; i 1;Countline2 = IR_HongwaiRead_LSB_Cmd(); /低电平延时0.56 判断if(Countline2 85)/小于425us 大于851us 一直循环否则跳出return;Cou
11、ntline2 = IR_HongwaiRead_MSB_Cmd(); /高电平延时0.56判断if(Countline2 200)/小于425us 大于1793us 一直循环否则跳出return;if( Countline2 130)/高电平延时大于1300us写1否则写0TimeByte |= 0x80;/写1IR_Tireafg0 = TimeByte;TimeByte = 0;for(i = 14; i 1;Countline2 = IR_HongwaiRead_LSB_Cmd();if(Countline2 85)return;Countline2 = IR_HongwaiRead_
12、MSB_Cmd();if(Countline2 200)return;if( Countline2 130)TimeByte |= 0x80;IR_Tireafg1 = TimeByte;TimeByte = 0;for(i = 27; i 1;Countline2 = IR_HongwaiRead_LSB_Cmd();if(Countline2 85)return;Countline2 = IR_HongwaiRead_MSB_Cmd();if(Countline2 200)return;if( Countline2 130)TimeByte |= 0x80;IR_Tireafg2 = Ti
13、meByte;TimeByte = 0;for(i = 35; i 1;Countline2 = IR_HongwaiRead_LSB_Cmd();while(Countline2 85)return;Countline2 = IR_HongwaiRead_MSB_Cmd();while(Countline2 200)return;if( Countline2 130)TimeByte |= 0x80;IR_Tireafg3 = TimeByte;/*判断用户正码和反码*/doif(IR_Tireafg0 = 0x08 & IR_Tireafg1 = 0xF7 )flag = 0; while
14、(flag = 1); /*判断按键正码和反码*/* doif(IR_Tireafg2 = IR_Tireafg3)flag1 = 0; while(flag1 = 0); */*按下键码对应LED点亮*/switch(IR_Tireafg2)case 0x00: /按键 0LED1_1; LED2_0; LED3_0; LED4_0;break;case 0x01: /按键 1LED1_0; LED2_1; LED3_0; LED4_0;break;case 0x02: /按键 2LED1_0; LED2_0; LED3_1; LED4_0;break;case 0x03: /按键 3LED
15、1_0; LED2_0; LED3_0; LED4_1;break;case 0x04: /按键 4LED1_0; LED2_0; LED3_1; LED4_0;break;case 0x05: /按键 5LED1_0; LED2_1; LED3_0; LED4_0;break;case 0x06: /按键 6LED1_1; LED2_0; LED3_0; LED4_0;break;case 0x07: /按键 7LED1_1; LED2_0; LED3_1; LED4_0;break;case 0x08: /按键 8LED1_0; LED2_0; LED3_0; LED4_0;break;c
16、ase 0x09: /按键 9LED1_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; L
17、ED3_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*/
限制150内