Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.

Newbie help needed!
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Newbie help needed!

by I_am_bad_at_this on Fri Aug 22, 2008 3:32 pm

Hello, im having a problem. I want my led to blink when the button is pushed, but its not working. How do i solve it.
Note: The led pin is connected to the negative lead of led, resistor, and VCC, thts why PB0 is set to high in the initialization.
Any help is great!
Code: Select all | TOGGLE FULL SIZE
#include <avr/io.h>
// PB1 is connected to switch (then to GND)
//PB0 is connected to LED (VCC on other side of LED)

#include <util/delay.h>

int main(void)

    DDRB = 0x01; //all input except PB0
   PORTB = 0xFF; //all internal resistors activated and PB0 is high
   if(PINB,1==0) {    //if button is pressed (no debounce)
       PORTB = 0x00;     //Set PB0  to ground
      _delay_ms(100); //delay
      PORTB = 0xFF;  //Set PB0 high
      _delay_ms(100);  //delay
    return 0;   /* never reached */

Posts: 31
Joined: Thu Dec 13, 2007 12:50 am
Location: AZ

by corndog on Sun Aug 24, 2008 10:01 pm

At first glance I'm not sure why that doesn't work. The "PINB,1" is new to me as I program my AVRs in assembly, but I'll assume that is valid in C. Maybe it should have an extra set of () around it like "if((PINB,1)==0)"?

The first thing I would do is make your loop ONLY blink the LED. This should give you a good quick hint on where to dig in further (i.e., is this an input problem or an output problem?).

A few other questions that could be asked:
What value of resistor are you using?
Have you verified your LED/resistor combo out-of-circuit?
Do you have any other known-functional programs using PORTB that you can run to verify that the port is still operating as expected?
Posts: 2
Joined: Sun Aug 24, 2008 1:54 pm

by mtbf0 on Sun Aug 24, 2008 10:50 pm

what compiler are you using? (PINB.1 == 0) does not look like avr-gcc syntax. if you are using avr-gcc you night want to change that to something like (~(PINB & (1 << PB1)))

also for avr-gcc the header file for delays is avr/delay.h
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by shadow on Tue Aug 26, 2008 9:07 pm

hello, your problem is first, as said by others, that using
Code: Select all | TOGGLE FULL SIZE
(PINB & 0x08)

or other methods instead of (PINB,1)is a very good idea. also, you must be having a problem with internal resistors. You activate them in you initialization, but deactivate them during you main code (to blink the LED).

Best of luck

Posts: 22
Joined: Sun Mar 02, 2008 5:59 pm

Please be positive and constructive with your questions and comments.