ATmega8A malfunction when using _dealy_ms(250);

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

Hello,
I have a terrible problem.
I want to make program. It reads values of two switches one at PB6 and one at PB7. Stores them in variables. If i press switch it lights up LED on PC4. If switch is unpressed LED is off.

Here is my code:

#define F_CPU 1000000UL
#include 
#include 
#include "lcd.c"
#include "ds18b20.c"
#include "buttons.c"

/************************************************
	Define LCD connections and DS18B20 connections
	in ds18b20.h and lcd.h, initialize buttons with
	buttons_init()
*************************************************/

int main(void)
{
	DDRC|=_BV(PC4);
	PORTC&=~_BV(PC4);
	char UP,DOWN;
	buttons_init();
	//LCDInit(LS_NONE);
	while(1)
	{
		UP=(BTUP_PIN&_BV(BTUP_CON));
		DOWN=(BTDOWN_PIN&_BV(BTDOWN_CON));
		//ds_conv();
		//LCDWriteStringXY(2,0,tempBuf);
		if(UP==0)
		{
			PORTC|=_BV(PC4);
		}
		else
		{
			PORTC&=~_BV(PC4);
		}
		if(DOWN==0)
		{
			PORTC|=_BV(PC4);
		}
		else
		{
			PORTC&=~_BV(PC4);
		}
		//LCDWriteStringXY(hor,ver,"X")
		_delay_ms(250);
		//LCDClear();
	}
}

buttons.c

#include 

/************************************************
	BUTTONS CONNECTIONS
*************************************************/
#define BTUP_DIR DDRB
#define BTUP_PORT PORTB
#define BTUP_PIN PINB
#define BTUP_CON 6
#define BTDOWN_DIR DDRB
#define BTDOWN_PORT PORTB
#define BTDOWN_PIN PINB
#define BTDOWN_CON 7


/************************************************
	BUTTONS SETUP (pins set as inputs, pulled up)
*************************************************/
void buttons_init(void)
{
	BTUP_DIR&=~_BV(BTUP_CON);
	BTUP_PORT|=_BV(BTUP_CON);
	BTDOWN_DIR&=~_BV(BTDOWN_CON);
	BTDOWN_PORT|=_BV(BTDOWN_CON);
}

Compilation is ok, no errors (using ATmel studio 6) but when i load program to AVR this is what happens:
No switch is pressed. LED is off (as it should). When i press button attached to PB7 led fully lights up (as it should). But when i press switch attached to PB6 (related to UP variable, first IF/ELSE) LED is barely visible, you see just a little of light and its blinking rapidly (led should fully light up without blinking just like before).

When i comment out //_delay_ms(250); everything is working fine. No matter which button i press, LEDs will light up fully.

I checked buttons, im even wiring pins without them (attaching to ground), checked it on other ATmega8, flashed it multiple times (using Sinaprog), rebooted PC, tried other USB port (using USBasp), restarted ATmel studio, recompiled project.
I also tried changing ATmega pins, attaching switches to other pins, changing led, attaching it to other pin. Nothing seems to work.

Im attaching my files to this post, if you have any question or want me to explain something ill answer you asap. Anything to find explanation to this.
Thank you in advance for help. [/code]

Attachment(s): 

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

What happens if you write the status of the second switch to a different LED , instead of the same LED? Ie. two switches & two LEDS!

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

If both buttons unpressed first IF tells it to off and second to off. If UP is pressed first if tells it to light up but second button lights it down immitiatly (the blink?) then its off for time of delay and blinks again when comes back to first IF?
But if DOWN is pressed, first if tells it to light off, then second IF tells it to light on, its on for peroid of delay but first it tells it to turn off (but i see its still up, or its too fast to notice diffrence between first if telling to light off and second telling to light up)?

Is it right?

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

If you push the UP button the LED will turn on briefly, then the code will see that the DOWN button is not pushed, and turn off the LED for 250ms. It's a bad design. If you want to light the LED if either button is pushed, try

if ((UP == 0) || (DOWN == 0))
  ..light LED..
else
  ..unlight LED..

It would be better to use ! in assigning UP and DOWN, so you can write

if (UP || DOWN)
  ..light LED..
else
  ..unlight LED..
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I have a terrible problem.
I want to make program.

:-)

(Sorry, it was irresistible..)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Let me also point out that using such a long delay is murder on your button response. You should be looking at your buttons at least 20 times per second, not 4 times per second. But updating your display 4 times per second is just about right. So you've really got two different loops in your program, one that runs 20/sec and one that runs 4/sec.

As soon as you learn how to use timers, stop using _delay_ms. It's a crutch that will hinder you from producing solid, responsive programs.

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

Quote:
If both buttons unpressed first IF tells it to off and second to off. If UP is pressed first if tells it to light up but second button lights it down immitiatly (the blink?) then its off for time of delay and blinks again when comes back to first IF?
But if DOWN is pressed, first if tells it to light off, then second IF tells it to light on, its on for peroid of delay but first it tells it to turn off (but i see its still up, or its too fast to notice diffrence between first if telling to light off and second telling to light up)?

Is it right?


Probably (answering only your last question), that is why I suggested you use two LED's so you can see what is going wrong with your logic.
I find your written description not very easy to understand. As a picture is worth a thousand words, try and do a State Transition Diagram and then implement it as a simple Finite State Machine. kk6gm has given some good ideas to help in the doing that.
Quote:
I have a terrible problem.
I want to make program.

How about
Quote:
I want to make program.
I have a terrible lot to learn.
:)

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?