Problem with UART

Started by mimran July 1, 2006
I m trying to run a simple UART program on EZ USB FX2. This program
continously transmits the char 'A'. This is compiling fine in Keil. But
when I run it and see it on oscilloscope, the data rate is not exact what
i have set it to 9600 baud. Moreover, the data bits and start stop bits
are inverted . So I have to send inverted data (A xor 0xff) to get actual
character A.
The program is as follows:

#define ALLOCATE_EXTERN
#include <fx2.h>
#include <fx2regs.h>

void init_uart(void){
		
        SCON0 = 0x52;                // 8 bit UART mode
        TMOD = 0x20;                // Timer 1 initialized to mode 2 
        TH1 = 0xEC;                // Reload value for 9600 baud rate at
48MHz clock
		T1M = 1;
		SMOD0 = 1;
        
        TR1 = 1;                // Start the Timer 1
}

void main(void)
{	
	unsigned char a = 0x41;                // Character A
	a = a ^ 0xff;                          // Invert the data
	init_uart();
   	while(1){
        while(TI){
				
                TI = 0;			// Clear the overflow flag
                SBUF0 = a;		// Put data onto tx reg
				
        
        }
   }
	
}

Hope if someone can help me.

Thanks in advance

Best Regards
Muhammad Imran

On Sat, 01 Jul 2006 10:35:16 -0500, "mimran" <Muhammad@delfic3.nl>
wrote in comp.arch.embedded:

> I m trying to run a simple UART program on EZ USB FX2. This program > continously transmits the char 'A'. This is compiling fine in Keil. But > when I run it and see it on oscilloscope, the data rate is not exact what > i have set it to 9600 baud. Moreover, the data bits and start stop bits > are inverted . So I have to send inverted data (A xor 0xff) to get actual > character A.
What do you mean by inverted? Where are you looking at the signal? On the serial data pins of the processor, a start bit is a 0, followed by the bits of the character from low to high. So 'A', 0x41, would look like this: 0100000101 | | start bit -------+ | | stop bit -----------------+ If you are looking at the signal on the other side of an RS232 driver, then it is inverted from this. The start bit will be high and the stop bit low.
> The program is as follows: > > #define ALLOCATE_EXTERN > #include <fx2.h> > #include <fx2regs.h> > > void init_uart(void){ > > SCON0 = 0x52; // 8 bit UART mode > TMOD = 0x20; // Timer 1 initialized to mode 2 > TH1 = 0xEC; // Reload value for 9600 baud rate at > 48MHz clock
What is the actual clock rate in to timer 1?
> T1M = 1; > SMOD0 = 1; > > TR1 = 1; // Start the Timer 1 > } > > void main(void) > { > unsigned char a = 0x41; // Character A > a = a ^ 0xff; // Invert the data > init_uart(); > while(1){ > while(TI){ > > TI = 0; // Clear the overflow flag > SBUF0 = a; // Put data onto tx reg > > > } > } > > } > > Hope if someone can help me. > > Thanks in advance
-- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://c-faq.com/ comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html