chiragshetty
Newbie level 3
- Joined
- Jun 4, 2014
- Messages
- 3
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 26
The code below is to show a problem I encountered while working on a bigger program.
I'm using ATmega16 with 16MHz external crystal. I measure the frequency at which PIN7 of portD is toggling (i.e time taken per loop).
When I don't include the statement "PORTC = 0x00;" inside if (count>1000), the frequency of PIND7 is 27kHz.
But when I include the statement, it reduces to 16kHz.
This problem doesn't occur if count is an int variable. I don't understand how a statement that is executed once in 10000 loops affects the loop time.
Surprisingly, this frequency change occurs even if I change the if condition to if(count<0), which will never happen.
I experimented a lot, but found no way to fix this.
I'm using ATmega16 with 16MHz external crystal. I measure the frequency at which PIN7 of portD is toggling (i.e time taken per loop).
When I don't include the statement "PORTC = 0x00;" inside if (count>1000), the frequency of PIND7 is 27kHz.
But when I include the statement, it reduces to 16kHz.
This problem doesn't occur if count is an int variable. I don't understand how a statement that is executed once in 10000 loops affects the loop time.
Surprisingly, this frequency change occurs even if I change the if condition to if(count<0), which will never happen.
I experimented a lot, but found no way to fix this.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> #include<math.h> #define F_CPU 16000000UL int main(void) { DDRD = 0xFF; PORTD = 0b11000000; DDRC = 0xFF; PORTC = 0b00000000; float count =0.0; while (1) { count = count + 1.0; if (count > 10000.0) { //PORTC = 0x00; //********************************** count = 0.0; } PORTD ^= 0x80; _delay_us(18); } }
Last edited by a moderator: