Newbie help needed!

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
I_am_bad_at_this
 
Posts: 31
Joined: Thu Dec 13, 2007 12:50 am

Newbie help needed!

Post by I_am_bad_at_this »

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

#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 
    
    
    for(;;){ 
   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 */ 
} 

[/code]

corndog
 
Posts: 2
Joined: Sun Aug 24, 2008 1:54 pm

Post by corndog »

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?

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Post by mtbf0 »

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

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

Post by shadow »

hello, your problem is first, as said by others, that using

Code: Select all

(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

-Shadow

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”