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.

Microcontroller Programming » Input pin Trouble

May 27, 2012
by Heno2au
Heno2au's Avatar

Hi , Sorry to go over old ground but i am trying to use a simple input to start a loop. As You can see i am using the led_blink code as a base. I have written the bootloader to the chip and all is OK.i It runs as it is written on a 328p

// led_blink.c
// for NerdKits with ATmega168
// hevans@nerdkits.edu

#define F_CPU 14745600

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

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

// PIN DEFINITIONS:
//
// PC5-- LED anode
uint8_t in1=0;

int main() {

    // LED as output
  DDRC |= (1<<PC5) | (1<<PC4) | (1<<PC3) | (1<<PC2) |(1<<PC1) | (1<<PC0);

  // loop keeps looking forever
  DDRD &= ~(1<<PD7);
  PORTD |= (1<<PD7);

  while (1) //(PIND & ((1<<PD7)==0))

    // turn on LED
    {

    PORTC |= (1<<PC5);

    //delay for 500 milliseconds to let the light stay on
    delay_ms(500);

    // turn off LED
    PORTC &= ~(1<<PC5);

    //delay for 500 milliseconds to let the light stay off
    delay_ms(500);

    // turn on LED
    PORTC |= (1<<PC4);

    //delay for 500 milliseconds to let the light stay on
    delay_ms(500);

    // turn off LED
    PORTC &= ~(1<<PC4);

    //delay for 500 milliseconds to let the light stay off
    delay_ms(500);
    // turn on LED
    PORTC |= (1<<PC3);

    //delay for 500 milliseconds to let the light stay on
    delay_ms(500);

    // turn off LED
    PORTC &= ~(1<<PC3);

    //delay for 500 milliseconds to let the light stay off
    delay_ms(500);
    // turn on LED
    PORTC |= (1<<PC2);

    //delay for 500 milliseconds to let the light stay on
    delay_ms(500);

    // turn off LED
    PORTC &= ~(1<<PC2);

    //delay for 500 milliseconds to let the light stay off
    delay_ms(500);
    // turn on LED
    PORTC |= (1<<PC1);

    //delay for 500 milliseconds to let the light stay on
    delay_ms(500);

    // turn off LED
    PORTC &= ~(1<<PC1);

    //delay for 500 milliseconds to let the light stay off
    delay_ms(500);
    // turn on LED
    PORTC |= (1<<PC0);

    //delay for 500 milliseconds to let the light stay on
    delay_ms(500);

    // turn off LED
    PORTC &= ~(1<<PC0);

    //delay for 500 milliseconds to let the light stay off
    delay_ms(500);
  }  
  return 0;
}
I have commented out at the line while(1)what I have tried to insert.

I know the pin is set as an input by measuring the voltage on the pin to see if it is high. I have tried all the examples I have found in the forum ( mostly refer to port c) but I cannot get it to work, All I want to do is wait until the input is low to start the while loop.

May 28, 2012
by pcbolt
pcbolt's Avatar

Hi Heno2au -

If you would like to have the loop execute it's code ONLY when PD7 is low, all you'd need is something like this:

while(1) {
  if (PIND & (1<<PD7)) continue;  // if PD7 is high -> go to the start of the loop
  // the rest of the LED code follows...

If you want to execute the loop code every time AFTER PD7 goes low, do something like this:

uint8_t loop_flag = 0;
while (1) {
   if ((PIND & (1<<PD7)) && (loop_flag == 0)) continue;
   loop_flag = 1;    // once PD7 goes low, the LED code will always execute
   // the rest of the LED code follows...

Post a Reply

Please log in to post a reply.

Did you know that you can connect a pushbutton to a microcontroller with only one wire? Learn more...