winavr "C" compiling help

223 views
Skip to first unread message

ROCKET100

unread,
Apr 1, 2009, 2:27:30 PM4/1/09
to CCCKC
I am trying to compile C for a ATtiny2313 with winavr and I got stuck.
I got the code from instructables ;
/*
-----------------------------------------------------------------------
* Title: flick led
* Author: Alexander Weber <alexander.weber.0 at gmail.com>
* Date: 21.02.2007
* Hardware: ATtiny13v
* Software: WinAVR 20060421
*/

// define as -D switch
//#define F_CPU 1200000 // Taktfrequenz: 1.2MHz, internal oscillator


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

#define TRUE 1
#define FALSE 0
#define ON 1
#define OFF 0

// use PB2 for led, pin 7
#define LED_BIT 2
// use PB3 to enable ldr, pin 2
#define ADC_ENA_BIT 3
// pause
#define PAUSE 25
// select ADC2, PB4, pin 3
#define CHANNEL 2
// values over threshold are recorded as "on"
#define THRESHOLD 800
// storage for recorded values
#define MAX 400
static uint8_t values[MAX / 8];


/*
* get_adc
* Return the 10bit value of the selected adc channel.
*/
uint16_t get_adc(uint8_t channel) {

// enable voltage for adc
PORTB |= (1 << ADC_ENA_BIT);

// ADC setup
ADCSRA =
(1 << ADEN) | // enable ADC
(1 << ADPS1) | (1 << ADPS0); // set prescaler to 8

// select channel
ADMUX = channel;

// select reference voltage
// ADMUX |= (1 << REFS0); // use internal reference

// warm up the ADC, discard the first conversion
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));

ADCSRA |= (1 << ADSC); // start single conversion
while (ADCSRA & (1 << ADSC)); // wait until conversion is done

PORTB &= ~(1 << ADC_ENA_BIT); // disable voltage for adc

return ADCW;
}



int main(void) {

uint16_t i = 0;
uint16_t count = 0;
uint8_t light = OFF;
uint8_t last_light = OFF;
uint16_t last_time = 0;
uint8_t programming = TRUE;

// define LED and ADC enable as outputs
DDRB |= (1 << LED_BIT) |
(1 <<ADC_ENA_BIT);

// intro
for (i = 0; i < 5; i++) {
PORTB |= (1 << LED_BIT);
_delay_ms(200);
PORTB &= ~(1 << LED_BIT);
_delay_ms(200);
}
for (i = 0; i < 5; i++) {
_delay_ms(100);
}

while (1) {

if (programming) {
// signal that we are ready to program
for (i = 0; i < 5; i++) {
PORTB |= (1 << LED_BIT);
_delay_ms(40);
PORTB &= ~(1 << LED_BIT);
_delay_ms(40);
}
// now read the ldr and store it
for (i = 0; i < MAX; i++) {
if (get_adc(CHANNEL) > THRESHOLD) {
values[i / 8] |= (1 << (i % 8));
PORTB |= (1 << LED_BIT);
}
else {
values[i / 8] &= ~(1 << (i % 8));
PORTB &= ~(1 << LED_BIT);
}
_delay_ms(PAUSE);
}
// signal that we are finished with programming
for (i = 0; i < 5; i++) {
PORTB |= (1 << LED_BIT);
_delay_ms(40);
PORTB &= ~(1 << LED_BIT);
_delay_ms(40);
}
// switch to playback
programming = FALSE;
count = 0;
}
else {
// have we detected a change?
light = (get_adc(CHANNEL) > THRESHOLD) ? ON : OFF;
if (light != last_light) {
// when was the last change?
if ((count - last_time) == 10) {
programming = TRUE; // switch to programming mode
}
else {
last_time = count;
}
}
last_light = light;

// replay recorded lights
if (values[count / 8] & (1 << (count % 8))) {
PORTB |= (1 << LED_BIT);
}
else {
PORTB &= ~(1 << LED_BIT);
}
_delay_ms(PAUSE);
count++;
if (count == MAX) {
count = 0;
}
}

}

return 0;

}



