malcolm lear
/
LabmbedV30
Labmbed device drivers
main.cpp
- Committer:
- malcolmlear
- Date:
- 2017-01-12
- Revision:
- 2:caa0e6b2b436
- Parent:
- 1:04e1ee8faa04
- Child:
- 3:8eee79f59b30
File content as of revision 2:caa0e6b2b436:
// Demo Program for Labmbed Board #include "mbed.h" #include "TextLCD.h" TextLCD lcd(p15, p16, p17, p18, p19, p20); // LCD: RS, E, D4-D7 SPI spi(p5, p6, p7); // SPI: MOSI, MISO, SCLK (MISO not used with LCD) DigitalOut lat(p8); // data latch for LED driver TLC59281 I2C i2c(p9, p10); // I2C: SDA, SCL int led_bits = 0; // global LED status used for readback const int tmp102addr = 0x92; // TMP102 temperature I2C address void initleds() { lat = 0; // latch must start low spi.format(16,0); // SPI 16 bit data, low state, high going clock spi.frequency(1000000); // 1MHz clock rate } void setleds(int ledall) { led_bits = ledall; // update global LED status spi.write((led_bits & 0x03ff) | ((led_bits & 0xa800) >> 1) | ((led_bits & 0x5400) << 1)); lat = 1; // latch pulse start lat = 0; // latch pulse end } void setled(int ledno, int ledstate) { ledno = ((ledno - 1) & 0x0007) + 1; // limit led number ledno = (8 - ledno) * 2; // offset of led state in 'led_bits' ledstate = ledstate & 0x0003; // limit led state ledstate = ledstate << ledno; int statemask = ((0x0003 << ledno) ^ 0xffff); // mask used to clear led state led_bits = ((led_bits & statemask) | ledstate); // clear and set led state setleds(led_bits); } int readled(int ledno) { ledno = ((ledno - 1) & 0x0007) + 1; // limit led number ledno = (8 - ledno) * 2; // offset of led state in 'led_bits' int ledstate = led_bits; ledstate = ledstate >> ledno; // shift selected led state into ls 2 bits return (ledstate & 0x0003); // mask out and return led state } int readleds() { return led_bits; // return LED status } //int readswitch(int switchno) { //} //int readswitches(int switchno) { //} //int readkey(int keyno) { //} //int readkeyrow(int keyrow) { //} //int readkeys() { //} float readtemp() { char cmd[3]; cmd[0] = 0x01; // pointer register value cmd[1] = 0x60; // byte 1 of the configuration register cmd[2] = 0xa0; // byte 2 of the configuration register i2c.write(tmp102addr, cmd, 3); // select configuration register and write 0x60a0 to it wait(0.5); // wait for conversion cmd[0] = 0x00; // pointer register value i2c.write(tmp102addr, cmd, 1); // select temperature register i2c.read(tmp102addr, cmd, 2); // read 16-bit temperature register return (float((cmd[0]<<8)|cmd[1]) / 256.0); // divide by 256 and return temperature } int main() { initleds(); while(1) { int a,b; for (b = 0; b < 4; b++ ) { for (a = 1; a < 9; a++ ) { setled (a,b); wait(.5); } } float temp = readtemp(); lcd.printf(" \n"); lcd.printf("Temp = %f\n", temp); wait(3); } }