avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] Using PORTB with variables, PORTB = var


From: Ned Konz
Subject: Re: [avr-gcc-list] Using PORTB with variables, PORTB = var
Date: Sat, 22 Apr 2006 11:20:40 -0700


On Apr 22, 2006, at 9:20 AM, Matthew MacClary wrote:

    Here is how I would implement your program. I apologize in advance
since I don't have a board available to try the code on. I hope that
the main ideas are apparent, even if I didn't squash all the bugs.

Here's a version of Matthew's code that will compile correctly (had to change the names of sleep() etc.); using avr-gcc 4.1.0 it compiles to 142 bytes with the compilation options given below.

/*
avr-gcc -g -mmcu=atmega8 -fwhole-program -Os -o xx2c.elf xx2.c
avr-objdump -S xx2c.elf > xx2c.lst
*/

/* default 1MHz internal oscillator */
#define F_CPU 1000000

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

inline void
blink(volatile uint8_t * const port, uint8_t mask)
{
    *port = ~mask;              // LEDs on
    _delay_ms(500);             // milliseconds
    *port = 255;                // LEDs off
    _delay_ms(500);             // milliseconds
}

inline void
change(uint8_t * const var)
{
    *var <<= 1;
    if (! *var)
        *var = 1;
}

int
main()
{
    uint8_t led_mask = 1;

    DDRB = 255;                 // PORTB as outputs
    PORTB = 255;                // LEDs off

    while (1)
    {
        blink(&PORTB, led_mask);
        change(&led_mask);
    }
    return 0;
}

--
Ned Konz
MetaMagix embedded consulting
address@hidden






reply via email to

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