problems with c for AVR course

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
bgkdavis
Posts: 3
Joined: Mon Jun 25, 2007 3:15 pm
Contact:

problems with c for AVR course

Post by bgkdavis »

Im trying to follow the C for AVR course, however Im finding it a frustrating experience!

many of the examples compile with warnings as follows

../Ex24.c:27: warning: return type of 'main' is not `int'

Example 2.4 debounce complete fails to work even if I run the example files.

When I program the AVR I get a reading FLASH : failed error, and the AVR wont run unless I disconenct the programmer.

any help would be appreciated

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Post by Sean »

Hello

First the compiler warning

The warning is caused by the line:

void main(void)

The GCC compiler is capable of generating code that can return an integer value (to a calling function or operating system) on termination. The programs compile without warnings if the line is changed to:

int main(void)

This indicates that the whole program will return an integer value to the system that initiated it when it completes its operation.

The programs in the C for AVR course are stand-alone (no underlying operating system) and generally run in infinite loops, so there is no use for a return value.

In these instances the use of void or int return type will not make much difference. Void is strictly correct, but int will suppress the warning message.


Reading the FLASH memory

Assuming no faults, there are two main reasons for failure to read the FLASH memory.

1. The ISP frequency is too high. This is set under the Board tab of the AVRISP MkII panel. The value should be 4MHz or less for a 20MHz target board.

2. The lock bits have been set. These are available under the LockBits tab of the programmer panel. There are three groups of lock bits (Mode, Application Protection Mode, Bootloader Protection Mode). Make sure each group is set to Mode 1.

Does this problem occur with other programs in the course?

Sean

bgkdavis
Posts: 3
Joined: Mon Jun 25, 2007 3:15 pm
Contact:

Post by bgkdavis »

thanks I think Ive solved the reading problem, the ISP frequency was set too high

Ive alse solved the problems with the sample prograsm not working, each instance of CHAR needs replacing with int... it looks like the programs were written for a processor that supports CHAR, yet the Mega32 processor supplied with the kit doesnt!!!

ie

unsigned char key ( void )
{
unsigned char count = 0 ;
unsigned char oldv, newv ;
oldv = ( PINA & 0x01 ) ;
while ( count < 20 ) {
newv = ( PINA & 0x01 ) ;
if ( oldv == newv ) {
count++ ;
}
else {
count = 0 ;
oldv = newv ;
}
}
return oldv ;
}

should read

unsigned char key ( void )
{
unsigned int count = 0 ;
unsigned int oldv, newv ;
oldv = ( PINA & 0x01 ) ;
while ( count < 20 ) {
newv = ( PINA & 0x01 ) ;
if ( oldv == newv ) {
count++ ;
}
else {
count = 0 ;
oldv = newv ;
}
}
return oldv ;
}

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Post by Sean »

Support for char, int and other data types is the responsibility of the C compiler, not the target device.

The example program (ex 2.4) compiles and runs on an ATMEGA32 with all the variables declared as unsigned char.

Do you get compiler warnings/errors when you compile the original program code (unsigned char variables), or does it compile successfully but fail to run when downloaded?

Sean

bgkdavis
Posts: 3
Joined: Mon Jun 25, 2007 3:15 pm
Contact:

Post by bgkdavis »

when I do a build and run from AVR studio and step through the code using CHAR I get the following error on the WHILE statement in the unsigned key (void) subroutine.

count=invalid location unsigned char 0x10F9[SRAM]

it works fine when I use INT

Post Reply