Hi to all.
I have written some simple code that is giving me some weird results.
I am using Codewarrior ver 5.5.1272.
The code has 2 main parts. A 1ms timer and a main function.
Here is the main function:
Code:void main(void){ /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/ PE_low_level_init(); /*** End of Processor Expert internal initialization. ***/ DDRB_BIT6 = 1; // set portB bit 6 to output (light outout) DDRB_BIT7 = 1; // set portB bit 7 to output (status_led) DDRA_BIT6 = 0; //set portA bit 6 to input gate_open_flag = 0; light_on_delay = 20; while(1) { if(light_trigger==0) // trigger turns on light { counter_1sec = 0; Light = !Light; while(light_trigger==0 && ( counter_1sec < 4)); //wait for button to be released or 4 sec to pass //TIE_C0I = 0; if(counter_1sec < 4) // button not held down , light on - TIMED { timeout_flag = 1; //enable timeout in 1ms timer module status_led_mode = 0; } else // button held down , light on permanently { //flashes status led (x) times status_led_mode = 1; //set led to flash-- delay 2 sec-- flash timeout_flag = 0; } while(light_trigger==0); }//--------------------------------------- if(gate_open_flag && Light == 0) //gate turns on light for delay time { counter_1sec = 0; Light = 1; } if( counter_1sec > light_on_delay && gate_open_flag) { gate_open_flag = 0; Light = 0; } //--------------------------------------- } What happens:
The variable counter_1sec is incremented in the 1ms timer every 1000ms. Light_trigger is mapped to portA bit 6.
When light_trigger is held low one of 2 things happen:
If it is held low for less than 4 seconds the IF part of the if..else statement executes. If it is held low for longer than 4 seconds the ELSE statement executes. Everything works fine up till here.The //TIE_C0I = 0; instruction turns off the 1ms interrupt. I put this in there to help me step through the code with the debugger (P&E USB multilink )without continuously jumping to the timer interrupt.
The main problem happens on the line: " status_led_mode = 1; " in the ELSE statement.
Before this line the value of the status_led_mode variable is 0.
After this line executes the value is 5 , not 1 as you would expect. I have stepped through this code many times with the same result.
Now if I change the instruction to " status_led_mode = 2; " or any number other than 1 it works correctly.
Any idea what may be going on here.
I have noticed that the assembler that is generated is different if I use the value 1 as opposed to any other number .
It looks like the compiler is performing some sort of optimization somewhere.
I have tried creating the project from scratch with Processor expert (all default settings) with the same result.
Any help appreciated as I'm really stumped.Cheers
Rob