kingcobra1986 kingcobra1986 - 10 months ago 46
C Question

Get pin input without the Arduino Library

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.

Answer Source

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;
    PORTB &= 0x00;