Labmbed device drivers

Dependencies:   TextLCD mbed

main.cpp

Committer:
malcolmlear
Date:
2017-01-12
Revision:
3:8eee79f59b30
Parent:
2:caa0e6b2b436
Child:
4:e2310d494d19

File content as of revision 3:8eee79f59b30:

// 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
DigitalOut sel0(p26);                               // input select bits:
DigitalOut sel1(p25);                               //  "
DigitalOut sel2(p24);                               //  "
DigitalIn in0(p14);                                 // input from switches, keypad etc
DigitalIn in1(p13);                                 //  "
DigitalIn in2(p12);                                 //  "
DigitalIn in3(p11);                                 //  "
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
}

void selinput(int input) {
    sel0 = input & 0x0001;                          // set sel[0:2] pins
    sel1 = (input >> 1) & 0x0001;                   //
    sel2 = (input >> 2) & 0x0001;                   //
}

int readswitches() {
    selinput(5);                                    // select least significant 4 switches in[3:0]
    int switches = in0 + (in1 << 1) + (in2 << 2) + (in3 << 3);
    selinput(4);                                    // select most significant 4 switches in[3:0]
    return (switches + (in0 << 4)+ (in1 << 5) + (in2 << 6) + (in3 << 7));
}

int readswitch(int switchno) {
    switchno = ((switchno - 1) & 0x0007) + 1;       // limit switch number
    switchno = 8 - switchno;                        // offset of switch state in readswitches()
    int switchstate = readswitches();
    switchstate = switchstate >> switchno;          // shift selected switch state into ls bit
    return (switchstate & 0x0001);                  // mask out and return switch state 
}

int readkeys() {
    selinput(0);                                    // select keypad top row 
    int keys = (in0 << 15) + (in1 << 14) + (in2 << 13) + (in3 << 12);
    selinput(1);                                    // select keypad second row
    keys = keys + (in0 << 3) + (in1 << 6) + (in2 << 9) + (in3 << 11);   
    selinput(2);                                    // select keypad third row
    keys = keys + (in0 << 2) + (in1 << 5) + (in2 << 8) + in3;  
    selinput(3);                                    // select keypad forth row
    keys = keys + (in0 << 1) + (in1 << 4) + (in2 << 7) + (in3 << 10);
    return (keys ^ 0xffff);                         // return inverted (key press active high)
}

int readkey(int keyno) {
    keyno = keyno & 0x000f;                         // limit key number 0 to 15 (0 to F)
    int keystate = readkeys();
    keystate = keystate >> keyno;                   // shift selected key state into ls bit
    return (keystate & 0x0001);                     // mask out and return key state     
}

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(.1);
            }
        }
        for (a= 1; a < 9; a++ ) {
            setled (a,(readswitch(a) * 3));
        }
        float temp = readtemp();
        lcd.printf("                ");   
        lcd.printf("Temp = %f\n", temp);
        wait(1);
        int switches = readswitches();
        lcd.printf("                ");   
        lcd.printf("Switches = %d\n", switches);
        wait(1);
        int keys = readkeys();
        lcd.printf("                ");   
        lcd.printf("Keys = %d\n", keys);
        wait(1);
        int key = readkey(5);
        lcd.printf("                ");   
        lcd.printf("Key5 = %d\n", key);
    }
}