How can I Blinking LED without using delay?

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

My previous code I use delay to turn ON and turn OFF my LED to make it blink by using delay.

As we know (for the expert in AVR), the delay will use lot of cycles rather than we use Timer.

I have problem to make the LED blinking with Timer.

Im using ATMEGA256rfr2.

Thank you in advance.

Here my code:

int main(void)
{
	
	DDRB |= (1<<PINB4);
		
	
	TCCR1A = 0x00;
	TCCR1B |= (1<<CS11) |(1<<CS10);
	TCCR1B = 0x00; 
    while (1) 
    {
		
		if (TIFR0 & (1<<TOV1))
		{
			TIFR0 |= (1<<TOV1);
			PORTB ^= (1<<PINB4);
			
		}	
		
	}
	
}

 

This topic has a solution.
Last Edited: Thu. Apr 6, 2017 - 04:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
	TCCR1B = 0x00; 

This stops the timer.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
PORTB ^= (1<<PINB4);

This LED will toggle from 0 to 1 only?

Or it will continuous blinking?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
PORTB ^= (1<<PINB4);

This LED will toggle from 0 to 1 only?

Or it will continuous blinking?

The ^ operator means 'exclusive or'.  The effect is that if the bit was 1 it will be changed to 0, and if it was 0 it will be changed to 1.  So each time that statement is executed, the bit will toggle.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

kyrul wrote:
Im using ATMEGA256rfr2.

 

WHY would you use an AVR with RF communications capabilities to learn programming on?  Did you get the XPRO kit as a gift?

 

I am going to parrot what others have said in the past to others....Get an Arduino UNO and learn to crawl and walk on that before you try and run.

 

Jim

 

EDIT: Changed Kit Name

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

Last Edited: Thu. Apr 6, 2017 - 03:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I already run the code but the LED is not blinking at all.

I try this code in actual board ATMEGA256RFR2.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kyrul wrote:

 

Here my code:

int main(void)
{
	
	DDRB |= (1<<PINB4);
		
	
	TCCR1A = 0x00;
	TCCR1B |= (1<<CS11) |(1<<CS10);
	TCCR1B = 0x00; 
    while (1) 
    {
		
		if (TIFR0 & (1<<TOV1))
		{
			TIFR0 |= (1<<TOV1);
			PORTB ^= (1<<PINB4);
			
		}	
		
	}
	
}

 

 

As joeymorin said, you're stopping the timer. Could you have meant to write TCCR1C = 0x00.

 

But you have another problem, you are checking if Timer0 has overflowed instead of Timer1. Use TIFR1 instead of TIFR0 and you will find your LED will blink on/off roughly every second if you are using an 8MHz clock.

 

Cheers

 

 

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

jgmdesign wrote:

kyrul wrote:
Im using ATMEGA256rfr2.

 

WHY would you use an AVR with RF communications capabilities to learn programming on?  Did you get the XPLAIN kit as a gift?

 

I am going to parrot what others have said in the past to others....Get an Arduino UNO and learn to crawl and walk on that before you try and run.

 

Jim

 

Actually i only have this ATMEGA256rfr2.

Is it Arduino programming same as Atmel Studio?

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

jgmdesign wrote:

kyrul wrote:
Im using ATMEGA256rfr2.

 

WHY would you use an AVR with RF communications capabilities to learn programming on?  Did you get the XPRO kit as a gift?

 

I am going to parrot what others have said in the past to others....Get an Arduino UNO and learn to crawl and walk on that before you try and run.

 

Jim

 

EDIT: Changed Kit Name

 

I really don't see what difference it makes what device you get and to be honest, kyrul is pretty much learning to crawl with the simple blink program he is uploading to the uC. It isn't as if he is developing a sprinkler control system that wirelessly communicates with his home PC to set time schedules on his first run wink

 

His only problems that I could see was a few typos in the register names. After he sorts that out, his LED should be blinking in no time at all.

 

Cheers

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

adam3141 wrote:

 

 

As joeymorin said, you're stopping the timer. Could you have meant to write TCCR1C = 0x00.

 

But you have another problem, you are checking if Timer0 has overflowed instead of Timer1. Use TIFR1 instead of TIFR0 and you will find your LED will blink on/off roughly every second if you are using an 8MHz clock.

 

Cheers

 

 

 

Thank you adam3141.

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

adam3141 wrote:

 

 

I really don't see what difference it makes what device you get and to be honest, kyrul is pretty much learning to crawl with the simple blink program he is uploading to the uC. It isn't as if he is developing a sprinkler control system that wirelessly communicates with his home PC to set time schedules on his first run wink

 

His only problems that I could see was a few typos in the register names. After he sorts that out, his LED should be blinking in no time at all.

 

Cheers

 

Yeah you are correct adam3141. Please keep teaching me on this AVR stuff.

I just started learned it last Monday.

Thank you so much guys.

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

adam3141 wrote:
I really don't see what difference it makes what device you get and to be honest, kyrul is pretty much learning to crawl with the simple blink program he is uploading to the uC.

Agreed, but my poorly worded comment was asking why go with such a complicated device to begin with.  You are correct in that one can certainly use the RFR2, its more common though for beginners to simply pick up a $3.00 UNO clone on ebay and get going.

 

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

Maybe the RFR2 was a gift. Cheaper than a "$3.00 UNO clone on ebay". laugh

Ross McKenzie ValuSoft Melbourne Australia

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

valusoft wrote:

Maybe the RFR2 was a gift. Cheaper than a "$3.00 UNO clone on ebay". laugh

 

I mentioned that in my post.

 

ALL

Lets not derail this thread over my comment about the development kit, it serves no purpose whatsoever.  The OP answered my post, leave it at that. - JIM

 

Thanks

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