|
Code for Transmitter:
#include<reg51.h>
void send(void); //function to transmit byte serially
void main()
{
TMOD=0x20; // set timer 1 in mode 2
SCON=0x40; // set mode 1 for serial comu
TH1=0xF3; // set baud rate as 9600 bps
TL1=0xF3;
TR1=1; // start timer 1
loop:P1=0xFF; // P1 as input port
while(P1==0xFF); // wait until any key press
switch(P1) // check which key is pressed
{
case 0xFE: // for first key
ACC='a'; // send code 'a'
send();
break;
case 0xFD: // for second key
ACC='b'; // send code 'b'
send();
break;
case 0xFB: // same as above
ACC='c';
send();
break;
case 0xF7:
ACC='d';
send();
break;
case 0xEF:
ACC='e';
send();
break;
}
goto loop; // jump to continu loop
}
void send(void)
{
SBUF=ACC; // move byte to sbuf SFR
while(TI==0); // wait until transmission completes
TI=0;
}
Code for Receiver (Case 2):
#include<reg51.h>
unsigned char c;
sbit rst = P0^2; // reset indicator
sbit inc = P0^3; // speed increase indicator
sbit dec = P0^4; // speed decrease indicator
void delay (void) // 1 ms delay
{
int x;
for(x=0;x<1000;x++);
}
void recv(void) interrupt 4 // interrupt subroutine to receive byte
{
RI=0;
c=SBUF; // get received byte
delay();
}
void main()
{
TMOD=0x20; // set timer 1
SCON=0x40; // initialize serial commu.
TH1=0xF3; // set baud rate
TL1=0xF3;
P0=0xFF;
P2=0x00;
TR1=1; // start timer
IE=0x90; // enable serial interrupt
REN=1; // enable reception
loop:c=0x00;
while(c==0x00); // loop until nothing is received
switch(c) //
{
case 'a':
P2=0x01; // give pulse on P2.0 to start drive
P0=0xFE; // give indication
delay();
P2=0x00;
break;
case 'b':
P2=0x02; // give pulse on P2.1 to stop drive
P0=0xFD; // give indication
delay();
P2=0x00;
break;
case 'c':
P2=0x04; // give pulse on P2.2 to reset drive
rst=0; // blink reset led
delay();
rst=1;
P2=0x00;
break;
case 'd':
P2=0x08; // give pulse on P2.3 to increase speed
inc=0; // blink increase speed led
delay();
inc=1;
P2=0x00;
break;
case 'e':
P2=0x10; // give pulse on P2.4 to decrease speed
dec=0; // blink decrease speed led
delay();
dec=1;
P2=0x00;
break;
}
goto loop; // jump to continuous loop
}
Code for Receiver (Case 1):
#include<reg51.h>
unsigned char c;
sbit rst = P0^2; // reset indication
sbit aut = P0^5; // auto mode indication
sbit man = P0^6; // manual mode indication
void delay (void) // 1 ms delay
{
int x;
for(x=0;x<1000;x++);
}
void recv(void) interrupt 4 // interrupt based data reception
{
RI=0;
c=SBUF;
delay();
}
void main()
{
TMOD=0x20; // set timer 1
SCON=0x40; // initialize serial commu.
TH1=0xF3; // set baud rate
TL1=0xF3;
P0=0xFF;
P2=0x00;
TR1=1; // start timer 1
IE=0x90; // enable serial interrupt
REN=1; // enable reception
loop:c=0x00;
while(c==0x00); // loop until nothing is received
switch(c)
{
case 'a':
P2=0x01; // same as case 2
P0=0xFE;
delay();
P2=0x00;
break;
case 'b':
P2=0x02;
P0=0xFD;
delay();
P2=0x00;
break;
case 'c':
P2=0x04;
rst=0;
delay();
rst=1;
P2=0x00;
break;
case 'd':
P2=0x18; // select auto mode
aut=0; // give indication
man=1;
break;
case 'e':
P2=0x00; // select manual mode
aut=1;
man=0;
break;
}
goto loop; // jump to continuous loop
} |