1

So. I'm trying to read an input on the Arduino Nano using C++/AVR/Eclipse instead of the regular arduino IDE. The code below is working in the arduino IDE

void setup() {
 // put your setup code here, to run once:
 pinMode(13, OUTPUT);
 pinMode(5, INPUT);
}
void loop() {
 // put your main code here, to run repeatedly:
 if(digitalRead(5)){
 digitalWrite(13,1);
 }else{
 digitalWrite(13,0);
 }
}

I'm porting the code to AVR/C++ in Eclipse, regular blinking leds is working... but I can't read inputs for some reason...

#define F_CPU 16000000UL //16MHz
#include <avr/io.h>
#include <util/delay.h>
#define set_bit(Y,bit_x) (Y|=(1<<bit_x))
#define clear_bit(Y,bit_x) (Y&=~(1<<bit_x))
#define isset_bit(Y,bit_x) (Y&(1<<bit_x))
#define toggle_bit(Y,bit_x) (Y^=(1<<bit_x))
int main( ){
 DDRB = 0xFF;//Setting all portB pins to output (D13/PB5 - LED)
 DDRD = 0x00;//Setting all portD pins to input (D5 /PD5 - INPUT)
 while(1){
 if(isset_bit(PORTD,PD5)){//if input... doesn't work?
 set_bit(PORTB,PB5);//Set output (Tested to be working)
 }else{
 clear_bit(PORTB,PB5);//Clear output
 }
 }
}
Ignacio Vazquez-Abrams
17.7k1 gold badge28 silver badges32 bronze badges
asked Apr 19, 2015 at 14:55
1
  • Use the checkmark next to the answer rather than changing the title to indicate that the question has been answered. Commented Apr 19, 2015 at 16:21

1 Answer 1

4

During the creation of this question I found out the answer... Though I wanted to share it with you guys for later reference. It has been quite a time since I have worked with AVR.

You should read pins from PINx

You should set pins in PORTx

Set DDRx to 1 for OUTPUT and to 0 for INPUT

^ This is fun because PIC/Microchip MCU's use 1 for input and 0 for output.

See code below:

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#define set_bit(Y,bit_x) (Y|=(1<<bit_x))
#define clear_bit(Y,bit_x) (Y&=~(1<<bit_x))
#define isset_bit(Y,bit_x) (Y&(1<<bit_x))
#define toggle_bit(Y,bit_x) (Y^=(1<<bit_x))
int main( ){
 DDRB = 0xFF;
 DDRD = 0x00;
 while(1){
 if((PIND&(1<<PD5))){
 set_bit(PORTB,PB5);
 }else{
 clear_bit(PORTB,PB5);
 }
 }
}
answered Apr 19, 2015 at 14:56
2
  • This is interesting, what pins are you reading and writing? And how would you take an analog reading? Commented Sep 10, 2017 at 9:58
  • 1
    @user2497 The microcontrollers use registers that you can read/write to/from. Arduino is an environment and a wrapper around this, to make it easier and more understandable. You should check the datasheet, to find which registers hold what. The registers are 1 byte (I bits), so you'll have to do some bit manipulation/arethmic and write it to the correct registers. Commented Sep 10, 2017 at 14:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.