Posted on Leave a comment

Easy!

 

8051 UART, interrupts:

   Serial data from of 1st microchip At89s52 transmit through pin P3.1, then received by pin P3.0 of 2nd microchip

   Baud rate 9600Hz by used crystal 11.0592MHz, time1 in mode 2

   11.0592Mhz/12 is machine cycle frequency

   11.0592Mhz/12/32 is bits per second of UART=28800bps

   To get 9600bps, need 3 cycles to overflows =>256-3=FD

   Load TH1=FD

  Use a button to start transfer data, interrupt serial

Important note: have to setup up the same Xtal 11.0592 in c, asm files, proteus simulation software.

asm files for transmit & reception:

 

asm file for transmit:

/* by Hoang N Tran*/
org 000h
   ljmp begin
org 23h
   ljmp CLR_TI
/*This file set up UART in mode 1 (8 bits UART) with
timer 1 in mode 2 */
org 0100h
begin:/* press button to transfer data*/
   JB P1.0,CLR_TI/* UART in mode 1 (8 bit), REN=1 */
   MOV SCON, #50h
   ORL TMOD, #20h
    MOV TH1, #0FDh
/* Timer 1 in mode 2 *//* 9600 bds at 11.059MHz */
   MOV TL1, #0FDh
   SETB ES;
/* Enable serial interrupt*/
   SETB EA;
   SETB TR1/* Sent letter A to 2nd microchip */
   MOV SBUF,#65
/* endless */  JMP $CLR_TI:/*clear Transmation flag for next Transmation*/
   CLR TI 
   RETI
end

 

asm file for receive:

/* by Hoang N Tran*/

org 00h
   SJMP BEGIN
org 23h
   LJMP CLR_RI
DISLAY:
   SETB P2.0
   CLR P2.1
   SETB P2.2
   CALL DELAY
   CLR P2.2
   RET
DELAY:
   MOV R1,#0EFH
   LR1: DJNZ R1,LR1
   RET
BEGIN:

 /* UART in mode 1 (8 bit), REN=1 */
   MOV SCON, #50h
   ORL TMOD, #20h
   MOV TH1, #0FDh
/* Timer 1 in mode 2 */
/* 9600 Bds at 11.059MHz */
   MOV TL1, #0FDh
   SETB ES;
/* Enable serial interrupt*/
   SETB EA;
   SETB TR1;
   MOV P1,#38H
   CALL INNT
   MOV P1,#0EH
   CALL INNT
   MOV P1,#06H
   CALL INNT
   MOV P1,#01H
   CALL INNT
   MOV P1,#80H
   CALL INNT
   SJMP LED
INNT:
   CLR P2.0
   CLR P2.1
   SETB P2.2
   CALL DELAY
   CLR P2.2
   CALL DELAY
   RET

LED:

/*Receive letter A from 1st microchip*/
   MOV B,SBUF
   JNB 0F0H,LED
   MOV P1,#32
   CALL DISLAY
   MOV P1,#32
   CALL DISLAY
   MOV P1,#32
   CALL DISLAY

   MOV P1,SBUF 
   CALL DISLAY
   MOV P1,#116
   CALL DISLAY
   MOV P1,#56
  CALL DISLAY

   MOV P1,#57
   CALL DISLAY
   MOV P1,#115
   CALL DISLAY
   MOV P1,#53
   CALL DISLAY
   MOV P1,#50
   CALL DISLAY

   JMP $

CLR_RI:
   MOV P0,SBUF
   CLR RI
   RETI
END

c files for transmit & reception:

 c file for transmit:

/* by Hoang N Tran */
#include <REGX51.H>
void UART_Init(){
   TMOD = 0x20; /* Timer 1, 8-bit auto reload mode */
   TH1 = 0xFD; /* Load value for 9600 baud rate */
   SCON = 0x50; /* Mode 1, reception enable */
   TR1 = 1; /* Start timer 1 */
}void Transmit_data(char char_data){
   SBUF = char_data; /* Load char in SBUF register */
   while (TI==0); /* Wait until stop bit transmit */
   TI = 0; /* Clear TI flag */
}

void String(char *str){
/* Send each char of string till the NULL */
   int i;
    for(i=0;str[i]!=0;i++) {
    Transmit_data(str[i]); }
}

void main(){

/* press button to transmit data*/

   while(P1_0==1);

/* UART initialize function */
   UART_Init();

/* Transmit  a string */

   String(“This’s At89s52 Hello KHANH”);

while(1);
}

 

c file for receive:

/* by Hoang N Tran*/
#include <REGX51.H>
int y,n;
char x[16];
char t[16];
/* delay approx 1ms */
void delay(unsigned int count){
int i,j;
for(i=0;i<count;i++)
for(j=0;j<112;j++);
}
/* LCD16x2 command funtion */
void LCD_Command (unsigned char cmd){
P1= cmd;/* P1 port as data port */
P2_0=0; /* RS pin */
P2_1=0; /* RW pin */
P2_2=1; /*E pin */
delay(1);
P2_2=0;
delay(5);
}
/* LCD data write function */
void LCD_Char (unsigned char char_data){
P1=char_data;
P2_0=1;
P2_1=0;
P2_2=1;
delay(1);
P2_2=0;
delay(5);
}
/* Send each char of string till the NULL */
void LCD_String (unsigned char *str){
int i;
for(i=0;str[i]!=0;i++)
{
LCD_Char (str[i]); /* Call LCD data write */
}
}void LCD_Init (void) /* LCD Initialize function */
{
delay(20); /* time >15ms to LCD ON*/
LCD_Command (0x38); /* Initialization of 16X2 LCD in 8bit mode */
LCD_Command (0x0E); /* Display ON Cursor*/
LCD_Command (0x06); /* Auto Increment cursor */
LCD_Command (0x01); /* clear display */
LCD_Command (0x80); /* cursor at home position */}void Ext_int_Init()
{
EA = 1; /* Enable global interrupt */
ES = 1; /* Enable serial interrupt */
}void UART_Init()
{
TMOD = 0x20; /* Timer 1, 8-bit auto reload mode */
TH1 = 0xFD; /* Load value for 9600 baud rate */
SCON = 0x50; /* Mode 1, reception enable */
TR1 = 1; /* Start timer 1 */
}void Serial_ISR() interrupt 4
{
if (y<=13)x[y]=SBUF;
if (y>13) {t[n]=SBUF;n++;}
y++;
RI = 0;
}void main(){
P0_0=1;
Ext_int_Init(); /* Call Ext. interrupt initialize */
UART_Init();/* UART initialize function */
while(RI==0);/* waiting for received data on port 1 */
LCD_Init(); /* initialization of LCD*/
P0=x[0];
LCD_String(x);
LCD_Command(0xC0);
LCD_String(t);
while(1);
}