我目前正在从事一个涉及LCD,传感器和键盘的项目。
Arduino Uno有14个输入/输出引脚(0-13,0为Rx,1为Tx)。
LCD本身占用5个引脚(引脚8-12),传感器1(引脚7)和键盘7(引脚0-6),这很好,因为我有14个电位引脚。
因此,我下载了一个基本程序来快速测试键盘并调整必要的变量。我想通过串行通信在计算机上显示数字。第1行和第4行有效,第2行和第3行无效。小键盘不会出错,因为我测量了每种组合的电阻。然后我意识到保存有关第2行和第3行信息的引脚已连接到Arduino Uno的引脚0和1(Rx&Tx)。
串行通信处于活动状态时,可以将引脚0和1用作输入吗?
如果是,则必须是以下代码:
/* SparkFun Keypad Pinout:
Rows and columns are connected as such:
-------------
R1 | 1 | 2 | 3 | - 5 (grün)
R2 | 4 | 5 | 6 | - 0 (braun)
R3 | 7 | 8 | 9 | - 1 (schwarz)
R4 | * | 0 | # | - 3 (violett)
-------------
|C1 |C2 |C3
4(blau) 6(gelb) 2(weiß)
*/
// Pins 1-7 of the keypad connected to the Arduino respectively:
int keypadPins[7] = {0, 1, 2, 3, 4, 5, 6};
int keypadStatus; // Used to monitor which buttons are pressed.
int timeout; // timeout variable used in loop
void setup()
{
Serial.begin(9600);
for (int i=0; i<7; i++)
{
pinMode(keypadPins[i], INPUT); // Set all keypad pins as inputs
digitalWrite(keypadPins[i], HIGH); // pull all keypad pins high
}
}
void loop()
{
keypadStatus = getKeypadStatus(); // read which buttons are pressed
if (keypadStatus != 0) // If a button is pressed go into here
{
sendKeyPress(keypadStatus); // send the button over USB
timeout = 2000; // top of the repeat delay
while ((getKeypadStatus() == keypadStatus) && (--timeout)) // Decrement timeout and check if key is being held down
delayMicroseconds(1);
while (getKeypadStatus() == keypadStatus) // while the same button is held down
{
sendKeyPress(keypadStatus); // continue to send the button over USB
delay(50); // 50ms repeat rate
}
}
}
/* sendKeyPress(int key): This function sends a single key over USB
It requires an int, of which the 12 LSbs are used. Each bit in
key represents a single button on the keypad.
This function will only send a key press if a single button
is being pressed */
void sendKeyPress(int key)
{
switch(key)
{
case 1: // 0x001
Serial.print('1'); // Sends a keyboard '1'
break;
case 2: // 0x002
Serial.print('2');
break;
case 4: // 0x004
Serial.print('3');
break;
case 8: // 0x008
Serial.print('4');
break;
case 16: // 0x010
Serial.print('5');
break;
case 32: // 0x020
Serial.print('6');
break;
case 64: // 0x040
Serial.print('7');
break;
case 128: // 0x080
Serial.print('8');
break;
case 256: // 0x100
Serial.print('9');
break;
case 512: // 0x200
Serial.print('*');
break;
case 1024: // 0x400
Serial.print('0'); // Sends a keyboard '0'
break;
case 2048: // 0x800
Serial.print('\n'); // Sends the 'ENTER' key
break;
}
}
/* getKeypadStatus(): This function returns an int that represents
the status of the 12-button keypad. Only the 12 LSb's of the return
value hold any significange. Each bit represents the status of a single
key on the button pad. '1' is bit 0, '2' is bit 1, '3' is bit 2, ...,
'#' is bit 11.
This function doesn't work for multitouch.
*/
int getKeypadStatus()
{
int rowPins[4] = {keypadPins[5], keypadPins[0], keypadPins[1], keypadPins[3]}; // row pins are 5, 0, 1, and 3 of the keypad
int columnPins[3] = {keypadPins[4], keypadPins[6], keypadPins[2]}; // column pins are pins 2, 4, and 6 of the keypad
int keypadStatus = 0; // this will be what's returned
/* initialize all pins, inputs w/ pull-ups */
for (int i=0; i<7; i++)
{
pinMode(keypadPins[i], INPUT);
digitalWrite(keypadPins[i], HIGH);
}
for (int row=0; row<4; row++)
{ // initial for loop to check all 4 rows
pinMode(rowPins[row], OUTPUT); // set the row pin as an output
digitalWrite(rowPins[row], LOW); // pull the row pins low
for (int col=0; col<3; col++)
{ // embedded for loop to check all 3 columns of each row
if (!digitalRead(columnPins[col]))
{
keypadStatus |= 1 << ((row+1)*3 + (col+1) - 4); // set the status bit of the keypad return value
}
}
pinMode(rowPins[row], INPUT); // reset the row pin as an input
digitalWrite(rowPins[row], HIGH); // pull the row pin high
}
return keypadStatus;
}
进行私人交流时,可以使用引脚0和1作为输入吗?
我从未使用过此部分,但仍然只花了几分钟我就通过RTFM ATmega328P p.71找到了相关文本。
•TXD / PCINT17 –端口D,位1 TXD,发送数据(USART的数据输出引脚)。使能USART发送器后,无论DDD1的值如何,该引脚都将配置为输出。
•RXD / PCINT16 –端口D,位0 RXD,接收数据(USART的数据输入引脚)。使能USART接收器后,无论DDD0的值如何,该引脚均被配置为输入。当USART强制该引脚作为输入时,上拉仍然可以由PORTD0位控制。
因此,我建议您熟悉友好的MCU手册,而不是Arduino this和Arduino。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句