Now, today we are gonna go a little further and will have a look at Serial Communication with 8051 Microcontroller and we will also design the simulation of this project in Proteus ISIS software. 8051 Microcontroller also supports Serial port similar to Arduino and PIC Microcontroller. And the communication protocol is exactly the same as its a Serial Port. But obviously the syntax is bit different as we are not working in Arduino software or MPLAB. So let's get started with it.
#include <reg52.h> #define Baud_rate 0xFD // BAUD RATE 9600 void SerialInitialize(void); void SendByteSerially(unsigned char); void cct_init(void); sbit Appliance1 = P1^0; sbit Appliance2 = P1^1; sbit Appliance3 = P1^2; sbit Appliance4 = P1^3; sbit Appliance5 = P1^4; sbit Appliance6 = P1^5; sbit Appliance7 = P1^6; sbit Appliance8 = P1^7; void main() { cct_init(); SerialInitialize(); EA = 1; ES = 1; while(1) {;} } void cct_init(void) //initialize cct { P0 = 0x00; //not used P1 = 0x00; //Used for Appliances P2 = 0x00; //not used P3 = 0x03; //used for serial } void SerialInitialize(void) // INITIALIZE SERIAL PORT { TMOD = 0x20; // Timer 1 IN MODE 2 -AUTO RELOAD TO GENERATE BAUD RATE SCON = 0x50; // SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT, REN ENABLED TH1 = Baud_rate; // LOAD BAUDRATE TO TIMER REGISTER TR1 = 1; // START TIMER } void SendByteSerially(unsigned char serialdata) { SBUF = serialdata; // LOAD DATA TO SERIAL BUFFER REGISTER while(TI == 0); // WAIT UNTIL TRANSMISSION TO COMPLETE TI = 0; // CLEAR TRANSMISSION INTERRUPT FLAG } void serial_ISR (void) interrupt 4 { //receive character char chr; if(RI==1) { chr = SBUF; RI = 0; } P0 = ~P0; //Show the data has been updated switch(chr) { case '1': Appliance1 = 1; SendByteSerially('k'); break; case '2': Appliance2 = 1; SendByteSerially('k'); break; case '3': Appliance3 = 1; SendByteSerially('k'); break; case '4': Appliance4 = 1; SendByteSerially('k'); break; case '5': Appliance5 = 1; SendByteSerially('k'); break; case '6': Appliance6 = 1; SendByteSerially('k'); break; case '7': Appliance7 = 1; SendByteSerially('k'); break; case '8': Appliance8 = 1; SendByteSerially('k'); break; case 'a': Appliance1 = 0; SendByteSerially('k'); break; case 'b': Appliance2 = 0; SendByteSerially('k'); break; case 'c': Appliance3 = 0; SendByteSerially('k'); break; case 'd': Appliance4 = 0; SendByteSerially('k'); break; case 'e': Appliance5 = 0; SendByteSerially('k'); break; case 'f': Appliance6 = 0; SendByteSerially('k'); break; case 'g': Appliance7 = 0; SendByteSerially('k'); break; case 'h': Appliance8 = 0; SendByteSerially('k'); break; default: ; break; //do nothing } RI = 0; }
Download Proteus Simulation and Code
So, that's how we can do Serial communication with 8051 Microcontroller. I don't think its much difficult but still if you have problems then ask in comments and I will resolve them. That's all for today and will meet in the next tutorial soon.
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w