0

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.

5
  • May be you need to post this question in the correct stack exchange for Arduino. api.jquery.com/category/selectors/attribute-selectors Commented Oct 5, 2016 at 6:26
  • Why aren't you allowed to use the arduino library? Commented Oct 5, 2016 at 6:26
  • @HussainPatel you posted a link to the jQuery api lol Commented Oct 5, 2016 at 6:27
  • Because the professor said that we cannot use the Library. If it were a real project and not for school, I would use the library. Commented Oct 5, 2016 at 6:34
  • i am sorry.. wrong windows.. here's the correct linkhttp://arduino.stackexchange.com/ please post your questions related to Arduino here Commented Oct 5, 2016 at 6:44

2 Answers 2

1

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;

}

Sign up to request clarification or add additional context in comments.

6 Comments

So, not using the library at all I would still write a function that accesses PIND correct?
Yes, since you are programming on an arduino, you are actually programming on an ATMega328P, so you'd have to write your code as such. The arduino library helps you to not having to worry about this.
@Foitn Or an ATMega168 - like in Arduino Pro mini boards.
I would normally use a library. I don't believe in re-inventing the wheel. Thanks for your answer.
@AdrianColomitchi True that
|
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.