How to Use RS232 Receiver in AT89C1051 Using Interrupt

The following code demonstrate, how to receive data from UART of AT89C1051 on interrupt. The code is written in Keil uVision2 IDE and simulation is done with Proteus 8.0. At the end of code, you can find complete project files for download.

Code Using Keil uVision2

#include
#include “stdio.h”
// proto-types
void ConfigSerial(void);
void ConfigInterrupt(void);
void WriteData(unsigned char ch);
unsigned char ReadData(void);
void Delayms(unsigned int x_time);
// port pin labling
sbit LED_0 = P1^0;
unsigned char rxd_buffer = 0;
// interrupt service routine here
static void com_isr (void) interrupt 4
{
if(RI != 0)
{
RI = 0;
rxd_buffer = SBUF;
WriteData(rxd_buffer);
}
}
void main(void)
{
code char company_name[] = “eTechEd.com: Engineering and Technology Education!\rType in Transmitter Window->:\r”;
unsigned int index;
LED_0 = 0; // as output
// serial port configration
ConfigSerial();
// interrupt setting
ConfigInterrupt();
for(index=0;index<sizeof(company_name);index++)
{
WriteData(company_name[index]);
}
while(1)
{
LED_0 = ~LED_0; // led blinking
Delayms(200);
}}
void ConfigSerial(void)
{
SCON = 0x50; // SCON: mode 1, 8-bit UART, enable rcvr
TMOD |= 0x20; // TMOD: timer-1, mode 2, 8-bit auto reload
TH1 = -3; // reload value for 9600 baud
//TH1 = -6; // reload value for 4800 baud
//TH1 = -12; // reload value for 2400 baud
//TH1 = -24; // reload value for 1200 baud
TR1 = 1; // TR1: timer-1 run
}
void ConfigInterrupt(void)
{
PS = 0; // set serial interrupts to low priority
ES = 1; // enable serial interrupts
EA = 1; // Enable Interrupts
}
void WriteData(unsigned char ch)
{
SBUF = ch;
while(TI == 0);
TI = 0; // reset transmit flag
}
/*
unsigned char ReadData(void)
{
return _getkey();
}
*/
void Delayms(unsigned int x_time)
{
unsigned int x,y;
for(x=0;x<x_time;x++)
for(y=0;y<122;y++);
}

Download Files

For download Keil project and Proteus 8.0 simulation files, click here.

Comments

Popular posts from this blog

How to Program Interrupts in PIC16F877A

How to Program Parallel Slave Port (PSP) in PIC16F877A

How to Program SPI in PIC16F877A (Slave Mode)