Home > Exploring Ideas, Hardware testing, Internet of Things > ATtiny104 Xplained NANO, experimenting.

ATtiny104 Xplained NANO, experimenting.

Atmel launches  the ATtiny104 Xplained Nano evaluation kit, a nice way of introducing this $0.40 microcontroller.

ATtiny104_img

The small and low cost evaluation board includes an on-board programmer.

After received two boards from Digikey, started downloading the Atmel Studio.

ATTINY104_X.jpg

The Atmel Studio is a “BIG” tool and takes time for the installation, but after the waiting, everything went smooth.

I spent some time drawing the Pinout Diagram, it helps me to learn more about the mini development kit.

ATtiny104_IOe.jpg

In no time the ATtiny104 was reading the PUSH BUTTON and controlling the on board LED.

Attiny104_blinking.jpg

Next thing to do:  experimenting with the serial port.

The character ‘U’ is sent every 500 milliseconds if the BUTTON is pressed.

The working code  transmitting at 9600 bauds:


 

/* ATTINY104_Serial.c 
*  Created: 3/24/2016 08:20:00 PM 
*  Author : Ardunaut
* Program to test serial transmission. 
* While the BUTTON is pressed: 
* 'U' character is transmitted every 500 milliseconds. 
* The LED blinks when a char transmitted. 
* FOSC=1000000 :"After powering up the device or after a reset the system clock is automatically 
* set to calibrated internal 8MHz oscillator, divided by 8" (Datasheet pag.32) 
* Using C functions from: http://www.atmel.com/Images/Atmel-42505-8-bit-AVR-Microcontroller-ATtiny102-ATtiny104_Datasheet.pdf 
* Note: Zeros ('0') were removed from the register names to make the code compatible. 
                 __________
           VCC--|1       14|--GND (TPI CLK)
           PA0--|2       13|--PB3 (CDC TX) -->USART_Transmit()
(TPI DATA) PA1--|3       12|--PB2 (CDC RX) <--USART_Receive()
   (RESET) PA2--|4       11|--PB1 (BUTTON)
           PA3--|5       10|--PB0
           PA4--|6        9|--PA7
     (LED) PA5--|7        8|--PA6
                \__________/
        Atmel ATtiny104 Xplained Nano
  */ 

#include <avr/io.h>
#include <util/delay.h>

#define LED_PIN    (1 << PA5)
#define BUTTON_PIN (1 << PB1)
#define FOSC 1000000
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD   //adding or subtracting 1 may be necessary.

void PORTS_init(void){  
  PUEB |= BUTTON_PIN;         // Enable Pull-Up function in PB1.
  PORTB |= BUTTON_PIN;        // Set Pull-Up for the Button.
  DDRA |= LED_PIN;            // Configure LED pin as Output.
} 

void USART_Init( unsigned int ubrr)
{
  /*Set baud rate */
  UBRRH = (unsigned char)(ubrr>>8);
  UBRRL = (unsigned char)ubrr;
  /*Enable receiver and transmitter */
  UCSRB = (1<<RXEN)|(1<<TXEN);
  /* Set frame format: 8data, 2stop bit */
  UCSRC = (1<<USBS)|(3<<UCSZ0);
  _delay_ms(250);
  PORTA |= LED_PIN;         // Switch off the LED.
  }
    
void USART_Transmit( unsigned char data )
{
  /* Wait for empty transmit buffer */
  while ( !( UCSRA & (1<<UDRE)) );
  /* Put data into buffer, sends the data */
  UDR = data;
}

unsigned char USART_Receive( void )
{
  /* Wait for data to be received */
  while ( !(UCSRA & (1<<RXC)) );
  /* Get and return received data from buffer */
  return UDR;
}

void USART_Flush( void )
{
  unsigned char dummy;
  while ( UCSRA & (1<<RXC) ) dummy = UDR;
}

/*----------------------------------------------------------------------------------------------------------------------------------------------------------*/
void main( void ){
  PORTS_init();
  USART_Init(MYUBRR);
  
  while(1){
    while(PINB & BUTTON_PIN){}  // Wait until the Button is pressed.
    PORTA &= ~LED_PIN;          // Switch on the LED.
    USART_Transmit( 'U' );
//    USART_Transmit( 85 );
    _delay_ms(50);
    PORTA |= LED_PIN;           // Switch off the LED.
    _delay_ms(450); 
  }
}

 

 

Some soldering to continue experimenting with the Xplained NANO in a breadboard.

ATtiny104_breadboard

ATtiny104_ready

Now, looking for an efficient code and low power solution to create an IoT node…

First, testing the hardware:

ATtiny104_RFcoms (1)


 

Transmitting at 300 bauds with the USART connected to the 433Mhz RF module:

ATtiny104_RF300


 

This is the code to transmit “HELLO” every second (while the button is pressed).

