J-M-L:
exactly - this should blink D13 on a UNO (untested)
Well, I was wrong. Not using the "address of" operator caused it to fail with ports above 0xFF (for example, PORTK).
However, I am now confused. The code below works (i.e. blinks the LED) but the print statements don't make sense:
#define BIT7 ((uint8_t)(1 << 7)) // pin 13 LED
void blink (volatile uint8_t *port, uint8_t bit)
{
fprintf (stdout, " PORT: 0x%04X\n", (port-0)); // -0 == PORT
fprintf (stdout, " DDR : 0x%04X\n", (port-1)); // -1 == DDR
fprintf (stdout, " PIN : 0x%04X\n", (port-2)); // -2 == PIN
fprintf (stdout, "\n"); // new line
// fprintf (stdout, "&PORT: 0x%04X\n", &(port-0)); // compile fails
// fprintf (stdout, "&DDR : 0x%04X\n", &(port-1));
// fprintf (stdout, "&PIN : 0x%04X\n", &(port-2));
// fprintf (stdout, "\n"); // new line
fprintf (stdout, "*PORT: 0x%04X\n", *(port-0));
fprintf (stdout, "*DDR : 0x%04X\n", *(port-1));
fprintf (stdout, "*PIN : 0x%04X\n", *(port-2));
fprintf (stdout, "\n"); // new line
*(port-1) |= BIT7; // set DDR
*(port-0) |= bit; // bit 7 high
_delay_ms (250);
*(port-0) &= ~bit; // bit 7 low
_delay_ms (250);
}
int main (void)
{
init();
Serial.begin (115200);
STDIO.open (Serial);
blink (&PORTB, BIT7);
while (1) {
// blink (&PORTB, BIT7);
}
}
The serial output is this:
[b]
PORT: 0x0025
DDR : 0x0024 [color=red][color=green]<-- correct for PORTB/DDRB/PINB[/color][/color]
PIN : 0x0023
*PORT: 0x0000
*DDR : 0x0000 [color=red]<-- HUH????????[/color]
*PIN : 0x0008
[/b]
What the heck am I seeing here????