单片机红外线实验
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
0x68,0x30,0x18,0x7a,
0x10,0x38,0x5a,0x42,
0x4a,0x52,0xa2,0x62,
0xe2,0x22,0x02,0xc2,
0xe0,0xa8,0x90,0x98,
0xb0}; //21个编码
unsigned char ir_dat_buf[4] = {0}; //红外遥控接收码存放位置
dat<<=1;
SRCLK_B=1;
}
LATCH_B=0; //锁存
_nop_();
LATCH_B=1;
}
void delayms(unsigned char t) //延时子程序, t=1时约延时1ms
{
unsigned char i;
while(--t)
{
i = 165;
while(--i);
}
EX0 = 1;
}
unsigned char ir_read(void) //数据0: 0.56ms低电平,0.56ms高电平
{ //数据1: 0.56ms低电平,1.58ms高电平
unsigned char i,ir_temp = 0;
for(i = 0; i < 8; i++)
{
while(!ir_port); //等待0.56ms低电平结束
while(1)
{
}
}
/*void delayms(unsigned char t) //延时子程序, t=1时约延时1ms
{
unsigned char i;
while(--t)
{
i = 165;
while(--i);
}
}*/
void delay1120us(void) // X = 65536 - T * Focs / 12 = 65536 - 1.12ms * 11.0592MHz / 12 = 0xfbf8
4、将图片转换为二进制代码。
4、烧录代码,打开开发板电源。
三、实验测试程序
/*******************************************************************/
/*RFID_MCU单片机开发系统演示程序红外线遥控实验*/
/*MCU:STC89C52RC ,晶体频率:11.0592MHz */
//unsigned char led_disp_buf[5] = {0}; //LED显示缓存,包括一个显示位置代号(程序中没用到,所以可去掉)
void init89c52(void);
void delayms(unsigned char t);
unsigned char ir_read(void);
for(i = 0; i < 10; i++) //引导码由9ms低电平及4.5ms高电平组成,接着是低8位地址码,高8位地址码
{ //连发码由9ms低电平及2.25ms高电平组成,跟着是0.56ms低电平,然后都是高电平
delay840us();
if(ir_port) //检测引导码9ms是否有高电平,有则退出
}
}
return ir_temp;
}
void timer0(void) interrupt 1
{unsigned char j;
ET0 = wenku.baidu.com;
TL0= 99;//定时4ms
TH0 = 0xf1;
for(j=0;j<21;j++)
{if(ir_dat_buf[2]==bianma[j])//只有接收的第2字节的内容为编码内容
0x00,0x6C,0x92,0x82,0x44,0x28,0x10,0x00,
0x50,0x50,0x50,0x02,0x02,0x50,0x50,0x50};
unsigned char code bianma[] = {
0x68,0x30,0x18,0x7a,
0x10,0x38,0x5a,0x42,
void delay840us(void);
void delay1120us(void);
sbit chipcs=P2^7;
sbit datacs=P2^6;
sbit ir_port = P3^2;
void main(void)
{
//delayms(200);可省略
init89c52();
//ir_port = 1;因为上电复位后P3^2=1,所以可省略此指令
TH0 = 0xf1;//定时4ms
TR0 = 1;
IT0 = 1;
ET0 = 1;
EX0 = 1;
//PX0 =1;因为同级中外部中断0的优先权最高,所以此指令可省略
EA = 1;
}
void int0(void) interrupt 0 //外部中断0服务程序
{
unsigned char i;
EX0 = 0; //关中断
------------------------------------------------*/
void Out595(void)
{
LATCH=0;
_nop_();
LATCH=1;
}
/*------------------------------------------------
发送位码字节程序
{
EX0 = 1;
return;
}
while(ir_port); //等待4.5ms高电平结束,开始接收数据
ir_dat_buf[0] = ir_read();
ir_dat_buf[1] = ir_read();
ir_dat_buf[2] = ir_read();
ir_dat_buf[3] = ir_read();
void Send2Byte(unsigned char dat1,unsigned char dat2)
{
SendByte(dat1);
SendByte(dat2);
}
/*------------------------------------------------
595锁存程序
595级联发送数据后,锁存有效
{
TL1 = 0xfa; //定时0.84ms @ 11.0592MHz
TH1 = 0xfc;
TR1 = 1; //启动定时器
while(!TF1);
TR1 = 0;
TF1 = 0;
}
void init89c52(void) //STC89C52RC芯片初始化子程序
{
TMOD = 0x11;
TL0=99;
#include <intrins.h>
unsigned char segout[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //8列
unsigned char code tab1[][8]={
0x08,0x1C,0x3E,0x7F,0x1C,0x1C,0x1C,0x1C,
/*时间:2012/12/01 */
/*******************************************************************/
#include <reg52.h>
#include <intrins.h>
unsigned char code led_disp_map[] = {
delay840us(); //从高电平开始延时0.84ms
if(ir_port == 1)
{
ir_temp = (ir_temp << 1) + 1; //读到数据1
delay1120us(); //延时等待1.68ms高电平结束
}
else
{
ir_temp = ir_temp << 1; //读到数据0
0x02,0x04,0x08,0x10,
0x20,0x40,0x80,0xc0,
0xa0};
unsigned char ir_dat_buf[4] = {0}; //红外遥控接收码存放位置
sbit LATCH = P1^0;
sbit SRCLK= P1^1;
sbit SER = P1^2;
sbit LATCH_B = P2^2;
{
TL1 = 0xf8; //定时1.12ms @ 11.0592MHz
TH1 = 0xfb;
TR1 = 1; //启动定时器
while(!TF1);
TR1 = 0;
TF1 = 0;
}
void delay840us(void) // X = 65536 - T * Focs / 12 = 65536 - 0.84ms * 11.0592MHz / 12 = 0xfcfa
sbit SRCLK_B= P2^1;
sbit SER_B= P2^0;
void init89c52(void);
void delayms(unsigned char t);
unsigned char ir_read(void);
void delay840us(void);
void delay1120us(void);
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x00,0x01,
0x02,0x04,0x08,0x10,
0x20,0x40,0x80,0xc0,
0xa0};//共阴极8段LED字型码
unsigned char code bianma[] = {
{
EX0 = 1;
return;
}
}
while(!ir_port); //等待高电平的到来注:尽量采用延时来检测,不然可能会进入死循环
delay840us();
delay840us(); //检测连按;延时2.52ms,检测是否有低电平,有表示按键连按,且退出
delay840us();
if(!ir_port)
使用另外一片单独595
------------------------------------------------*/
void SendSeg(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++) //发送字节
{
SRCLK_B=0;
SER_B=dat&0x80;
{ chipcs=1;
datacs=0;
P0 = 0xfe;
chipcs=0;
datacs=1;
P0 = led_disp_map[j];
datacs=0;
}
}
ET0 = 1;}
四、拓展试验
利用红外遥控器按键选择0-2号按键选择8*8双色点阵实现显示不同的的图形。
#include <reg52.h>
sbit ir_port = P3^2;
void main(void)
{
delayms(200);
init89c52();
ir_port = 1;
while(1)
{
}
}
void SendByte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
SRCLK=0;
SER=dat&0x80;
dat<<=1;
SRCLK=1;
}
}
/*------------------------------------------------
发送双字节程序
595级联,n个595,就需要发送n字节后锁存
------------------------------------------------*/
(2)设置输出HEX文件,勾选上Output下的Create HEX Fi:,单击确定保存,运行程序得到HEX文件。
2、将P2的1,2用跳线帽接好,将DS1接P2.0,SH1接P2.1,ST1接P2.2;
DS2接P1.2,SH2接P1.1,ST2接P1.0,用杜邦线连接。
3、单片机的IO口P3^2与开发板P5右边的插针连接。可以通过操作遥控器在数码管显示对应的数值。
0x4a,0x52,0xa2,0x62,
0xe2,0x22,0x02,0xc2,
0xe0,0xa8,0x90,0x98,
0xb0}; //编码
unsigned char code led_disp_map[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x00,0x01,
实验
一、实验目的
1、掌握红外线传感器的原理
2、掌握红外线传感器MCU的连接及编程方法
二、实验步骤
1、用Keil uVision4建项目流程:
(1)运行Keil uVision4软件,启动后,点击Project菜单新建项目,新建项目后,单击File选择New新建程序文件,保存后,右击Source Group1添加入程序文件。
}
void delay1120us(void) // X = 65536 - T * Focs / 12 = 65536 - 1.12ms * 11.0592MHz / 12 = 0xfbf8
{
TL1 = 0xf8; //定时1.12ms @ 11.0592MHz
0x10,0x38,0x5a,0x42,
0x4a,0x52,0xa2,0x62,
0xe2,0x22,0x02,0xc2,
0xe0,0xa8,0x90,0x98,
0xb0}; //21个编码
unsigned char ir_dat_buf[4] = {0}; //红外遥控接收码存放位置
dat<<=1;
SRCLK_B=1;
}
LATCH_B=0; //锁存
_nop_();
LATCH_B=1;
}
void delayms(unsigned char t) //延时子程序, t=1时约延时1ms
{
unsigned char i;
while(--t)
{
i = 165;
while(--i);
}
EX0 = 1;
}
unsigned char ir_read(void) //数据0: 0.56ms低电平,0.56ms高电平
{ //数据1: 0.56ms低电平,1.58ms高电平
unsigned char i,ir_temp = 0;
for(i = 0; i < 8; i++)
{
while(!ir_port); //等待0.56ms低电平结束
while(1)
{
}
}
/*void delayms(unsigned char t) //延时子程序, t=1时约延时1ms
{
unsigned char i;
while(--t)
{
i = 165;
while(--i);
}
}*/
void delay1120us(void) // X = 65536 - T * Focs / 12 = 65536 - 1.12ms * 11.0592MHz / 12 = 0xfbf8
4、将图片转换为二进制代码。
4、烧录代码,打开开发板电源。
三、实验测试程序
/*******************************************************************/
/*RFID_MCU单片机开发系统演示程序红外线遥控实验*/
/*MCU:STC89C52RC ,晶体频率:11.0592MHz */
//unsigned char led_disp_buf[5] = {0}; //LED显示缓存,包括一个显示位置代号(程序中没用到,所以可去掉)
void init89c52(void);
void delayms(unsigned char t);
unsigned char ir_read(void);
for(i = 0; i < 10; i++) //引导码由9ms低电平及4.5ms高电平组成,接着是低8位地址码,高8位地址码
{ //连发码由9ms低电平及2.25ms高电平组成,跟着是0.56ms低电平,然后都是高电平
delay840us();
if(ir_port) //检测引导码9ms是否有高电平,有则退出
}
}
return ir_temp;
}
void timer0(void) interrupt 1
{unsigned char j;
ET0 = wenku.baidu.com;
TL0= 99;//定时4ms
TH0 = 0xf1;
for(j=0;j<21;j++)
{if(ir_dat_buf[2]==bianma[j])//只有接收的第2字节的内容为编码内容
0x00,0x6C,0x92,0x82,0x44,0x28,0x10,0x00,
0x50,0x50,0x50,0x02,0x02,0x50,0x50,0x50};
unsigned char code bianma[] = {
0x68,0x30,0x18,0x7a,
0x10,0x38,0x5a,0x42,
void delay840us(void);
void delay1120us(void);
sbit chipcs=P2^7;
sbit datacs=P2^6;
sbit ir_port = P3^2;
void main(void)
{
//delayms(200);可省略
init89c52();
//ir_port = 1;因为上电复位后P3^2=1,所以可省略此指令
TH0 = 0xf1;//定时4ms
TR0 = 1;
IT0 = 1;
ET0 = 1;
EX0 = 1;
//PX0 =1;因为同级中外部中断0的优先权最高,所以此指令可省略
EA = 1;
}
void int0(void) interrupt 0 //外部中断0服务程序
{
unsigned char i;
EX0 = 0; //关中断
------------------------------------------------*/
void Out595(void)
{
LATCH=0;
_nop_();
LATCH=1;
}
/*------------------------------------------------
发送位码字节程序
{
EX0 = 1;
return;
}
while(ir_port); //等待4.5ms高电平结束,开始接收数据
ir_dat_buf[0] = ir_read();
ir_dat_buf[1] = ir_read();
ir_dat_buf[2] = ir_read();
ir_dat_buf[3] = ir_read();
void Send2Byte(unsigned char dat1,unsigned char dat2)
{
SendByte(dat1);
SendByte(dat2);
}
/*------------------------------------------------
595锁存程序
595级联发送数据后,锁存有效
{
TL1 = 0xfa; //定时0.84ms @ 11.0592MHz
TH1 = 0xfc;
TR1 = 1; //启动定时器
while(!TF1);
TR1 = 0;
TF1 = 0;
}
void init89c52(void) //STC89C52RC芯片初始化子程序
{
TMOD = 0x11;
TL0=99;
#include <intrins.h>
unsigned char segout[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //8列
unsigned char code tab1[][8]={
0x08,0x1C,0x3E,0x7F,0x1C,0x1C,0x1C,0x1C,
/*时间:2012/12/01 */
/*******************************************************************/
#include <reg52.h>
#include <intrins.h>
unsigned char code led_disp_map[] = {
delay840us(); //从高电平开始延时0.84ms
if(ir_port == 1)
{
ir_temp = (ir_temp << 1) + 1; //读到数据1
delay1120us(); //延时等待1.68ms高电平结束
}
else
{
ir_temp = ir_temp << 1; //读到数据0
0x02,0x04,0x08,0x10,
0x20,0x40,0x80,0xc0,
0xa0};
unsigned char ir_dat_buf[4] = {0}; //红外遥控接收码存放位置
sbit LATCH = P1^0;
sbit SRCLK= P1^1;
sbit SER = P1^2;
sbit LATCH_B = P2^2;
{
TL1 = 0xf8; //定时1.12ms @ 11.0592MHz
TH1 = 0xfb;
TR1 = 1; //启动定时器
while(!TF1);
TR1 = 0;
TF1 = 0;
}
void delay840us(void) // X = 65536 - T * Focs / 12 = 65536 - 0.84ms * 11.0592MHz / 12 = 0xfcfa
sbit SRCLK_B= P2^1;
sbit SER_B= P2^0;
void init89c52(void);
void delayms(unsigned char t);
unsigned char ir_read(void);
void delay840us(void);
void delay1120us(void);
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x00,0x01,
0x02,0x04,0x08,0x10,
0x20,0x40,0x80,0xc0,
0xa0};//共阴极8段LED字型码
unsigned char code bianma[] = {
{
EX0 = 1;
return;
}
}
while(!ir_port); //等待高电平的到来注:尽量采用延时来检测,不然可能会进入死循环
delay840us();
delay840us(); //检测连按;延时2.52ms,检测是否有低电平,有表示按键连按,且退出
delay840us();
if(!ir_port)
使用另外一片单独595
------------------------------------------------*/
void SendSeg(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++) //发送字节
{
SRCLK_B=0;
SER_B=dat&0x80;
{ chipcs=1;
datacs=0;
P0 = 0xfe;
chipcs=0;
datacs=1;
P0 = led_disp_map[j];
datacs=0;
}
}
ET0 = 1;}
四、拓展试验
利用红外遥控器按键选择0-2号按键选择8*8双色点阵实现显示不同的的图形。
#include <reg52.h>
sbit ir_port = P3^2;
void main(void)
{
delayms(200);
init89c52();
ir_port = 1;
while(1)
{
}
}
void SendByte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
SRCLK=0;
SER=dat&0x80;
dat<<=1;
SRCLK=1;
}
}
/*------------------------------------------------
发送双字节程序
595级联,n个595,就需要发送n字节后锁存
------------------------------------------------*/
(2)设置输出HEX文件,勾选上Output下的Create HEX Fi:,单击确定保存,运行程序得到HEX文件。
2、将P2的1,2用跳线帽接好,将DS1接P2.0,SH1接P2.1,ST1接P2.2;
DS2接P1.2,SH2接P1.1,ST2接P1.0,用杜邦线连接。
3、单片机的IO口P3^2与开发板P5右边的插针连接。可以通过操作遥控器在数码管显示对应的数值。
0x4a,0x52,0xa2,0x62,
0xe2,0x22,0x02,0xc2,
0xe0,0xa8,0x90,0x98,
0xb0}; //编码
unsigned char code led_disp_map[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x00,0x01,
实验
一、实验目的
1、掌握红外线传感器的原理
2、掌握红外线传感器MCU的连接及编程方法
二、实验步骤
1、用Keil uVision4建项目流程:
(1)运行Keil uVision4软件,启动后,点击Project菜单新建项目,新建项目后,单击File选择New新建程序文件,保存后,右击Source Group1添加入程序文件。
}
void delay1120us(void) // X = 65536 - T * Focs / 12 = 65536 - 1.12ms * 11.0592MHz / 12 = 0xfbf8
{
TL1 = 0xf8; //定时1.12ms @ 11.0592MHz