Software > Software

atmega source code

(1/2) > >>

anyone have source code for either the atmega8 or atmega644 that they can post? a simple LED flash would suffice.

i figure it would speed me up in development for two separate projects im working on . . .

i dont remember the last time i had less than 4 robots to work on simultaneously . . . i must focus more . . .

I found this written for an Atmega128 so it should be similar

--- Code: ---/****************************************************************************
*   Copyright (c) 2006 Dave Hylands     <[email protected]>
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*   See README and COPYING for more details.

#include <avr/io.h>

#include "Delay.h"
#include "Timer.h"

#define RED_LED_PIN     4
#define RED_LED_MASK    ( 1 << RED_LED_PIN )
#define RED_LED_DDR     DDRG

#define BLUE_LED_PIN    3
#define BLUE_LED_MASK   ( 1 << BLUE_LED_PIN )
#define BLUE_LED_DDR    DDRG

#define YELLOW_LED_PIN  4

// Setting the pin to 0 turns the LED on

#define LED_ON( color )     color ## _LED_PORT &= ~color ## _LED_MASK
#define LED_OFF( color )    color ## _LED_PORT |= color ## _LED_MASK

int main(void)
    int led;


    ASSR &= ~( 1 << AS0 );  // Make sure Port G LED pins are setup for I/O


    led = 0;

    while( 1 )
        // Turn all of the LEDs off

        LED_OFF( RED );
        LED_OFF( BLUE );
        LED_OFF( YELLOW );

        switch ( led )
            case    0:  LED_ON( RED );      break;
            case    1:  LED_ON( BLUE );     break;
            case    2:  LED_ON( YELLOW );   break;

        if ( ++led > 2 )
            led = 0;

            int i;

            // Tick rate is 100/sec so waiting for 100 waits for 1 sec

            for ( i = 0; i < 100; i++ )

    return 0;
--- End code ---

by strange coincidence i just uploaded an article on my AVR based USB servo controller to your members area.
circuit diagrams and source code included.

the code is probably a bit more complicated than you want for a simple "hello world" though...

for my first attempt with AVRs i followed this article:
but it's very much from a Linux user's point of view.

which complier are you using?

there's a good article on AVR Freaks called "Which C-compiler should I choose" (i think you have to sign up for an account to get it).
it has flashing LEDs example code for some of the more common compilers.

if you are using GCC i can find and test one for you and paste it here if you want.
(although not tonight, it's getting late.)

good luck!


Here is my project all zipped up for you code browsing:

It's compiled with gcc. so go to /FreeRTOS/App/AVR_ATMega128_RbStx/
and run 'make' if you are using linux and have everything installed for
avr-gcc and gnu make and some other stuff (if you need help PM me)

I've used FreeRTOS and AVRlib and made changes to AVRlib when I had to.
The reason I had to modify and do some odd things with AVRlib is because it
makes the assumption that you are running under a single line of execution
with no context switches.

If there is alot of interest in this let me know and I will write a tutorial on it when I can.

back in 2005 i programmed the atmega168 to control servos . . . but at that time gcc was buggy and there was like no source code for it on the web . . . so my code was all ugly and hacked together from a total lack of understanding of why my code even worked (yea, it worked!) . . . this is what happens when you let a mechanical engineer program . . . :P

anyway, here is the atmega168 source code for others to use:

--- Code: ---#include <avr/io.h>
//#include <avr/interrupt.h>
//#include <string.h>

//***************DELAY CODE**************
//delay exactly 1 clock cycle
void delay_cycles(unsigned int timed) {

//*************************BEEPER CODE**********************
//beep for cycle time at certain frequency
void beep (unsigned int cycles, unsigned int freq) {
DDRB |= 1<<PB0; //make PB0 an output
long i;
while (cycles > 0) {
PORTB |= 1<<PB0; // buzzer pin high
PORTB &= !(1<<PB0); // buzzer pin low

//*****required for ADC conversion (screws up everything!)

//this came from the main:
//******end of ADC code

//main function, begins with initialization
//NOTE servo actuation code does not work outside of main for some odd reason
int main(void) {

#include <servoPositions.h>//this include ONLY works in main cause the compiler sucks like that
unsigned int step=0;
unsigned int i=0;

//DDRn Data Direction Registers (0 for input vs 1 for output)
//DDRC = 0xFF; //configure all ports for output
//DDRC = 0x00; //configure all ports for input
//pull up resistors generally good for inputs
//PORTC = 0xFF; //turns pullup resistors on for all C ports
//PORTC = 0x00; //turns pullup resistors off for all C ports
//DDRD |= 1<<PD1; // OR set PD1 to output
//DDRC |= 1<<PC3; // OR set PC3 to output

//DDRC |= 0<<PC6;//set ADC6 to input
//PORTC |= 1<<PC6;//turn on ADC6 pullup resistor

//ADC init
ADMUX = 0; // channel 0 //ADC init
    ADCSRA |= _BV(ADEN) | _BV(ADPS1) | _BV(ADPS2);//ADC init

ADCSRA |= _BV(ADSC);//ADC Start Conversion

//!(ADCSRA & _BV(ADIF));//ADC wait for conversion

//ADCSRA |= _BV(ADIF);//ADC clear ADIF


beep(1500, 25);

DDRC |= 0x7C; //setup portC for output 0b1111100 (PC0-PC5)
//DDRC |= 0x3 //setup portC for input 0b0000011 (PC6-PC7)

PORTC &= !(1<<PC5);//initialize PWM off

while(1) {

while(i<7) {
//***************SERVO ACTUATION CODE***************
//accepts a time in clock cycles to pulse each servo
PORTC |= 1<<PC3; //servo 1
PORTC &= !(1<<PC3);

PORTC |= 1<<PC2; //servo 2
PORTC &= !(1<<PC2);

PORTC |= 1<<PC1; //servo 3
PORTC &= !(1<<PC1);

PORTC |= 1<<PC0; //servo 4
PORTC &= !(1<<PC0);

PORTC |= 1<<PC4; //servo 5
PORTC &= !(1<<PC4);
PORTC |= 1<<PC5; //motor 1
PORTC &= !(1<<PC5);*/


//in future, have it ++ only if the encoder detects a change in position
// -also have it reset to 0 if it detects restart mark

if (step>=8) {
//beep(1500, 25);//step=0;

PORTD |= 1<<PD1; // LED on
PORTD &= !(1<<PD1); // LED off
return 0;

--- End code ---

now for my questions . . .

looking at Dave Hyland's code, i have a few questions that will help me port this over to the atmega644:
(atmega128 datasheet for reference)

im kinda confused what this code does
    #define RED_LED_PIN     4
    #define RED_LED_MASK    ( 1 << RED_LED_PIN )
    #define RED_LED_DDR     DDRG
    #define RED_LED_PORT    PORTG


what i think the last line is saying is this:
DDRG |= 1 << 4;
which i think means configure port PG4 to output, right?

also this code . . .

    #define LED_ON( color )     color ## _LED_PORT &= ~color ## _LED_MASK
    #define LED_OFF( color )    color ## _LED_PORT |= color ## _LED_MASK

what i think this is saying is when I write LED_ON( RED ) in my main, it changes to this:
    PORTG  &= 1 << 4;
which i think means set PG4 to 0 (if i did PORTG  |= 1 << 4 , this would set the pin to 1), right?

what is the difference between these? i think this might clear up my confusion . . .
    PORTG  &= !(1 << 4);
    PORTG  |= !(1 << 4);
    PORTG  &= 1 << 4;//sets PG4 to 1
    PORTG  |= 1 << 4;

also, im looking at the AVRlib for AVR . . . quite possibly a dumb question . . . i just copy/paste the functions from the c files and put into my code, right? then i just include the .h files and it will work, right? believe it or not it took me like 20 min of confusion to figure that out . . . :-[

and a link to dunks new USB Servo Controller Tutorial (thanks, dunk!!! im already putting some of it to use)

(edit: changed dunk's link)


[0] Message Index

[#] Next page

Go to full version