红外遥控器
- 17.1红外遥控的基础知识
- 1.红外遥控简介
- 2.硬件电路
- 3.基本发送和接收
- 4.NEC编码
- 5.遥控器键码
- 6.51单片机的外部中断
- 7.外部中断寄存器
- 17.2红外遥控的程序代码
- 1.红外遥控
- (1)工程目录
- (2)main.c函数
- (3)Int0.c函数
- (4)Timer0.c函数
- (5)IR.c函数
- (6)IR.h函数
- 2.遥控电机调速
- (1)工程目录
- (2)main.c函数
- (3)Timer1.c函数
- (4)Motor.c函数
17.1红外遥控的基础知识
1.红外遥控简介
红外遥控是利用红外光进行通信的设备,由红外LED将调制后的信号发出,由专用的红外接收头进行解调输出
通信方式:单工,异步
红外LED波长:940nm
通信协议标准:NEC标准
2.硬件电路
3.基本发送和接收
空闲状态:红外LED不亮,接收头输出高电平
发送低电平:红外LED以38KHz频率闪烁发光,接收头输出低电平
发送高电平:红外LED不亮,接收头输出高电平
4.NEC编码
5.遥控器键码
6.51单片机的外部中断
7.外部中断寄存器
17.2红外遥控的程序代码
1.红外遥控
(1)工程目录
(2)main.c函数
#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "IR.h"unsigned char Num;
unsigned char Address;
unsigned char Command;void main()
{LCD_Init();LCD_ShowString(1,1,"ADDR CMD NUM");LCD_ShowString(2,1,"00 00 000");IR_Init();while(1){if(IR_GetDataFlag()||IR_GetRepeatFlag()){Address=IR_GetAddress();Command=IR_GetCommand();LCD_ShowHexNum(2,1,Address,2);LCD_ShowHexNum(2,7,Command,2);if(Command==IR_VOL_MINUS){Num--;}if(Command==IR_VOL_ADD){Num++;}LCD_ShowNum(2,12,Num,3);}}
}
(3)Int0.c函数
#include <REGX52.H>void Int0_Init(void)
{IT0=1;IE0=0;EX0=1;EA=1;PX0=1;
}//void Int0_Routine(void) interrupt 0
//{//}
(4)Timer0.c函数
#include <REGX52.H>void Timer0_Init(void)
{TMOD &= 0xF0; //设置定时器模式TMOD |= 0x01; //设置定时器模式TL0 = 0; //设置定时初始值TH0 = 0; //设置定时初始值TF0 = 0; //清除TF0标志TR0 = 0; //定时器0不计时
}void Timer0_SetCounter(unsigned int Value)
{TH0=Value/256;TL0=Value%256;
}unsigned int Timer0_GetCounter(void)
{return (TH0<<8)|TL0;
}void Timer0_Run(unsigned char Flag)
{TR0=Flag;
}
(5)IR.c函数
#include <REGX52.H>void Timer0_Init(void)
{TMOD &= 0xF0; //设置定时器模式TMOD |= 0x01; //设置定时器模式TL0 = 0; //设置定时初始值TH0 = 0; //设置定时初始值TF0 = 0; //清除TF0标志TR0 = 0; //定时器0不计时
}void Timer0_SetCounter(unsigned int Value)
{TH0=Value/256;TL0=Value%256;
}unsigned int Timer0_GetCounter(void)
{return (TH0<<8)|TL0;
}void Timer0_Run(unsigned char Flag)
{TR0=Flag;
}
(6)IR.h函数
#ifndef __IR_H_
#define __IR_H_#define IR_POWER 0x45
#define IR_MODE 0x46
#define IR_MUTE 0x47
#define IR_START_STOP 0x44
#define IR_PREVIOUS 0x40
#define IR_NEXT 0x43
#define IR_EQ 0x07
#define IR_VOL_MINUS 0x15
#define IR_VOL_ADD 0x09
#define IR_O 0x16
#define IR_RPT 0x19
#define IR_USD 0x0D
#define IR_1 0x0C
#define IR_2 0x18
#define IR_3 0x5E
#define IR_4 0x08
#define IR_5 0x1C
#define IR_6 0x5A
#define IR_7 0x42
#define IR_8 0x52
#define IR_9 0x4Avoid IR_Init(void);
unsigned char IR_GetDataFlag(void);
unsigned char IR_GetRepeatFlag(void);
unsigned char IR_GetAddress(void);
unsigned char IR_GetCommand(void);#endif
2.遥控电机调速
(1)工程目录
(2)main.c函数
#include <REGX52.H>
#include "Key.h"
#include "Delay.h"
#include "Nixie.h"
#include "Motor.h"
#include "IR.h"unsigned char Command,Speed;
void main()
{Motor_Init();IR_Init();while(1){if(IR_GetDataFlag()){Command=IR_GetDataFlag();if(Command==IR_0){Speed=0;}if(Command==IR_1){Speed=1;}if(Command==IR_2){Speed=2;}if(Command==IR_3){Speed=3;}if(Speed==0){Motor_SetSpeed(0);}if(Speed==1){Motor_SetSpeed(50);}if(Speed==2){Motor_SetSpeed(75);}if(Speed==3){Motor_SetSpeed(100);}}Nixie(1,Speed);}
}
(3)Timer1.c函数
#include <REGX52.H>
/*** @brief 定时器0工作模式一的寄存器设置* @param 无* @retval 无*/
void Timer1_Init(void)
{TMOD &= 0x0F; //设置定时器模式TMOD |= 0x10; //设置定时器模式TL1 = 0xA4; //设置定时初始值TH1 = 0xFF; //设置定时初始值TF1 = 0; //清除TF0标志TR1 = 1; //定时器0开始计时EA=1;ET1=1;PT1=0;
}/*
/**
* @brief 定时器以一毫秒为间隔的中断* @param * @retval */
/*
void Timer0_Routine() interrupt 1
{static unsigned int Count;TL1 = 0x66;TH1 = 0xFC;Count++;if(Count>=1000){Count=0;}
}
*/
(4)Motor.c函数
#include <REGX52.H>
#include "Timer1.h"sbit Motor=P1^0;
unsigned char Counter,Compare;void Motor_Init(void)
{Timer1_Init();
}void Motor_SetSpeed(unsigned char Speed)
{Compare=Speed;
}void Timer1_Routine() interrupt 1
{TL1 = 0xA4;TH1 = 0xFF;Counter++;if(Counter>=100){Counter=0;}if(Counter<Compare){Motor=1;}else{Motor=0;}}
总体上是已经大概了解了51单片机的过程,接下来就是自己多动手练习代码,理解代码的组成和查看手册的工作了。