I am trying to understand each instruction in the following piece of code:
void setup() {
DDRD |= B00111000; // Sets D3, D4, D5 outputs
DDRB |= B00000100; // Sets D10 as output
PCICR |= (1 << PCIE0); //enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); //Set pin D8 (trigger pin) set to fire interrupt on state change.
PCMSK0 |= (1 << PCINT1); //Set pin D9 (echo in) set to fire an interrupt on state change.
}
Could somebody suggest reference(s) where I can learn the meaning of the syntax for each line in above code as well as what the code is precisely doing?? Thank you. AA
I can’t look it up right now, but I think the Arduino uno sales page has a link to the processor data sheet that explains each of those constants in great, authoritative detail.
Syntax-wise, you're looking at standard bit manipulation. While this is part of "ordinary C", it's something that doesn't show up much except in the embedded world. var |= val; is the same as var = var | val; - it does a bitwise "OR" of the values and puts the result in var. This "sets the bits" that were ones in val, in the destination variable. More detail can be found at [TUT] [C] Bit manipulation (at avrfreaks)
as well as what the code is precisely doing??
In this case, the destination variables are "special function registers" of the AVR, controlling the internal operation of the chip. DDRx is the register that controls the pin direction, and the PCxxx registers control the configuration of he "Pin Change Interrupt" function. Details can be found the in the ATmega328p datasheet. (and probably other tutorials as well, but the datasheet is the definitive reference. All 600+ pages...)
where pinMode(...) does the work of translating the hardware pin numbers into the low-level registers (DDRD and DDRB) and the appropriate bits within the registers as defined in the processor's datasheet.