Datasheet

19 | Page
8 Example programs
8.1 Atmega-328
blink.c source code:
/*
* blink.c
*
* Created: 23/09/2013 21:04:02
* Author: G.J. van Loo
* Simple example program to 'walk' the LEDs
*/
#include <avr/io.h>
#define DELAY 250
#define F_CPU 16000000
// Some macros that make the code more readable
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)
// Outputs are:
// LED0 = PB5
// LED1 = PB1
// LED2 = PB2
// LED3 = PD3
// LED4 = PD5
// LED5 = PD6
void delay_ms(unsigned int ms)
{
uint16_t delay_count = F_CPU / 17500;
volatile uint16_t i;
while (ms != 0) {
for (i=0; i != delay_count; i++);
ms--;
}
} // delay_ms
void delay()
{ long d;
unsigned char oldb,oldd;
for (d=0; d<DELAY; d++)
{
delay_ms(1);
if ((PINC & 0b00001000)==0)
{
oldb = PORTB;