??? 09/26/09 14:31 Read: times |
#169164 - Re: Hello, World! Responding to: ???'s previous message |
Thanks Andy. I am quite OK to:
1. Initial the serial port with Timer 3 Generated BAUD rate(ADuC848): e.g. void serial_init(void) { //Configure UART T3CON = 0x83; //configure timer 3... T3FD = 0x12; //...for 9600 baud SCON = 0x52; } 2. Get a single character and also send a single character or string: e.g unsigned char c; printf ("Press a key.rn"); c = getchar (); delay(1000); printf ("rn"); delay(1000); printf ("You pressed '%c'.rnrn", c); But in my project, I need to read a string command from PC then reply with: e.g. A. If PC send S1 + cr --> reply with MCU's status (e.g. 0,1,2 where 2 means MCU has finished the test and ADC result is ready to send back to PC) + cr B. If PC send V1 + cr --> send ADC channel 1 measurement result (PC will ask for from V1 to V10) + cr My problem is to use buffer I probably have to use Timer1, mode 1, 8 bit variable BAUD rate similar to "intsio" project (see below). So I initialise the serial with timer 1. But I could not get it work. When I send a string as in step 2 above, I could not see any message on the terminal. I don't know what crystal value should I put in to this formula for generating the BAUD rate: TH1 = (unsigned char) (256 - (Crystal / (16L * 12L * baudrate))); so I put the Crystal value as 12.582912MHz (384 x 32.768KHz, page 90 "Clock Oscillator" ADUc848 datasheet). Note that the ADuC848 use 32.768KHz external crystal. /*------------------------------------------------------------------------------ SIO.C: Serial Communication Routines. Copyright 1995-2002 KEIL Software, Inc. ------------------------------------------------------------------------------*/ //#include <reg51.h> //#include <ADuC848.h> //To use this with an ADuC847 or ADuC848, simply change the #include <string.h> #include "sio.h" /*------------------------------------------------------------------------------ ------------------------------------------------------------------------------*/ #define TBUF_SIZE 256 /* DO NOT CHANGE */ #define RBUF_SIZE 256 /* DO NOT CHANGE */ 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 (1200); 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 - (Crystal / (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); } /*------------------------------------------------------------------------------ ------------------------------------------------------------------------------*/ Please give any possible advice. Thanks, Ralf |