前言
本篇文章属于STC89C52单片机(以下简称单片机)的学习笔记,来源于B站教学视频。下面是这位up主的视频链接。本文为个人学习笔记,只能做参考,细节方面建议观看视频,肯定受益匪浅。
一、红外遥控介绍
二、硬件电路
其中的三极管是低电平导通,红外信号发送时要进行调制,接受时要进行解调,OUT输出的就是解调后的方波,OUT要接到单片机的外部中断上,保证单片机能捕捉到这个信号
三、基本发送与接受
四、NEC编码
注意Data的逻辑0和逻辑1,不是单纯的高低电平,而是高低电平的组合,Repeat信号是当一个按键一直处于按下状态,就重复操作这个按键
五、遥控器键码
六、51单片机的外部中断
七、外部中断寄存器
八、实例一(红外遥控)
主要有Timer0.c 和 Int0.c 和IR.c 和 main.c
1.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 char Value)
{
TH0=Value/256;
TL0=Value%256;
}
unsigned int Timer0_GetCounter(void)
{
return (TH0<<8)|TL0;
}
void Timer0_Run(unsigned char Flag)
{
TR0=Flag;
}
2.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
{
}
*/
3.IR.c 红外遥控模块 ,解码过程
#include <REGX52.H>
#include "Int0.h"
#include "Timer0.h"
unsigned int IR_Time;
unsigned char IR_State;
unsigned char IR_Data[4];
unsigned char IR_pData;
unsigned char IR_DataFlag;
unsigned char IR_RepeatFlag;
unsigned char IR_Address;
unsigned char IR_Command;
void IR_Init(void)
{
Timer0_Init();
Int0_Init();
}
unsigned char IR_GetDataFlag(void)
{
if (IR_DataFlag)
{
IR_DataFlag=0;
return 1;
}
return 0;
}
unsigned char IR_GetRepeatFlag(void)
{
if (IR_RepeatFlag)
{
IR_RepeatFlag=0;
return 1;
}
return 0;
}
unsigned char IR_GetAddress(void)
{
return IR_Address;
}
unsigned char IR_GetCommand(void)
{
return IR_Command;
}
void Int0_Routine(void) interrupt 0
{
if(IR_State==0)
{
Timer0_SetCounter(0);
Timer0_Run(1);
IR_State=1;
}
else if (IR_State==1)
{
IR_Time=Timer0_GetCounter();
Timer0_SetCounter(0);
if (IR_Time>13500-1000 && IR_Time<13500+1000)
{
IR_State=2;
}
else if (IR_Time>11250-1000 && IR_Time<11250+1000)
{
IR_RepeatFlag=1;
Timer0_Run(0);
IR_State=0;
}
else
{
IR_State=1;
}
}
else if (IR_State==2)
{
IR_Time=Timer0_GetCounter();
Timer0_SetCounter(0);
if (IR_Time>1120-500 && IR_Time<1120+500)
{
IR_Data[IR_pData/8]&=~(0x01<<(IR_pData%8));
IR_pData++;
}
else if (IR_Time>2250-500 && IR_Time<2250+500)
{
IR_Data[IR_pData/8]|=(0x01<<(IR_pData%8));
IR_pData++;
}
else
{
IR_pData=0;
IR_State=1;
}
if (IR_pData>=32)
{
IR_pData=0;
if ((IR_Data[0]==~IR_Data[1]) && (IR_Data[2]==~IR_Data[3]))
{
IR_Address=IR_Data[0];
IR_Command=IR_Data[2];
IR_DataFlag=1;
}
Timer0_Run(0);
IR_State=0;
}
}
}
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_0 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 0x4A
void IR_Init(void);
unsigned char IR_GetDataFlag(void);
unsigned char IR_GetRepeatFlag(void);
unsigned char IR_GetAddress(void);
unsigned char IR_GetCommand(void);
#endif
4.main.c 主函数调用模块
#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
#include "IR.h"
unsigned char Address;
unsigned char Command;
unsigned char Num;
void main()
{
LCD_Init();
LCD_ShowString(1,1,"ADD");
LCD_ShowString(1,6,"CMD");
LCD_ShowString(1,11,"Num");
LCD_ShowString(2,1,"00");
LCD_ShowString(2,6,"00");
LCD_ShowString(2,11,"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,6,Command,2);
if (Command==IR_VOL_MINUS)
{
Num--;
}
if (Command==IR_VOL_ADD)
{
Num++;
}
LCD_ShowNum(2,11,Num,3);
}
}
}
九、实例二(红外遥控电机调速)
先把电机模块化
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_Rountine() interrupt 3
{
TH1=0xFF;
TL1=0xA4;
Counter++;
Counter%=100;
if (Counter<Compare)
{
Motor=1;
}
else
{
Motor=0;
}
}
然后在主程序中应用红外模块和马达模块
main.c
#include <REGX52.H>
#include "Nixie.h"
#include "Motor.h"
#include "Timer1.h"
#include "IR.h"
unsigned char Command,Speed;
void main()
{
Motor_Init();
IR_Init();
while(1)
{
if(IR_GetDataFlag())
{
Command=IR_GetCommand();
if (Command==IR_0)
{
Speed=0;
}
if (Command==IR_1)
{
Speed=1;
}
if (Command==IR_2)
{
Speed=2;
}
if (Command==IR_3)
{
Speed=3;
}
switch(Speed)
{
case 0:Motor_SetSpeed(0);break;
case 1:Motor_SetSpeed(50);break;
case 2:Motor_SetSpeed(75);break;
case 3:Motor_SetSpeed(100);break;
}
}
Nixie(1,Speed);
}
}