Marks256
New Member
So i've been getting quite good at microcontrollers recently, but the one thing i don't quite understand is, how can i calculate how fast a task is carried out? Take the following code for example;
So, assuming the 1MHz clock, how would one calculate how fast the LED will turn on/off? It can't be 1MHz, can it?
What about this code?
Obviously the number 17500 had to come from somewhere... So how was that calculated? How did someone know that it would take F_CPU divided by 17500 loops of decrementing ms, to equal 1ms?
Do you have to look at the compiled assembly and add up all the instructions or what? Let's assume compiler optimizations are turned on, if it will matter (which i'm sure it will.) For the record i did not write the examples i've posted. Here is where i got most of them Project 1 - Basic Blinking. i was too lazy to write my own code
Thanks!
Code:
#define F_CPU 1000000UL /* 1 MHz CPU clock */
#include <avr/io.h>
int main (void)
{
DDRC =0xFF; //set port c to outputs
while (1)
{
PORTC=0x00; //Turn LED off
PORTC=0xFF; //Turn LED on. Notice there was no delay.
}
return (0);
}
So, assuming the 1MHz clock, how would one calculate how fast the LED will turn on/off? It can't be 1MHz, can it?
What about this code?
00016 void delay_ms(uint8_t ms) {
uint16_t delay_count = F_CPU / 17500;
volatile uint16_t i;
while (ms != 0) {
for (i=0; i != delay_count; i++);
ms--;
}
}
Obviously the number 17500 had to come from somewhere... So how was that calculated? How did someone know that it would take F_CPU divided by 17500 loops of decrementing ms, to equal 1ms?
Do you have to look at the compiled assembly and add up all the instructions or what? Let's assume compiler optimizations are turned on, if it will matter (which i'm sure it will.) For the record i did not write the examples i've posted. Here is where i got most of them Project 1 - Basic Blinking. i was too lazy to write my own code
Thanks!