[Top][All Lists]

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

Re: [avr-gcc-list] simple program

From: Timothy Smith
Subject: Re: [avr-gcc-list] simple program
Date: Thu, 25 Aug 2005 19:39:15 +1000
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050721)

Colin Paul Gloster wrote:

Someone emailed with a very incorrect timestamp of Thu, 25 Aug 2005 16:52:52
"hi there i'm new to gcc and avr development and i've tried to make a
very simple program to turn on an LED connected to one of the pins.
cat led.c
#include <inttypes.h>
#include <avr/io.h>

int main(void)
  sbi(DDRD, 1);

and below you can see the error. i also tried i simple test program i
downloaded and i got similar errors, so i'm not sure what i'm doing wrong?

titan# avr-gcc led.c

In file included from led.c:2:
warning: #warning "device type not defined"
led.c: In function `main':
led.c:6: error: `DDRD' undeclared (first use in this function)
led.c:6: error: (Each undeclared identifier is reported only once
led.c:6: error: for each function it appears in.)
led.c:7: error: `PORTD' undeclared (first use in this function)"

The warning

"In file included from led.c:2:
warning: #warning "device type not defined""

probably arises because you do not seem to have specified the processor type
and #include <avr/io.h> (which despite the AVR-LibC documentation, should
really be #include "avr/io.h" ) tries to #include a device-specific I/O file
which it will fail to do unless you invoke GCC like so:

avr-gcc -mmcu=atmega64 source_code.c

This will produce different error messages:

"In function `main': : undefined reference to `sbi'"


"In function `main': : undefined reference to `sbi'".

Hopefully by now you realize that sbi and outb are assembly instructions,
not C code.

In C, you can replace your sbi call with
DDRD = (1 << 1);

Replacing your attempt at outb with C is more obtuse, because what you were
trying to do with outb is unclear because it would not have worked in assembly
as outb is not an AVR assembly instruction. However, out is and its second
operand is not a literal, but a register. You could explicitly state a register
in your C code, but unless you need to you should let the C code handle this.
So we would have
#include <inttypes.h>
#include <avr/io.h>

int main(void)
  char substitution_for_a_regsiter;

  DDRD = (1 << 1);//sbi(DDRD, 1);
  substitution_for_a_regsiter = 1;
  PORTD = substitution_for_a_regsiter;//outb(PORTD,1);

But it may be better to remove things we do not need, so:
#include <avr/io.h>

void main(void)
  DDRD = (1 << 1);//sbi(DDRD, 1);

It is unclear whether you want to program in assembly or C. I have given
you C examples, if you want to use assembly you can insert the instructions
with GCC but perhaps you should use an assembler. Whichever you want, be
clear to yourself which you are using.

Good luck.

AVR-GCC-list mailing list

ahh thanks for the informative response!
what screwed me up was the tut i was following http://ccrma.stanford.edu/workshops/pid2004/lectures/programming/programming/Anatomy_C_program.html i had no way of knowing sbi() etc wasn't a function, i just assumed it was correct :/

i'm a python programming, i did learn c++ while in uni and this is my first attempt at c and programing ic's. so learning curve.

oh and it works now, thanks.

reply via email to

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