Blinking LED with 1s delay

Status
Not open for further replies.

Don_dody

Full Member level 1
Joined
Nov 4, 2012
Messages
96
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Indonesia
Visit site
Activity points
1,921
Hi,,

I want to make a blinking LED with 1s delay using ATmega8535. The compiler is codevisionAVR and here is my code:

Code:
#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);
      }
}

It is working actually but the delay is not exactly 1s. Anybody can help me to make it exactly 1s??

Thank you
 

How much delay are you getting. If the standard delay is not working write your own delay routine. What is your Fosc?
 

How much time does your ATmega8535 takes to execute 1 instruction? Check the datasheet.

https://www.electro-tech-online.com/microcontrollers/35346-pic-delay-calculator.html

or you can use timer to generate 1 sec delay. http://www.quora.com/How-can-I-write-a-software-delay-C-function-for-the-Microchip-PIC32-MCU

You can also do like this

Code:
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
}
 
Last edited:

Well, that calculator is useful anyway but firstly I want to learn and understand how to build the code by myself..
Is there any code example in C ?
 

check my last post. It is updated.
Try this code

Code:
for(i=0;i<8333333;i++){
	_asm{
		nop
	}endasm
}

This code is for PIC

Code:
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
      	};
}

You have to use the similar method for your mcu.
 
Last edited:

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**


@internetuser2k12
Why do you insist on giving references on PIC, when the OP needs help on AVR? I will take a wild guess (since I don't use CodeVision AVR) to say that the code provided in post #8 will not be compiled.
**broken link removed**

I will then make my second wild guess to say that the delay provided by this same post will not be exactly 1sec. You should give a delay code based on AVR timers, or the compiler functions.

Don_dody look like he is new on microcontrollers and you are confusing him in this way. It is admirable that you want to help other people, but if you are not aware of an issue, you will help by staying silent and not give irrelevant references. You just stand on the OP's way and nothing more.


@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.
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106
 

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....


hope this helps...........sequel
 

@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.

You are right, honestly I'm very very new on microcontrollers.


Well I mean the LED is blinking but it is not exactly 1s, maybe it is approaching 2s. I've been searching for AVR timer but I found some instant codes without explanation. I just want to learn and completely understand the concept, not copying.

- - - Updated - - -


Well thank you, I will try it first and maybe will come again with problems :-D
 

Well I mean the LED is blinking but it is not exactly 1s, maybe it is approaching 2s.
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?
 

I use 12 MHz crystal. For a simple program like blinking LED, I think I don't need to define any other thing instead of the code above.
 

What is the value of the corresponding fuse bits? Is there any frequency definition inside codevision libraries?
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…