Hands On AVR: Difference between revisions

From SGMK-SSAM-WIKI
Jump to navigation Jump to search
No edit summary
No edit summary
Line 14: Line 14:
<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
#define F_CPU 9600000 // Define software reference clock for delay duration
#define F_CPU 9600000 // Define software reference clock for delay duration
#include <avr/io.h>
#include <avr/io.h>
#include <util/delay.h>
#include <util/delay.h>
#define LED PB4 // Define led output on PB4
#define LED PB4 // Define led output on PB4
uint16_t d;
 
uint16_t d; // delay variable
 
/**
* delay
*
* @param ms duration in milliseconds
*
*/
void delay_ms(uint16_t ms)
void delay_ms(uint16_t ms)
{
{
Line 24: Line 34:
  }
  }
}
}
int main()
int main()
{
{
d = 250; // delay time in ms
d = 250; // delay time in ms
DDRB |= (1 << LED); // Set direction to output for LED
DDRB |= (1 << LED); // Set direction to output for LED
for (;;) { // forever
for (;;) { // forever
PORTB ^= (1 << LED);
PORTB ^= (1 << LED);
delay_ms(d);
delay_ms(d);
Line 37: Line 46:
return 0;
return 0;
}
}
</syntaxhighlight>
</syntaxhighlight >


* run avrdude, make sure you choose the correct USB port and speed, i gave the program the name 'led.hex':
* run avrdude, make sure you choose the correct USB port and speed, i gave the program the name 'led.hex':

Revision as of 15:15, 5 October 2011

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
  • Compile this code:
#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;
}
  • run avrdude, make sure you choose the correct USB port and speed, i gave the program the name 'led.hex':

avrdude -P COM9 -b 19200 -c avrisp -p t13 -U flash:w:led.hex:i

AVR

more on AVR soon.