Reputation: 971
I am not allowed to use the Arduino Library (or any Library) for this program. How would I check the input of a pin?
I found two different functions:
In Arduino.h:
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
Following digitalRead back to pgmspace.h:
#define __LPM_enhanced__(addr) \
(__extension__({ \
uint16_t __addr16 = (uint16_t)(addr); \
uint8_t __result; \
__asm__ __volatile__ \
( \
"lpm %0, Z" "\n\t" \
: "=r" (__result) \
: "z" (__addr16) \
); \
__result; \
}))
For the first one, I don't know where bit and value come from and I just don't understand the second one at all.
Upvotes: 0
Views: 685
Reputation: 518
I will assume that you use Arduino Uno, however, general rule applies to any Arduino.
First, you need to check Arduino pin mapping:
Then, let's assume you want to use digital pin 2, so PD2 on Atmega168/328. (PD2 is short for PORTD pin 2). To use it as an input you need to do:
DDRD &= ~(1 << PD2);
DDRD
is data direction register for port D. Whole operation sets bit corresponding to pin 2 to 0.
Then to read this pin:
if (PIND & (1<<PD2)) {
// do something
}
Also, please check, how to manipulate single bits: How do you set, clear, and toggle a single bit?
Upvotes: 0
Reputation: 34
There is no need to go to these implementations. It pretty simple as follows.
LED13 will turn on when Pin 0 is high. I tested this code on arduino
#include <avr/io.h> // Includes all the definition of register port etc
#ifndef F_CPU
#define F_CPU 16000000UL //Need to include it before <util/delay.h>
#endif //Change 16000000 with your crystal freq. In my case its 16 MHz
#include <util/delay.h> //includes delay functions delay_ms and delay_us
void setup() {
// put your setup code here, to run once:
DDRB |= 0xFF; //Configured Port B as OP
DDRD &= 0x00; //Configured Port D as IP
}
void loop() {
// put your main code here, to run repeatedly:
if (PIND&(0x01)) //to check pin0 of portD (which is Pin 0 of arduino)
PORTB |= 0xFF;
else
PORTB &= 0x00;
}
Upvotes: 1