We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I got a RS232 Serial routine from this site. But when I tried on my actual hardware it did not work. I work OK using Dscope simulator. Could somebody be kindly to take a quick look at the code to see if why/how it dit not work on the actual hardware. My hardware uses a 14.318MHX oscillator, and I want to config the protocol for 9600 baudrate. Here is the code: //*--------------------------------------------------- SIO.C: Serial Communication Routines. #include <reg52a.h> #include <string.h> #include "sio.h" //------------------------------------- -----------------*/ //----- void main (void) { com_initialize (); /* initialize interrupt driven serial I/O */ com_baudrate (9600); /* setup for 9600 baud */ printf ("Interrupt-driver Serial I/O Example\r\n\r\n"); while (1) { unsigned char c; printf ("Press a key from the keyboard .\r\n"); c = getchar (); //store pressed character printf ("\r\nYou pressed '%c'.\r\n\r\n", c); //print out Character } } //end Main //-------------------------------------------------*/ char _getkey (void) { int k; do { k = com_getchar (); } while (k == -1); return ((unsigned char) k); } //*--- char putchar (char c) { volatile unsigned int i; while (com_putchar (c) != 0) { for (i=0; i<1000; i++) { /*** DO NOTHING ***/ } } return (c); } /////////// end main() ////// #define TBUF_SIZE 256 #define RBUF_SIZE 256 #define XTAL 14318000 //XTAL frequency static xdata unsigned char tbuf [TBUF_SIZE]; static xdata unsigned char rbuf [RBUF_SIZE]; static xdata unsigned char t_in = 0; static xdata unsigned char t_out = 0; static xdata unsigned char t_disabled = 0; static xdata unsigned char r_in = 0; static xdata unsigned char r_out = 0; //-----------------------------------------*/ static void com_isr (void) interrupt 4 using 2 { /*------------------------------------------------ Received data interrupt. ------------------------------------------------*/ if (RI != 0) { RI = 0; if ((r_in + 1) != r_out) rbuf [r_in++] = SBUF; } /*------------------------------------------------ Transmitted data interrupt. ------------------------------------------------*/ if (TI != 0) { TI = 0; if (t_in != t_out) SBUF = tbuf [t_out++]; else t_disabled = 1; } } //--------------------------------------------*/ void com_initialize (void) { /*------------------------------------------------ Setup TIMER1 to generate the proper baud rate. ------------------------------------------------*/ com_baudrate (9600); /*------------------------------------------------ Clear com buffer indexes. ------------------------------------------------*/ EA = 0; /* Disable Interrupts */ t_in = 0; t_out = 0; t_disabled = 1; r_in = 0; r_out = 0; /*------------------------------------------------ Setup serial port registers. ------------------------------------------------*/ SM0 = 0; SM1 = 1; /* serial port MODE 1 */ SM2 = 0; REN = 1; /* enable serial receiver */ TI = 0; /* clear transmit interrupt */ RI = 0; /* clear receiver interrupt */ ES = 1; /* enable serial interrupts */ PS = 0; /* set serial interrupts to low priority */ EA = 1; /* Enable Interrupts */ } /*------------------------------------------------------------------------------ ------------------------------------------------------------------------------*/ void com_baudrate ( unsigned baudrate) { EA = 0; /* Disable Interrupts */ /*------------------------------------------------ Clear transmit interrupt and buffer. ------------------------------------------------*/ TI = 0; /* clear transmit interrupt */ t_in = 0; /* empty transmit buffer */ t_out = 0; t_disabled = 1; /* disable transmitter */ /*------------------------------------------------ Set timer 1 up as a baud rate generator. ------------------------------------------------*/ TR1 = 0; /* stop timer 1 */ ET1 = 0; /* disable timer 1 interrupt */ PCON |= 0x80; /* 0x80=SMOD: set serial baudrate doubler */ TMOD &= ~0xF0; /* clear timer 1 mode bits */ TMOD |= 0x20; /* put timer 1 into MODE 2 */ TH1 = (unsigned char) (256 - (XTAL / (16L * 12L * baudrate))); TR1 = 1; /* start timer 1 */ EA = 1; /* Enable Interrupts */ } //---------------------------------------*/ char com_putchar ( unsigned char c) { /*------------------------------------------------ If the buffer is full, return an error value. ------------------------------------------------*/ if ((TBUF_SIZE - com_tbuflen ()) <= 2) return (-1); /*------------------------------------------------ Add the data to the transmit buffer. If the transmit interrupt is disabled, then enable it. ------------------------------------------------*/ EA = 0; /* Disable Interrupts */ tbuf [t_in++] = c; if (t_disabled) /* if transmitter is disabled */ { t_disabled = 0; TI = 1; /* enable it */ } EA = 1; /* Enable Interrupts */ return (0); } //------------------------------ int com_getchar (void) { int c; if (com_rbuflen () == 0) return (-1); EA = 0; /* Disable Interrupts */ c = rbuf [r_out++]; EA = 1; /* Enable Interrupts */ return (c); } //------------------------------------*/ unsigned char com_rbuflen (void) { return (r_in - r_out); } //---------------------------------------*/ unsigned char com_tbuflen (void) { return (t_in - t_out); } //---------------------------------------- ///////////////////////////////////////