#include <mega8535.h>
#include <delay.h>
void main(void)
{
PORTC=0x00;
DDRC=0x08;
while (1)
{
PORTC=0x08;
delay_ms(1000);
PORTC=0x00;
delay_ms(1000);
}
}
lets take that 1 nop instruction takes 25ns
then claculate x such that x = no of nops for 1 sec.
then use the for loop
for(i=0;i<x;i++) {
_asm {
nop
} endasm
}
for(i=0;i<8333333;i++){
_asm{
nop
}endasm
}
unsigned long int i,x;
for(i=0;i<3003003;i++){
/* Assembly language code sequence */
#asm
nop //1 nop instruction takes 333ns if Fosc = 12 MHz
#endasm
};
}
Don_dody said:I want to make a blinking LED with 1s delay using ATmega8535.
internetuser2k12 said:http://www.piclist.com/techref/piclist/codegen/delay.htm
**broken link removed**
**broken link removed**
@Don_dody
What do you mean by "It is working actually but the delay is not exactly 1s"? Is the difference a few microseconds or many milliseconds?
Except compiler's in-built delay function, you can do it by using AVR timer itself.
the actual delay may vary if the your code makes use of internal oscillator "8MHz, 4MHz, 2MHz, or 1MHz" on chip clock generator provided by the AVR controllers. this is so because the frequency of oscillation is not trimmed for optimum accuracy. you have to try to calibrate the frequency of choice that is to say if the internal clock option has been selected.
the factory default is set to the 1MHz internal clock option so you also have to be sure of what you are actually doing. you may end up compiling your code with say a 12MHz crystal value while the clock option is set to an internal value of say 2MHz and this will surely crop up to bizarre attitude of your design. so i advice you do this check now
1) check the selected frequency in your compiler....
2) if it is one of the internal clock option then you can make use of the internal oscillator provided bearing in mind that you may have to calibrate your micro since process variation will affect its norminal frequency value.
3) also be sure that you have not programmed the clock option "CKOPT" fuse because this can lead to errors when it is not needed' for example if your code is based on 4MHz crystal and you have it programmed; then you will end up having 8MHz that is when you are run on external crystal value.
4) above all you have to match the frequency that your board runs to that of what you have in your compiler
As I said I'm not aware of codevision AVR, but if there is such a great difference, there must be something simple you are missing. Let's start from the crystal. What crystal are you using? Maybe the internal RC? In what frequency? What is the value of the corresponding fuse bits? Is there any frequency definition inside codevision libraries?Well I mean the LED is blinking but it is not exactly 1s, maybe it is approaching 2s.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?