And when I compile it this is what it says;

error: 'ADCSRA' undeclared (first use in this function)
error: (Each undeclared identifier is reported only once for each
function it appears in.)
error: 'ADEN' undeclared (first use in this function)
error: 'ADPS1' undeclared (first use in this function)
error: 'ADPSO' undeclared (first use in this function)
error: 'ADMUX' undeclared (first use in this function)
error: 'ADSC' undeclared (first use in this function)
error: 'ADCW' undeclared (first use in this function)
warning: 'main' is normally a non-static function
error: expected declaration or statement at end of input
make.exe:*** [PRG_LED.o] error 1

thank you

Ryan

unread,
Apr 1, 2009, 2:51:21 PM4/1/09
to cc...@googlegroups.com
What are you compiling on windows? Also with what compiler?


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


[The entire original message is not included]

ROCKET100

unread,
Apr 1, 2009, 3:04:52 PM4/1/09
to CCCKC

I am using winavr with windows and the code is for a programmable led
here is the site;; http://www.instructables.com/id/Programmable-LED/

Jestin Stoffel

unread,
Apr 1, 2009, 3:14:31 PM4/1/09
to cc...@googlegroups.com
I don't see where the variables the compiler is complaining about are
declared. The C programming language requires you to declare your
variables before you use them. The variables might very well be
declared in one of the libraries you are including, in which case you
should make sure that the compiler is looking for them.

Could you supply us with the command line that you are compiling with?
If you are using something like Eclipse (as the instructable suggests)
you can usually find the actual compiler command in the Console tab at
the bottom. This command will tell us how you are calling the compiler,
so we can make sure that is correct.

ROCKET100

unread,
Apr 1, 2009, 3:28:15 PM4/1/09
to CCCKC

I am using notepad++ to write the code to C then I transfer that code
to winavr.

Jestin Stoffel

unread,
Apr 1, 2009, 4:02:43 PM4/1/09
to cc...@googlegroups.com
Sorry, I don't think I follow. I thought winavr was just a compiler you
run from the command line, the windows version of avr-gcc. It sounds to
me like you are using some IDE (itegrated development environment) that
is supposed to call winavr to compile the code.

Without knowing much else, I'd say that your IDE is calling the wrong
compiler. It didn't give any errors about finding your .h files, so
that means the undeclared variables are probably defined in the compiler
itself, as they just represent pins on the actual chip.

My guess would be that you are calling the gcc compiler rather than the
avr-gcc compiler that is provided by the winavr suite or applications.

I would try opening a command prompt and typing:

avr-gcc main.c -o flickled.hex

I have never used avr-gcc, but I imagine this is what the command would
look like. Don't be discouraged if this does not compile it. I didn't
know what to specify as include and linker directories, so most likely
this won't work. It will tell us at the very least if you have the
compiler installed.

Ryan

unread,
Apr 1, 2009, 4:22:26 PM4/1/09
to cc...@googlegroups.com
Sine you are on windows, lets do some remote assistance. Mstsc anyone?

avr-gcc main.c -o flickled.hex

SomeoneKnows

unread,
Apr 1, 2009, 9:36:38 PM4/1/09
to CCCKC
I got your code to compile when the MCU selected in the makefile is
attiny13 (as was shown in the comment at the top of your source code).
When I change the MCU to the attiny2313 I get mostly the same error
messages.

I'm using a Windows Vista machine and have the WinAVR tools and copied
the source code into Programmer's Notepad 2 (included with the WinAVR
toolset. I created the makefile using the MFILE program (also part of
the WinAVR toolset) to change the MCU type between attiny 2313 &
attiny13. The compiler is run from the Programmer's Notepad -> Tools
MakeClean then MakeAll.

