48 lines
1.1 KiB
Plaintext
48 lines
1.1 KiB
Plaintext
|
= Arduino Programming Techniques =
|
||
|
|
||
|
This is mostly gonna be C++
|
||
|
|
||
|
== Accessing a GPIO line ==
|
||
|
|
||
|
{{{
|
||
|
//data direction register bit
|
||
|
unsigned char *portDDRB;
|
||
|
|
||
|
portDDRB = (unsigned char *) 0x24;
|
||
|
}}}
|
||
|
|
||
|
First we get an 8bit pointer and load it with a hard coded value of
|
||
|
the data-space address for that register
|
||
|
|
||
|
See [[arduino_nano#Pins]]
|
||
|
|
||
|
== Bit masks ==
|
||
|
|
||
|
{{{
|
||
|
#define BIT6_MASK 0x40 // 0100 0000
|
||
|
#define BIT5_MASK 0x20 // 0010 0000
|
||
|
|
||
|
*register_pointer = (*regiser_pointer) & (~BIT6_MASK);
|
||
|
*register_pointer = (*regiser_pointer) | BIT5_MASK;
|
||
|
|
||
|
}}}
|
||
|
|
||
|
For the first one we find the compilemnt then '&' it with the current
|
||
|
register values, which will only flip the selected bit.
|
||
|
|
||
|
For the second one we '|' it with the contents, setting a 1 in the 5th position
|
||
|
|
||
|
== Polling GPIO pins ==
|
||
|
|
||
|
repeatedly reading the value of the input pins and comparing the current value
|
||
|
to the previously-read value to determine if an external signal event has
|
||
|
occurred.
|
||
|
|
||
|
We can do this by comparing the last known value of the gpio pin with the
|
||
|
current one
|
||
|
|
||
|
See [[arduino_nano#Pins]]
|
||
|
|
||
|
|
||
|
[[embedded]]
|