最简单的,发送端:按一下键发一个脉冲;接收端:进行脉冲计数,用led显示。
发送端://12M晶体
#include "reg51.h"
#define KeyB P1^6
#define OutPin P1^7
bit Key1,Key2;
//timer init 20ms定时中断
void initTimer(void)
{
TMOD=0x1;
TH0=0x3c;
TL0=0xb0;
}
//timer0/counter0 interrupt
void timer0(void) interrupt 1
{
TH0=0x3c;
TL0=0xb0;
//add your code here.
Key1=Key2;
Key2=KeyB;
}
//the main fun
void main(void)
{
initTimer();
TR0=1;
ET0=1;
EA=1;//开中断
while(1)
{
OutPin=key1||key2;
}
}
接收端:
#include "reg51.h"
char Count
//int0 interrupt
void int0(void) interrupt 0
{
//add your code here
Count++;
}
//the main fun
void main(void)
{
IT0=1; //INT0下降沿中断
EX0=1; //允许INT1中断
EA=1;
Count=0;//初始化计数器
while(1)
{
P1=~Count;//图中的led应该是串电阻公共端接电源,P1口拉低亮,置高灭。
}
}