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
}