ATtiny816 UART example

Go To Last Post
8 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

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 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

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

Hello, everyone:

I am working on a project using attiny816 as a node to realize three functions: NTC temperature acquisition using ADC, acceleration using SPI, and then send the data to the upper level device (atmega256) using RS-485 (UART).

I followed Majid's advice and started from "start.atmel.com". I added ADC, SPI, and RS-485 from the "add software component".  But I have encountered two problems:

1) RS-485 requires a pin as TXEN, I don't know where this pin should connect to ? There is no TXEN pin I can use in the 485 chip.

2) RS-485 uses UART, PB0 is assigned as XDIR. When I download the configuration, it gives the following error" Bad Request:   {'drivers': {'variant': 'Could not find signal in variant <DriverVariantDefinition CMODE=0> with label: XDIR'}},  or if I export the project, it gives another error "Unable to generate project".  The pin layout and schematics are given in the following figures.

 

Could you please help tell me where I get wrong?  Thank you very much!

 

Jun

 


 

Jun

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

If you want to use tiny816, please read the datasheet first.

The TXEN you say is indicated by XDIR on the 816.

You can select 485 mode in USART setting of START and PB0 should be assigned automatically.

 

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

Thank you very much for you reply, Kabasan: 

Yes, you are right. There was a bit confusion to me, since there are XCK and XDIR under USART which should be left unselected. Thank you for helping. It is working fine now.

 

Jun

Jun