Simple Serial Communication Using 8051
Code:
#include <REGX51.H> #include <stdio.h> /* prototype declarations for I/O functions */ sbit MES1 = P2^0; sbit MES2 = P2^1; sbit MES3 = P2^2; sbit MES4 = P2^3; sbit MES5 = P2^4; int i; void time1ms(); void delay(int n); void main (void) { /* execution starts here after stack init */ SCON = 0x52; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 0xfd; /* TH1: reload value for 9600 baud@11.0592mhz */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */ while (1) { P2=0xFF; if (MES1 == 0) { printf ("Welcome Serial Data Transfer\n "); printf ("Inside Conditional 1\n "); delay(1000); } if (MES2 == 0) { printf ("Inside Conditional 3\n "); delay(1000); } if (MES3 == 0) { printf ("Inside Conditional 3\n "); delay(1000); } if (MES4 == 0) { printf ("Inside Conditional 4\n "); delay(1000); } if (MES5 == 0) { printf ("Inside Conditional 5\n "); printf ("Data transmission successful\n\n "); delay(1000); } } } void time1ms() /* 1 ms delay */ { int i; for(i=0;i<50;i++); // the value shown in this line, 50 was calibrated for 1ms // you may change it! } void delay(int n) /* do nothing n*1ms */ { int i; for(i=0;i<n;i++) time1ms(); }