The first include file in your source code is "#include <avr/io.h>". I
found the io.h file in the directory WinAVR/avr/include/avr/ (your
WinAVR root directory may be different). If you open the io.h file
you'll find a bunch of #elif defined statements. These correspond to
the MCU type:

#elif defined (__AVR_ATtiny13__)
# include <avr/iotn13.h>

and

#elif defined (__AVR_AT90S2313__)
# include <avr/io2313.h>

If you open and look in the avr/iotn13h file you can find the macros
for the undeclared items:
/* ADC Control and Status Register A */
#define ADCSRA _SFR_IO8(0x06)

If you look in the avr/io2313.h file and do a search for ADCSRA it
responds that the text was not found.

Each of the various MCU devices that Atmel (and WinAVR) supports has
these device definition files. Not all MCU devices are alike so you
need to consult the datasheets http://www.atmel.com/dyn/products/alldatasheets.asp?family_id=607#791
If you open the ATTiny 13 datasheet http://www.atmel.com/dyn/resources/prod_documents/doc2535.pdf
and do a search for ADCSRA it has a bunch of information about how
this value is used.

The datasheet for the ATTiny2313 is found here:
http://www.atmel.com/dyn/resources/prod_documents/doc2543.pdf and if
you search for ADCSRA it doesn't return any search results.

Rewriting code between MCU devices is beyond my current level of
experience (and the reason I'm using the Arduinos for now). I still
want to learn more about programming at the level you're attempting.

Hope this helps (some).

SomeoneKnows

unread,
Apr 1, 2009, 9:55:01 PM4/1/09
to CCCKC
Henry,

FYI, the reason I went looking at the header files is because each of
the undeclared values were all capitalized. A convention C programmers
often use is to define macro names using all capitalized letters.

Vince
> need to consult the datasheetshttp://www.atmel.com/dyn/products/alldatasheets.asp?family_id=607#791
> If you open the ATTiny 13 datasheethttp://www.atmel.com/dyn/resources/prod_documents/doc2535.pdf
> and do a search for ADCSRA it has a bunch of information about how
> this value is used.
>
> The datasheet for the ATTiny2313 is found here:http://www.atmel.com/dyn/resources/prod_documents/doc2543.pdfand if

SomeoneKnows

unread,
Apr 1, 2009, 10:14:39 PM4/1/09
to CCCKC
Henry,

The path of least resistance in getting your project built would be to
purchase an ATTiny13 so the source code doesn't have to be changed.
(not the ideal solution when learning to program controller devices).

If you go to the DigiKey web site they sell these devices
http://search.digikey.com/scripts/DkSearch/dksus.dll?lang=en&site=US&keywords=attiny13v&x=0&y=0.
It can be tricky ordering the right parts. One thing to look for is
the package type. You want the 8-DIP package for through hole devices.
The part number is visible on the Instructable as ATTINY13V 10PU which
is here http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=ATTINY13V-10PU-ND

SomeoneKnows

unread,
Apr 1, 2009, 10:34:38 PM4/1/09
to CCCKC
OK, I finally looked at the datasheets to see what the difference
might be. The ATTiny2313 has 20 pins while the ATTiny13 has only 8
pins. The program could be changed with some effort to identify which
pins you want to use but the artistic value of using an 8 pin part
would be compromised.




On Apr 1, 9:14 pm, SomeoneKnows <SomeoneKnows2...@gmail.com> wrote:
> Henry,
>
> The path of least resistance in getting your project built would be to
> purchase an ATTiny13 so the source code doesn't have to be changed.
> (not the ideal solution when learning to program controller devices).
>
> If you go to the DigiKey web site they sell these deviceshttp://search.digikey.com/scripts/DkSearch/dksus.dll?lang=en&site=US&....
> It can be tricky ordering the right parts. One thing to look for is
> the package type. You want the 8-DIP package for through hole devices.
> The part number is visible on the Instructable as ATTINY13V 10PU which
> is herehttp://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=ATTI...
Reply all
Reply to author
Forward
0 new messages