for loop never

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
ianz
Posts: 2
Joined: Wed Jul 02, 2008 1:36 am
Contact:

for loop never

Post by ianz »

Hi
I have recently embarked on the C4AVR course and almost imediately ran into a stone wall. I was following along with the tutorial, though using different hardware. I had the LED's turning off and turning on, but when it came to the part where you add a 'for loop' to create a time delay, in order that the LED's flash slowly, I ran into difficulty. The time delay loop did not work for some reason. The LED's just flashed away at full speed and half power. I was able, using a multimeter on the output pin, to see that adding additional PORTA = ??; statements, I was able to change the mark to space ratio of the output, so the code I was writing was affecting the action. All that appears to be wrong is that the 'for loops' in the code appear to be being ignored.

Initially I tried this code on a Tiny13 based board, but when I ran into the problems as outlined above, I changed to a Mega128 board (Futurelec) that I also have. But exactly the same problem arose.

What I would note is that the Mega128 board has its clock set to 1MHz, so is compatible with the code in the tutorial.

I even changed the delay to a while loop, but that had the same result..Jack.

Hardware:
Futurelec Mega128 board
AVRISP Mk2 Programmer

Software:
XP Proffesional
AVR Studio 4
Win AVR
C4AVR

Any help appreciated
Ian
NZ

/* Ex 1.4 Flash one LED with delay */
/* (c) Rob Miles August 2005 */

#include <AVR\io.h>

void main(void)
{
unsigned int i;

DDRC=0x01;

while (1)
{
PORTC = 0x01;
for ( i=0 ; i < 65000 ; i= i + 1 ) ;
PORTC = 0x00;
for ( i=0 ; i < 65000 ; i= i + 1 ) ;
}
}

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: for loop never

Post by Benj »

Hello

Some C compilers do not like to have for loops without a piece of code to execute. I have noticed this problem also on the IAR compilers.

You should be able to use something like a nop instruction to allow the program to function correctly. Im not quite sure of the nop syntax for AVR but hopefully the code below should work correctly.

eg..

Code: Select all

#include <AVR\io.h>

void main(void)
{
unsigned int i;

DDRC=0x01;

while (1)
{
PORTC = 0x01;
for ( i=0 ; i < 65000 ; i= i + 1 ) 
   nop();
PORTC = 0x00;
for ( i=0 ; i < 65000 ; i= i + 1 ) 
   nop();
}
}

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: for loop never

Post by Benj »

Hello

I have just tested the program from the course and it is working correctly here. eg the LED is lit for approx 1 second and off for approx 1 second.

Have you edited any of the compiler settings (eg optimisations) that may have caused this problem to occur? Also are you using the version of GCC that is supplied with the course or have you upgraded this. Your compiler build messages may prove useful to tracking down the problem if you post them up on the forum.

ianz
Posts: 2
Joined: Wed Jul 02, 2008 1:36 am
Contact:

Re: for loop never

Post by ianz »

Its definitely a compiler issue. I discovered that i had both the 20060125 and 20080610 versions of winAVR installed and after much uninstalling and reinstalling and rebooting I managed to get some code to compile using the older version.

The code which compiled and worked using the GCC that came with C4AVR is.

Code: Select all

/*  For Tiny13 */
/*   */

#include <AVR\io.h>


int main(void)
{
unsigned int w;
unsigned int q;
unsigned int h;


DDRB = 0xFF;	// Entire port C as output

	while (1)
	{
		PORTB = 0x00;
			for (q=0;q<65000;q=q+1)
			{
				h=h+1;
			}
		PORTB = 0xFF; 
			for (q=0;q<65000;q=q+1)
			{
				h=h-1;
			}  
	}
The code that didnt work was that shown in the tutorial. But at least something works.. and LED's do flash!!
But I am left with an issue in that I cannot upgrade WinAVR because it is with the new version that I have the issue.

At least I can move on with the lessons now. Till the next problem

Ian

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: for loop never

Post by Steve »

Hello ianz,

This seems strange. Perhaps the compiler automatically does more aggressive optimisation when the target is an ATTINY device.

The course was written around a larger AVR chip and the tutorials have been thoroughly tested both here and "in the field" (we have been selling this course without this issue for a few years).

If we have access to the chip you are using, we will try the tutorials to see if we can replicate your problem.

You may also want to investigate the compiler optimisation features of GCC to see if you can compile so that the "do-nothing" for loops are not optimised out.

Post Reply