Blinking LED without delay

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 Hello friends,

i'm trying to blink a LED using Atmel studio and Proteus. I use two "for loops", one to set "PA0" LOW; the other to set "PA0" LOW. The problem is when i set the counter of the for loop above 200 the LED does not turn off 

#define F_CPU 1000000UL
#include <avr/io.h>

int main(void)
{
    DDRA |= (1 << PA0);
    while (1) 
    {
		for (uint8_t i = 0; i <= 300; ++i)
		{
			PORTA|= (1 << PA0);
			
		}
		for (uint8_t x = 0 ; x <= 300; x++)
		{
		PORTA&=~ (1 << PA0);
		}
    }
	return 0;
}

 

This topic has a solution.
Last Edited: Tue. Mar 26, 2019 - 08:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So much nicer looking when you use proper spacing:

#define F_CPU 1000000UL
#include < avr / io.h >

  int main(void) {
    DDRA |= (1 << PA0);
    while (1) {
      for (uint8_t i = 0; i <= 300; ++i) {
        PORTA |= (1 << PA0);
      }
      for (uint8_t x = 0; x <= 300; x++) {
        PORTA &= ~(1 << PA0);
      }
    }
    return 0;
  }

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Mon. Mar 25, 2019 - 07:34 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
uint8_t

What is the range of values for an unsigned 8 bit variable type? 

 

Can that possibly count to 300?

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using a timer would be simpler and frees up the AVR for other tasks too....

 

 

East Side Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For some reason, this thread title reminded me of this..

https://www.youtube.com/watch?v=...

 

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes ki0bk, you got what i miss. This solves the problem.

uint8_t  is just 1 byte = 256 bits, so it cannot count above that number

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thread title says "without delay". I'd say that sticking in a loop doing the same thing 300 times is still a "delay" !

 

If you really want to flash "without delay" then explore using a timer interrupt.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The problem is, what if i cannot use a delay because the external crystal (oscillator) is broken or does not work. So i just wanted to do that trick to avoid the existence of the oscillator.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Salman22 wrote:

The problem is, what if i cannot use a delay because the external crystal (oscillator) is broken or does not work. So i just wanted to do that trick to avoid the existence of the oscillator.

If there's no clock, then your code will not work anyway.

 

And 255 is the highest number held in a uint8_t, not 256.

 

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Of course there's an internal clock. And yes 255 number, but 256 counts ( 0 --> 255).