Hands On AVR: Difference between revisions

From SGMK-SSAM-WIKI
Jump to navigation Jump to search
mNo edit summary
Line 45: Line 45:
* On the Arduino, the ArduinoISP code must be running. Open the Arduino IDE -> examples -> ArduinoISP and write.
* On the Arduino, the ArduinoISP code must be running. Open the Arduino IDE -> examples -> ArduinoISP and write.
* Build the circuit as shown above. Pins are documented in the source code. Use a 470Ω resistor between PB4 (that's pin 3 on the attiny13) and the LED.
* Build the circuit as shown above. Pins are documented in the source code. Use a 470Ω resistor between PB4 (that's pin 3 on the attiny13) and the LED.
* Install AVR toolchain: [http://winavr.sourceforge.net Windows], [http://tinkerlog.com/2007/09/29/programming-avr-with-a-macbook Mac]
* Install AVR toolchain: [http://winavr.sourceforge.net Windows], [http://tinkerlog.com/2007/09/29/programming-avr-with-a-macbook Mac], Linux users: install avr-gcc, avr-gcc-c++ and avr-libc
* Save the following code to a file named "led.c" and save it in a new folder somewhere:
* Save the following code to a file named "led.c" and save it in a new folder somewhere:


Line 100: Line 100:
</syntaxhighlight >
</syntaxhighlight >


* download and run this make file in the cmd/shell, with <code>make all</code>. this will generate the file "lex.hex"
* upload it manually, change correct com port and baudrate: <code>avrdude -P COM1 -b 19200 -c avrisp -p attiny13 -U flash:w:led.hex:i</code> linux: use /dev/ttyUSB.. instead of COM1.
=ISP Programming=
* save the source code from the section [[#ArduinoISP]] to a file named "led.c"
* download this make and save it to the same location as the source file is in. then open the Makefile and check the following:
* download this make and save it to the same location as the source file is in. then open the Makefile and check the following:
** TARGET: i called the source file "led.c", thus in the makefile the TARGET has to be named "led" (without extension).
** TARGET: i called the source file "led.c", thus in the makefile the TARGET has to be named "led" (without extension).
** AVRDUDE_PROGRAMMER: here "avrisp" is set, because we're using arduino.
** AVRDUDE_PROGRAMMER: here "usbtiny" is set, because we're using AVR Pocket programmer. make your choice. (see note below).
** AVRDUDE_PORT: set to the comport arduino is connected. windows: COM1, COM2, etc. Linux: /dev/ttyUSB...
** AVRDUDE_PORT: set to the comport arduino is connected. windows: COM1, COM2, etc. Linux: /dev/ttyUSB...


Note: other programmers can be defined, if you are for example using the "AVR Pocket programmer", AVRDUDE_PROGRAMMER must be set to "usbtiny", for "AVRISP mkII", set it to 'stk500'. all programmers can be listed with the command <code>avrdude -c ?</code>
Note: other programmers can be defined, if you are for example using the "AVR Pocket programmer", AVRDUDE_PROGRAMMER must be set to "usbtiny", for "AVRISP mkII", set it to 'stk500'. all programmers can be listed with the command <code>avrdude -c ?</code>


* save the Makefile in the same folder as the source file is in.
* make sure the Makefile is in the same folder as the source file is in.
    
    
* run these commands from the cmd (windows) or shell (linux):
* run these commands from the cmd (windows) or shell (linux):

Revision as of 05:48, 16 February 2012

Overview

This wiki page is dedicated to AVR development with focus on the ATtiny13.

Memory

The ATtiny13 has two main memories:

Program memory is 1K Bytes. It's organized as 512x16 Bits, because all AVR instructions are 16 or 32 bits wide.

Data memory is 64 bytes of SRAM (internal memory) + 64 I/O Registers + 32 general purpose registers.

Furthermore there's electrically erasable programmable read-only memory (EEPROM) of 64 Bytes. Single bytes can be read and written.

I/O Ports

Individual I/O ports can be set as input or output. Each output can deliver 40mA.

The ATtiny13 has 6 I/O pins named: PB0, PB1, PB2, PB3, PB4 and PB5. Generally AVR ports are named Pxn, x is the letter of the port, n is the pin number.

All pins of PORTB of the attiny13 can be configured with only three registers:

  • Data Register - PORTB - write values to port PBx / set internal pull-up of PBx
  • Data Direction Register – DDRB - set PBx as input (0) or output (1).
  • Port Input Pins – PINB - read values at port PBx

Notes:

  • If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated.
  • The pin numbers don't correspond to the acutal hardware pin numbers, for example PB0 is located at hardware pin 5.
  • All ports have also alternate functions (ADC, PWM, ..).


ArduinoISP

Arduino can be used as an In-system programming (ISP) interface for the ATtiny and ATmega microcontrollers.

Here are step-by-step instructions on how to program an attiny13 with an LED blinking program:

  • On the Arduino, the ArduinoISP code must be running. Open the Arduino IDE -> examples -> ArduinoISP and write.
  • Build the circuit as shown above. Pins are documented in the source code. Use a 470Ω resistor between PB4 (that's pin 3 on the attiny13) and the LED.
  • Install AVR toolchain: Windows, Mac, Linux users: install avr-gcc, avr-gcc-c++ and avr-libc
  • Save the following code to a file named "led.c" and save it in a new folder somewhere:
/*
hello, LED!

attiny13 Pins:

1: RESET (Arduino 10)
2: NC (not connected)
3: R (470Ω) to LED to GND
4: GND (Arduino Gnd)
5: MOSI (Arduino 11)
6: MISO (Arduino 12)
7: SCK (Arduino 13)
8: VCC (Arduino 5V)

*/

#define F_CPU 9600000	// Define software reference clock for delay duration

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

#define LED PB4 // Define led output on PB4

uint16_t d; // delay variable

/**
 * delay
 *
 * @param ms duration in milliseconds
 *
 */
void delay_ms(uint16_t ms)
{
	for (uint16_t i = 0 ; i < ms ; i++) {
 		_delay_ms (1); // Loop delay
 	}
}

int main()
{
	d = 250; // delay time in ms
	DDRB |= (1 << LED); // Set direction to output for LED
	
	for (;;) {	// forever
		PORTB ^= (1 << LED);
		delay_ms(d);
	}
	return 0;
}
  • download and run this make file in the cmd/shell, with make all. this will generate the file "lex.hex"
  • upload it manually, change correct com port and baudrate: avrdude -P COM1 -b 19200 -c avrisp -p attiny13 -U flash:w:led.hex:i linux: use /dev/ttyUSB.. instead of COM1.

ISP Programming

  • save the source code from the section #ArduinoISP to a file named "led.c"
  • download this make and save it to the same location as the source file is in. then open the Makefile and check the following:
    • TARGET: i called the source file "led.c", thus in the makefile the TARGET has to be named "led" (without extension).
    • AVRDUDE_PROGRAMMER: here "usbtiny" is set, because we're using AVR Pocket programmer. make your choice. (see note below).
    • AVRDUDE_PORT: set to the comport arduino is connected. windows: COM1, COM2, etc. Linux: /dev/ttyUSB...

Note: other programmers can be defined, if you are for example using the "AVR Pocket programmer", AVRDUDE_PROGRAMMER must be set to "usbtiny", for "AVRISP mkII", set it to 'stk500'. all programmers can be listed with the command avrdude -c ?

  • make sure the Makefile is in the same folder as the source file is in.
  • run these commands from the cmd (windows) or shell (linux):
    • to check, if we are using the correct compiler: avr-gcc --version (this should give you something like "avr-gcc (WinAVR 20100110) 4.3.3")
    • make hex file: make all
    • download hex file to AVR: make program
    • at this stage the LED should already blink, but too slow, because the fuse bits are not set correctly yet.

We have to change the fuse bits.

  • Windows: avrdude -P COM*** -c avrisp -p t13 -U lfuse:w:0x7a:m -U hfuse:w:0xff:m (***select appropriate usb device, can be COM1, COM2, etc.)
  • Linux: avrdude -P /dev/ttyUSB*** -c avrisp -p t13 -U lfuse:w:0x7a:m -U hfuse:w:0xff:m (***select appropriate usb device)
  • enjoy the blinking LED!

High Voltage Fuse Resetter

If the fusebits are messed up, the AVR can be factory resetted. This can be done with "high-voltage serial programming". Here is a excellent project to reset attiny13/25/45/85 chips: hvsp fuse resetter

tips:

  • The schema on the website seems to have bugs, i fixed them, check my copy File:Hvsp-fuse-resetter-schema.pdf.
  • The 7x4 LED module can be found on ebay. If the display doesn't work, you probably have to comment out "#define _REVERSE" in the code, this switches common anode/common cathode of the display.

AVR

more on AVR soon.