ATtiny816 UART example

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

hi 

I am trying to use UART of ATtiny816

datasheet is lack of examples,

 

could anyone show some reference for me ?

 

thanks

Majid

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

Take a look at atmel start....   start.atmel.com   IIRC

put usart in search box

 

Jim

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

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

ki0bk wrote:
start.atmel.com

 

thanks

Majid

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

Or if you don't use start, something like this:

/*
    Minimal USART test for attiny816
 */ 

#include <avr/io.h>

#ifndef F_CPU
#warning "You haven't defined F_CPU. I'm using F_CPU = 3333333"
#define F_CPU 3333333
#endif

#define BAUD_RATE 9600

#define UROUND(x) ((2UL*(x)+1)/2)

// Set up USART0 in asynchronous mode.
// Default pin locations (i.e. not remaped)
// RX = PB3
// TX = PB2
void usart_init(void)
{
    PORTB.OUTSET = PIN2_bm;
    PORTB.DIRSET = PIN2_bm;
    USART0.BAUD = UROUND(64UL*F_CPU/16/BAUD_RATE);
    USART0.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
}

uint8_t usart_ischar(void)
{
    return (USART0.STATUS & USART_RXCIF_bm) != 0;
}

char usart_getchar(void)
{
    while(!usart_ischar())
        ;
    return USART0_RXDATAL;
}

void usart_putchar(char ch)
{
    while((USART0.STATUS & USART_DREIF_bm) == 0)
        ;
    USART0.TXDATAL = ch;
}

int main(void)
{
    usart_init();

    while (1)
    {
        if (usart_ischar())
        {
            usart_putchar(usart_getchar());
        }
    }
}

 

Edit: corrected typo, USART_DREIE_bm should be USART_DREIF_bm (but they have the same value).

Last Edited: Fri. Jan 5, 2018 - 09:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks
Nice clean code

Majid