Simple push button and simple LED out does not work

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

I have used the following code to get me started on programming ATTINY13.


/* -----------------------------------------------------------------------
 * Title: Led blinking reaction on pressed switch (I/O)
 * Hardware: ATtiny13
 * Software: WinAVR 20060421
 -----------------------------------------------------------------------*/
 
 #define F_CPU 9600000UL // Define software reference clock for delay duration
 // Must be write before call delay.h
 #include 
 #include 
 
 #define SWT PB4 // Define ext switch pin on PB0 *1
 #define LED PB2 // Define ext led pin on PB2 *2
 
 int main(void) {
 	 DDRB &= ~(1<<SWT); // Set input direction on SWT (PB2) *1
	 DDRB |= (1 << LED); // Set output direction on LED (PB0) *2
 
	 for (;;) // Endless loop
	 {
	 	if (bit_is_set(PINB, SWT)) // Read SWT pin (if SWT pressed, do the loop one time)
		{			
			PORTB &= ~(1 << LED); // Set 0 on LED pin (led turn on)
			_delay_ms(300); // Call delay for 300 milisec
			PORTB |= (1 << LED); // Set 1 on LED pin (led turn off)
			_delay_ms(300); // Call delay for 300 milisec
		}
		else
		{
			PORTB &= ~(1 << LED); // Set 0 on LED pin (led turn on)
		}			
	 }

	 return 0;
}

I am not sure where I've got the code from but the LED is just flashing on and off regardless of whether I push a button or not. What am I doing wrong? I am feeding 5V into the input SWT.

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

Quote:

#define SWT PB4 // Define ext switch pin on PB0 *1

PB4 or PB0?

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

Schematic/wiring fault?

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

Does bit-is-set look for the switch input to be hi? That's 'set' if you think in positive logic, but I bet the voltage on the input goes to zero when you close the contacts. That's 'reset' in positive logic.

Imagecraft compiler user

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

Which level have you applied on the input pin, if the switch was open?
You must apply high or low level !
A floating input was never an option !

Peter

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

theusch wrote:
Quote:

#define SWT PB4 // Define ext switch pin on PB0 *1

PB4 or PB0?

Sorry the comment was wrong. It's PB4.

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

bobgardner wrote:
Does bit-is-set look for the switch input to be hi? That's 'set' if you think in positive logic, but I bet the voltage on the input goes to zero when you close the contacts. That's 'reset' in positive logic.

I have straight 5V going into the input pin. Are you suggesting that I should apply GND to the input pin to run within the if statement?

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

danni wrote:
Which level have you applied on the input pin, if the switch was open?
You must apply high or low level !
A floating input was never an option !

Peter

I basically have switch that connects to 5V. Was I supposed to use some sort of pull down resitor?

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

Quote:

I have straight 5V going into the input pin.

Well, then your program is doing exactly what you are telling it to--as long as the pin is high, it is going to go through your blink sequence. If you remove the 5V and pull to Gnd, then you won't.

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

clawson wrote:
Schematic/wiring fault?

For input, I have switch that connects to 5V. For output(LED), I have LED in series with 330 ohm resistor to ground.

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

Quote:

For input, I have switch that connects to 5V

OK, and when not connected to 5V it is floating. It probably floats high. Thus your results.

As suggested, connect your "made" switch to Gnd, and use the internal pull-up to keep it high when the switch is not made.

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

theusch wrote:
Quote:

For input, I have switch that connects to 5V

OK, and when not connected to 5V it is floating. It probably floats high. Thus your results.

As suggested, connect your "made" switch to Gnd, and use the internal pull-up to keep it high when the switch is not made.

So I should change "bit_is_set" to "bit_is_clear" instead while having GND connected to the switch? Pardon me but do I need to set something up in AVR studio to uss the internal pull-up to be kept at high when not intentioned?

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

OK I've changed my input to the pin4 to GND from 5V and it now works.

Thanks everyone. :) Problem solved.

I'm going to post another question related to ADC. I'll post the link in a few seconds.

Thanks everyone.

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

Quote:

OK I've changed my input to the pin4 to GND from 5V and it now works.

You realize if it "works" you are lucky if you are not pulling up the input--otherwise it floats and can have any value.

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

It seems to work. I haven't found any info on pulling up the input yet.

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

Quote:

I haven't found any info on pulling up the input yet.

It's in the AVR datasheet. For any IO pin if the controlling bit in DDR is 0 but the same bit in PORT is set to 1 then that enables a pull-up (about 50K) on the pin. By default the bits in DDR and PORT are both 0. If you only set DDR (or don't even bother with that - relying on it being 0) then the PORT bit will also be 0 and there is no pull-up so the input is "floating", also known as tristate or high-Z and does just what the name suggests - it "floats" between 0 and 1 and any moment might be read as 1 or 0.

So to enable the pin up just add the following line:

     DDRB &= ~(1<<SWT); // Set input direction on SWT (PB2) *1 
     PORTB |= (1<<SWT); // ++++ enable pull up ++++

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

Thanks. Nice explanation and it totally makes sense to me. Thanks alot!