0

How can I read digital pin 6 in AVR?

I set the pin as input as follows:

DDRD = B0100000; //this will set pin 6 as input

But how can I now digital read this pin?:

Another way of putting it: how can I convert this Arduino code to be used in Atmel Studio?:

digitalRead(DATA_PIN);

Edit:

while (1) {      

        unsigned long data = 0;//32 bit variable to store encoder data

        for (int i=0; i<25; i++) {//25 bit encoder data, first 12 bit is CCW count data and the rest 13 bit is the position angle data
        data <<= 1;//shift all read data left one bit   
        PORTD &= ~(1 << 5);//clock pin goes low    
        _delay_us(2);    
        PORTD |= (1 << 5);//clock pin goes high   
        _delay_us(2);   

        data |= digitalRead(DATA_PIN);//cat the new read bit to the whole read data  
        }

I want to use usual C code for AVR instead of using digitalRead above. data is an 32 bit array storing 25 bit encoder data. It is bit by bit filled by digitalRaed.

I think I found it:

while (1) {      

        unsigned long data = 0;//32 bit variable to store encoder data
        uint8_t val;

        for (int i=0; i<25; i++) {//25 bit encoder data, first 12 bit is CCW count data and the rest 13 bit is the position angle data
        data <<= 1;//shift all read data left one bit   
        PORTD &= ~(1 << 5);//clock pin goes low    
        _delay_us(2);    
        PORTD |= (1 << 5);//clock pin goes high   
        _delay_us(2);   




        if ( (PIND & (1 << PIND6)) == (1 << PIND6) ) {
            val = 1;
            } else {
            val = 0;
        }


        data |= val;//cat the new read bit to the whole read data    
        }  
  • Did you read the tutorial? – Edgar Bonet Jan 6 at 16:10
  • Im a bit in a hurry. But basically my code line in Arduino is: data |= digitalRead(DATA_PIN); ..... Now I need to get the state of DATA_PIN which is pin 6 in Nano. I would like to learn in more detail but I think it will take long time before I need to convert this. – ty_1917 Jan 6 at 16:13
  • @Juraj I cant see in that tutorial how I can read the pin state as one or zero. – ty_1917 Jan 6 at 16:26
  • See the update edit – ty_1917 Jan 6 at 17:24
0
  1. Read the port state with PIND
  2. Isolate the bit you want with &
  3. Profit
if (PIND & 0b0100000) {
    val |= 1;
}

Or, as a ternary:

val |= ((PIND & 0b01000000) ? 1 : 0);
  • Thanks I will try this but what could be the data type for val ? For the sake of speed – ty_1917 Jan 6 at 16:37
  • uint8_t (or unsigned char if you prefer) – Majenko Jan 6 at 16:38
  • Thanks a lot! One last question. This will be in a for loop like val |= ((PORTD & 0b01000000) ? 1 : 0); and then data |= val; . Do you think I should declare val at the very beginning of the code(before the main) as uint8_t val = 0; or volatile uint8_t val = 0;? Does that matter on this case? – ty_1917 Jan 6 at 16:43
  • If all you're doing is data |= val then replace val with data – Majenko Jan 6 at 16:46
  • Not really data is a 32 bit array unsigned long type. I store incoming bits to this data variable as array 25 bits of encoder data. The incoming bit is the pin state val in this case. – ty_1917 Jan 6 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.