0

I'm starting to play around with a Teensy 2, as well as learning C code. Currently I'm trying to figure out how to have a pin setup as an input. My code is as follows:

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

#define RED_LED_ON  (PORTB |= (1<<7))
#define RED_LED_OFF  (PORTB &= ~(1<<7))

#define GREEN_LED_ON  (PORTD |= (1<<2))
#define GREEN_LED_OFF  (PORTD &= ~(1<<2))

#define BLUE_LED_ON  (PORTC |= (1<<7))
#define BLUE_LED_OFF  (PORTC &= ~(1<<7))

#define SWITCH_OUT_CONFIG (DDRD |= (1<<6), PORTD |= (1<<6))

#define SWITCH_IN_CONFIG (DDRF &= ~(1<<1), PORTF |= (1<<1))

#define LED_CONFIG  (DDRB |= (1<<0))
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))

#define MY_DELAY 100

int main(void) {
    // set for 16 MHz clock, and make sure the LED is off
    CPU_PRESCALE(0);
    LED_CONFIG;
    /*SWITCH_IN_CONFIG;
    SWITCH_OUT_CONFIG;*/

    DDRD |= (1<<6); //Set pin D6 as output
    DDRF &= ~(1<<1); //Set pin F1 as input

    PORTD |= (1<<6); //Set pin D6 output to high
    PORTF |= (1<<1); //Set pin F1 to act as pullup resistor

    RED_LED_OFF;
    GREEN_LED_OFF;
    BLUE_LED_OFF;

    // initialize the USB, but don't want for the host to
    // configure.  The first several messages sent will be
    // lost because the PC hasn't configured the USB yet,
    // but we care more about blinking than debug messages!
    usb_init();

    for(;;) {
        if(PINF & (1<<1)) {
            /*Do stuff here, since button is pushed*/
    }
    else {
        /*Do nothing*/
    }
}

}

The issue that I'm running in to currently is that my input put (F1) isn't completing the circuit. I have it connected to a push button. When I run that push button directly to ground, the circuit is complete and the LED it is connected to will light up when I push the button. When I switch the connection over to this pin, it does nothing. From what I know currently (which would appear to be incorrect), this pin should read high when connected to another pin that is outputting a high signal (in this case D6, which I know is working since I can use that in the pushbutton circuit when it is connected to ground). Instead, it would appear that the value of this pin in the register it is connected to is always '1' (the "do stuff" part of the if else statement is always running).

Any help on what I'm getting wrong would be greatly appreciated!

4

1 回答 1

0

You talk about and configure input on port F, but you read port B from the PINB register.

Presumably, you should be reading PINF.

于 2014-10-28T03:43:33.557 回答