malcolm lear
/
LabmbedV30
Labmbed device drivers
main.cpp
- Committer:
- malcolmlear
- Date:
- 2017-01-11
- Revision:
- 0:416329806e55
- Child:
- 1:04e1ee8faa04
File content as of revision 0:416329806e55:
// 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); 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; 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; statemask = ((statemask << ledno) ^ 0xffff); led_bits = ((led_bits & statemask) | ledstate); 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; return (ledstate & 0x0003); } int readleds() { return led_bits; } //int readswitch(int switchno) { //} //int readswitches(int switchno) { //} //int readkey(int keyno) { //} //int readkeyrow(int keyrow) { //} //int readkeys() { //} float readtemp() { char cmd[2]; cmd[0] = 0x01; cmd[1] = 0x00; i2c.write(tmp102addr, cmd, 2); wait(0.5); cmd[0] = 0x00; i2c.write(tmp102addr, cmd, 1); i2c.read(tmp102addr, cmd, 2); return (float((cmd[0]<<8)|cmd[1]) / 256.0); } int main() { initleds(); while(1) { setled (5,2); setled (6,1); setled (7,3); int led = readled(6); lcd.printf(" \n"); lcd.printf("LED6 = %d\n", led); wait(3); setleds(0x5555); led = readleds(); lcd.printf(" \n"); lcd.printf("LEDs = %d\n", led); wait(3); float temp = readtemp(); lcd.printf(" \n"); lcd.printf("Temp = %f\n", temp); wait(3); } }