Thursday, February 6, 2014

pull-up resistor, Push-button and switching LEDs

I was having a lot of trouble thinking about the circuitry and safety for ADB, also discovered this geekhack post where other people are experimenting with getting ADB working and I decided that I was going too fast.

I should take much smaller steps at first so I followed this tutorial here which shows how to set up a pullup resistor for a push button. When I first build the push-button circuit (which you power from USB using the +5V and GND pins, the micro isn't really involved here) and tested it with my voltemeter it didn't work since I had the push button in 90 degrees from how it needed to be. Had to bend the pins a bit to get it to plug in right

After that I built my circuit with the two LEDs in remembering that the shorter lead goes into GND and using 470 ohm resistors to give me about 10 mA of current through them each.



and wrote code to make it so that the other LED switched on if you press the button down:

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

#define CPU_PRESCALE(n)    (CLKPR = 0x80, CLKPR = (n))
#define DIT 60

#define LED1 1
#define LED2 2

#define BUTTON1 1

int main(void) {
  // set for 16 MHz clock
  CPU_PRESCALE(0);
 
  // http://www.pjrc.com/teensy/pins.html
 
  DDRD |= LED1 | LED2; // set D0 and D1 to be output pins
  PORTD = LED1; // switch only LED1 on
 
  DDRC &= ~BUTTON1; // set C0 to an input pin
  PORTC &= ~BUTTON1; // configure it to normal mode since we have an external pullup resistor
 
  while (1) {
    if(PINC & BUTTON1)
      PORTD = LED1;
    else
      PORTD = LED2;
   
    _delay_ms(DIT);
  }
}


when I loaded my code onto the chip nothing happened :(

I did continuity testing with my voltmeter to check whether the GND strip was connected and it wasn't. I though that the whole GND strip was one line (and I observed this ) - I guess don't really understand the breadboard. Maybe I should open it up at the back.

When I plugged that in it worked perfect:

No comments:

Post a Comment