avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-chat] sample code for ATmega48?


From: Matthew MacClary
Subject: Re: [avr-chat] sample code for ATmega48?
Date: Fri, 1 Jun 2007 10:34:10 -0700

On 5/31/07, address@hidden <address@hidden> wrote:
A good test program would be something that, say, sets all output pins
of the chip to high for 3 seconds, then low for 3 seconds, and repeats.

Here is some code, I haven't tried it on an Atmega48 yet though. Here
are the compile commands:

$ avr-gcc -g -Wall -pedantic -Os    -mmcu=atmega48    -c -o main.o main.c
$ avr-gcc -I  -g -Wall -pedantic -Os    -mmcu=atmega48
-Wl,-Map,main.map -o main.elf main.o
$ avr-objcopy -j .text -j .data -O ihex main.elf main.hex

/* This program flashes all of the I/O pins on the Atmega48 on and off
  with a 3 second delay. */

#include <avr/io.h>

#undef F_CPU
#define F_CPU 20000000UL /* 20MHz */

#include <util/delay.h>

void delay_ms(uint32_t time) {
 uint32_t i;
 for (i = 0; i < time; i++) {
   _delay_ms(1);
 }
}

int main() {
 /* Set ports to outputs */
 DDRB = 0xff;
 DDRC = 0x7f; /* PORTC has only 7 pins */
 DDRD = 0xff;

 while (1) {
   PORTB ^= 0xff;
   PORTC ^= 0x7f;
   PORTD ^= 0xff;
   delay_ms(3000);
 }

 return 0;
}

Attachment: main.hex
Description: Binary data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]