Explore Intel Edison Programs
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.
There is also the problem of what happens when NetBeans changes and the downloads no longer work.
The best solution is to provide the source code of the programs in a form that can be copied and pasted into a NetBeans project. This means that you have some interaction with the code rather than just running it and it means that it is immune to changes in NetBeans.
The only downside is that you have to create a project to paste the code into.
To do this follow the instruction in chapter 2.
If anything you consider important is missing or if you have any requests or comments contact me
This email address is being protected from spambots. You need JavaScript enabled to view it.
page 33
#include "mraa.h" #include #include int main() { mraa_gpio_context pin = mraa_gpio_init(13); mraa_gpio_dir(pin, MRAA_GPIO_OUT); for (;;) { mraa_gpio_write(pin, 0); sleep(1); mraa_gpio_write(pin, 1); sleep(1); } return MRAA_SUCCESS; }
page 44
#include "mraa.h" #include #include int main() { mraa_init(); mraa_gpio_context pin15 = mraa_gpio_init(15); mraa_gpio_dir(pin15, MRAA_GPIO_OUT_HIGH); mraa_gpio_context pin31 = mraa_gpio_init(31); mraa_gpio_dir(pin31, MRAA_GPIO_OUT_LOW); for (;;) { mraa_gpio_write(pin15, 0); mraa_gpio_write(pin31, 1); mraa_gpio_write(pin15, 1); mraa_gpio_write(pin31, 0); } return MRAA_SUCCESS; }
page 46
#include "mraa.h" #include #include int main() { mraa_init(); mraa_gpio_context pin31 = mraa_gpio_init(31); mraa_gpio_dir(pin31, MRAA_GPIO_IN); int in; for (;;) { in=mraa_gpio_read(pin31); printf("switch %d \n",in); usleep(1000*1000); } return MRAA_SUCCESS; }
page 48
#include "mraa.h" #include #include void switchChange(); int main(){ mraa_init(); mraa_gpio_context pin31 = mraa_gpio_init(31); mraa_gpio_dir(pin31, MRAA_GPIO_IN); mraa_gpio_isr(pin31, MRAA_GPIO_EDGE_FALLING, &switchChange,NULL); for (;;) {}; return MRAA_SUCCESS; } void switchChange(){ printf("switch \n"); }
page 49
#include "mraa.h" #include #include void switchChange(void* pin); int main(){ mraa_init(); mraa_gpio_context pin31 = mraa_gpio_init(31); mraa_gpio_dir(pin31, MRAA_GPIO_IN); mraa_gpio_isr(pin31, MRAA_GPIO_EDGE_BOTH, &switchChange,pin31); for (;;) {}; return MRAA_SUCCESS; } void switchChange(void* pin){ int s=mraa_gpio_read((mraa_gpio_context) pin); printf("switch %d \n",s); }
page 52
#include "mraa.h" #include #include #include
page 60
#include "mraa.h" #include #include int main() { mraa_gpio_context pin31=mraa_gpio_init(31); mraa_gpio_dir(pin31, MRAA_GPIO_OUT); mraa_gpio_use_mmaped(pin31,1); for (;;) { mraa_gpio_write(pin31, 0); mraa_gpio_write(pin31, 1); } return MRAA_SUCCESS; }
page 61
#include "mraa.h" #include #include int main() { mraa_gpio_context pin31=mraa_gpio_init(31); mraa_gpio_dir(pin31, MRAA_GPIO_OUT); mraa_gpio_use_mmaped(pin31,1); int i; for (;;) { mraa_gpio_write(pin31, 0); for(i=1;i<7500;i++){}; mraa_gpio_write(pin31, 1); for(i=1;i<7500;i++){}; } return MRAA_SUCCESS; }
page 62
#include #include int main(){ mraa_init(); mraa_gpio_context pin15 = mraa_gpio_init(15); mraa_gpio_dir(pin15, MRAA_GPIO_OUT_HIGH); mraa_gpio_use_mmaped(pin15,1); mraa_gpio_context pin31 = mraa_gpio_init(31); mraa_gpio_dir(pin31, MRAA_GPIO_OUT_LOW); mraa_gpio_use_mmaped(pin31,1); for (;;) { mraa_gpio_write(pin15, 0); mraa_gpio_write(pin31, 1); mraa_gpio_write(pin15, 1); mraa_gpio_write(pin31, 0); } return MRAA_SUCCESS; }
page 66
#include #include int main() { mraa_init(); mraa_gpio_context pin31 = mraa_gpio_init(31); mraa_gpio_dir(pin31, MRAA_GPIO_IN); mraa_gpio_use_mmaped(pin31,1); int i; for(;;){ if(mraa_gpio_read(pin31)==0)break; } for(i=0;i<10000;i++){ if(mraa_gpio_read(pin31)==1)break; } for(i=1;i<10000;i++){ if(mraa_gpio_read(pin31)==0)break; } printf("%d \n",i); return MRAA_SUCCESS; }
page 68
#include #include #include <sys/mman.h> #include <sys/stat.h> uint8_t* load_memmap(); int main() { mraa_init(); mraa_gpio_context pin31 = mraa_gpio_init(31); mraa_gpio_dir(pin31, MRAA_GPIO_OUT); load_memmap(); int pin= 44; uint32_t mask=(uint32_t)(1 << (pin % 32)); uint8_t valoffsetOn = 0x34; uint8_t valoffsetOff = 0x4c; uint8_t* loc=(pin / 32) * sizeof(uint32_t)+ mmap_reg; for (;;) { *(volatile uint32_t*) (loc + valoffsetOn) = mask; *(volatile uint32_t*) (loc + valoffsetOff) = mask; } return MRAA_SUCCESS; } uint8_t* load_memmap(){ int mmap_fd; struct stat fd_stat; uint8_t* mmap_reg; mmap_fd = open( "/sys/devices/pci0000:00/0000:00:0c.0/resource0", O_RDWR); fstat(mmap_fd, &fd_stat); mmap_reg =(uint8_t*) mmap(NULL, fd_stat.st_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, mmap_fd, 0); return mmap_reg; }
page 76
#include #include #include "mraa.h" #include int main() { mraa_gpio_context pin = mraa_gpio_init(13); mraa_gpio_dir(pin, MRAA_GPIO_OUT); for (;;) { mraa_gpio_write(pin, 0); mraa_gpio_write(pin, 1); } return MRAA_SUCCESS; }
page 78
#include #include #include "mraa.h" #include int main() { const struct sched_param priority={1}; sched_setscheduler(0,SCHED_FIFO,&priority); mraa_gpio_context pin = mraa_gpio_init(13); mraa_gpio_dir(pin, MRAA_GPIO_OUT); mraa_gpio_use_mmaped(pin,1); for (;;) { mraa_gpio_write(pin, 0); mraa_gpio_write(pin, 1); } return MRAA_SUCCESS; }
page 85
#include "mraa.h" #include #include int main() { mraa_pwm_context pwm0 = mraa_pwm_init(20); mraa_pwm_context pwm1 = mraa_pwm_init(14); mraa_pwm_context pwm2 = mraa_pwm_init(0); mraa_pwm_context pwm3 = mraa_pwm_init(21); mraa_pwm_period_us(pwm0, 10); mraa_pwm_period_us(pwm1, 20); mraa_pwm_period_us(pwm2, 30); mraa_pwm_period_us(pwm3, 40); mraa_pwm_write(pwm0, 0.5f); mraa_pwm_write(pwm1, 0.4f); mraa_pwm_write(pwm2, 0.3f); mraa_pwm_write(pwm3, 0.2f); mraa_pwm_enable(pwm0,1 ); mraa_pwm_enable(pwm1,1 ); mraa_pwm_enable(pwm2,1 ); mraa_pwm_enable(pwm3,1 ); return MRAA_SUCCESS; }
page 89
#include "mraa.h" #include #include int main() { mraa_pwm_context pwm0 = mraa_pwm_init(20); mraa_pwm_enable(pwm0,0 ); mraa_pwm_period_ms(pwm0,1); mraa_pwm_enable(pwm0,1 ); int w=1; int inc=1; for (;;) { mraa_pwm_pulsewidth_us (pwm0, w); w=w+inc; if(w>100 || w<2)inc=(-1)*inc; usleep(1000); } return MRAA_SUCCESS; }
page 90
#include "mraa.h" #include #include int main() { mraa_pwm_context pwm0 = mraa_pwm_init(20); mraa_pwm_enable(pwm0,0 ); mraa_pwm_period_ms(pwm0,1); mraa_pwm_enable(pwm0,1 ); int w=0; int b=0; int inc=1; for (;;) { b+=inc; w=b*b*b; mraa_pwm_pulsewidth_us (pwm0, w); if(w>=1000 || w<=0)inc=(-1)*inc; usleep(40000); } return MRAA_SUCCESS; }
page 93
#include "mraa.h" #include #include int main() { mraa_pwm_context pwm3 = mraa_pwm_init(21); mraa_pwm_enable(pwm3,0 ); mraa_pwm_period_ms(pwm3,20); mraa_pwm_enable(pwm3,1 ); for (;;) { mraa_pwm_pulsewidth_us (pwm3,20000-1000); sleep(4); mraa_pwm_pulsewidth_us (pwm3,20000-1500); sleep(4); mraa_pwm_pulsewidth_us (pwm3,20000-2000); sleep(4); } return MRAA_SUCCESS; }
page 113
#include "mraa.h" #include #include uint8_t crcCheck(uint8_t, uint8_t, uint8_t); int main() { mraa_i2c_context i2c; i2c = mraa_i2c_init(1); mraa_i2c_address(i2c, 0x40); uint8_t data =mraa_i2c_read_byte_data (i2c, 0xE7); printf("Register= %d \n", data); uint8_t buf[3]; mraa_i2c_read_bytes_data(i2c,0xE3,buf,3); uint8_t msb= buf[0]; uint8_t lsb= buf[1]; uint8_t check= buf[2]; printf(" msb %d \n lsb %d \n checksum %d \n",msb,lsb,check); printf("crc %d \n ", crcCheck(msb,lsb,check)); unsigned int data16=((unsigned int) msb << 8) | (unsigned int) (lsb & 0xFC); float temp = (float)(-46.85 + (175.72 * data16 / (float)65536)); printf("Temperature %f C \n",temp); mraa_i2c_read_bytes_data(i2c,0xE5,buf,3); msb= buf[0]; lsb= buf[1]; check= buf[2]; printf(" msb %d \n lsb %d \n checksum %d \n", msb,lsb,check); printf("crc %d \n ", crcCheck(msb,lsb,check)); data16=((unsigned int) msb << 8) | (unsigned int) (lsb & 0xFC); float hum = (float)(-6 + (125.0 * data16 / (float)65536)); printf("Humidity %f %% \n",hum);return MRAA_SUCCESS; } 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 135
#include "mraa.h" #include #include uint getByte(int,int[]); int main() { const struct sched_param priority={1}; sched_setscheduler(0,SCHED_FIFO,&priority); mraa_gpio_context pin = mraa_gpio_init(31); mraa_gpio_use_mmaped(pin,1); mraa_gpio_dir(pin, MRAA_GPIO_OUT_HIGH); mraa_gpio_write(pin, 0); usleep(1000); mraa_gpio_dir(pin, MRAA_GPIO_IN);int buf[40]; int i, j; for(j=0;j<=40;j++){ for(i=1;i<200;i++){ if(mraa_gpio_read(pin)==1)break; }; for(i=1;i<200;i++){ if(mraa_gpio_read(pin)==0)break; } buf[j]=0; if(i>75)buf[j]=1; } for(j=0;j<=40;j++){ printf("%d %d \n",j,buf[j]); } int byte1=getByte(1,buf); int byte2=getByte(2,buf); int byte3=getByte(3,buf); int byte4=getByte(4,buf); int byte5=getByte(5,buf); printf("Checksum %d %d \n",byte5, (byte1+byte2+byte3+byte4) & 0xFF); float humidity= (float) (byte1<0)temperature=-temperature; printf("Temperature= %f \n",temperature); return MRAA_SUCCESS; } uint getByte(int b,int buf[]){ int i; uint result=0; b=(b-1)*8+1; for(i=b;i<=b+7;i++){ result<<=1; result |= buf[i]; } return result; }
page 150
#include "mraa.h" #include #include int init(); int readBit(); int readByte(); void writeBit(int); void writeByte(int); int convert(); void putSetting(int,char[],char[]); mraa_gpio_context pinIn; mraa_gpio_context pinOut; int main() { const struct sched_param priority = { 1 }; sched_setscheduler(0, SCHED_FIFO, &priority); pinIn = mraa_gpio_init(32); mraa_gpio_use_mmaped(pinIn, 1); mraa_gpio_dir(pinIn, MRAA_GPIO_IN);pinOut = mraa_gpio_init(31); mraa_gpio_use_mmaped(pinOut, 1); mraa_gpio_dir(pinOut, MRAA_GPIO_OUT_HIGH); putSetting(44,"pullmode","nopull"); putSetting(44,"opendrain","enable"); if (init() == 1) { printf("No device \n"); return MRAA_SUCCESS; } writeByte(0xCC); convert(); if (init() == 1) { printf("No device \n"); return MRAA_SUCCESS; } writeByte(0xCC); writeByte(0xBE); int b1 = readByte(); printf("%d \n", b1); int b2 = readByte(); printf("%d \n", b2); int16_t temp1 = (b2 << 8 | b1); float temp = (float) temp1 / 16; printf("temperature = %f C \n", temp); return MRAA_SUCCESS; } void writeBit(int b) { int i; int delay1, delay2; if (b == 1) { delay1 = 60; delay2 = 4000; } else { delay1 = 4000; delay2 = 60; } mraa_gpio_write(pinOut, 0); for (i = 1; i < delay1; i++) { }; mraa_gpio_write(pinOut, 1); for (i = 1; i < delay2; i++) { }; } int readBit() { int i; mraa_gpio_write(pinOut, 0); for (i = 1; i < 60; i++) { }; mraa_gpio_write(pinOut, 1); for (i = 1; i < 450; i++) { }; int b = mraa_gpio_read(pinIn); for (i = 1; i < 3500; i++) { }; return b; } 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; } int convert() { int i; writeByte(0x44); for (i = 1; i < 1000; i++) { usleep(10000); if (readBit() == 1) break; }; return (i == 1000); } int init() { mraa_gpio_write(pinOut, 0); usleep(500); mraa_gpio_write(pinOut, 1); usleep(60); int b = mraa_gpio_read(pinIn); usleep(500); return b; } void putSetting(int pin,char prop[],char value[]){ char buf[200]; sprintf(buf, "/sys/kernel/debug/gpio_debug/gpio%d/current_%s",pin,prop); int fd = open(buf, O_WRONLY); write(fd, value, strlen(value)); close(fd); }
page 161
#include "mraa.h" #include #include int main() { mraa_spi_context spi = mraa_spi_init(0); mraa_spi_mode(spi, MRAA_SPI_MODE0); mraa_spi_frequency(spi, 400000); mraa_spi_lsbmode(spi, 0); mraa_spi_bit_per_word(spi, 8); uint16_t read_data = mraa_spi_write(spi, 0xAA); if (read_data == 0xAA) printf("data received correctly"); mraa_spi_stop(spi); return MRAA_SUCCESS; }
page 168
#include "mraa.h" #include #include uint8_t sendbyte(uint8_t byte, int delay); mraa_gpio_context CS1; mraa_gpio_context SCK; mraa_gpio_context MOSI; mraa_gpio_context MISO; int main() { const struct sched_param priority = { 1 }; sched_setscheduler(0, SCHED_FIFO, &priority); CS1 = mraa_gpio_init(9); mraa_gpio_use_mmaped(CS1, 1); mraa_gpio_dir(CS1, MRAA_GPIO_OUT); mraa_gpio_write(CS1, 1); SCK = mraa_gpio_init(10); mraa_gpio_use_mmaped(SCK, 1); mraa_gpio_dir(SCK, MRAA_GPIO_OUT); mraa_gpio_write(SCK, 0); MOSI = mraa_gpio_init(11); mraa_gpio_use_mmaped(MOSI, 1); mraa_gpio_dir(MOSI, MRAA_GPIO_OUT); mraa_gpio_write(MOSI, 0); MISO = mraa_gpio_init(24); mraa_gpio_use_mmaped(MISO, 1); mraa_gpio_dir(MISO, MRAA_GPIO_IN); mraa_gpio_read(MISO); int delay = 1000; uint8_t read; for (;;) { read = sendbyte(0xAA, delay); if (read != 0xAA) printf("Error \n"); } return MRAA_SUCCESS; } uint8_t sendbyte(uint8_t byte, int delay) { int i, j; int read = 0; mraa_gpio_write(CS1, 0); for (j = 1; j < delay / 8 + 100; j++) { }; for (i = 0; i < 8; i++){ mraa_gpio_write(MOSI, byte & 0x80); byte = byte << 1; for (j = 1; j < delay; j++) { }; mraa_gpio_write(SCK, 1); read = read << 1; read = read | (mraa_gpio_read(MISO)); for (j = 1; j < delay; j++) { }; mraa_gpio_write(SCK, 0); } mraa_gpio_write(CS1, 1); for (j = 1; j < delay / 8 + 20; j++) { }; return (uint8_t) read; }
page 182
#include #include #include "mraa.h" #include int readADC(mraa_spi_context spi, uint8_t chan); mraa_spi_context initADC(int freq); int readADCsoft(uint8_t chan, int delay); void initSPIsoft(); mraa_gpio_context CS1; mraa_gpio_context SCK; mraa_gpio_context MOSI; mraa_gpio_context MISO; int main() { const struct sched_param priority = { 1 }; sched_setscheduler(0, SCHED_FIFO, &priority); // mraa_spi_context spi=initADC(1000000); int data; initSPIsoft(); for (;;) { // data =readADC(spi,0); data = readADCsoft(0, 0); } printf("Data %d \n", data); float volts = ((float) data) * 3.3f / 1023.0f; printf("volts= %f \n", volts); // mraa_spi_stop(spi); return 0; } int readADC(mraa_spi_context spi, uint8_t chan) { uint8_t buf[] = {0x01,(0x08 | chan) << 4, 0x00}; uint8_t readBuf[3]; mraa_spi_transfer_buf(spi, buf, readBuf, 3); return ((int) readBuf[1] & 0x03) << 8 | (int) readBuf[2]; } mraa_spi_context initADC(int freq) { mraa_spi_context spi = mraa_spi_init(0);mraa_spi_mode(spi, MRAA_SPI_MODE0); mraa_spi_frequency(spi, freq); mraa_spi_lsbmode(spi, 0); mraa_spi_bit_per_word(spi, 8); return spi; } int readADCsoft(uint8_t chan, int delay) { int i, j; int read; int result; uint8_t byte; read = 0; mraa_gpio_write(CS1, 0); for (j = 1; j < 100; j++) { }; byte = 0x01; for (i = 0; i < 8; i++) { mraa_gpio_write(MOSI, byte & 0x80); byte = byte << 1; for (j = 1; j < delay; j++) { }; mraa_gpio_write(SCK, 1); for (j = 1; j < delay / 2; j++) { }; read = read << 1; read = read | (mraa_gpio_read(MISO)); for (j = 1; j < delay / 2; j++) { }; mraa_gpio_write(SCK, 0); } byte = (0x08 | chan) << 4; for (i = 0; i < 8; i++) { mraa_gpio_write(MOSI, byte & 0x80); byte = byte << 1; for (j = 1; j < delay; j++) { }; mraa_gpio_write(SCK, 1); for (j = 1; j < delay / 2; j++) { }; read = read << 1; read = read | (mraa_gpio_read(MISO)); for (j = 1; j < delay / 2; j++) { }; mraa_gpio_write(SCK, 0); } result = (int) read & 0x03 << 8; byte = 0; for (i = 0; i < 8; i++) { mraa_gpio_write(MOSI, byte & 0x80); byte = byte << 1; for (j = 1; j < delay; j++) { }; mraa_gpio_write(SCK, 1); for (j = 1; j < delay / 2; j++) { }; read = read << 1; read = read | (mraa_gpio_read(MISO)); for (j = 1; j < delay / 2; j++) { }; mraa_gpio_write(SCK, 0); } mraa_gpio_write(CS0, 1); for (j = 1; j < 10; j++) { }; return result | read; } void initSPIsoft() { CS1 = mraa_gpio_init(9); mraa_gpio_dir(CS1, MRAA_GPIO_OUT); mraa_gpio_use_mmaped(CS1, 1); mraa_gpio_write(CS1, 1); SCK = mraa_gpio_init(10); mraa_gpio_dir(SCK, MRAA_GPIO_OUT); mraa_gpio_use_mmaped(SCK, 1); mraa_gpio_write(SCK, 0); MOSI = mraa_gpio_init(11); mraa_gpio_dir(MOSI, MRAA_GPIO_OUT); mraa_gpio_use_mmaped(MOSI, 1); mraa_gpio_write(MOSI, 0); MISO = mraa_gpio_init(24); mraa_gpio_use_mmaped(MISO, 1); mraa_gpio_dir(MISO, MRAA_GPIO_IN); }
page 189
#include #include #include "mraa.h" int main() { int fd; char buf[100]; int gpio = 44; fd = open("/sys/class/gpio/export", O_WRONLY); sprintf(buf, "%d", gpio); write(fd, buf, strlen(buf)); close(fd); sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio); fd = open(buf, O_WRONLY); write(fd, "out", 3); close(fd); sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio); fd = open(buf, O_WRONLY); for(;;){ write(fd, "1", 1); write(fd, "0", 1); } close(fd); fd = open("/sys/class/gpio/unexport", O_WRONLY); sprintf(buf, "%d", gpio); write(fd, buf, strlen(buf)); close(fd); return 0; }
Micro:bit IoT In C
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.
There is also the problem of what happens when NetBeans or mbed changes and the downloads no longer work.
The best solution is to provide the source code of the programs in a form that can be copied and pasted into a NetBeans or mbed project. This means that you have some interaction with the code rather than just running it and it means that it is immune to changes in the IDEs.
The only downside is that you have to create a project to paste the code into.
To do this follow the instruction in chapter 2 for mbed and chapter 3 for NetBeans
If anything you consider important is missing or if you have any requests or comments contact me
This email address is being protected from spambots. You need JavaScript enabled to view it.
page 14
#include "MicroBit.h" MicroBit uBit; int main() { uBit.init(); uBit.display.scroll("NEW PROGRAM"); while (1) { uBit.io.P0.setDigitalValue(1); uBit.io.P0.setDigitalValue(0); } release_fiber(); }
page 22
#include "MicroBit.h" MicroBit uBit; int main() { uBit.init(); uBit.display.scroll("NEW PROGRAM"); release_fiber(); }
page 58
#include "MicroBit.h" MicroBitSerial serial(USBTX, USBRX); int main() { volatile unsigned int*in=(unsigned int *)(0x50000000UL+0x510); volatile unsigned int*pin3=(unsigned int *)(0x50000000UL+0x70C); unsigned int mask = 1 << 3; *pin3=0x0; volatile int i; while (1) { while (mask == ((*in) & mask)) {}; while (mask != ((*in) & mask)) {}; for (i = 0; i < 5000; i++) { if (mask != ((*in) & mask)) break; } serial.printf("%d\n\r", i); } return 0; }
page 65
#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 68
#include "MicroBit.h" 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 72
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 73
#include "MicroBit.h" 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 76
#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 77
#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 79
#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 88
#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 97
#include "MicroBit.h" MicroBit uBit; int main() { uBit.init(); char buf[4] = {0xE3}; uBit.i2c.write(0x80, buf, 1); uBit.i2c.read(0x80, buf, 3); uint8_t msb = buf[0]; uint8_t lsb = buf[1]; uint8_t check = buf[2]; printf("\n\rmsb %d \n\rlsb %d \n\rchecksum %d \n\r", msb, lsb, check); unsigned int data16 = ((unsigned int) msb << 8) |(unsigned int) (lsb & 0xFC); int temp = -4685 + ((17572 * data16) >> 16); printf("Temperature %d.%d C \n\r", temp / 100, temp % 100); release_fiber(); return 0; }
page 100
#include "MicroBit.h" MicroBit uBit; uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check); int main() { uBit.init(); char buf[4] = {0xE3}; uBit.i2c.write(0x80, buf, 1); uBit.i2c.read(0x80, buf, 3); uint8_t msb = buf[0]; uint8_t lsb = buf[1]; uint8_t check = buf[2]; printf("\n\rmsb %d \n\rlsb %d \n\rchecksum %d \n\r", msb, lsb, check); printf("crc %d \n ", crcCheck(msb,lsb,check)); unsigned int data16 = ((unsigned int) msb << 8) | (unsigned int) (lsb & 0xFC); int temp = -4685 + ((17572 * data16) >> 16); printf("Temperature %d.%d C \n\r", temp / 100, temp % 100); buf[0] = 0xE5; uBit.i2c.write(0x80, buf, 1); uBit.i2c.read(0x80, buf, 3); msb = buf[0]; lsb = buf[1]; check = buf[2]; printf("\n\rmsb %d \n\rlsb %d \n\rchecksum %d \n\r", msb, lsb, check); printf("crc %d \n ", crcCheck(msb,lsb,check)); data16 = ((unsigned int) msb << 8) | (unsigned int) (lsb & 0xFC); int hum = -600 + ((12500 * data16) >> 16); printf("Humidity %d.%d %% \n\r", hum / 100, hum % 100); uint32_t data32 = ((uint32_t) msb << 16) | ((uint32_t) lsb << 8) | (uint32_t) check; release_fiber(); return 0; } 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 106
#include "MicroBit.h" MicroBit uBit; MicroBitPin P0=uBit.io.P0;; int main() { uBit.init(); P0.setDigitalValue(1); P0.setDigitalValue(0); volatile int i; for (i = 0; i < 1200; i++) { }; int b = P0.getDigitalValue(); }
page 111
#include "MicroBit.h" uint getByte(int, int[]); MicroBit uBit; MicroBitPin P0 = uBit.io.P0; int main() { uBit.init(); P0.setDigitalValue(1); for(;;){ P0.setDigitalValue(0); volatile int i; uBit.sleep(1); P0.getDigitalValue(); for (i = 1; i < 200; i++) { if (P0.getDigitalValue() == 0)break; }; int buf[41]; int j; for (j = 0; j < 41; j++) { for (i = 1; i < 200; i++) { if (P0.getDigitalValue() == 1)break; }; for (i = 1; i < 200; i++) { if (P0.getDigitalValue() == 0) break; } buf[j] = 0; if (i > 11)buf[j] = 1; } uint byte1 = getByte(1, buf); uint byte2 = getByte(2, buf); uint byte3 = getByte(3, buf); uint byte4 = getByte(4, buf); uint byte5 = getByte(5, buf); int16_t humidity = (byte1 << 8) | byte2; int16_t temperature = (byte3 << 8) | byte4; if(temperature & 0x8000) temperature=-(temperature & 0x7FFF); printf("Checksum %X %X \n\r", byte5, (byte1 + byte2 + byte3 + byte4) & 0xFF); printf("Humidity %hd.%hd%%\n\r", humidity / 10, abs(humidity % 10)); printf("Temperature %hd.%hdC\n\r", temperature / 10, abs(temperature % 10)); char buff[10]; sprintf(buff,"%hd.%hd%%",humidity / 10, abs(humidity % 10)); uBit.display.scroll(buff, 200); sprintf(buff,"%hd.%hdC",temperature / 10, abs(temperature%10)); uBit.display.scroll(buff, 200); uBit.sleep(2500); } } uint getByte(int b, int buf[]) { int i; uint result = 0; b = (b - 1)*8 + 1; for (i = b; i <= b + 7; i++) { result = result << 1; result = result | buf[i]; } return result; }
page 127
#include "MicroBit.h" int init(); void sendskip(); void writeBit(int); void sendOne(); void sendZero(); void writeByte(int); int readBit(); int convert(); int readByte(); MicroBit uBit; MicroBitPin P0 = uBit.io.P0; MicroBitPin P1 = uBit.io.P1; int main() { uBit.init(); P0.setDigitalValue(1); P0.setPull(PullUp); P1.getDigitalValue(); uBit.sleep(1); while (1) { init(); writeByte(0xCC); int r = convert(); init(); writeByte(0xCC); writeByte(0xBE); int b1 = readByte(); int b2 = readByte(); int16_t temp = (b2 << 8 | b1); temp = temp * 100 / 16; char buff[10]; sprintf(buff, "%d.%d", temp / 100, abs(temp % 100)); uBit.display.scroll(buff, 200); uBit.sleep(1000); } release_fiber(); } int init() { volatile int i;P0.setDigitalValue(0); for (i = 0; i < 600; i++) {}; P0.setDigitalValue(1); for (i = 0; i < 30; i++) {}; int b = P1.getDigitalValue(); for (i = 0; i < 600; i++) {}; return b; } void sendZero() { volatile int i; P0.setDigitalValue(0); for (i = 1; i < 75; i++) {}; P0.setDigitalValue(1); for (i = 1; i < 6; i++) {}; } void sendOne() { volatile int i; P0.setDigitalValue(0); for (i = 1; i < 1; i++) {}; P0.setDigitalValue(1); for (i = 1; i < 80; i++) {}; } void writeBit(int b) { volatile int i; int delay1, delay2; if (b == 1) { delay1 = 1; delay2 = 80; } else { delay1 = 75; delay2 = 6; } P0.setDigitalValue(0); for (i = 1; i < delay1; i++) {}; P0.setDigitalValue(1); for (i = 1; i < delay2; i++) {}; } void sendskip() { writeBit(0); writeBit(0); writeBit(1); writeBit(1); writeBit(0); writeBit(0); writeBit(1); writeBit(1); } void writeByte(int byte) { int i; for (i = 0; i < 8; i++) { if (byte & 1) { writeBit(1); } else { writeBit(0); } byte = byte >> 1; } } int readBit() { volatile int i; P0.setDigitalValue(0); P0.setDigitalValue(1); for (i = 1; i < 20; i++) {}; int b = P1.getDigitalValue(); for (i = 1; i < 60; i++) {}; return b; } int convert() { volatile int i; int j; writeByte(0x44); for (j = 1; j < 1000; j++) { for (i = 1; i < 900; i++) {}; if (readBit() == 1)break; }; return (j); } int readByte() { int byte = 0; int i; for (i = 0; i < 8; i++) { byte = byte | readBit() << i; }; return byte; }
page 137
#include "MicroBit.h" SPI spi(MOSI, MISO, SCK); MicroBitSerial serial(USBTX, USBRX); MicroBitPin CS(MICROBIT_ID_IO_P16, MICROBIT_PIN_P16, PIN_CAPABILITY_DIGITAL); int main() { CS.setDigitalValue(1); spi.format(8, 0); spi.frequency(1000000); CS.setDigitalValue(0); uint8_t read_data = spi.write(0xAA); CS.setDigitalValue(1); if (read_data == 0xAA) printf("data received correctly"); release_fiber(); return 0; }
page 165
#include "MicroBit.h" MicroBit uBit; void initWiFi(); int ATWiFi(); int find(ManagedString c, ManagedString s); void debug(ManagedString s); #define Tx MICROBIT_PIN_P0 #define Rx MICROBIT_PIN_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(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 178
#include "MicroBit.h" MicroBit uBit; 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); #define Tx MICROBIT_PIN_P0 #define Rx MICROBIT_PIN_P1 #define DEBUG 1 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(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 ="Temperature{\"humidity\":81%,\"airtemperature\":23.5C}\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 185
#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 189
#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); } } }
Marketing, rights and trade orders: This email address is being protected from spambots. You need JavaScript enabled to view it.
Commissioning Editor: This email address is being protected from spambots. You need JavaScript enabled to view it.
Send any error reports or questions concerning content to: This email address is being protected from spambots. You need JavaScript enabled to view it.
All other matters email: This email address is being protected from spambots. You need JavaScript enabled to view it.
Page 3 of 3