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 /* UART in mode 1 (8 bit), REN=1 */ LED: /*Receive letter A from 1st microchip*/ MOV P1,SBUF MOV P1,#57 JMP $ CLR_RI: |
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){ void main(){ /* press button to transmit data*/ while(P1_0==1); /* UART initialize function */ /* 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); } |