我开始玩Teensy 2,以及学习C代码。目前我正在试图找出如何将引脚设置作为输入。我的代码如下:
#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*/
}
}
}
我现在遇到的问题是我的输入放(F1)没有完成电路。我把它连接到一个按钮。当我将按钮直接接地时,电路完成,当我按下按钮时,它连接的LED将亮起。当我将连接切换到此引脚时,它什么都不做。根据我目前所知(这看起来不正确),当连接到另一个输出高信号的引脚时,此引脚应该读为高电平(在这种情况下D6,我知道它正在工作,因为我可以在按钮电路中使用它当它接地时)。相反,看起来它所连接的寄存器中该引脚的值始终为'1'(if else语句的“do stuff”部分始终在运行)。
非常感谢任何有关我出错的帮助!
答案 0 :(得分:0)
您在端口F上讨论并配置输入,但是您从PINB
寄存器读取端口B.
据推测,您应该阅读PINF
。