马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
前言
本篇文章属于STC89C52单片机(以下简称单片机)的学习笔记,泉源于B站教学视频。下面是这位up主的视频链接。本文为个人学习笔记,只能做参考,细节方面建议观看视频,肯定受益匪浅。
[1-1] 课程简介_哔哩哔哩_bilibili
一、红外遥控介绍
二、硬件电路
此中的三极管是低电平导通,红外信号发送时要进行调制,继承时要进行解调,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 红外遥控模块 ,解码过程
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);
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |