LEDを自由に点滅させることができるとデバッグにも便利なので、micro:bitの5x5 LEDを点滅させてみた。5x5の一番左上のLEDは、P0.13とP0.4に接続されているので、それぞれのポートを出力設定にして、L/Hのレベル切り替えにより点滅させることができる。
左が動作結果。
下記のソースはWAITが入っていないので人の目で見て点滅しているようには見えない。。チカチカさせるにはWAIT文が必要。
#define GPIO_BASE 0x50000000 #define GPIO_PIN_CNF_BASE 0x700 #define GPIO_OUTSET 0x508 #define GPIO_OUTCLR 0x50C void gpio_setup(void); void led_blink(void); /********************************************************************* * * main() * * Function description * Application entry point. */ void main(void) { int i; gpio_setup(); for (i = 0; i < 100; i++) { printf("Hello World %d!\n", i); } do { i++; led_blink(); } while (1); } void led_blink(void){ static int state = 0; unsigned int *addr; unsigned int port_no; unsigned int flg; port_no = 13; flg = 1 << port_no; // make flag if(state == 0){ addr = (unsigned int *)(GPIO_BASE + GPIO_OUTSET); *addr = flg; // port_13 to H state = 1; }else{ addr = (unsigned int *)(GPIO_BASE + GPIO_OUTCLR); *addr = flg; // port_13 to H state = 0; } } void gpio_setup(void){ unsigned int * addr; unsigned int port_no; unsigned int flg; port_no = 4; addr = (unsigned int *)(GPIO_BASE + GPIO_PIN_CNF_BASE + port_no * 4); *addr = 0x1; // dir:output Port:4 port_no = 13; addr = (unsigned int *)(GPIO_BASE + GPIO_PIN_CNF_BASE + port_no * 4); *addr = 0x1; // dir:output Port:13 port_no = 4; addr = (unsigned int *)(GPIO_BASE + GPIO_OUTCLR); flg = 1 << port_no; // make flag *addr = flg; // port_4 to L port_no = 13; addr = (unsigned int *)(GPIO_BASE + GPIO_OUTSET); flg = 1 << port_no; // make flag *addr = flg; // port_13 to H }