Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MCP23S17 PCF8574 TextLCD eeprom mbed-dev
main.cpp@2:4a8eacbb3657, 2016-08-12 (annotated)
- Committer:
- jolyon
- Date:
- Fri Aug 12 11:01:07 2016 +0000
- Revision:
- 2:4a8eacbb3657
- Parent:
- 1:d99dad8a1f50
- Child:
- 3:b8218b61b0e2
working breadboard code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jolyon | 0:9d301ae51ecf | 1 | #include "mbed.h" |
| jolyon | 0:9d301ae51ecf | 2 | #include "TextLCD.h" |
| jolyon | 2:4a8eacbb3657 | 3 | #include "MCP23S17.h" |
| jolyon | 0:9d301ae51ecf | 4 | |
| jolyon | 0:9d301ae51ecf | 5 | #define FACTOR 1 |
| jolyon | 0:9d301ae51ecf | 6 | |
| jolyon | 0:9d301ae51ecf | 7 | #define SENSOR_ADDR (41 << 1) |
| jolyon | 1:d99dad8a1f50 | 8 | #define SENSOR_WHITE_REG 148 |
| jolyon | 1:d99dad8a1f50 | 9 | #define SENSOR_RED_REG 150 |
| jolyon | 1:d99dad8a1f50 | 10 | #define SENSOR_GREEN_REG 152 |
| jolyon | 1:d99dad8a1f50 | 11 | #define SENSOR_BLUE_REG 154 |
| jolyon | 1:d99dad8a1f50 | 12 | |
| jolyon | 1:d99dad8a1f50 | 13 | #define WHITE_THRESHOLD 20 |
| jolyon | 1:d99dad8a1f50 | 14 | |
| jolyon | 1:d99dad8a1f50 | 15 | |
| jolyon | 1:d99dad8a1f50 | 16 | |
| jolyon | 0:9d301ae51ecf | 17 | |
| jolyon | 0:9d301ae51ecf | 18 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 19 | //----------- LED's ------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 20 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 21 | // Detection LED's use the onboard MBED LED's |
| jolyon | 0:9d301ae51ecf | 22 | // These are all blue on the MBED but on DSE PCB they areas follows |
| jolyon | 0:9d301ae51ecf | 23 | // LED1 = GREEN |
| jolyon | 0:9d301ae51ecf | 24 | // LED2 = BLUE |
| jolyon | 0:9d301ae51ecf | 25 | // LED3 = RED |
| jolyon | 0:9d301ae51ecf | 26 | // LED4 = YELLOW |
| jolyon | 0:9d301ae51ecf | 27 | DigitalOut GreenLed(LED1); |
| jolyon | 0:9d301ae51ecf | 28 | DigitalOut BlueLed(LED2); |
| jolyon | 0:9d301ae51ecf | 29 | DigitalOut RedLed(LED3); |
| jolyon | 0:9d301ae51ecf | 30 | DigitalOut YellowLed(LED4); |
| jolyon | 0:9d301ae51ecf | 31 | |
| jolyon | 0:9d301ae51ecf | 32 | //DigitalOut myled(LED1); |
| jolyon | 0:9d301ae51ecf | 33 | //DigitalOut myGreen(p21); |
| jolyon | 0:9d301ae51ecf | 34 | //DigitalOut myYellow(p22); |
| jolyon | 0:9d301ae51ecf | 35 | //DigitalOut myled4(LED4); |
| jolyon | 0:9d301ae51ecf | 36 | //DigitalOut green(LED1); |
| jolyon | 0:9d301ae51ecf | 37 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 38 | |
| jolyon | 0:9d301ae51ecf | 39 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 40 | //----------- Analogue Inputs --------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 41 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 42 | //Analogue inputs are used to set thresholds for detector levels |
| jolyon | 2:4a8eacbb3657 | 43 | //AnalogIn RedTrimmer(p15); |
| jolyon | 2:4a8eacbb3657 | 44 | //AnalogIn GreenTrimmer(p16); |
| jolyon | 2:4a8eacbb3657 | 45 | //AnalogIn BlueTrimmer(p17); |
| jolyon | 2:4a8eacbb3657 | 46 | //AnalogIn HysTrimmer(p18); |
| jolyon | 0:9d301ae51ecf | 47 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 48 | |
| jolyon | 0:9d301ae51ecf | 49 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 50 | //------------ Text display ----------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 51 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 52 | //debug display. Standard display driver set up in 4 bit mode |
| jolyon | 0:9d301ae51ecf | 53 | //final version uses I2C port |
| jolyon | 0:9d301ae51ecf | 54 | TextLCD lcd(p14, p16, p17, p18, p19, p20, TextLCD::LCD16x2); // rs, e, d4-d7 |
| jolyon | 0:9d301ae51ecf | 55 | |
| jolyon | 0:9d301ae51ecf | 56 | //set up I2C Communication to LCD |
| jolyon | 0:9d301ae51ecf | 57 | //I2C i2c_lcd(p9,p10); // SDA, SCL |
| jolyon | 0:9d301ae51ecf | 58 | |
| jolyon | 0:9d301ae51ecf | 59 | //I2C Portexpander PCF8574 for LCD |
| jolyon | 0:9d301ae51ecf | 60 | //TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD16x2); // I2C bus, PCF8574 Slaveaddress, LCD Type ok |
| jolyon | 0:9d301ae51ecf | 61 | |
| jolyon | 0:9d301ae51ecf | 62 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 63 | |
| jolyon | 2:4a8eacbb3657 | 64 | //------------------------------------------------------------------------------- |
| jolyon | 2:4a8eacbb3657 | 65 | //------------ Create SPI bus ----------------------------------------------------- |
| jolyon | 2:4a8eacbb3657 | 66 | //------------------------------------------------------------------------------- |
| jolyon | 2:4a8eacbb3657 | 67 | //set up the SPI port for use as output driver |
| jolyon | 2:4a8eacbb3657 | 68 | SPI spi(p5, p6, p7); |
| jolyon | 2:4a8eacbb3657 | 69 | |
| jolyon | 2:4a8eacbb3657 | 70 | char Opcode = 0x40; |
| jolyon | 2:4a8eacbb3657 | 71 | |
| jolyon | 2:4a8eacbb3657 | 72 | // Next create a MCP23S17 |
| jolyon | 2:4a8eacbb3657 | 73 | // mbed p20 is connected to ~chipSelect on the MCP23S17 |
| jolyon | 2:4a8eacbb3657 | 74 | MCP23S17 chip = MCP23S17(spi, p20, Opcode); |
| jolyon | 2:4a8eacbb3657 | 75 | |
| jolyon | 2:4a8eacbb3657 | 76 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 77 | |
| jolyon | 0:9d301ae51ecf | 78 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 79 | //------------ colour sensor Comms Pins ----------------------------------------- |
| jolyon | 0:9d301ae51ecf | 80 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 81 | //TMC Sensor: this is the raw I2C Port, the clock like is muxed |
| jolyon | 0:9d301ae51ecf | 82 | I2C i2c(p28, p27); //pins for I2C communication (SDA, SCL) |
| jolyon | 0:9d301ae51ecf | 83 | |
| jolyon | 0:9d301ae51ecf | 84 | |
| jolyon | 0:9d301ae51ecf | 85 | typedef struct{ |
| jolyon | 0:9d301ae51ecf | 86 | uint16_t White; |
| jolyon | 0:9d301ae51ecf | 87 | uint16_t Red; |
| jolyon | 0:9d301ae51ecf | 88 | uint16_t Green; |
| jolyon | 0:9d301ae51ecf | 89 | uint16_t Blue; |
| jolyon | 0:9d301ae51ecf | 90 | }Colour; |
| jolyon | 0:9d301ae51ecf | 91 | |
| jolyon | 0:9d301ae51ecf | 92 | Colour SensorData; |
| jolyon | 0:9d301ae51ecf | 93 | |
| jolyon | 0:9d301ae51ecf | 94 | //------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 95 | |
| jolyon | 0:9d301ae51ecf | 96 | //------------TSC Stuff---------------- |
| jolyon | 0:9d301ae51ecf | 97 | int sensor_addr = 41 << 1; |
| jolyon | 0:9d301ae51ecf | 98 | |
| jolyon | 0:9d301ae51ecf | 99 | char TempReg[1] = {0}; |
| jolyon | 0:9d301ae51ecf | 100 | char TempData[2] = {0,0}; |
| jolyon | 0:9d301ae51ecf | 101 | |
| jolyon | 0:9d301ae51ecf | 102 | |
| jolyon | 0:9d301ae51ecf | 103 | //------------TSC Stuff---------------- |
| jolyon | 0:9d301ae51ecf | 104 | |
| jolyon | 0:9d301ae51ecf | 105 | uint16_t Red = 1; |
| jolyon | 0:9d301ae51ecf | 106 | uint16_t Green = 2; |
| jolyon | 0:9d301ae51ecf | 107 | uint16_t Blue = 3; |
| jolyon | 0:9d301ae51ecf | 108 | uint16_t White = 4; |
| jolyon | 1:d99dad8a1f50 | 109 | uint16_t Yellow = 0; |
| jolyon | 0:9d301ae51ecf | 110 | |
| jolyon | 0:9d301ae51ecf | 111 | uint32_t AvgRed = 0; |
| jolyon | 0:9d301ae51ecf | 112 | uint32_t AvgGreen = 0; |
| jolyon | 0:9d301ae51ecf | 113 | uint32_t AvgBlue = 0; |
| jolyon | 0:9d301ae51ecf | 114 | uint32_t AvgWhite = 0; |
| jolyon | 0:9d301ae51ecf | 115 | uint32_t Temp = 0; |
| jolyon | 0:9d301ae51ecf | 116 | uint32_t TempR = 0; |
| jolyon | 0:9d301ae51ecf | 117 | uint32_t TempG = 0; |
| jolyon | 0:9d301ae51ecf | 118 | uint32_t TempB = 0; |
| jolyon | 0:9d301ae51ecf | 119 | |
| jolyon | 0:9d301ae51ecf | 120 | |
| jolyon | 0:9d301ae51ecf | 121 | uint16_t RedProp = 0; |
| jolyon | 0:9d301ae51ecf | 122 | uint16_t GreenProp = 0; |
| jolyon | 0:9d301ae51ecf | 123 | uint16_t BlueProp = 0; |
| jolyon | 0:9d301ae51ecf | 124 | |
| jolyon | 0:9d301ae51ecf | 125 | uint16_t myMix = 0; |
| jolyon | 0:9d301ae51ecf | 126 | |
| jolyon | 0:9d301ae51ecf | 127 | float MyCCT; |
| jolyon | 0:9d301ae51ecf | 128 | float MyCCTi; |
| jolyon | 0:9d301ae51ecf | 129 | uint8_t DisplayIndex = 0; |
| jolyon | 0:9d301ae51ecf | 130 | |
| jolyon | 0:9d301ae51ecf | 131 | uint8_t Config = 0x30; |
| jolyon | 0:9d301ae51ecf | 132 | // bit description setting |
| jolyon | 0:9d301ae51ecf | 133 | // 0 SD, 0 = enabled 0 |
| jolyon | 0:9d301ae51ecf | 134 | // 1 AF, 0 = Automode 0 |
| jolyon | 0:9d301ae51ecf | 135 | // 2 TRIG, 0 = no trigrer 0 |
| jolyon | 0:9d301ae51ecf | 136 | // 3 N/F, 0 0 |
| jolyon | 0:9d301ae51ecf | 137 | // 4 IT0 0 0 |
| jolyon | 0:9d301ae51ecf | 138 | // 5 IT1 1 = 160ms 1 |
| jolyon | 0:9d301ae51ecf | 139 | // 6 IT2 0 0 |
| jolyon | 0:9d301ae51ecf | 140 | // 7 N/F 0 0 |
| jolyon | 0:9d301ae51ecf | 141 | |
| jolyon | 0:9d301ae51ecf | 142 | |
| jolyon | 2:4a8eacbb3657 | 143 | uint16_t MyPort = 1; |
| jolyon | 2:4a8eacbb3657 | 144 | |
| jolyon | 2:4a8eacbb3657 | 145 | //working thresholds from pot inputs, scaled 0-100 |
| jolyon | 2:4a8eacbb3657 | 146 | uint16_t RedThreshold = 0; |
| jolyon | 2:4a8eacbb3657 | 147 | uint16_t GreenThreshold = 0; |
| jolyon | 2:4a8eacbb3657 | 148 | uint16_t BlueThreshold = 0; |
| jolyon | 2:4a8eacbb3657 | 149 | uint16_t Hysteresis = 0; |
| jolyon | 2:4a8eacbb3657 | 150 | |
| jolyon | 2:4a8eacbb3657 | 151 | |
| jolyon | 0:9d301ae51ecf | 152 | uint16_t Filter(uint32_t *Acc,uint16_t NewData, uint8_t factor) |
| jolyon | 0:9d301ae51ecf | 153 | { |
| jolyon | 0:9d301ae51ecf | 154 | uint32_t Temp = 0; |
| jolyon | 0:9d301ae51ecf | 155 | Temp = *Acc >> factor ; |
| jolyon | 0:9d301ae51ecf | 156 | Temp = *Acc - Temp + NewData; |
| jolyon | 0:9d301ae51ecf | 157 | *Acc = Temp; |
| jolyon | 0:9d301ae51ecf | 158 | return *Acc >> factor; |
| jolyon | 0:9d301ae51ecf | 159 | } |
| jolyon | 0:9d301ae51ecf | 160 | |
| jolyon | 0:9d301ae51ecf | 161 | |
| jolyon | 0:9d301ae51ecf | 162 | void ConfigureTSC(uint8_t Address) |
| jolyon | 0:9d301ae51ecf | 163 | { |
| jolyon | 0:9d301ae51ecf | 164 | I2C i2c(p28, p27); //pins for I2C communication (SDA, SCL) |
| jolyon | 0:9d301ae51ecf | 165 | i2c.frequency(200000); |
| jolyon | 0:9d301ae51ecf | 166 | |
| jolyon | 0:9d301ae51ecf | 167 | |
| jolyon | 0:9d301ae51ecf | 168 | |
| jolyon | 0:9d301ae51ecf | 169 | char id_regval[1] = {146}; |
| jolyon | 0:9d301ae51ecf | 170 | char data[1] = {0}; |
| jolyon | 0:9d301ae51ecf | 171 | i2c.write(sensor_addr,id_regval,1, true); |
| jolyon | 0:9d301ae51ecf | 172 | i2c.read(sensor_addr,data,1,false); |
| jolyon | 0:9d301ae51ecf | 173 | |
| jolyon | 0:9d301ae51ecf | 174 | if (data[0]==68) { |
| jolyon | 0:9d301ae51ecf | 175 | GreenLed = 0; |
| jolyon | 0:9d301ae51ecf | 176 | wait (2); |
| jolyon | 0:9d301ae51ecf | 177 | GreenLed = 1; |
| jolyon | 0:9d301ae51ecf | 178 | } else { |
| jolyon | 0:9d301ae51ecf | 179 | GreenLed = 1; |
| jolyon | 0:9d301ae51ecf | 180 | } |
| jolyon | 0:9d301ae51ecf | 181 | |
| jolyon | 0:9d301ae51ecf | 182 | // Initialize color sensor |
| jolyon | 0:9d301ae51ecf | 183 | |
| jolyon | 0:9d301ae51ecf | 184 | char enable_register[2] = {128,3}; //enable the sensor |
| jolyon | 0:9d301ae51ecf | 185 | i2c.write(sensor_addr,enable_register,2,false); |
| jolyon | 0:9d301ae51ecf | 186 | |
| jolyon | 0:9d301ae51ecf | 187 | char timing_register[2] = {0x81,0xC0}; //approx 100ms |
| jolyon | 0:9d301ae51ecf | 188 | i2c.write(sensor_addr,timing_register,2,false); |
| jolyon | 0:9d301ae51ecf | 189 | |
| jolyon | 0:9d301ae51ecf | 190 | char control_register[2] = {0x8F,2}; //gain = x16 |
| jolyon | 0:9d301ae51ecf | 191 | i2c.write(sensor_addr,control_register,2,false); |
| jolyon | 0:9d301ae51ecf | 192 | |
| jolyon | 0:9d301ae51ecf | 193 | |
| jolyon | 0:9d301ae51ecf | 194 | |
| jolyon | 0:9d301ae51ecf | 195 | // Read data from color sensor (Clear/Red/Green/Blue) |
| jolyon | 0:9d301ae51ecf | 196 | //led = 1; |
| jolyon | 0:9d301ae51ecf | 197 | |
| jolyon | 0:9d301ae51ecf | 198 | } |
| jolyon | 0:9d301ae51ecf | 199 | |
| jolyon | 1:d99dad8a1f50 | 200 | Colour GetSensorData(char SensorNumber) |
| jolyon | 1:d99dad8a1f50 | 201 | { |
| jolyon | 1:d99dad8a1f50 | 202 | Colour NewData; |
| jolyon | 1:d99dad8a1f50 | 203 | char AddrReg[1] = {0}; |
| jolyon | 1:d99dad8a1f50 | 204 | char DataReg[5] ={0,0,0,0,0}; |
| jolyon | 1:d99dad8a1f50 | 205 | |
| jolyon | 1:d99dad8a1f50 | 206 | NewData.White = 0; |
| jolyon | 1:d99dad8a1f50 | 207 | NewData.Red = 0; |
| jolyon | 1:d99dad8a1f50 | 208 | NewData.Green = 0; |
| jolyon | 1:d99dad8a1f50 | 209 | NewData.Blue = 0; |
| jolyon | 1:d99dad8a1f50 | 210 | |
| jolyon | 1:d99dad8a1f50 | 211 | AddrReg[0] = SENSOR_WHITE_REG; |
| jolyon | 1:d99dad8a1f50 | 212 | i2c.write(SENSOR_ADDR, AddrReg,1, true); |
| jolyon | 1:d99dad8a1f50 | 213 | i2c.read(SENSOR_ADDR,DataReg,2, false); |
| jolyon | 1:d99dad8a1f50 | 214 | NewData.White = ((int)DataReg[1] << 8) | DataReg[0]; |
| jolyon | 1:d99dad8a1f50 | 215 | |
| jolyon | 1:d99dad8a1f50 | 216 | AddrReg[0] = SENSOR_RED_REG; |
| jolyon | 1:d99dad8a1f50 | 217 | i2c.write(SENSOR_ADDR, AddrReg,1, true); |
| jolyon | 1:d99dad8a1f50 | 218 | i2c.read(SENSOR_ADDR,DataReg,2, false); |
| jolyon | 1:d99dad8a1f50 | 219 | NewData.Red = ((int)DataReg[1] << 8) | DataReg[0]; |
| jolyon | 1:d99dad8a1f50 | 220 | |
| jolyon | 1:d99dad8a1f50 | 221 | AddrReg[0] = SENSOR_GREEN_REG; |
| jolyon | 1:d99dad8a1f50 | 222 | i2c.write(SENSOR_ADDR, AddrReg,1, true); |
| jolyon | 1:d99dad8a1f50 | 223 | i2c.read(SENSOR_ADDR,DataReg,2, false); |
| jolyon | 1:d99dad8a1f50 | 224 | NewData.Green = ((int)DataReg[1] << 8) | DataReg[0]; |
| jolyon | 1:d99dad8a1f50 | 225 | |
| jolyon | 1:d99dad8a1f50 | 226 | AddrReg[0] = SENSOR_BLUE_REG; |
| jolyon | 1:d99dad8a1f50 | 227 | i2c.write(SENSOR_ADDR, AddrReg,1, true); |
| jolyon | 1:d99dad8a1f50 | 228 | i2c.read(SENSOR_ADDR,DataReg,2, false); |
| jolyon | 1:d99dad8a1f50 | 229 | NewData.Blue = ((int)DataReg[1] << 8) | DataReg[0]; |
| jolyon | 1:d99dad8a1f50 | 230 | |
| jolyon | 1:d99dad8a1f50 | 231 | return NewData; |
| jolyon | 1:d99dad8a1f50 | 232 | } |
| jolyon | 1:d99dad8a1f50 | 233 | |
| jolyon | 2:4a8eacbb3657 | 234 | void InitOutputs(void) |
| jolyon | 2:4a8eacbb3657 | 235 | { |
| jolyon | 2:4a8eacbb3657 | 236 | // Set all 8 Port A bits to output direction |
| jolyon | 2:4a8eacbb3657 | 237 | chip.direction(PORT_A, 0x00); |
| jolyon | 2:4a8eacbb3657 | 238 | // Set all 8 Port B bits to output direction |
| jolyon | 2:4a8eacbb3657 | 239 | chip.direction(PORT_B, 0x00); |
| jolyon | 2:4a8eacbb3657 | 240 | } |
| jolyon | 0:9d301ae51ecf | 241 | |
| jolyon | 2:4a8eacbb3657 | 242 | void SetOutputBit(char bit) |
| jolyon | 2:4a8eacbb3657 | 243 | { |
| jolyon | 2:4a8eacbb3657 | 244 | uint16_t OutputWord = 1; |
| jolyon | 2:4a8eacbb3657 | 245 | char BitA, BitB, PortA, PortB; |
| jolyon | 2:4a8eacbb3657 | 246 | |
| jolyon | 2:4a8eacbb3657 | 247 | OutputWord <<= bit; |
| jolyon | 2:4a8eacbb3657 | 248 | BitA = OutputWord & 0xFF; |
| jolyon | 2:4a8eacbb3657 | 249 | BitB = OutputWord >> 8; |
| jolyon | 2:4a8eacbb3657 | 250 | |
| jolyon | 2:4a8eacbb3657 | 251 | PortA = chip.read(PORT_A); |
| jolyon | 2:4a8eacbb3657 | 252 | PortA |=BitA; |
| jolyon | 2:4a8eacbb3657 | 253 | chip.write(PORT_A, PortA); |
| jolyon | 2:4a8eacbb3657 | 254 | |
| jolyon | 2:4a8eacbb3657 | 255 | PortB = chip.read(PORT_B); |
| jolyon | 2:4a8eacbb3657 | 256 | PortB |=BitB; |
| jolyon | 2:4a8eacbb3657 | 257 | chip.write(PORT_B, PortB); |
| jolyon | 2:4a8eacbb3657 | 258 | } |
| jolyon | 2:4a8eacbb3657 | 259 | |
| jolyon | 2:4a8eacbb3657 | 260 | void ClearOutputBit(char bit) |
| jolyon | 2:4a8eacbb3657 | 261 | { |
| jolyon | 2:4a8eacbb3657 | 262 | uint16_t OutputWord = 1; |
| jolyon | 2:4a8eacbb3657 | 263 | char BitA, BitB, PortA, PortB; |
| jolyon | 2:4a8eacbb3657 | 264 | |
| jolyon | 2:4a8eacbb3657 | 265 | OutputWord <<= bit; |
| jolyon | 2:4a8eacbb3657 | 266 | OutputWord = ~OutputWord; |
| jolyon | 2:4a8eacbb3657 | 267 | |
| jolyon | 2:4a8eacbb3657 | 268 | BitA = OutputWord & 0xFF; |
| jolyon | 2:4a8eacbb3657 | 269 | BitB = OutputWord >> 8; |
| jolyon | 2:4a8eacbb3657 | 270 | |
| jolyon | 2:4a8eacbb3657 | 271 | PortA = chip.read(PORT_A); |
| jolyon | 2:4a8eacbb3657 | 272 | PortA &=BitA; |
| jolyon | 2:4a8eacbb3657 | 273 | chip.write(PORT_A, PortA); |
| jolyon | 2:4a8eacbb3657 | 274 | |
| jolyon | 2:4a8eacbb3657 | 275 | PortB = chip.read(PORT_B); |
| jolyon | 2:4a8eacbb3657 | 276 | PortB &=BitB; |
| jolyon | 2:4a8eacbb3657 | 277 | chip.write(PORT_B, PortB); |
| jolyon | 2:4a8eacbb3657 | 278 | } |
| jolyon | 2:4a8eacbb3657 | 279 | |
| jolyon | 2:4a8eacbb3657 | 280 | void WriteOutputPort(uint16_t FullPort) |
| jolyon | 2:4a8eacbb3657 | 281 | { |
| jolyon | 2:4a8eacbb3657 | 282 | chip.write(PORT_A, FullPort & 0xFF); |
| jolyon | 2:4a8eacbb3657 | 283 | chip.write(PORT_B, FullPort >> 8); |
| jolyon | 2:4a8eacbb3657 | 284 | } |
| jolyon | 2:4a8eacbb3657 | 285 | |
| jolyon | 2:4a8eacbb3657 | 286 | |
| jolyon | 2:4a8eacbb3657 | 287 | void GetThresholds(void) |
| jolyon | 2:4a8eacbb3657 | 288 | { |
| jolyon | 2:4a8eacbb3657 | 289 | //Load the pot values ant make 0 to 100% value |
| jolyon | 2:4a8eacbb3657 | 290 | /* |
| jolyon | 2:4a8eacbb3657 | 291 | RedThreshold = RedTrimmer.read_u16() * 100 >> 11; |
| jolyon | 2:4a8eacbb3657 | 292 | GreenThreshold = GreenTrimmer.read_u16() * 100 >> 11; |
| jolyon | 2:4a8eacbb3657 | 293 | BlueThreshold = BlueTrimmer.read_u16() * 100 >> 11; |
| jolyon | 2:4a8eacbb3657 | 294 | Hysteresis = HysTrimmer.read_u16() * 100 >> 11; |
| jolyon | 2:4a8eacbb3657 | 295 | |
| jolyon | 2:4a8eacbb3657 | 296 | */ |
| jolyon | 2:4a8eacbb3657 | 297 | RedThreshold = 45; |
| jolyon | 2:4a8eacbb3657 | 298 | GreenThreshold = 35; |
| jolyon | 2:4a8eacbb3657 | 299 | BlueThreshold = 10; |
| jolyon | 2:4a8eacbb3657 | 300 | Hysteresis = 5; |
| jolyon | 2:4a8eacbb3657 | 301 | } |
| jolyon | 0:9d301ae51ecf | 302 | |
| jolyon | 0:9d301ae51ecf | 303 | int main() |
| jolyon | 0:9d301ae51ecf | 304 | { |
| jolyon | 2:4a8eacbb3657 | 305 | InitOutputs(); |
| jolyon | 1:d99dad8a1f50 | 306 | //----------------------Initialise My TCS3472-------------------- |
| jolyon | 0:9d301ae51ecf | 307 | ConfigureTSC(0); |
| jolyon | 1:d99dad8a1f50 | 308 | //---------------------- My TCS3472 -------------------- |
| jolyon | 0:9d301ae51ecf | 309 | |
| jolyon | 1:d99dad8a1f50 | 310 | while(1) |
| jolyon | 1:d99dad8a1f50 | 311 | { |
| jolyon | 2:4a8eacbb3657 | 312 | |
| jolyon | 2:4a8eacbb3657 | 313 | //WriteOutputPort(MyPort); |
| jolyon | 2:4a8eacbb3657 | 314 | ClearOutputBit(MyPort); |
| jolyon | 2:4a8eacbb3657 | 315 | MyPort++; |
| jolyon | 2:4a8eacbb3657 | 316 | if (MyPort == 16) |
| jolyon | 2:4a8eacbb3657 | 317 | { |
| jolyon | 2:4a8eacbb3657 | 318 | MyPort = 0; |
| jolyon | 2:4a8eacbb3657 | 319 | } |
| jolyon | 2:4a8eacbb3657 | 320 | SetOutputBit(MyPort); |
| jolyon | 2:4a8eacbb3657 | 321 | |
| jolyon | 2:4a8eacbb3657 | 322 | GetThresholds(); |
| jolyon | 0:9d301ae51ecf | 323 | |
| jolyon | 0:9d301ae51ecf | 324 | DisplayIndex++; |
| jolyon | 0:9d301ae51ecf | 325 | if(DisplayIndex >= 20) { |
| jolyon | 0:9d301ae51ecf | 326 | DisplayIndex = 0; |
| jolyon | 0:9d301ae51ecf | 327 | } |
| jolyon | 2:4a8eacbb3657 | 328 | |
| jolyon | 0:9d301ae51ecf | 329 | wait(0.1); |
| jolyon | 2:4a8eacbb3657 | 330 | |
| jolyon | 0:9d301ae51ecf | 331 | //----------------------My TCS3472-------------------- |
| jolyon | 0:9d301ae51ecf | 332 | |
| jolyon | 0:9d301ae51ecf | 333 | |
| jolyon | 1:d99dad8a1f50 | 334 | SensorData = GetSensorData(0); |
| jolyon | 0:9d301ae51ecf | 335 | |
| jolyon | 1:d99dad8a1f50 | 336 | White = SensorData.White; |
| jolyon | 1:d99dad8a1f50 | 337 | Red = SensorData.Red; |
| jolyon | 1:d99dad8a1f50 | 338 | Green = SensorData.Green; |
| jolyon | 1:d99dad8a1f50 | 339 | Blue = SensorData.Blue; |
| jolyon | 0:9d301ae51ecf | 340 | |
| jolyon | 0:9d301ae51ecf | 341 | //Red filter |
| jolyon | 0:9d301ae51ecf | 342 | //Red = Filter(&AvgRed, Red, FACTOR); |
| jolyon | 0:9d301ae51ecf | 343 | //Green Filter |
| jolyon | 0:9d301ae51ecf | 344 | //Green = Filter(&AvgGreen, Green, FACTOR); |
| jolyon | 0:9d301ae51ecf | 345 | //Blue Filter |
| jolyon | 0:9d301ae51ecf | 346 | //Blue = Filter(&AvgBlue, Blue, FACTOR); |
| jolyon | 0:9d301ae51ecf | 347 | //White Filter |
| jolyon | 0:9d301ae51ecf | 348 | //White = Filter(&AvgWhite, White, FACTOR); |
| jolyon | 0:9d301ae51ecf | 349 | |
| jolyon | 1:d99dad8a1f50 | 350 | |
| jolyon | 1:d99dad8a1f50 | 351 | |
| jolyon | 2:4a8eacbb3657 | 352 | //#define USEWHITE //normalised % for each |
| jolyon | 0:9d301ae51ecf | 353 | #ifdef USEWHITE |
| jolyon | 0:9d301ae51ecf | 354 | Temp = (Red+Blue+Green)*10000; |
| jolyon | 0:9d301ae51ecf | 355 | Temp = Temp / White; |
| jolyon | 0:9d301ae51ecf | 356 | TempR = (Red* 10000) / Temp ; |
| jolyon | 0:9d301ae51ecf | 357 | TempG = (Green* 10000) / Temp ; |
| jolyon | 0:9d301ae51ecf | 358 | TempB = (Blue* 10000) / Temp ; |
| jolyon | 0:9d301ae51ecf | 359 | |
| jolyon | 0:9d301ae51ecf | 360 | RedProp = (TempR*100) / White; |
| jolyon | 0:9d301ae51ecf | 361 | GreenProp = (TempG*100) / White; |
| jolyon | 0:9d301ae51ecf | 362 | BlueProp = (TempB*100) / White; |
| jolyon | 0:9d301ae51ecf | 363 | #else |
| jolyon | 0:9d301ae51ecf | 364 | // Vn2 without using white.... |
| jolyon | 0:9d301ae51ecf | 365 | Temp = (Red+Blue+Green); |
| jolyon | 0:9d301ae51ecf | 366 | RedProp = (Red* 100) / Temp ; |
| jolyon | 0:9d301ae51ecf | 367 | GreenProp = (Green* 100) / Temp ; |
| jolyon | 0:9d301ae51ecf | 368 | BlueProp = (Blue* 100) / Temp ; |
| jolyon | 0:9d301ae51ecf | 369 | |
| jolyon | 0:9d301ae51ecf | 370 | //RedProp = (TempR*100) / White; |
| jolyon | 0:9d301ae51ecf | 371 | //GreenProp = (TempG*100) / White; |
| jolyon | 0:9d301ae51ecf | 372 | //BlueProp = (TempB*100) / White; |
| jolyon | 0:9d301ae51ecf | 373 | #endif |
| jolyon | 0:9d301ae51ecf | 374 | |
| jolyon | 0:9d301ae51ecf | 375 | |
| jolyon | 0:9d301ae51ecf | 376 | |
| jolyon | 0:9d301ae51ecf | 377 | //make 12 bit only (display) |
| jolyon | 0:9d301ae51ecf | 378 | Red >>=4; |
| jolyon | 0:9d301ae51ecf | 379 | Green >>=4; |
| jolyon | 0:9d301ae51ecf | 380 | Blue >>=4; |
| jolyon | 0:9d301ae51ecf | 381 | White >>=4; |
| jolyon | 0:9d301ae51ecf | 382 | |
| jolyon | 1:d99dad8a1f50 | 383 | |
| jolyon | 2:4a8eacbb3657 | 384 | |
| jolyon | 1:d99dad8a1f50 | 385 | lcd.locate(0, 0); |
| jolyon | 2:4a8eacbb3657 | 386 | lcd.printf("R%02i G%02i B%02i W%04i",RedProp,GreenProp,BlueProp,White); |
| jolyon | 2:4a8eacbb3657 | 387 | lcd.locate(0, 1); |
| jolyon | 2:4a8eacbb3657 | 388 | lcd.printf("R%02i G%02i B%02i H%02i ",RedThreshold, GreenThreshold, BlueThreshold, Hysteresis); |
| jolyon | 2:4a8eacbb3657 | 389 | |
| jolyon | 0:9d301ae51ecf | 390 | |
| jolyon | 0:9d301ae51ecf | 391 | //guess the colour |
| jolyon | 2:4a8eacbb3657 | 392 | if(White > 15) //looking at intensity somthing is on! |
| jolyon | 2:4a8eacbb3657 | 393 | { |
| jolyon | 2:4a8eacbb3657 | 394 | /* |
| jolyon | 2:4a8eacbb3657 | 395 | if((GreenProp <= 30) && (RedProp >=50)) |
| jolyon | 2:4a8eacbb3657 | 396 | { //pretty sure it's Yellow |
| jolyon | 0:9d301ae51ecf | 397 | YellowLed = 1; |
| jolyon | 0:9d301ae51ecf | 398 | GreenLed = 0; |
| jolyon | 2:4a8eacbb3657 | 399 | } |
| jolyon | 2:4a8eacbb3657 | 400 | else |
| jolyon | 2:4a8eacbb3657 | 401 | { |
| jolyon | 0:9d301ae51ecf | 402 | YellowLed = 0; |
| jolyon | 2:4a8eacbb3657 | 403 | if((GreenProp > 40) && (RedProp <=40)) { //pretty sure it's green |
| jolyon | 0:9d301ae51ecf | 404 | GreenLed = 1; |
| jolyon | 0:9d301ae51ecf | 405 | } else { |
| jolyon | 0:9d301ae51ecf | 406 | GreenLed = 1; |
| jolyon | 0:9d301ae51ecf | 407 | YellowLed = 1; |
| jolyon | 0:9d301ae51ecf | 408 | } |
| jolyon | 0:9d301ae51ecf | 409 | } |
| jolyon | 2:4a8eacbb3657 | 410 | */ |
| jolyon | 2:4a8eacbb3657 | 411 | if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) |
| jolyon | 2:4a8eacbb3657 | 412 | { //pretty sure it's Yellow |
| jolyon | 2:4a8eacbb3657 | 413 | YellowLed = 1; |
| jolyon | 2:4a8eacbb3657 | 414 | GreenLed = 0; |
| jolyon | 2:4a8eacbb3657 | 415 | } |
| jolyon | 2:4a8eacbb3657 | 416 | else |
| jolyon | 2:4a8eacbb3657 | 417 | { |
| jolyon | 2:4a8eacbb3657 | 418 | YellowLed = 0; |
| jolyon | 2:4a8eacbb3657 | 419 | if( (GreenProp > (GreenThreshold+Hysteresis)) && (RedProp <=(RedThreshold-Hysteresis)) ) |
| jolyon | 2:4a8eacbb3657 | 420 | { //pretty sure it's green |
| jolyon | 2:4a8eacbb3657 | 421 | GreenLed = 1; |
| jolyon | 2:4a8eacbb3657 | 422 | } |
| jolyon | 2:4a8eacbb3657 | 423 | else |
| jolyon | 2:4a8eacbb3657 | 424 | { |
| jolyon | 2:4a8eacbb3657 | 425 | GreenLed = 1; |
| jolyon | 2:4a8eacbb3657 | 426 | YellowLed = 1; |
| jolyon | 2:4a8eacbb3657 | 427 | } |
| jolyon | 2:4a8eacbb3657 | 428 | } |
| jolyon | 2:4a8eacbb3657 | 429 | } |
| jolyon | 2:4a8eacbb3657 | 430 | else |
| jolyon | 2:4a8eacbb3657 | 431 | { |
| jolyon | 0:9d301ae51ecf | 432 | //not enough intensity to determine |
| jolyon | 0:9d301ae51ecf | 433 | GreenLed = 0; |
| jolyon | 0:9d301ae51ecf | 434 | YellowLed = 0; |
| jolyon | 0:9d301ae51ecf | 435 | } |
| jolyon | 0:9d301ae51ecf | 436 | } |
| jolyon | 0:9d301ae51ecf | 437 | } |
| jolyon | 0:9d301ae51ecf | 438 | |
| jolyon | 0:9d301ae51ecf | 439 | |
| jolyon | 0:9d301ae51ecf | 440 | |
| jolyon | 2:4a8eacbb3657 | 441 | /* |
| jolyon | 2:4a8eacbb3657 | 442 | --------------------------------------------------------------------------------------------- |
| jolyon | 2:4a8eacbb3657 | 443 | //SPI SampleCode |
| jolyon | 0:9d301ae51ecf | 444 | |
| jolyon | 2:4a8eacbb3657 | 445 | #include "mbed.h" |
| jolyon | 2:4a8eacbb3657 | 446 | #include "MCP23S17.h" |
| jolyon | 2:4a8eacbb3657 | 447 | // Create SPI bus |
| jolyon | 2:4a8eacbb3657 | 448 | SPI spi(p5, p6, p7); |
| jolyon | 2:4a8eacbb3657 | 449 | // |
| jolyon | 2:4a8eacbb3657 | 450 | |
| jolyon | 2:4a8eacbb3657 | 451 | char Opcode = 0x40; |
| jolyon | 2:4a8eacbb3657 | 452 | |
| jolyon | 2:4a8eacbb3657 | 453 | // Next create a MCP23S17 |
| jolyon | 2:4a8eacbb3657 | 454 | // mbed p20 is connected to ~chipSelect on the MCP23S17 |
| jolyon | 2:4a8eacbb3657 | 455 | MCP23S17 chip = MCP23S17(spi, p20, Opcode); |
| jolyon | 2:4a8eacbb3657 | 456 | |
| jolyon | 2:4a8eacbb3657 | 457 | DigitalOut led1(LED1); // mbed LED1 is used for test status display |
| jolyon | 2:4a8eacbb3657 | 458 | |
| jolyon | 2:4a8eacbb3657 | 459 | int main() { |
| jolyon | 2:4a8eacbb3657 | 460 | // |
| jolyon | 2:4a8eacbb3657 | 461 | // Set all 8 Port A bits to output direction |
| jolyon | 2:4a8eacbb3657 | 462 | chip.direction(PORT_A, 0x00); |
| jolyon | 2:4a8eacbb3657 | 463 | // Set all 8 Port B bits to input direction |
| jolyon | 2:4a8eacbb3657 | 464 | chip.direction(PORT_B, 0xFF); |
| jolyon | 2:4a8eacbb3657 | 465 | led1=0; |
| jolyon | 2:4a8eacbb3657 | 466 | // Start Loopback test sending out and reading back values |
| jolyon | 2:4a8eacbb3657 | 467 | // loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together |
| jolyon | 2:4a8eacbb3657 | 468 | while (1) { |
| jolyon | 2:4a8eacbb3657 | 469 | // write 0xAA to MCP23S17 Port A |
| jolyon | 2:4a8eacbb3657 | 470 | chip.write(PORT_A, 0xAA); |
| jolyon | 2:4a8eacbb3657 | 471 | wait(.5); |
| jolyon | 2:4a8eacbb3657 | 472 | // read back value from MCP23S17 Port B and display B0 on mbed led1 |
| jolyon | 2:4a8eacbb3657 | 473 | led1 = chip.read(PORT_B)& 0x01; |
| jolyon | 2:4a8eacbb3657 | 474 | // write 0x55 to MCP23S17 Port A |
| jolyon | 2:4a8eacbb3657 | 475 | chip.write(PORT_A, 0x55); |
| jolyon | 2:4a8eacbb3657 | 476 | wait(.5); |
| jolyon | 2:4a8eacbb3657 | 477 | // read back value from MCP23S17 Port B and display B0 on mbed led1 |
| jolyon | 2:4a8eacbb3657 | 478 | led1 = chip.read(PORT_B)& 0x01; |
| jolyon | 2:4a8eacbb3657 | 479 | // led1 should blink slowly when it is all working |
| jolyon | 0:9d301ae51ecf | 480 | } |
| jolyon | 0:9d301ae51ecf | 481 | } |
| jolyon | 2:4a8eacbb3657 | 482 | */ |
| jolyon | 0:9d301ae51ecf | 483 | |
| jolyon | 0:9d301ae51ecf | 484 | //--------------------------------------------------------------------------------------------- |
| jolyon | 0:9d301ae51ecf | 485 | //--------------------------------------------------------------------------------------------- |