Micro:bit IoT In C Second Edition
We have decided not to make the programs available as a download because this is not the point of the book - the programs are not finished production code but something you should type in and study.
The best solution is to provide the source code of the programs in a form that can be copied and pasted into a NetBeans of VS Code project.
The only downside is that you have to create a project to paste the code into.
To do this follow the instructions in the book.
All of the programs below were copy and pasted from working programs in the IDE. They have been formatted using the built in formatter and hence are not identical in layout to the programs in the book. This is to make copy and pasting them easier. The programs in the book are formatted to be easy to read on the page.
If anything you consider important is missing or if you have any requests or comments contact:
This email address is being protected from spambots. You need JavaScript enabled to view it.
Page 29
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE);
for (;;)
{
uBit.display.image.setPixelValue(2, 2, 255);
uBit.sleep(1000);
uBit.display.image.setPixelValue(2, 2, 0);
uBit.sleep(1000);
}
}
Page 38
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uBit.io.P0.setDigitalValue(1);
}
#include "MicroBit.h"
int main()
{
MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0,
PIN_CAPABILITY_ALL);
P0.setDigitalValue(1);
}
Page 44
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
while (1)
{
uBit.io.P0.setDigitalValue(1);
uBit.io.P0.setDigitalValue(0);
}
}
Page 45
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
while (1)
{
uBit.io.P0.setDigitalValue(1);
uBit.sleep(9);
uBit.io.P0.setDigitalValue(0);
uBit.sleep(9);
}
}
Page 46
MicroBit uBit;
int main()
{
uBit.init();
while (1)
{
uBit.io.P0.setDigitalValue(1);
wait_us(20);
uBit.io.P0.setDigitalValue(0);
wait_us(20);
}
}
Page 46
#include "MicroBit.h"
MicroBit uBit;
int main()
{
int i;
int n = 57;
while (1)
{
for (i = 0; i < n; i++)
{
};
uBit.io.P0.setDigitalValue(1);
for (i = 0; i < n; i++)
{
};
uBit.io.P0.setDigitalValue(0);
}
}
Page 47
#include "MicroBit.h"
MicroBit uBit;
int main()
{
int i;
uBit.init();
while (1)
{
uBit.io.P0.setDigitalValue(1);
uBit.io.P1.setDigitalValue(0);
uBit.io.P0.setDigitalValue(0);
uBit.io.P1.setDigitalValue(1);
}
}
Page 49
#include "MicroBit.h"
MicroBit uBit;
int main()
{
int i;
uBit.init();
while (1)
{
for (i = 0; i < 100; i++)
{
};
uBit.io.P0.setDigitalValue(1);
uBit.io.P1.setDigitalValue(0);
for (i = 0; i < 100; i++)
{
};
uBit.io.P0.setDigitalValue(0);
uBit.io.P1.setDigitalValue(1);
}
}
Page 52
#include "MicroBit.h"
MicroBit uBit;
int main()
{
int i;
uBit.init();
while (1)
{
while (1 == uBit.io.P0.getDigitalValue())
;
while (0 == uBit.io.P0.getDigitalValue())
;
for (i = 0; i < 1000; i++)
{
if (0 == uBit.io.P0.getDigitalValue())
break;
}
uBit.serial.printf("%d\n\r", i);
}
}
Page 54
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uint64_t t;
volatile int i;
while (1)
{
while (1 == uBit.io.P0.getDigitalValue())
;
while (0 == uBit.io.P0.getDigitalValue())
;
t = system_timer_current_time_us();
for (i = 0; i < 1000; i++)
{
if (0 == uBit.io.P0.getDigitalValue())
break;
}
t = system_timer_current_time_us() - t;
uBit.serial.printf("%d,%d\n\r", i, (int)t);
}
}
Page 56
#include "MicroBit.h"
MicroBit uBit;
uint64_t t[20];
uint64_t temp = 0;
int i = 0;
void onPulseEdge(MicroBitEvent evt)
{
t[i] = (evt.timestamp - temp);
i++;
temp = evt.timestamp;
if (i < 20)
return;
uBit.io.P0.eventOn(MICROBIT_PIN_EVENT_NONE);
}
int main()
{
uBit.init();
uBit.messageBus.listen(MICROBIT_ID_IO_P0,
MICROBIT_PIN_EVT_RISE,
onPulseEdge,
MESSAGE_BUS_LISTENER_IMMEDIATE);
uBit.messageBus.listen(MICROBIT_ID_IO_P0,
MICROBIT_PIN_EVT_FALL,
onPulseEdge,
MESSAGE_BUS_LISTENER_IMMEDIATE);
uBit.io.P0.eventOn(MICROBIT_PIN_EVENT_ON_EDGE);
}
uBit.sleep(1000);
for (i = 1; i < 20; i++)
{
printf("%d\n\r", (int)t[i]);
}
Page 63
int main()
{
int GPIO = 3;
// GPIO=2; //Uncomment for V2
volatile unsigned int *dirset = (unsigned int *)(0x50000000UL + 0x518);
volatile unsigned int *outset = (unsigned int *)(0x50000000UL + 0x508);
volatile unsigned int *outclr = (unsigned int *)(0x50000000UL + 0x50C);
unsigned int mask = 1 << GPIO;
*dirset = mask;
for (;;)
{
*outset = mask;
*outclr = mask;
}
}
Page 64
int main()
{
int GPIO = 3;
//GPIO = 2; // Uncomment for V2
volatile unsigned int *dirset = (unsigned int *)(0x50000000UL + 0x518);
volatile unsigned int *outset = (unsigned int *)(0x50000000UL + 0x508);
volatile unsigned int *outclr = (unsigned int *)(0x50000000UL + 0x50C);
unsigned int mask = 1 << GPIO;
*dirset = mask;
volatile int i;
int n = 200;
for (;;)
{
for (i = 0; i < n; i++)
{
};
*outset = mask;
for (i = 0; i < n; i++)
{
};
*outclr = mask;
}
}
Page 65
int main()
{
volatile unsigned int *dirset = (unsigned int *)(0x50000000UL + 0x518);
volatile unsigned int *outset = (unsigned int *)(0x50000000UL + 0x508);
volatile unsigned int *outclr = (unsigned int *)(0x50000000UL + 0x50C);
volatile int i;
int n = 200;
unsigned int mask = 1 << 2 | 1 << 3;
*dirset = mask;
for (;;)
{
*outset = mask;
for (i = 0; i < n; i++)
{
};
*outclr = mask;
for (i = 0; i < n; i++)
{
};
}
}
Page 66
int main()
{
volatile unsigned int *dirset = (unsigned int *)(0x50000000UL + 0x518);
volatile unsigned int *outset = (unsigned int *)(0x50000000UL + 0x508);
volatile unsigned int *outclr = (unsigned int *)(0x50000000UL + 0x50C);
volatile int i;
int n = 200;
unsigned int mask = 1 << 3 | 1 << 2;
unsigned int mask1 = 1 << 3;
unsigned int mask2 = 1 << 2;
*dirset = mask;
for (;;)
{
*outset = mask1;
*outclr = mask2;
for (i = 0; i < n; i++)
{
};
*outclr = mask1;
*outset = mask2;
for (i = 0; i < n; i++)
{
};
}
}
Page 67
int main()
{
volatile unsigned int *dirset = (unsigned int *)(0x50000000UL + 0x518);
volatile unsigned int *out = (unsigned int *)(0x50000000UL + 0x504);
volatile int i;
int n = 200;
unsigned int mask1 = 1 << 2;
unsigned int mask2 = 1 << 3;
*dirset = (1 << 2) | (1 << 3);
for (;;)
{
*out = mask1;
for (i = 0; i < n; i++)
{
};
*out = mask2;
for (i = 0; i < n; i++)
{
};
}
}
Page 68
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
int GPIO = 3;
GPIO = 2; // Uncomment for V2
volatile unsigned int *in = (unsigned int *)(0x50000000UL + 0x510);
volatile unsigned int *pinDir =
(unsigned int *)(0x50000000UL + 0x700 + GPIO * 4);
unsigned int mask = 1 << GPIO;
*pinDir = 0x0;
volatile int i;
uBit.serial.printf("Ready\n\r");
while (1)
{
while (mask == ((*in) & mask))
{
};
while (mask != ((*in) & mask))
{
};
for (i = 0; i < 5000; i++)
{
if (mask != ((*in) & mask))
break;
}
uBit.serial.printf("%d\n\r", i);
}
return 0;
}
Page 74
#include "MicroBit.h"
MicroBit uBit;
int main() {
uBit.init();
uBit.io.P0.setAnalogValue(511);
uBit.io.P0.setAnalogPeriod(1);
release_fiber();
return 0;
}
Page 75
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uBit.io.P0.setAnalogValue(200);
uBit.io.P1.setAnalogValue(511);
uBit.io.P2.setAnalogValue(800);
uBit.io.P0.setAnalogPeriod(1);
uBit.io.P1.setAnalogPeriod(1);
uBit.io.P2.setAnalogPeriod(1);
release_fiber();
return 0;
}
Page 76
#include "MicroBit.h"
MicroBit uBit;
int main()
{
volatile int i;
uBit.init();
uBit.io.P0.setAnalogValue(200);
uBit.io.P0.setAnalogPeriodUs(50);
for (;;)
{
for (i = 1; i < 100; i++)
{
};
uBit.io.P0.setAnalogValue(200);
for (i = 1; i < 100; i++)
{
};
uBit.io.P0.setAnalogValue(800);
}
}
Page 78
#include "MicroBit.h"
//#define wait_us system_timer_wait_us // uncomment for V2
MicroBit uBit;
int main()
{
int i;
uBit.init();
uBit.io.P0.setAnalogValue(200);
uBit.io.P0.setAnalogPeriodUs(50);
for (;;)
{
for (i = 0; i < 1024; i = i + 10)
{
uBit.io.P0.setAnalogValue(i);
wait_us(100);
}
}
release_fiber();
return 0;
}
Page 82
#include "MicroBit.h"
//#define wait_us system_timer_wait_us // uncomment for V2
MicroBit uBit;
int main()
{
uBit.init();
uBit.io.P0.setAnalogValue(511);
uBit.io.P0.setAnalogPeriodUs(1000);
int w = 1;
int inc = 1;
for (;;)
{
uBit.io.P0.setAnalogValue(w);
w = w + inc;
if (w > 1024 || w <= 0)
inc = -inc;
wait_us(5000);
}
release_fiber();
return 0;
}
Page 83
#include "MicroBit.h"
//#define wait_us system_timer_wait_us // uncomment for V2
MicroBit uBit;
int main()
{
uBit.init();
uBit.io.P0.setAnalogValue(511);
uBit.io.P0.setAnalogPeriodUs(1000);
int w = 0;
int inc = 1;
for (;;)
{
uBit.io.P0.setAnalogValue(w * w * w);
w = w + inc;
if (w > 10 || w <= 0)
inc = -inc;
wait_us(50000);
}
release_fiber();
return 0;
}
Page 86
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
for (;;)
{
uBit.io.P0.setServoValue(0);
uBit.sleep(1000);
uBit.io.P0.setServoValue(90);
uBit.sleep(1000);
uBit.io.P0.setServoValue(180);
uBit.sleep(1000);
}
release_fiber();
return 0;
}
Page 87
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uBit.io.P0.setAnalogValue(0);
uBit.io.P0.setAnalogPeriod(20);
for (;;)
{
uBit.io.P0.setAnalogValue(52 * 0 / 180 + 920);
uBit.sleep(1000);
uBit.io.P0.setAnalogValue(52 * 90 / 180 + 920);
uBit.sleep(1000);
uBit.io.P0.setAnalogValue(52 * 180 / 180 + 920);
uBit.sleep(1000);
}
release_fiber();
return 0;
}
Page 90
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
for (;;)
{
uBit.io.P0.setServoValue(0, 1000);
uBit.sleep(1000);
uBit.io.P0.setServoValue(90, 1000);
uBit.sleep(1000);
uBit.io.P0.setServoValue(180, 1000);
uBit.sleep(1000);
}
release_fiber();
return 0;
}
Page 102
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
char buf[] = {0x07};
uBit.i2c.write(0x1C, buf, 1, true);
uBit.i2c.read(0x1C, buf, 1);
printf("Id %X\r\n", (int)buf[0]);
release_fiber();
return 0;
}
Page 104
#include "MicroBit.h"
#define LSM303_ADDR 0x32
#define LSM303_WHO 0x0F
#define LSM303_VAL 0x33
#define FXOS8700_ADDR 0x3C
#define FXOS8700_WHO 0x0D
#define FXOS8700_VAL 0xC7
MicroBit uBit;
int main()
{
uBit.init();
uint8_t data[1];
int status = 0;
status = uBit._i2c.readRegister(LSM303_ADDR, LSM303_WHO, data, 1);
if (data[0] == LSM303_VAL)
uBit.serial.printf("LSM303 \n\r");
status = uBit._i2c.readRegister(FXOS8700_ADDR, FXOS8700_WHO, data, 1);
if (data[0] == FXOS8700_VAL)
uBit.serial.printf("FXOS8700 \n\r");
return 0;
}
Page 105
#include "MicroBit.h"
#define LSM303_ADDR 0x32
#define LSM303_WHO 0x0F
#define LSM303_VAL 0x33
#define FXOS8700_ADDR 0x3C
#define FXOS8700_WHO 0x0D
#define FXOS8700_VAL 0xC7
MicroBit uBit;
int main()
{
uBit.init();
uint8_t data[2];
int status = 0;
uBit.compass.requestUpdate();
int x = uBit.compass.getX();
uBit.serial.printf("X %d \n\r", x);
int y = uBit.compass.getY();
uBit.serial.printf("Y %d \n\r", y);
int z = uBit.compass.getZ();
uBit.serial.printf("Z %d \n\r", z);
status = uBit._i2c.readRegister(0x3C, 0x68, &data[0], 1);
status = uBit._i2c.readRegister(0x3C, 0x69, &data[1], 1);
int16_t X = data[1] << 8 | data[0];
uBit.serial.printf("X %d \n\r", X);
status = uBit._i2c.readRegister(0x3C, 0x6A, &data[0], 1);
status = uBit._i2c.readRegister(0x3C, 0x6B, &data[1], 1);
int16_t Y = data[1] << 8 | data[0];
uBit.serial.printf("Y %d \n\r", Y);
status = uBit._i2c.readRegister(0x3C, 0x6C, &data[0], 1);
status = uBit._i2c.readRegister(0x3C, 0x6D, &data[1], 1);
int16_t Z = data[1] << 8 | data[0];
uBit.serial.printf("Z %d \n\r", Z);
return 0;
}
Page 111
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uint8_t buf[] = {0xE7};
uBit.i2c.write(0x80, buf, 1);
uBit.i2c.read(0x80, buf, 1);
uBit.serial.printf("User Register = %X \r\n", buf[0]);
return 0;
}
Page 112
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uint8_t buf[] = {0};
uBit.i2c.readRegister(0x80, 0xE7, buf, 1);
uBit.serial.printf("User Register = %X \r\n", buf[0]);
return 0;
}
Page 114
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uint8_t buf[3] = {0};
uBit.i2c.readRegister(0x80, 0xE3, buf, 3);
uint8_t msb = buf[0];
uint8_t lsb = buf[1];
uint8_t check = buf[2];
uBit.serial.printf("msb %d \n lsb %d \n checksum %d \n\r",
msb, lsb, check);
return 0;
}
Page 117
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uint8_t buf[3] = {0};
uBit.i2c.readRegister(0x80, 0xE3, buf, 3);
uint8_t msb = buf[0];
uint8_t lsb = buf[1];
uint8_t check = buf[2];
unsigned int data16 = ((unsigned int)msb << 8) |
(unsigned int)(lsb & 0xFC);
int temp = -4685 + ((17572 * data16) >> 16);
uBit.serial.printf("Temperature %d.%d C \n\r",
temp / 100, temp % 100);
return 0;
}
Page 119
uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check)
{
uint32_t data32 = ((uint32_t)msb << 16) |
((uint32_t)lsb << 8) | (uint32_t)check;
uint32_t divisor = 0x988000;
for (int i = 0; i < 16; i++)
{
if (data32 & (uint32_t)1 << (23 - i))
data32 ^= divisor;
divisor >>= 1;
};
return (uint8_t)data32;
}
Page 121
#include "MicroBit.h"
MicroBit uBit;
uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check)
{
uint32_t data32 = ((uint32_t)msb << 16) | ((uint32_t)lsb << 8) |
(uint32_t)check;
uint32_t divisor = 0x988000;
for (int i = 0; i < 16; i++)
{
if (data32 & (uint32_t)1 << (23 - i))
data32 ^= divisor;
divisor >>= 1;
};
return (uint8_t)data32;
}
int main()
{
uBit.init();
uint8_t buf[3] = {0};
uBit.i2c.readRegister(0x80, 0xE3, buf, 3);
uint8_t msb = buf[0];
uint8_t lsb = buf[1];
uint8_t check = buf[2];
unsigned int data16 = ((unsigned int)msb << 8) |
(unsigned int)(lsb & 0xFC);
int temp = -4685 + ((17572 * data16) >> 16);
uBit.serial.printf("\n\rmsb %d \n\rlsb %d \n\rchecksum %d \n\r",
msb, lsb, check);
uBit.serial.printf("Temperature %d.%d C \n\r",
temp / 100, temp % 100);
uBit.serial.printf("crc %d \n ", crcCheck(msb, lsb, check));
uBit.i2c.readRegister(0x80, 0xE5, buf, 3);
msb = buf[0];
lsb = buf[1];
check = buf[2];
uBit.serial.printf("\n\rmsb %d \n\rlsb %d \n\rchecksum %d \n\r",
msb, lsb, check);
data16 = ((unsigned int)msb << 8) | (unsigned int)(lsb & 0xFC);
int hum = -600 + ((12500 * data16) >> 16);
uBit.serial.printf("Humidity %d.%d %% \n\r",
hum / 100, hum % 100);
uBit.serial.printf("crc %d \n ", crcCheck(msb, lsb, check));
return 0;
}
Page 128
#include "MicroBit.h"
MicroBit uBit;
MicroBitPin P0 = uBit.io.P0;
int main()
{
int n = 1200;
//n = 4400; // Uncomment for V2
volatile int i;
uBit.init();
P0.setDigitalValue(1);
P0.setDigitalValue(0);
for (i = 0; i < n; i++)
{
};
P0.setDigitalValue(1);
int b = P0.getDigitalValue();
}
Page 134
#include "MicroBit.h"
#include <cstdio>
MicroBit uBit;
MicroBitPin P0 = uBit.io.P0;
uint32_t getByte(int b, int buf[])
{
int i;
uint32_t result = 0;
b = (b - 1) * 8 + 1;
for (i = b; i <= b + 7; i++)
{
result = result << 1;
result = result | buf[i];
}
return result;
}
int main()
{
int n = 800;
int threshold = 11;
threshold = 40; // uncomment for V2
volatile int i;
uBit.init();
P0.setDigitalValue(1);
for (;;)
{
P0.setDigitalValue(0);
uBit.sleep(1);
P0.getDigitalValue();
for (i = 1; i < n; i++)
{
if (P0.getDigitalValue() == 0)
break;
};
int buf[41];
int j;
__disable_irq();
for (j = 0; j < 41; j++)
{
for (i = 1; i < n; i++)
{
if (P0.getDigitalValue() == 1)
break;
};
for (i = 1; i < n; i++)
{
if (P0.getDigitalValue() == 0)
break;
}
buf[j] = 0;
if (i > threshold)
buf[j] = 1;
}
__enable_irq();
uint32_t byte1 = getByte(1, buf);
uint32_t byte2 = getByte(2, buf);
uint32_t byte3 = getByte(3, buf);
uint32_t byte4 = getByte(4, buf);
uint32_t byte5 = getByte(5, buf);
int16_t humidity = (byte1 << 8) | byte2;
int16_t temperature = (byte3 << 8) | byte4;
if (temperature & 0x8000)
temperature = -(temperature & 0x7FFF);
uBit.serial.printf("Checksum %X %X \n\r", byte5,
(byte1 + byte2 + byte3 + byte4) & 0xFF);
uBit.serial.printf("Humidity %d.%d%%\n\r",
humidity / 10, abs(humidity % 10));
uBit.serial.printf("Temperature %d.%dC\n\r",
temperature / 10, abs(temperature % 10));
char buff[10];
sprintf(buff, "%d.%d%%", humidity / 10, abs(humidity % 10));
uBit.display.scroll(buff, 200);
sprintf(buff, "%hd.%dC", temperature / 10,
abs(temperature % 10));
uBit.display.scroll(buff, 200);
uBit.sleep(2500);
}
}
Page 150
#include "MicroBit.h"
#include <cstdio>
MicroBit uBit;
MicroBitPin P0 = uBit.io.P0;
MicroBitPin P1 = uBit.io.P1;
int init()
{
int reset = 700;
//reset = 2250; // uncomment V2
int gap = 60;
//gap = 200; // uncomment V2
volatile int i;
P0.setDigitalValue(1);
P0.setPull(Pull - up);
// P0.setPull(PullMode::Up); //V2
P1.getDigitalValue();
P0.setDigitalValue(0);
__disable_irq();
for (i = 0; i < reset; i++)
{
};
P0.setDigitalValue(1);
for (i = 0; i < gap; i++)
{
};
int b = P1.getDigitalValue();
for (i = 0; i < reset - gap; i++)
{
};
__enable_irq();
return b;
}
void sendZero()
{
int zero = 75;
//zero = 240; // uncomment for V2
int slot = 30;
//slot = 300;// uncomment for V2
volatile int i;
P0.setDigitalValue(0);
for (i = 1; i < zero; i++)
{
};
P0.setDigitalValue(1);
for (i = 1; i < slot - zero; i++)
{
};
}
void sendOne()
{
int one = 1;
//one = 8; // uncomment for V2
int slot = 30;
//slot = 300; // uncomment for V2
volatile int i;
P0.setDigitalValue(0);
for (i = 1; i < one; i++)
{
};
P0.setDigitalValue(1);
for (i = 1; i < slot - one; i++)
{
};
}
int readBit()
{
volatile int i;
int sample = 20;
//sample = 55; // uncomment for V2
int pulse = 1;
//pulse = 8; // uncomment for V2
int slot = 30;
//slot = 300; // uncomment for V2
__disable_irq();
P0.setDigitalValue(0);
for (i = 1; i < pulse; i++)
{
};
P0.setDigitalValue(1);
for (i = 1; i < sample; i++)
{
};
int b = P1.getDigitalValue();
__enable_irq();
for (i = 1; i < slot - sample - pulse; i++)
{
};
return b;
}
void writeBit(int b)
{
int zero = 75;
//zero = 240; // uncomment for V2
int one = 1;
//one = 8; // uncomment for V2
int slot = 30;
//slot = 300; // uncomment for V2
volatile int i;
int delay1, delay2;
if (b == 1)
{
delay1 = one;
}
else
{
delay1 = zero;
}
delay2 = slot - delay1;
__disable_irq();
P0.setDigitalValue(0);
for (i = 1; i < delay1; i++)
{
};
P0.setDigitalValue(1);
for (i = 1; i < delay2; i++)
{
};
__enable_irq();
}
void writeByte(int byte)
{
int i;
for (i = 0; i < 8; i++)
{
if (byte & 1)
{
writeBit(1);
}
else
{
writeBit(0);
}
byte = byte >> 1;
}
}
int readByte()
{
int byte = 0;
int i;
for (i = 0; i < 8; i++)
{
byte = byte | readBit() << i;
};
return byte;
}
void convert()
{
writeByte(0x44);
uBit.sleep(900);
}
int main()
{
uBit.init();
if (init() == 1)
{
uBit.serial.printf("No device \n\r");
}
else
{
uBit.serial.printf("Device detected \n\r");
}
while (1)
{
init();
writeByte(0xCC);
convert();
init();
writeByte(0xCC);
writeByte(0xBE);
int b1 = readByte();
int b2 = readByte();
uBit.serial.printf("bytes %X,%X\n\r", b1, b2);
int16_t temp = (b2 << 8 | b1);
temp = temp * 100 / 16;
uBit.serial.printf("Temperature %d.%d\n\r",
temp / 100, abs(temp % 100));
char buff[10];
sprintf(buff, "%d.%d", temp / 100, abs(temp % 100));
uBit.display.scroll(buff, 200);
uBit.sleep(2000);
}
}
Page 154
int readBit()
{
volatile int i;
int sample = 20;
sample = 55; // uncomment for V2
int pulse = 1;
pulse = 8; // uncomment for V2
int slot = 30;
slot = 300; // uncomment for V2
__disable_irq();
P0.setDigitalValue(0);
for (i = 1; i < pulse; i++)
{
};
P0.setDigitalValue(1);
for (i = 1; i < sample; i++)
{
};
*pinDir = 0x0;
unsigned int b = (*inp) & mask;
b = b >> GPIO;
*pinDir = 0x1;
__enable_irq();
for (i = 1; i < slot - sample - pulse; i++)
{
};
return b;
}
Page 162
int SPItransfer(uint8_t *txBuffer, uint32_t txSize,
uint8_t *rxBuffer, uint32_t rxSize)
{
CS.setDigitalValue(0);
uint32_t i;
for (i = 0; i < txSize; i++)
{
if (i < rxSize)
{
rxBuffer[i] = spi.write(txBuffer[i]);
}
else
{
spi.write(txBuffer[i]);
}
}
CS.setDigitalValue(1);
return 0;
}
Page 164
#include "MicroBit.h"
MicroBit uBit;
SPI spi(MOSI, MISO, SCK);
MicroBitPin CS(MICROBIT_ID_IO_P16, MICROBIT_PIN_P16,
PIN_CAPABILITY_DIGITAL);
//Used by V1 only
int SPItransfer(uint8_t *txBuffer, uint32_t txSize, uint8_t *rxBuffer, uint32_t rxSize)
{
CS.setDigitalValue(0);
uint32_t i;
for (i = 0; i < txSize; i++)
{
if (i < rxSize)
{
rxBuffer[i] = spi.write(txBuffer[i]);
}
else
{
spi.write(txBuffer[i]);
}
}
CS.setDigitalValue(1);
return 0;
}
int main()
{
CS.setDigitalValue(1);
spi.format(8, 0);
spi.frequency(1000000);
uint8_t buf[1] = {0xAA};
while (1)
{
CS.setDigitalValue(0);
SPItransfer(buf, 1, buf, 1); //comment out for V2
// spi.transfer(buf, 1, buf, 1); //uncomment for V1
CS.setDigitalValue(1);
uBit.serial.printf("data %X\n\r", buf[0]);
if (buf[0] == 0xAA)
uBit.serial.printf("data received correctly\n\r");
};
release_fiber();
return 0;
}
Page 174
#include "MicroBit.h"
MicroBit uBit;
SPI spi(MOSI, MISO, SCK);
MicroBitPin CS(MICROBIT_ID_IO_P16, MICROBIT_PIN_P16,
PIN_CAPABILITY_DIGITAL);
//SPItransfer function is only needed for V1
int SPItransfer(uint8_t *txBuffer, uint32_t txSize,
uint8_t *rxBuffer, uint32_t rxSize)
{
CS.setDigitalValue(0);
uint32_t i;
for (i = 0; i < txSize; i++)
{
if (i < rxSize)
{
rxBuffer[i] = spi.write(txBuffer[i]);
}
else
{
spi.write(txBuffer[i]);
}
}
CS.setDigitalValue(1);
return 0;
}
int main()
{
CS.setDigitalValue(1);
spi.format(8, 0);
spi.frequency(1000000);
uint8_t buf[] = {0x01, 0x80, 0x00};
uint8_t readBuf[3];
CS.setDigitalValue(0);
SPItransfer(buf, 3, readBuf, 3); // comment out for V2
//spi.transfer(buf, 3, readBuf, 3); //uncomment for V2
//uBit.sleep(1);//uncomment for V2
CS.setDigitalValue(1);
int data = ((int)readBuf[1] & 0x03) << 8 | (int)readBuf[2];
int volts = data * 330 / 1023;
uBit.serial.printf("data %d\n\r", volts);
release_fiber();
return 0;
}
Page 175
int readADC(uint8_t chan)
{
uint8_t buf[] = {0x01, (0x08 | chan) << 4, 0x00};
uint8_t readBuf[3];
CS.setDigitalValue(0);
SPItransfer(buf, 3, readBuf, 3); //Comment out for V2
//spi.transfer(buf, 3, readBuf, 3); //uncomment for V2
//uBit.sleep(1);//uncomment for V2
CS.setDigitalValue(1);
return (readBuf[1] & 0x03) << 8 | readBuf[2];
}
Page 177
volatile unsigned int *outset = (unsigned int *)(0x50000000UL + 0x508);
volatile unsigned int *outclr = (unsigned int *)(0x50000000UL + 0x50C);
unsigned int mask = 1 << 16;
void transferBytes(int *buf, int *readBuf, int len)
{
*outclr = mask;
readBuf[0] = spi.write(buf[0]);
readBuf[1] = spi.write(buf[1]);
readBuf[2] = spi.write(buf[2]);
*outset = mask;
}
Page 182
#include "MicroBit.h"
//#define baud setBaudrate // Uncomment for V2
MicroBit uBit;
int main()
{
uBit.init();
uBit.serial.baud(115200);
uBit.serial.send("A\r\n");
}
Page 182
#include "MicroBit.h"
/* Uncomment for V2
#define baud setBaudrate
#define USBTX MICROBIT_PIN_UART_TX
#define USBRX MICROBIT_PIN_UART_RX
*/
MicroBitSerial serial(USBTX, USBRX);
int main()
{
serial.baud(115200);
serial.send("A\r\n");
release_fiber();
}
Page 189
#include "MicroBit.h"
//#define baud setBaudrate //uncomment for V2
void onChar(MicroBitEvent e)
{
ManagedString s = uBit.serial.read(1);
uBit.serial.clearRxBuffer();
uBit.display.scroll(s);
uBit.serial.eventAfter(1);
};
MicroBit uBit;
int main()
{
uBit.init();
uBit.serial.(20);
uBit.messageBus.listen(MICROBIT_ID_SERIAL,
MICROBIT_SERIAL_EVT_HEAD_MATCH, onChar);
uBit.serial.eventAfter(1);
while (1)
uBit.sleep(1000);
release_fiber();
}
Page 198
#include "MicroBit.h"
MicroBit uBit;
void initWiFi();
int ATWiFi();
int find(ManagedString c, ManagedString s);
void debug(ManagedString s);
//Comment out for V2
#define Tx MICROBIT_PIN_P0
#define Rx MICROBIT_PIN_P1
//
/* Uncomment For V2
#define baud setBaudrate
#define USBTX uBit.io.usbTx
#define USBRX uBit.io.usbRx
#define Tx uBit.io.P0
#define Rx uBit.io.P1
*/
#define DEBUG 1
int main()
{
initWiFi();
ATWiFi();
while (1)
uBit.sleep(1000);
release_fiber();
}
void initWiFi()
{
uBit.serial.redirect(Tx, Rx);
uBit.serial.baud(115200);
uBit.serial.setRxBufferSize((uint8_t) + 500);
}
int ATWiFi()
{
uBit.serial.send("AT\r\n", SYNC_SPINWAIT);
uBit.sleep(150);
ManagedString s = uBit.serial.read(500, ASYNC);
if (DEBUG)
debug("\n\rAT \n\r" + s + "\n\r");
return find("OK", s);
}
void debug(ManagedString s)
{
uBit.serial.redirect(USBTX, USBRX);
uBit.serial.send(s, SYNC_SPINWAIT);
uBit.serial.redirect(Tx, Rx);
}
int find(ManagedString c, ManagedString s)
{
int i;
for (i = 0; i < (s.length() - c.length()); i++)
{
if (c == s.substring(i, c.length()))
break;
}
if (i == (s.length() - c.length()))
return 0;
return 1;
}
Page 211
#include "MicroBit.h"
MicroBit uBit;
void initWiFi();
int ATWiFi();
int find(ManagedString c, ManagedString s);
void debug(ManagedString s);
//Comment out for V2
#define Tx MICROBIT_PIN_P0
#define Rx MICROBIT_PIN_P1
//
/* Uncomment for V2
#define baud setBaudrate
#define USBTX uBit.io.usbTx
#define USBRX uBit.io.usbRx
#define Tx uBit.io.P0
#define Rx uBit.io.P1
*/
#define DEBUG 1
#include "MicroBit.h"
void initWiFi();
int ATWiFi();
int resetWiFi();
int setUARTWiFi();
int scanWiFi();
int getIPWiFi();
int modeWiFi(int mode);
int connectWiFi(ManagedString ssid, ManagedString pass);
int getWebPageWiFi(ManagedString URL, ManagedString page);
int getVersionWiFi();
int startServerWiFi();
int waitForWiFi(ManagedString target, int retry, int pause);
int find(ManagedString c, ManagedString s);
void debug(ManagedString s);
int main()
{
uBit.init();
initWiFi();
modeWiFi(1);
connectWiFi("ssid", "password");
getIPWiFi();
startServerWiFi();
release_fiber();
}
void initWiFi()
{
uBit.serial.redirect(Tx, Rx);
uBit.serial.baud(115200);
uBit.serial.setRxBufferSize((uint8_t) + 500);
}
int ATWiFi()
{
uBit.serial.send("AT\r\n", SYNC_SPINWAIT);
return waitForWiFi("OK", 150, 10);
}
int getVersionWiFi()
{
uBit.serial.send("AT+GMR\r\n", SYNC_SPINWAIT);
return waitForWiFi("OK", 200, 10);
}
int resetWiFi()
{
uBit.serial.send("AT+RST\r\n", SYNC_SPINWAIT);
return waitForWiFi("OK", 1000, 10);
}
int setUARTWiFi()
{
uBit.serial.send("AT+UART_CUR=115200,8,1,0,0\r\n", SYNC_SPINWAIT);
return waitForWiFi("OK", 200, 10);
}
int scanWiFi()
{
uBit.serial.send("AT+CWLAP\r\n", SYNC_SPINWAIT);
return waitForWiFi("OK", 500, 50);
}
int modeWiFi(int mode)
{
ManagedString cmd = "AT+CWMODE_CUR=" + ManagedString(mode) + "\r\n";
uBit.serial.send(cmd, SYNC_SPINWAIT);
return waitForWiFi("OK", 200, 10);
}
int connectWiFi(ManagedString ssid, ManagedString pass)
{
ManagedString cmd = "AT+CWJAP_CUR=\"" + ssid + "\",\"" + pass + "\"\r\n";
uBit.serial.send(cmd, SYNC_SPINWAIT);
return waitForWiFi("OK", 200, 20);
}
int getIPWiFi()
{
uBit.serial.send("AT+CIFSR\r\n", SYNC_SPINWAIT);
return waitForWiFi("OK", 200, 10);
}
int getWebPageWiFi(ManagedString URL, ManagedString page)
{
ManagedString cmd = "AT+CIPSTART=\"TCP\",\"" + URL + "\",80\r\n";
uBit.serial.send(cmd, SYNC_SPINWAIT);
if (waitForWiFi("OK", 100, 20) == 0)
return 0;
ManagedString http = "GET " + page + "HTTP/1.0\r\nHost:" + URL + "\r\n\r\n";
cmd = "AT+CIPSEND=" + ManagedString(http.length()) + "\r\n";
uBit.serial.send(cmd, SYNC_SPINWAIT);
int retry;
ManagedString s;
s = "";
retry = 40;
do
{
uBit.sleep(100);
s = s + uBit.serial.read(500, ASYNC);
retry--;
} while (find(">", s) == 0 && retry != 0);
uBit.serial.send(http, SYNC_SPINWAIT);
retry = 100;
do
{
uBit.sleep(100);
s = s + uBit.serial.read(500, ASYNC);
retry--;
} while (s.length() < 500 && retry != 0);
if (DEBUG)
debug("\n\rPage\n\r" + s + "\n\r");
return 1;
}
int startServerWiFi()
{
uBit.serial.send("AT+CIPMUX=1\r\n", SYNC_SPINWAIT);
if (waitForWiFi("OK", 100, 20) == 0)
return 0;
uBit.serial.send("AT+CIPSERVER=1,80\r\n", SYNC_SPINWAIT);
if (waitForWiFi("OK", 100, 20) == 0)
return 0;
ManagedString s;
for (;;)
{
s = "";
do
{
uBit.sleep(100);
if (s > 500)
s = "";
s = uBit.serial.read(500, ASYNC);
} while (find("+IPD", s) == 0);
if (DEBUG)
debug("\n\rClient Connected\n\r" + s + "\n\r");
int b = find("+IPD", s);
s = s.substring(b + 1, s.length());
b = find(",", s);
s = s.substring(b + 1, s.length());
b = find(",", s);
ManagedString id = s.substring(0, b);
if (DEBUG)
debug("\n\rTCP id:" + id + "\n\r");
ManagedString headers = "HTTP/1.0 200 OK\r\n";
headers = headers + "Server: micro:bit\r\n";
headers = headers + "Content-type: text/html\r\n\r\n";
ManagedString html =
"<html><head><title>Temperature</title></"
"head><body>{\"humidity\":81%,\"airtemperature\":23.5C}</p></body></html>\r\n";
ManagedString data = headers + html;
ManagedString cmd = "AT+CIPSEND=" + id + "," + ManagedString(data.length()) + "\r\n";
uBit.serial.send(cmd, SYNC_SPINWAIT);
s = "";
int retry = 40;
do
{
uBit.sleep(100);
s = s + uBit.serial.read(500, ASYNC);
retry--;
} while (find(">", s) == 0 && retry != 0);
uBit.serial.send(data, SYNC_SPINWAIT);
if (waitForWiFi("OK", 100, 100) == 0)
return 0;
if (DEBUG)
debug("\n\rData Sent\n\r");
cmd = "AT+CIPCLOSE=" + id + "\r\n";
uBit.serial.send(cmd, SYNC_SPINWAIT);
if (waitForWiFi("OK", 100, 100) == 0)
return 0;
}
}
void debug(ManagedString s)
{
uBit.serial.redirect(USBTX, USBRX);
uBit.serial.send(s, SYNC_SPINWAIT);
uBit.serial.redirect(Tx, Rx);
}
int find(ManagedString c, ManagedString s)
{
int i;
for (i = 0; i < (s.length() - c.length()); i++)
{
if (c == s.substring(i, c.length()))
break;
}
if (i == (s.length() - c.length()))
return 0;
return i;
}
int waitForWiFi(ManagedString target, int retry, int pause)
{
ManagedString s;
do
{
uBit.sleep(pause);
if (s.length() > 500)
s = "";
s = s + uBit.serial.read(500, ASYNC);
retry--;
} while (find(target, s) == 0 && retry != 0);
if (DEBUG)
debug("\n\r" + s + "\n\r");
return retry;
}
Page 222
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uBit.display.disable();
DigitalOut col3(P0_6, 0);
DigitalOut row2(P0_14);
while (1)
{
row2 = 1;
wait(0.2);
row2 = 0;
wait(0.2);
}
release_fiber();
return 0;
}
Page 224
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uBit.display.disable();
uBit.io.row3.setDigitalValue(1);
while (1)
{
uBit.io.col3.setDigitalValue(1);
uBit.sleep(100);
uBit.io.col3.setDigitalValue(0);
uBit.sleep(100);
}
release_fiber();
return 0;
}
Page 224
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uBit.display.disable();
while (1)
{
uBit.io.col1.setDigitalValue(0);
uBit.io.row1.setDigitalValue(1);
uBit.sleep(500);
uBit.io.col1.setDigitalValue(1);
uBit.io.row1.setDigitalValue(0);
uBit.io.col2.setDigitalValue(0);
uBit.io.row2.setDigitalValue(1);
uBit.sleep(500);
uBit.io.col2.setDigitalValue(1);
uBit.io.row2.setDigitalValue(0);
uBit.io.col3.setDigitalValue(0);
uBit.io.row3.setDigitalValue(1);
uBit.sleep(500);
uBit.io.col3.setDigitalValue(1);
uBit.io.row3.setDigitalValue(0);
uBit.io.col4.setDigitalValue(0);
uBit.io.row4.setDigitalValue(1);
uBit.sleep(500);
uBit.io.col4.setDigitalValue(1);
uBit.io.row4.setDigitalValue(0);
uBit.io.col5.setDigitalValue(0);
uBit.io.row5.setDigitalValue(1);
uBit.sleep(500);
uBit.io.col5.setDigitalValue(1);
uBit.io.row5.setDigitalValue(0);
}
release_fiber();
return 0;
}
Page 228
#include "MicroBit.h"
MicroBit uBit;
int clickCount = 0;
void startGame();
int playGame();
void endGame(int position);
void countDown();
void drawPlayArea();
void flashMan(int x, int y, int p, int vx, int vy);
void setUpButton();
void moveHorizontal();
void moveVertical(int x, int h);
void buttonClickCount(MicroBitEvent e);
int main()
{
uBit.init();
startGame();
int position = playGame();
endGame(position);
release_fiber();
return 0;
}
void startGame()
{
countDown();
uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE);
drawPlayArea();
setUpButton();
}
void countDown()
{
int t;
for (t = 5; t >= 0; t--)
{
uBit.display.scroll(t);
}
uBit.display.scroll("!");
}
void drawPlayArea()
{
uBit.display.clear();
int y;
for (y = 1; y <= 5; y++)
{
uBit.display.image.setPixelValue(2, y, 255);
}
}
int playGame()
{
int position = 4;
clickCount = 0;
int t1 = uBit.systemTime();
while (uBit.systemTime() - t1 < 20000)
{
flashMan(4, position, clickCount, 0, -1);
if (clickCount > 10)
{
position = position - 1;
clickCount = 0;
if (position == 0)
break;
}
}
return position;
}
void endGame(int position)
{
if (position == 0)
{
moveHorizontal();
moveVertical(0, 0);
}
else
{
moveVertical(4, position);
}
}
void setUpButton()
{
uBit.buttonB.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B,
MICROBIT_BUTTON_EVT_CLICK,
buttonClickCount);
}
void buttonClickCount(MicroBitEvent e)
{
clickCount++;
}
void flashMan(int x, int y, int p, int vx, int vy)
{
uBit.display.image.setPixelValue(x, y, 0);
uBit.display.image.setPixelValue(x + vx, y + vy, 0);
uBit.sleep(100);
uBit.display.image.setPixelValue(x, y, 25 * (10 - p));
uBit.display.image.setPixelValue(x + vx, y + vy, 25 * p);
uBit.sleep(100);
}
void moveHorizontal()
{
int x, fraction;
for (x = 4; x >= 0; x--)
{
for (fraction = 0; fraction <= 10; fraction++)
{
flashMan(x, 0, fraction, -1, 0);
}
}
}
void moveVertical(int x, int h)
{
int y, fraction;
if (h != 0)
uBit.display.image.setPixelValue(x, h - 1, 0);
for (y = h; y <= 4; y++)
{
for (fraction = 0;
fraction <= 10; fraction++)
{
flashMan(x, y, fraction, 0, 1);
}
}
}
Page 238
void onData(MicroBitEvent e)
{
uBit.display.print("Click");
return;
}
int main()
{
uBit.init();
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A,
MICROBIT_BUTTON_EVT_CLICK, onData);
uBit.radio.enable();
while (1)
{
uBit.sleep(1000);
}
}
Page 239
#include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
uBit.radio.enable();
uBit.display.print("TX");
while (1)
{
if (uBit.buttonA.isPressed())
{
uBit.display.print("1");
uBit.radio.datagram.send("1");
}
uBit.sleep(100);
uBit.display.clear();
}
return 0;
}
Page 239
#include "MicroBit.h"
MicroBit uBit;
void onData(MicroBitEvent e)
{
ManagedString s = uBit.radio.datagram.recv();
uBit.display.print(s);
}
int main()
{
uBit.init();
uBit.messageBus.listen(MICROBIT_ID_RADIO,
MICROBIT_RADIO_EVT_DATAGRAM, onData);
uBit.radio.enable();
uBit.display.print("RX");
while (1)
{
uBit.sleep(100);
uBit.display.clear();
}
}
Page 243
#include "MicroBit.h"
MicroBit uBit;
ManagedString setSound(ManagedString audio,
ManagedString data, int s)
{
return audio.substring(0, s) + data +
audio.substring(s + data.length(),
audio.length() - s - data.length());
}
int main()
{
uBit.init();
// 72 characters
static ManagedString soundData(
"000000000000000000000000000000000000
000000000000000000000000000000000000");
soundData = setSound(soundData, "0", 0); // Wave
soundData = setSound(soundData, "1023", 1); // volume start
soundData = setSound(soundData, "1023", 26); // volume end
soundData = setSound(soundData, "1000", 5); // frequency start
soundData = setSound(soundData, "1000", 18); // frequency end
soundData = setSound(soundData, "00", 13); // shape
soundData = setSound(soundData, "500", 9); // duration
soundData = setSound(soundData, "0128", 30); // steps
uBit.audio.soundExpressions.play(soundData);
return 0;
}
Page 245
#include "MicroBit.h"
MicroBit uBit;
void beepOn(MicroBitEvent e)
{
uBit.io.P0.setAnalogValue(512);
uBit.io.P0.setAnalogPeriodUs(1000000 / 500);
uBit.display.image.setPixelValue(2, 2, 255);
}
void beepOff(MicroBitEvent e)
{
uBit.io.P0.setAnalogValue(0);
uBit.display.image.setPixelValue(2, 2, 0);
}
int main()
{
uBit.init();
uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE);
uBit.radio.enable();
uBit.radio.event.listen(MICROBIT_ID_BUTTON_A,
MICROBIT_BUTTON_EVT_DOWN);
uBit.radio.event.listen(MICROBIT_ID_BUTTON_A,
MICROBIT_BUTTON_EVT_UP);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A,
MICROBIT_BUTTON_EVT_DOWN, beepOn);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A,
MICROBIT_BUTTON_EVT_UP, beepOff);
while (1)
{
uBit.sleep(100);
}
}