gpt4 book ai didi

c - 读取引脚上的输入(Teensy)

转载 作者:行者123 更新时间:2023-11-30 17:26:53 25 4
gpt4 key购买 nike

我开始摆弄 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”部分始终运行)。

任何有关我所犯错误的帮助将不胜感激!

最佳答案

您讨论并配置端口 F 上的输入,但您从 PINB 寄存器读取端口 B。

想必,您应该阅读PINF

关于c - 读取引脚上的输入(Teensy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com