USART TX issue - Weird Framing issue

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

I'm trying to get the USART working and having issue. The odd thing is that if open and close the com port on the terminal app, it sometimes works - like its not sure what's the beginning of the bit stream. 

 

Im using AtXMega 64A4U and just trying with the default 2Mhz clock (I commented out the 32Mhz setup for the moment to see if it was a clock issue). Baud is for 9600, 8 bit, no parity, 1 stop bit.

 

This is sample code off the net which I altered a bit to try this out. (could not the the original to compile).

 

Im sure the issue is blindingly simple - but cant see it.

 

Many Thanks

 

#define F_CPU 2000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include "CommTest.h"

static int uart_putchar(char c, FILE *stream);


void setUp32MhzInternalOsc()
{
	//OSC_CTRL |= OSC_RC32MEN_bm; //Setup 32Mhz crystal
	//
	//while(!(OSC_STATUS & OSC_RC32MRDY_bm));
	//
	//CCP = CCP_IOREG_gc; //Trigger protection mechanism
	//CLK_CTRL = CLK_SCLKSEL_RC32M_gc; //Enable internal  32Mhz crystal
}


void setUpSerial()
{
	
	
	
	PORTD_DIRSET = PIN3_bm; //TX pin as output
	PORTD_OUTSET = PIN3_bm; //TX pin High
	PORTD_DIRCLR = PIN2_bm; //RX pin as input
	PORTD_OUTCLR = PIN2_bm; //RX pin low ??

	
	
	USARTD0_BAUDCTRLB = 0; //Just to be sure that BSCALE is 0
	USARTD0_BAUDCTRLA = 12; // 207
	
	
	//Disable interrupts
	USARTD0_CTRLA = 0;
	//8 data bits, no parity and 1 stop bit
	USARTD0_CTRLC =  USART_CHSIZE_8BIT_gc;
	//USARTD0_CTRLC = USART_CMODE_ASYNCHRONOUS_gc | USART_PMODE_DISABLED_gc | USART_CHSIZE_8BIT_gc;
	
	//Enable receive and transmit
	USARTD0_CTRLB = USART_TXEN_bm | USART_RXEN_bm; // And enable high speed mode
	
}

void sendChar(char c)
{
	
	while( !(USARTD0_STATUS & USART_DREIF_bm) ); //Wait until DATA buffer is empty
	
	USARTD0_DATA = c;
	
}

void sendString(char *text)
{
	while(*text)
	{
		sendChar(*text++);
	}
}








int main(void)
{
	
	
	//setUp32MhzInternalOsc();
	setUpSerial();
	
	
	while(1)
	{
		sendChar('A');
		sendChar('B');
		sendChar('C');
		sendChar(' ');
	}
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suspect that it just comes down to a false Start bit.    Try:

	PORTD_OUTSET = PIN3_bm; //TX pin High
	PORTD_DIRSET = PIN3_bm; //TX pin as output

And ideally,   add a small delay before you start the while(1) loop

 

David.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

are you using a USB adapter?

If you power cycle a USB adapter the USB serial port closes so you have to reopen it

Keith Vasilakes

Firmware engineer

Minnesota

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The wrong, or not close enough, clock speed causes framing errors.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, 

 

Thanks - the delay loop seemed to fix it. (i was already setting the TX pin to high).

 

Now if I cold just get it working for 115200.....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As far as I can tell the clock is set up correctly?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, no this is to a standard RS232 card on my PC (Old School :-) )

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Codevision Wizard produces:

// USARTC0 initialization
void usartc0_init(void)
{
// Note: The correct PORTC direction for the RxD, TxD and XCK signals
// is configured in the ports_init function.

// Transmitter is enabled
// Set TxD=1
PORTC.OUTSET=0x08;

// Communication mode: Asynchronous USART
// Data bits: 8
// Stop bits: 1
// Parity: Disabled
USARTC0.CTRLC=USART_CMODE_ASYNCHRONOUS_gc | USART_PMODE_DISABLED_gc | USART_CHSIZE_8BIT_gc;

// Receive complete interrupt: Disabled
// Transmit complete interrupt: Disabled
// Data register empty interrupt: Disabled
USARTC0.CTRLA=(USARTC0.CTRLA & (~(USART_RXCINTLVL_gm | USART_TXCINTLVL_gm | USART_DREINTLVL_gm))) |
	USART_RXCINTLVL_OFF_gc | USART_TXCINTLVL_OFF_gc | USART_DREINTLVL_OFF_gc;

// Required Baud rate: 115200
// Real Baud Rate: 115107.9 (x1 Mode), Error: 0.1 %
USARTC0.BAUDCTRLA=0x0B;
USARTC0.BAUDCTRLB=((0x09 << USART_BSCALE_gp) & USART_BSCALE_gm) | 0x00;

// Receiver: On
// Transmitter: On
// Double transmission speed mode: Off
// Multi-processor communication mode: Off
USARTC0.CTRLB=(USARTC0.CTRLB & (~(USART_RXEN_bm | USART_TXEN_bm | USART_CLK2X_bm | USART_MPCM_bm | USART_TXB8_bm))) |
	USART_RXEN_bm | USART_TXEN_bm;
}

Note that the Xmega can produce a wide range of Baud rates @ 2MHz

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hmmm- still getting junk with this code you posted - cant figure out whats wrong

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that you want to start with a short delay e.g. 10ms.   Otherwise,   your Terminal might see an incorrect first Start bit.    Your program provides no means for subsequent synchronisation.    i.e. you have a gapless transmission.

 

What do you actually see?

Can your Terminal display HEX values?

Does it respond to Framing and Parity errors?

 

David.

Last Edited: Wed. Jan 7, 2015 - 10:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm getting a variety of garbage  - see attached in Hex

 

https://www.avrfreaks.net/sites/default/files/forum_attachments/Garbage.JPG

 

Note that on this new site you don't need to "attach" pictures - you can just paste them into the text of the editor so they are immediately visible rather than requiring the reader to open an attachment. I have embedded your attachment here - Cliff, moderator

Attachment(s): 

Last Edited: Thu. Jan 8, 2015 - 09:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I gather you're using a max232 or similar device for the rs232 interface?
I think you need an oscilloscope to see what is happening. Google soundcard oscilloscope

Last Edited: Thu. Jan 8, 2015 - 09:36 AM