NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Basic Electronics » Simple I/O circuit

September 23, 2012
by wadaltmon
wadaltmon's Avatar

Hello, I am looking to construct a simple I/O circuit using a SPST input at PC1 and an LED output at PC0. What I want to do is simply have the LED on when the button is pushed, and the LED off when the button is not being pushed (I am aware it is possible without a microcontroller, but I want to get this basic code down for a more complicated project later). My current code is as follows:

#define F_CPU 14745600

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"

int button = 0;
int main() {
  // LED as output
  DDRC |= (1<<PC0);
  DDRC &= ~(1<<PC1); // set PC1 as input
  PORTC |= (1<<PC1); // turn on internal pull up resistor for PC1
    button = (PINC & (1<<PC1)) >> PC1;
    if (button == 1){
        // turn on LED
        PORTC |= (1<<PC0);}
    else
    if (button ==0){
        PORTC &= ~(1<<PC0);}

  return 0;
}

However, I cannot get this code to accomplish what I want it to. What kind of mistake am I making, and how can I change my code to accomplish the simple task I mentioned? Thanks, Dalton

September 23, 2012
by Noter
Noter's Avatar

Where's the while() loop? And there is nothing to return to anyway so you can declare main as a void and leave out the return.

September 23, 2012
by wadaltmon
wadaltmon's Avatar

Wow... when I was editing to post the code, I took out the while loop. Whoops. Anyway, I decided to modify the code that I have:

#define F_CPU 14745600

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"

// PIN DEFINITIONS:
// PC1 - PUSHBUTTON
// PB5 - LED

void main() {

// Set the output pin
  DDRB |= (1<<PB5);

// Set the pin to input mode - Pushbutton
  DDRC &= ~(1<<PC2); // set PC2 as input

  // turn on the internal resistor for the pin
  PORTC |= (1<<PC2); // turn on internal pull up resistor

// declare the variables to represent pushbutton input
  uint8_t button;

  while(1) {

    button = (PINC & (1<<PC2)) >> PC2;

        if (button == 0)
        PORTB |= (1<<PB5);
        else
        if (button == 1)
        PORTB &= ~(1<<PB5);

}

}

Yet, still, it doesn't work. I see nothing wrong with it, really.

September 23, 2012
by wadaltmon
wadaltmon's Avatar

Nevermind guys, I figured it out. Just had to switch the input of the SPST to ground, and then put the cathode of the LED in the output pin, and the anode in my VCC rail. Hopefully this helps someone with the same problem as me in the future.

Post a Reply

Please log in to post a reply.

Did you know that a motor is harder to turn when its terminals are shorted together? Learn more...