Datasheet
23 | Page
8.2 Atmega-48
This section shows an example program for the Atmega48. You will find that the makefile and
the programming files are very similar to the 328 example.
low_power.c source code:
// Example code which uses the 32767KHz
// Crystal to implement a 1-second event
// handler
//
// Atmega Low power operation example
// Using a 32768 Khz crystal on timer 2 and full power down mode
// to implement a 1-second event handler
//
// This code is written for the GCC compiler
// Example for the GertDuino Atmega 48PA device
// (This program will NOT run on the 328!)
// This code is freeware
//
#include <avr/interrupt.h>
#include <avr/sleep.h>
volatile unsigned long count_seconds;
main()
{
// set PB0 as output
DDRB = 0xFE;
// Set-up 32 KHz oscillator
TIMSK2 = 0x00; // No interrupts
ASSR = 0x20; // async run from xtal
TCNT2 = 0; // clear counter
TCCR2B = 0x05; // prescale 5=/128
// Wait for all 'busy' bits to be clear
// That happens on the first timer overflow
// which can take 8 seconds if you have a max pre-scaler!!
while (ASSR&0x07) ;
TIMSK2 = 0x01; // overflow IRQ enable
count_seconds = 0; // clear seconds counter
sei(); //set the Global Interrupt Enable Bit
while (1)
{
SMCR = 0x7; // Go into lowest power sleep mode
asm("sleep");
asm("nop");
// Interrupt woke us up
// If we get here the interrupt routine has already been called
// Toggle LED on port B0 using LS timer bit
PORTB = count_seconds & 0x01;
}
} // main
//