Hi everyone, In my code receive session is not working.. what changes should i made to receive a character from my keyboard?
// Program to test serial communication of controller with PC using hyper terminal #include<reg51.h> void ini() // Initialize Timer 1 for serial communication { TMOD=0x20; //Timer1, mode 2, baud rate 9600 bps TH1=0XFD; SCON=0x50; TR1=1; } void recieve() //Function to receive serial data { unsigned char value; while(RI==0); value=SBUF; P1=value; RI=0; } void transmit() // Funtion to transmit serial data { P2=P1-32; SBUF=P2; while(TI==0); TI=0; SBUF=P1; while(TI==0); TI=0; } void main() { while(1) { ini(); recieve(); transmit(); } }
Thank in advance
You have to study uart methods. This is the a simple one using polling.
/* ********************************************************** */ /* */ /* Echo Back: test for uart @ 9600 */ /* Polled uart implementation */ /* */ /* ********************************************************** */ // // Needs proper connection of RXpin and TXpin between PC D9M and connector of embedded device. // If PC as DTE(D9M) and embedded as DCE(D9F) then 2 - 2, 3 - 3, 5 - 5 (straight cable). // If PC as DTE(D9M) and embedded as DTE(D9M) then 2 - 3, 3 - 2, 5 - 5 (cross over cable). // A chip as MAX232A(or like) must be used for proper signaling on RS232 line. // Terminal on PC like Hyperterminal must be configured for 9600, 8bit, no parity, no handshake. // At 9600 bps it is needed about 1ms to send(or receive) a byte through uart. // // Uses a standard 8051 header file. // P89V51RD2 has more options than 8051, so the proper header must be used to exploit those features. // #include <reg51.h> void uart_init(); // Initializes UART peripheral char uart_getc(); // Polled method to Receive a byte(8bit) void uart_putc (char c); // Polled method to Transmit a byte(8bit) void uart_puts(char *s); // Typical puts for uart Transmit a null-terminated-string void uart_init() // Initializes UART peripheral { TMOD = 0x20; // Timer1, mode 2 TH1 = 0xFD; // 9600 bps 11059200 Clock, core(12cycle), uart(SMOD=0) TL1 = 0xFD; // First byte timing SCON = 0x52; // 0x52 : 0101 0010 Receive Enable, TI as 1 (set as ready) TR1 = 1; } char uart_getc() // Polled method to Receive a byte(8bit) { while (RI == 0) // Wait until a byte has been received ; // RI = 0; // Acknowledge RI return SBUF; // } void uart_putc (char c) // Polled method to Transmit a byte(8bit) { while (TI == 0) // TI as 0 indicates TX busy, ; // if busy then wait for TX completion of the previous byte SBUF = c; // Does not wait for TX of current byte to complete, // so gives time to caller for other actions } void uart_puts(char *s) // Typical puts for uart Transmit a string-null-terminated { while (*s) // Check for null byte termination uart_putc(*s++); // and send each byte using uart } void main () { char c; uart_init(); // Init UART to desired settings uart_puts("Hello, echoback testing serial communication\n\r"); while (1) { // This loop echoes back whatever is typed on PC Keyboard c = uart_getc(); // get next byte from PC uart_putc(c); // and send it back as echo } }
void uart_puts(char *s)
is inherently dangerous - "s" does not have to be null terminated! Better to deliver the buffer length/maximum buffer length as well.
puts() isn't really a big problem - somewhere in memory there is likely to be a zero. puts() isn't much different from printf() - also assuming that the string is zero-terminated. Just as strlen() and a huge number of other functions.
gets() on the other hand is lethal unless it is one of the non-standard implementations that takes a buffer size.
View all questions in Keil forum