I am trying to use LabVIEW to read the serial output from the simple ISR-Driven Serial I/O V2 Example (http://www.keil.com/download/docs/200.asp) used on a C8051F300 MCU. I know my LabVIEW can acquire string or character data from the serial port. The C code compiles without any errors or warnings and I have included the proper header files.
void main (void) { com_initialize (); /* initialize interrupt driven serial I/O */ com_baudrate (1200); /* setup for 1200 baud */ EA = 1; /* Enable Interrupts */ printf ("Interrupt-driver Serial I/O Example\r\n\r\n"); while (1) { unsigned char c; printf ("Press a key.\r\n"); c = getchar (); printf ("\r\n"); printf ("You pressed '%c'.\r\n\r\n", c); } }
void com_baudrate ( unsigned baudrate) { /*------------------------------------------------ Clear transmit interrupt and buffer. ------------------------------------------------*/ TI = 0; /* clear transmit interrupt */ t_in = 0; /* empty transmit buffer */ t_out = 0; /*------------------------------------------------ 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 */ }
TH1 = (unsigned char) (256 - (24500000 / (16L * 12L * baudrate)));
Hi Karl, try to set
TI=1;
You have to check that this baudrate can be used, for this XTAL freqency, without being too far off the correct timing. Double-check the decision made by your C51 expression with the baud rate calculator found elsewhere on this web site.
Here are some suggestions: 1. Compile the code, load and run the simulator. Stop it and check the TH1 register value. Is it what you expect? 2. Look at the serial port window, are the correct values coming out? Use the SIN vtreg as your keyboard and see if the progarm executes correctly. 3. 24.5 MHz seems kind of high for 1200 baud, are you sure about the range of baudrates for that frequency? 4. You can set the simulator to use the serial port on your computer, connect it to another computer running labview or preferrably a simple shareware serial port utility and try running your program that way. 5. Download to your target and use an oscilloscope to capture a serial port transmission if it's there, have you checked this? This is the real world baudrate right in front of you. Try these steps , you'll find something there.
Thanks a lot for all your help. My code had a number of problems, but I have been able to debug most of them using your suggestions. I did have to fix the baudrate and ensure the TH1 value was what I expected. Also, downloading the serial port monitor simplified the debugging process and will come in handy in the future. Thanks again.