LED Flash Issue

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

Hi guys, Im continuing on a project started by someone else and having a few issues just debugging to see where the code gets up to.

I'm using a Mega8-16 with AVRISP2 in conjunction with AVRStudio.

I've hooked up an LED to PortD and when I run a basic LED flash program everything is fine (5V at PD7 with the LED flashing once). eg.

DDRD = 0xff;
PORTD |= (1 << PD7);
_delay_ms(4000);
PORTD &= ~(1 << PD7);

However when I insert this code into the main function of the existing program (not forgetting to import and ), I only get about 1.3V at the pin and the LED doesn't switch on and then after 4 seconds the pin doesn't change to 0V as expected.

Any ideas?

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

You need 2 delays. Do you have a current limit resistor in series with the led?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Yes there is ~500R limiting the current. I have also tried:

DDRD = 0xff;
PORTD |= (1 << PD7);
_delay_ms(4000);
PORTD &= ~(1 << PD7);
_delay_ms(4000);
PORTD |= (1 << PD7);

But same story, the pin stays at 1.3V and doesn't go down to 0V then back high as expected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
DDRD = 0xff; 
PORTD |= (1 << PD7); 
_delay_ms(4000); 
PORTD &= ~(1 << PD7); 
_delay_ms(4000); 

Don't need the last line as the code should loop up.

But where is the rest of your code? ie the for( ;; ) or while (1) for the loop.

Also I'm not sure if delay_ms can go that high. I think the latest winAvr can now do it?? but previously it was limited to a max of 200 something ms/clock. Have a read of that part of the docs for delay_ms.

Which chip are you using?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

The working test program I used to flash the LED is:

#include 
#include 

int main(void)
{
    DDRD |= (1 << 7);
    // Blink 
    PORTD |= (1 << PD7);
    _delay_ms(400);
    PORTD &= ~(1 << PD7);  
    _delay_ms(4000);      
    PORTD |= (1 << PD7);	

    return 1;
}

The program above works with no problems but as soon as I copy the code into the full program which I am debugging, the issue outlined in the first post arises. I've also tried _delay_ms(400) to no avail. The program uses 30kHz and 8kHz ISRs which may be causing the issue, but being a newbie I'm still just trying to figure out how they function. :)

I am using the ATMega8-16PU DIP-28

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

PD7 is also the analog comparator. Is any code accidentally turning that on?

Also the above program in standalone mode will crash, I believe, as it has nowhere to return to.ie most likely hang at the last line or restart from the beginning. Not sure if GCC handles that properly.

This loops always.

#include  
#include  

int main(void) 
{ 
    DDRD |= (1 << PD7);

    for( ;; )
	{ 
    // Blink 
    PORTD |= (1 << PD7); 
    _delay_ms(4000); 		//Max 6.5535 seconds with latest winAvr
    PORTD &= ~(1 << PD7);  
    _delay_ms(4000);
	}     
}

Of course the ISR if not done properly will cause a lot of problems too.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Tue. Mar 18, 2008 - 07:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Whoa - JS helping out on a C question - a miracle has happened! :D

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

Quote:
JS helping out on a C question
The poor OP has no chance of solving his problem now!! :evil:

Today a flashing led...tomorrow ze vorld...

Definetely a bad case of Clawson-ites here...

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I managed to partially solve the problem, the code im debugging already had the ports and pins defined with specific names in the header, so using the defined names works. Now I can switch an LED on to see where the code runs until although it switches on and stays on not giving me a single flash like I want.

//////LED DEBUG------------
	LED1_PORT |= LED1A;
	msDelay(10);
	LED1_PORT &= ~LED1A;
	msDelay(10);		
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Whoa - JS helping out on a C question - a miracle has happened!

Yeah John, WTF

I thought you and I were the only assembly-only folks left.
WEll I did admit I may have to sit down and learn 'c' at the ripe old age of 39.

If Dean opens his mouth I will fly to australia and sick a kangaroo up his Buttloader :lol:

Ok, now back to the OP
Has your issue resolved?

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

Quote:

If Dean opens his mouth I will fly to australia and sick a kangaroo up his Buttloader Laughing

I do love the C language, but I think I'll keep my mouth shut on this occasion...

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

Quote:
not giving me a single flash like I want.
How is LED1A defined??

Have you tried

  LED1_PORT |= 1<<LED1A; 
   msDelay(10); 
   LED1_PORT &= ~1<<LED1A; 
   msDelay(10);       

(I could show off with _BV ... and why aren't the C gurus helping??)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js, LED1A is defined as:

#define LED1A (1 << PD7)

I tried the code you suggested but I did't get anything. The LED just stayed off.

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

Quote:
The LED just stayed off.
hmmmm 10mS is prety fast. Tried a few hundred ms?

So, with all the time you spend debugging, you could have got a partime job and earn the 50 bucks for a Dragon and changed the chip to a M88, then you could have had debugWire facility :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Does your real application actually define PD7 somewhere as output? - just to make sure.

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

...and does the code actually get to the point where it is supposed to flash in the larger program?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Quote:
So, with all the time you spend debugging, you could have got a partime job and earn the 50 bucks for a Dragon and changed the chip to a M88, then you could have had debugWire facility Smile

Without a doubt, and carton of beer for the weekend. Coming from only working with HC12s to AVRs I never anticipated that something so simple would have me stumped for so long.

Quote:
Does your real application actually define PD7 somewhere as output? - just to make sure.

I've checked that PD7 is set to output in the code and through the debugger just to be sure.

Does anyone have a simple LED flash program that they know works on an ATmega8?

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

You do realize that the analog comparator is enabled by default, don't you? Now, you'll have to check whether that does an override on port direction like some of the peripherals.

We still haven't seen the complete "new" program.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Which way is the led connected to the pin?
Between Vcc and pin or between pin and GND?
This is to ask which way lits the led: setting or clearing the bit.

Debugging is for sissies and delivery for surgeons. Real men do demonstration.

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

Quote:
Does anyone have a simple LED flash program
6th post down??

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Fixed.

  
ACSR |= (1 << 7); // Disable analog comparator

PORTD &= ~(1 << PD7);  
_delay_ms(50);
PORTD |= (1 << PD7);  
_delay_ms(500);
PORTD &= ~(1 << PD7);

That did the trick. Thanks everyone

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

Quote:
Disable analog comparator
Wasn't this one of the early things I said? :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Good point js, I assumed that it was off by default. But strangely removing that line doesnt seem to make a difference, it works the same. Thanks a bundle for your help.