IoT 2018
Dependencies: mbed
Fork of microbit_blinky by
main.cpp@1:0b38b848a5f0, 2018-09-20 (annotated)
- Committer:
- f3d
- Date:
- Thu Sep 20 09:17:38 2018 +0000
- Revision:
- 1:0b38b848a5f0
- Parent:
- 0:025387782f3e
- Child:
- 2:83f7d8e01b11
Low level button and blink example for BBC Microbit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JonnyA | 0:025387782f3e | 1 | #include "mbed.h" |
f3d | 1:0b38b848a5f0 | 2 | |
f3d | 1:0b38b848a5f0 | 3 | // Button A is on P0 bit 17 |
f3d | 1:0b38b848a5f0 | 4 | // Pin labelled "0" on edge connector is in fact connected to P0_3 |
f3d | 1:0b38b848a5f0 | 5 | volatile uint32_t * P0OUT = (uint32_t *)0x50000504; |
f3d | 1:0b38b848a5f0 | 6 | volatile uint32_t * P0DIR = (uint32_t *)0x50000514; |
f3d | 1:0b38b848a5f0 | 7 | volatile uint32_t * P0IN = (uint32_t *)0x50000510; |
f3d | 1:0b38b848a5f0 | 8 | volatile uint32_t * P0CONF = (uint32_t *)(0x50000700); |
JonnyA | 0:025387782f3e | 9 | |
f3d | 1:0b38b848a5f0 | 10 | void dly(volatile uint32_t len) |
f3d | 1:0b38b848a5f0 | 11 | { |
f3d | 1:0b38b848a5f0 | 12 | // "volatile" modifier is necessary here |
f3d | 1:0b38b848a5f0 | 13 | // to prevent the compiler from optimizing |
f3d | 1:0b38b848a5f0 | 14 | // this software delay to nothing |
f3d | 1:0b38b848a5f0 | 15 | while(len--); |
f3d | 1:0b38b848a5f0 | 16 | } |
f3d | 1:0b38b848a5f0 | 17 | int ButtonAPressed() |
f3d | 1:0b38b848a5f0 | 18 | { |
f3d | 1:0b38b848a5f0 | 19 | // Button A pulls down so bit is zero when pressed. |
f3d | 1:0b38b848a5f0 | 20 | if ((*P0IN & (1 << 17))==0) |
f3d | 1:0b38b848a5f0 | 21 | return 1; |
f3d | 1:0b38b848a5f0 | 22 | else |
f3d | 1:0b38b848a5f0 | 23 | return 0; |
f3d | 1:0b38b848a5f0 | 24 | } |
JonnyA | 0:025387782f3e | 25 | int main() { |
f3d | 1:0b38b848a5f0 | 26 | |
f3d | 1:0b38b848a5f0 | 27 | *P0DIR = (1 << 3); |
f3d | 1:0b38b848a5f0 | 28 | P0CONF[17] = 0; // On power up, input buffer is not connected so must do this |
f3d | 1:0b38b848a5f0 | 29 | while(1) { |
f3d | 1:0b38b848a5f0 | 30 | if (ButtonAPressed()) |
f3d | 1:0b38b848a5f0 | 31 | *P0OUT = (1 << 3); |
f3d | 1:0b38b848a5f0 | 32 | else |
f3d | 1:0b38b848a5f0 | 33 | *P0OUT = 0; |
JonnyA | 0:025387782f3e | 34 | } |
JonnyA | 0:025387782f3e | 35 | } |