/* * ATTINY104_RF300.c 
* -Using the USART for 300 bauds transmission with the RF Module (433Mhz). 
* -Tree 0xAA bytes are sent as preamble of the message to balance the receptor. 
* -While the BUTTON is pressed "HELLO" message is transmitted every second aprox. 
* -Characters are separated 50 milliseconds (20 milliseconds of mark). 
* -The LED is ON during the message is transmission. 
* -FOSC= 1000000 : "After powering up the device or after a reset the system clock is automatically 
*  set to calibrated internal 8MHz oscillator, divided by 8" (Datasheet pag.32) 
* -Using C functions from: http://www.atmel.com/Images/Atmel-42505-8-bit-AVR-Microcontroller-ATtiny102-ATtiny104_Datasheet.pdf 
* -Note: Zeros ('0') were removed from the register names to make the code compatible. 
* -Created: 3/26/2016 07:25:00 PM 
* -Author : Ardunaut
                 __________
           VCC--|1       14|--GND (TPI CLK)
           PA0--|2       13|--PB3 (CDC TX) -->USART_Transmit()
(TPI DATA) PA1--|3       12|--PB2 (CDC RX) <--USART_Receive()
   (RESET) PA2--|4       11|--PB1 (BUTTON)
           PA3--|5       10|--PB0
           PA4--|6        9|--PA7
     (LED) PA5--|7        8|--PA6
                \__________/
        Atmel ATtiny104 Xplained Nano  
*/ 

#include <avr/io.h>
#include <util/delay.h>

#define LED_PIN    (1 << PA5)
#define BUTTON_PIN (1 << PB1)
#define FOSC 1000000 // Clock Speed 
#define BAUD 300     
#define MYUBRR FOSC/16/BAUD-1

void PORTS_init(void){  
  PUEB |= BUTTON_PIN;         // Enable Pull-Up function in PB1.
  PORTB |= BUTTON_PIN;        // Set Pull-Up for the Button.
  DDRA |= LED_PIN;            // Configure LED pin as Output.
} 

void USART_Init( unsigned int ubrr){
  //Set baud rate:
  UBRRH = (unsigned char)(ubrr>>8);
  UBRRL = (unsigned char)ubrr;
  //Enable receiver and transmitter:
  UCSRB = (1<<RXEN)|(1<<TXEN);
  // Set frame format: 8data, 2stop bit :
  UCSRC = (1<<USBS)|(3<<UCSZ0);
  _delay_ms(250);
  PORTA |= LED_PIN;         // Switch off the LED.
  }
    
void USART_Transmit( unsigned char data ){
  // Wait for empty transmit buffer:
  while ( !( UCSRA & (1<<UDRE)) );
  // Put data into buffer, sends the data:
  UDR = data;
}

unsigned char USART_Receive( void ){
  // Wait for data to be received:
  while ( !(UCSRA & (1<<RXC)) );
  // Get and return received data from buffer :
  return UDR;
}

void USART_Flush( void ){
  unsigned char dummy;
  while ( UCSRA & (1<<RXC) ) dummy = UDR;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------*/
void main( void ){
  PORTS_init();
  USART_Init(MYUBRR);
  
  while(1){
    while(PINB & BUTTON_PIN){}  // Wait until the Button is pressed.
    PORTA &= ~LED_PIN;          // Switch on the LED.
    
    USART_Transmit( 0xAA );   // To balance the receptor.
    _delay_ms(50);
    USART_Transmit( 0xAA );   // To balance the receptor.
    _delay_ms(50);    
    USART_Transmit( 0xAA );   // To balance the receptor.
    _delay_ms(50);
    USART_Transmit( 'H' );
    _delay_ms(50);
    USART_Transmit( 'E' );
    _delay_ms(50);
    USART_Transmit( 'L' );
    _delay_ms(50);
    USART_Transmit( 'L' );
    _delay_ms(50);
    USART_Transmit( 'O' );    

    PORTA |= LED_PIN;         // Switch off the LED.
    _delay_ms(650);           // delay to complete one second.
  }
}

More experiments and learning …

blogger-at-work

Advertisements
  1. xxx
    May 11, 2016 at 1:29 AM

    testing….3GB Windows Software on hdd for programming 1KB atmel ? hi hi .. unusable…

  2. martin szakodyn
    May 14, 2016 at 10:26 PM

    Hi, and thank you for sharing with us. So I tried the second example with multiple characters being transmitted 50ms apart and the data appears to get corrupted when the baud rate is increased from 300 (in your example) to 9600. Do you have any ideas why?
    Thanks.

    • May 15, 2016 at 1:46 AM

      The transmission rate limit is 10Khz but with a very distorted wave shape (rise and fall times are different). Asincronous serial may be not the best approach if you want to maximize the baudrate.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: