Help with USART on an Attiny817

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

Hey everybody,

 

I'm having trouble getting the USART working on my Attiny817. I know this is the Xmega forum, but since the 817's coding is similar I thought I'd try my luck here

 

I'm getting an output in the terminal window, but it's not quite right to say the least. Bunch of strange ASCII characters.

The code is posted below, and a screenshot of the output.

 

Any help is really appreciated.

 

PS: one thing that might look strange is that I've set the TX pin to be an input, and the RX pin to be an output. This appears to be the right way to do it. I got a hold of some examples for the 817 on Atmel's website, and the initialise the USART pins the same way.

 

// Libraries
/* -------------------------------------------------------*/
#include <avr/io.h>
#include <util/delay.h>

// Macros
/* -------------------------------------------------------*/
#define F_CPU 20000000
/* Normal Mode, Baud register value */
#define USART0_BAUD_RATE(BAUD_RATE) ((float)20000000.0 * 64 / (16 * (float)BAUD_RATE))

// Function Prototypes
/* -------------------------------------------------------*/

int8_t USART_init(void);
void USART_write(const uint8_t data);
uint8_t USART_read(void);
void sendChar(char c);
void printString(const char myString[]);

// Function Main
/* -------------------------------------------------------*/
int main(void)
{

	USART_init();
	PORTC_DIR = (1<<PIN0_bp);

    while (1)
    {
		printString("5");
		_delay_ms(2000);
		PORTC_OUTTGL = (1<<PIN0_bp);

    }
}

// Function Definitions
/* -------------------------------------------------------*/
int8_t USART_init(){
	PORTB.DIRSET = PIN2_bm; // RX pin as output
	PORTB.OUTCLR = PIN2_bm; //

	PORTB.DIRCLR = PIN3_bm; // TX pin set as input
	//PORTB.OUTCLR = PIN3_bm;
	PORTB_PIN3CTRL &= ~PORT_PULLUPEN_bm; // Leave it as pulled-off

	USART0.BAUD = (uint16_t)USART0_BAUD_RATE(9600); /* set baud rate register */

	USART0.CTRLB = 0 << USART_MPCM_bp       /* Multi-processor Communication Mode: disabled */
	| 0 << USART_ODME_bp     /* Open Drain Mode Enable: disabled */
	| 1 << USART_RXEN_bp     /* Receiver enable: enabled */
	| USART_RXMODE_NORMAL_gc /* Normal mode */
	| 0 << USART_SFDEN_bp    /* Start Frame Detection Enable: disabled */
	| 1 << USART_TXEN_bp;    /* Transmitter Enable: enabled */

	USART0.CTRLC = USART_CMODE_ASYNCHRONOUS_gc /* Asynchronous Mode */
		 | USART_CHSIZE_8BIT_gc /* Character size: 8 bit */
		 | USART_PMODE_DISABLED_gc /* No Parity */
		 | USART_SBMODE_1BIT_gc; /* 1 stop bit */

	return 0;
}

void USART_write(const uint8_t data)
{
	while (!(USART0.STATUS & USART_DREIF_bm));
	USART0.TXDATAL = data;
}

void printString(const char myString[]) {
	uint8_t i = 0;
	while (myString[i]) {
		USART_write(myString[i]);
		//sendChar(myString[i]);
		i++;
	}
}

void sendChar(char c)
{

	while( !(USART0.STATUS & USART_DREIF_bm) ); //Wait until DATA buffer is empty

	USART0.TXDATAL = c;

}

uint8_t USART_read()
{
	while (!(USART0.STATUS & USART_RXCIF_bm))
	;
	return USART0.RXDATAL;
}

Here's the output:

Last Edited: Mon. Jun 12, 2017 - 03:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

daisy148 wrote:
Bunch of strange ASCII characters.
So it's not 9600 baud coming out of the AVR. In turn that probably means the AVR is not running at the speed you think it is.

 

(see FAQ#3 below - this experience is as old as the hills!)

 

So I'd question whether it really is the 20MHz your code seems to assume.

Last Edited: Mon. Jun 12, 2017 - 03:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My god. You're right. I'm laughing