Brian Pearson
/
LPC1768BirefInterface
Biref interface
main.cpp@0:b6f6b0483eff, 2016-01-05 (annotated)
- Committer:
- BPPearson
- Date:
- Tue Jan 05 16:42:18 2016 +0000
- Revision:
- 0:b6f6b0483eff
Biref interface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BPPearson | 0:b6f6b0483eff | 1 | #include "mbed.h" |
BPPearson | 0:b6f6b0483eff | 2 | |
BPPearson | 0:b6f6b0483eff | 3 | #define COMMAND 0 |
BPPearson | 0:b6f6b0483eff | 4 | #define DATA 1 |
BPPearson | 0:b6f6b0483eff | 5 | |
BPPearson | 0:b6f6b0483eff | 6 | Serial pc(USBTX, USBRX); |
BPPearson | 0:b6f6b0483eff | 7 | Serial birefSensor(p9,p10); |
BPPearson | 0:b6f6b0483eff | 8 | DigitalOut led1(LED1); |
BPPearson | 0:b6f6b0483eff | 9 | DigitalOut led2(LED2); |
BPPearson | 0:b6f6b0483eff | 10 | DigitalOut led3(LED3); |
BPPearson | 0:b6f6b0483eff | 11 | DigitalOut led4(LED4); |
BPPearson | 0:b6f6b0483eff | 12 | AnalogOut birefOut(p18); |
BPPearson | 0:b6f6b0483eff | 13 | BusInOut databus(p21, p22, p23, p24, p25, p26, p27, p28); |
BPPearson | 0:b6f6b0483eff | 14 | DigitalOut registerSelect(p30); |
BPPearson | 0:b6f6b0483eff | 15 | DigitalOut readWriteClock(p29); |
BPPearson | 0:b6f6b0483eff | 16 | Ticker updateLCD; |
BPPearson | 0:b6f6b0483eff | 17 | |
BPPearson | 0:b6f6b0483eff | 18 | |
BPPearson | 0:b6f6b0483eff | 19 | char rcvBuff[80]; |
BPPearson | 0:b6f6b0483eff | 20 | int buffIdx = 0; |
BPPearson | 0:b6f6b0483eff | 21 | float birefVal, birefStdDev; |
BPPearson | 0:b6f6b0483eff | 22 | |
BPPearson | 0:b6f6b0483eff | 23 | void writeToLCD(bool rs, char data){ |
BPPearson | 0:b6f6b0483eff | 24 | |
BPPearson | 0:b6f6b0483eff | 25 | // set register select pin |
BPPearson | 0:b6f6b0483eff | 26 | registerSelect = rs; |
BPPearson | 0:b6f6b0483eff | 27 | |
BPPearson | 0:b6f6b0483eff | 28 | // set read/write pin to write |
BPPearson | 0:b6f6b0483eff | 29 | //readWrite = 0; |
BPPearson | 0:b6f6b0483eff | 30 | |
BPPearson | 0:b6f6b0483eff | 31 | // set bus as output |
BPPearson | 0:b6f6b0483eff | 32 | databus.output(); |
BPPearson | 0:b6f6b0483eff | 33 | |
BPPearson | 0:b6f6b0483eff | 34 | // put data onto bus |
BPPearson | 0:b6f6b0483eff | 35 | databus = data; |
BPPearson | 0:b6f6b0483eff | 36 | |
BPPearson | 0:b6f6b0483eff | 37 | // pulse read/write clock |
BPPearson | 0:b6f6b0483eff | 38 | readWriteClock = 1; |
BPPearson | 0:b6f6b0483eff | 39 | |
BPPearson | 0:b6f6b0483eff | 40 | wait_us(1); |
BPPearson | 0:b6f6b0483eff | 41 | |
BPPearson | 0:b6f6b0483eff | 42 | readWriteClock = 0; |
BPPearson | 0:b6f6b0483eff | 43 | |
BPPearson | 0:b6f6b0483eff | 44 | wait_us(1); |
BPPearson | 0:b6f6b0483eff | 45 | |
BPPearson | 0:b6f6b0483eff | 46 | // clear data bus |
BPPearson | 0:b6f6b0483eff | 47 | databus = 0; |
BPPearson | 0:b6f6b0483eff | 48 | |
BPPearson | 0:b6f6b0483eff | 49 | //pc.printf("%02x\n", data); |
BPPearson | 0:b6f6b0483eff | 50 | |
BPPearson | 0:b6f6b0483eff | 51 | } |
BPPearson | 0:b6f6b0483eff | 52 | |
BPPearson | 0:b6f6b0483eff | 53 | |
BPPearson | 0:b6f6b0483eff | 54 | char readFromLCD(bool rs){ |
BPPearson | 0:b6f6b0483eff | 55 | |
BPPearson | 0:b6f6b0483eff | 56 | char data; |
BPPearson | 0:b6f6b0483eff | 57 | |
BPPearson | 0:b6f6b0483eff | 58 | // set register select pin |
BPPearson | 0:b6f6b0483eff | 59 | registerSelect = rs; |
BPPearson | 0:b6f6b0483eff | 60 | |
BPPearson | 0:b6f6b0483eff | 61 | // set read/write pin to read |
BPPearson | 0:b6f6b0483eff | 62 | //readWrite = 1; |
BPPearson | 0:b6f6b0483eff | 63 | |
BPPearson | 0:b6f6b0483eff | 64 | // set bus as output |
BPPearson | 0:b6f6b0483eff | 65 | databus.input(); |
BPPearson | 0:b6f6b0483eff | 66 | |
BPPearson | 0:b6f6b0483eff | 67 | // put data onto bus |
BPPearson | 0:b6f6b0483eff | 68 | data = databus; |
BPPearson | 0:b6f6b0483eff | 69 | |
BPPearson | 0:b6f6b0483eff | 70 | // pulse read/write clock |
BPPearson | 0:b6f6b0483eff | 71 | readWriteClock = 1; |
BPPearson | 0:b6f6b0483eff | 72 | |
BPPearson | 0:b6f6b0483eff | 73 | wait_us(10); |
BPPearson | 0:b6f6b0483eff | 74 | |
BPPearson | 0:b6f6b0483eff | 75 | readWriteClock = 0; |
BPPearson | 0:b6f6b0483eff | 76 | |
BPPearson | 0:b6f6b0483eff | 77 | return data; |
BPPearson | 0:b6f6b0483eff | 78 | } |
BPPearson | 0:b6f6b0483eff | 79 | |
BPPearson | 0:b6f6b0483eff | 80 | |
BPPearson | 0:b6f6b0483eff | 81 | void resetLCD(){ |
BPPearson | 0:b6f6b0483eff | 82 | } |
BPPearson | 0:b6f6b0483eff | 83 | |
BPPearson | 0:b6f6b0483eff | 84 | |
BPPearson | 0:b6f6b0483eff | 85 | void initLCD(){ |
BPPearson | 0:b6f6b0483eff | 86 | |
BPPearson | 0:b6f6b0483eff | 87 | // wait 15 ms to allow LCD to initialise |
BPPearson | 0:b6f6b0483eff | 88 | wait_ms(15); |
BPPearson | 0:b6f6b0483eff | 89 | |
BPPearson | 0:b6f6b0483eff | 90 | // set interface for 8 bit mode |
BPPearson | 0:b6f6b0483eff | 91 | writeToLCD(COMMAND, 0x30); |
BPPearson | 0:b6f6b0483eff | 92 | |
BPPearson | 0:b6f6b0483eff | 93 | // give it time |
BPPearson | 0:b6f6b0483eff | 94 | wait_ms(5); |
BPPearson | 0:b6f6b0483eff | 95 | |
BPPearson | 0:b6f6b0483eff | 96 | // set interface for 8 bit mode again |
BPPearson | 0:b6f6b0483eff | 97 | writeToLCD(COMMAND, 0x30); |
BPPearson | 0:b6f6b0483eff | 98 | |
BPPearson | 0:b6f6b0483eff | 99 | // give it time |
BPPearson | 0:b6f6b0483eff | 100 | wait_us(100); |
BPPearson | 0:b6f6b0483eff | 101 | |
BPPearson | 0:b6f6b0483eff | 102 | // set interface for 8 bit mode again, last one before we can configure the display |
BPPearson | 0:b6f6b0483eff | 103 | writeToLCD(COMMAND, 0x30); |
BPPearson | 0:b6f6b0483eff | 104 | |
BPPearson | 0:b6f6b0483eff | 105 | // give it time |
BPPearson | 0:b6f6b0483eff | 106 | wait_us(500); |
BPPearson | 0:b6f6b0483eff | 107 | |
BPPearson | 0:b6f6b0483eff | 108 | // set interface for 8 bit mode, 2 display lines and 5 x 8 character font |
BPPearson | 0:b6f6b0483eff | 109 | writeToLCD(COMMAND, 0x38); |
BPPearson | 0:b6f6b0483eff | 110 | |
BPPearson | 0:b6f6b0483eff | 111 | // give it time |
BPPearson | 0:b6f6b0483eff | 112 | wait_us(100); |
BPPearson | 0:b6f6b0483eff | 113 | |
BPPearson | 0:b6f6b0483eff | 114 | // display off |
BPPearson | 0:b6f6b0483eff | 115 | writeToLCD(COMMAND, 0x08); |
BPPearson | 0:b6f6b0483eff | 116 | |
BPPearson | 0:b6f6b0483eff | 117 | // give it time |
BPPearson | 0:b6f6b0483eff | 118 | wait_us(100); |
BPPearson | 0:b6f6b0483eff | 119 | |
BPPearson | 0:b6f6b0483eff | 120 | // clear the screen |
BPPearson | 0:b6f6b0483eff | 121 | writeToLCD(COMMAND, 0x01); |
BPPearson | 0:b6f6b0483eff | 122 | |
BPPearson | 0:b6f6b0483eff | 123 | // give it time to finish |
BPPearson | 0:b6f6b0483eff | 124 | wait_ms(2); |
BPPearson | 0:b6f6b0483eff | 125 | |
BPPearson | 0:b6f6b0483eff | 126 | // set entry mode to increment cursor position cursor on write |
BPPearson | 0:b6f6b0483eff | 127 | writeToLCD(COMMAND, 0x03); |
BPPearson | 0:b6f6b0483eff | 128 | |
BPPearson | 0:b6f6b0483eff | 129 | // give it time to finish |
BPPearson | 0:b6f6b0483eff | 130 | wait_us(100); |
BPPearson | 0:b6f6b0483eff | 131 | |
BPPearson | 0:b6f6b0483eff | 132 | // position cursor at home |
BPPearson | 0:b6f6b0483eff | 133 | writeToLCD(COMMAND, 0x02); |
BPPearson | 0:b6f6b0483eff | 134 | |
BPPearson | 0:b6f6b0483eff | 135 | // give it time to finish |
BPPearson | 0:b6f6b0483eff | 136 | wait_ms(2); |
BPPearson | 0:b6f6b0483eff | 137 | |
BPPearson | 0:b6f6b0483eff | 138 | // display on |
BPPearson | 0:b6f6b0483eff | 139 | writeToLCD(COMMAND, 0x0C); |
BPPearson | 0:b6f6b0483eff | 140 | } |
BPPearson | 0:b6f6b0483eff | 141 | |
BPPearson | 0:b6f6b0483eff | 142 | |
BPPearson | 0:b6f6b0483eff | 143 | |
BPPearson | 0:b6f6b0483eff | 144 | |
BPPearson | 0:b6f6b0483eff | 145 | void positionCursor(uint8_t x, uint8_t y){ |
BPPearson | 0:b6f6b0483eff | 146 | |
BPPearson | 0:b6f6b0483eff | 147 | if (x > 7) x = 0; |
BPPearson | 0:b6f6b0483eff | 148 | |
BPPearson | 0:b6f6b0483eff | 149 | if (y == 1) |
BPPearson | 0:b6f6b0483eff | 150 | writeToLCD(COMMAND, 0x80 + 0x40 + x); |
BPPearson | 0:b6f6b0483eff | 151 | else |
BPPearson | 0:b6f6b0483eff | 152 | writeToLCD(COMMAND, 0x80 + 0x00 + x); |
BPPearson | 0:b6f6b0483eff | 153 | |
BPPearson | 0:b6f6b0483eff | 154 | wait_us(50); |
BPPearson | 0:b6f6b0483eff | 155 | } |
BPPearson | 0:b6f6b0483eff | 156 | |
BPPearson | 0:b6f6b0483eff | 157 | |
BPPearson | 0:b6f6b0483eff | 158 | void displayString(int x, int y, char *str){ |
BPPearson | 0:b6f6b0483eff | 159 | |
BPPearson | 0:b6f6b0483eff | 160 | // position cursor |
BPPearson | 0:b6f6b0483eff | 161 | positionCursor(x, y); |
BPPearson | 0:b6f6b0483eff | 162 | |
BPPearson | 0:b6f6b0483eff | 163 | // write string to screen |
BPPearson | 0:b6f6b0483eff | 164 | for (int i=0; i<strlen(str); i++){ |
BPPearson | 0:b6f6b0483eff | 165 | writeToLCD(DATA, str[i]); |
BPPearson | 0:b6f6b0483eff | 166 | |
BPPearson | 0:b6f6b0483eff | 167 | wait_us(50); |
BPPearson | 0:b6f6b0483eff | 168 | } |
BPPearson | 0:b6f6b0483eff | 169 | } |
BPPearson | 0:b6f6b0483eff | 170 | |
BPPearson | 0:b6f6b0483eff | 171 | |
BPPearson | 0:b6f6b0483eff | 172 | |
BPPearson | 0:b6f6b0483eff | 173 | void updateDisplay(){ |
BPPearson | 0:b6f6b0483eff | 174 | char str[20]; |
BPPearson | 0:b6f6b0483eff | 175 | |
BPPearson | 0:b6f6b0483eff | 176 | sprintf( str, "b %5.3f", birefVal); |
BPPearson | 0:b6f6b0483eff | 177 | |
BPPearson | 0:b6f6b0483eff | 178 | displayString(0, 0, str); |
BPPearson | 0:b6f6b0483eff | 179 | |
BPPearson | 0:b6f6b0483eff | 180 | if (birefStdDev <= 0.0) |
BPPearson | 0:b6f6b0483eff | 181 | birefStdDev = 0.0001; |
BPPearson | 0:b6f6b0483eff | 182 | |
BPPearson | 0:b6f6b0483eff | 183 | sprintf( str, "s %5.3f", birefStdDev); |
BPPearson | 0:b6f6b0483eff | 184 | |
BPPearson | 0:b6f6b0483eff | 185 | displayString(0, 1, str); |
BPPearson | 0:b6f6b0483eff | 186 | } |
BPPearson | 0:b6f6b0483eff | 187 | |
BPPearson | 0:b6f6b0483eff | 188 | |
BPPearson | 0:b6f6b0483eff | 189 | void rcvCharISR() |
BPPearson | 0:b6f6b0483eff | 190 | { |
BPPearson | 0:b6f6b0483eff | 191 | float tempf; |
BPPearson | 0:b6f6b0483eff | 192 | char chr = birefSensor.getc(); |
BPPearson | 0:b6f6b0483eff | 193 | |
BPPearson | 0:b6f6b0483eff | 194 | //led1 = !led1; |
BPPearson | 0:b6f6b0483eff | 195 | |
BPPearson | 0:b6f6b0483eff | 196 | //pc.printf("%c (%02x)\n", chr, chr); |
BPPearson | 0:b6f6b0483eff | 197 | |
BPPearson | 0:b6f6b0483eff | 198 | rcvBuff[buffIdx++] = chr; |
BPPearson | 0:b6f6b0483eff | 199 | |
BPPearson | 0:b6f6b0483eff | 200 | if (buffIdx >= 80) |
BPPearson | 0:b6f6b0483eff | 201 | { |
BPPearson | 0:b6f6b0483eff | 202 | buffIdx = 0; |
BPPearson | 0:b6f6b0483eff | 203 | } |
BPPearson | 0:b6f6b0483eff | 204 | |
BPPearson | 0:b6f6b0483eff | 205 | if (chr == '\n') |
BPPearson | 0:b6f6b0483eff | 206 | { |
BPPearson | 0:b6f6b0483eff | 207 | rcvBuff[buffIdx] = '\0'; |
BPPearson | 0:b6f6b0483eff | 208 | |
BPPearson | 0:b6f6b0483eff | 209 | switch (rcvBuff[0]){ |
BPPearson | 0:b6f6b0483eff | 210 | case 'm': |
BPPearson | 0:b6f6b0483eff | 211 | sscanf(rcvBuff, "m%f %f\n", &birefVal, &birefStdDev); |
BPPearson | 0:b6f6b0483eff | 212 | birefOut = birefVal; |
BPPearson | 0:b6f6b0483eff | 213 | break; |
BPPearson | 0:b6f6b0483eff | 214 | case 'd': |
BPPearson | 0:b6f6b0483eff | 215 | sscanf(rcvBuff, "d%f %f %f %f %f %f \n", &tempf, &tempf, &tempf, &tempf, &birefVal, &birefStdDev); |
BPPearson | 0:b6f6b0483eff | 216 | birefOut = birefVal; |
BPPearson | 0:b6f6b0483eff | 217 | break; |
BPPearson | 0:b6f6b0483eff | 218 | default: ; |
BPPearson | 0:b6f6b0483eff | 219 | } |
BPPearson | 0:b6f6b0483eff | 220 | buffIdx = 0; |
BPPearson | 0:b6f6b0483eff | 221 | } |
BPPearson | 0:b6f6b0483eff | 222 | } |
BPPearson | 0:b6f6b0483eff | 223 | |
BPPearson | 0:b6f6b0483eff | 224 | |
BPPearson | 0:b6f6b0483eff | 225 | |
BPPearson | 0:b6f6b0483eff | 226 | int main() { |
BPPearson | 0:b6f6b0483eff | 227 | |
BPPearson | 0:b6f6b0483eff | 228 | initLCD(); |
BPPearson | 0:b6f6b0483eff | 229 | |
BPPearson | 0:b6f6b0483eff | 230 | birefSensor.baud(115200); |
BPPearson | 0:b6f6b0483eff | 231 | |
BPPearson | 0:b6f6b0483eff | 232 | pc.printf("Hello\n"); |
BPPearson | 0:b6f6b0483eff | 233 | |
BPPearson | 0:b6f6b0483eff | 234 | updateLCD.attach(&updateDisplay, 0.5); |
BPPearson | 0:b6f6b0483eff | 235 | |
BPPearson | 0:b6f6b0483eff | 236 | birefSensor.attach(&rcvCharISR); |
BPPearson | 0:b6f6b0483eff | 237 | |
BPPearson | 0:b6f6b0483eff | 238 | while(1) { |
BPPearson | 0:b6f6b0483eff | 239 | |
BPPearson | 0:b6f6b0483eff | 240 | wait(0.02); |
BPPearson | 0:b6f6b0483eff | 241 | //wait(1.0); |
BPPearson | 0:b6f6b0483eff | 242 | } |
BPPearson | 0:b6f6b0483eff | 243 | } |