![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
The example program is LPC812MAXdemo board default program supplied by mbed.org
Fork of mbed_blinky by
main.cpp@1:7620606cdb4e, 2013-11-06 (annotated)
- Committer:
- okwh
- Date:
- Wed Nov 06 15:20:37 2013 +0000
- Revision:
- 1:7620606cdb4e
- Parent:
- 0:7dec7e9ac085
1st default program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan | 0:7dec7e9ac085 | 1 | #include "mbed.h" |
dan | 0:7dec7e9ac085 | 2 | |
okwh | 1:7620606cdb4e | 3 | DigitalOut ledRed(LED_RED); |
okwh | 1:7620606cdb4e | 4 | DigitalOut ledGreen(LED_GREEN); |
okwh | 1:7620606cdb4e | 5 | DigitalOut ledBlue(LED_BLUE); |
okwh | 1:7620606cdb4e | 6 | |
okwh | 1:7620606cdb4e | 7 | int flashPattern = 0; |
okwh | 1:7620606cdb4e | 8 | |
okwh | 1:7620606cdb4e | 9 | const unsigned char I2C_ADDR_PCF8591T = 0x9E; |
okwh | 1:7620606cdb4e | 10 | const unsigned char I2C_ADDR_PCA9672PW = 0x46; |
okwh | 1:7620606cdb4e | 11 | |
okwh | 1:7620606cdb4e | 12 | const unsigned char LED_ON = 0; |
okwh | 1:7620606cdb4e | 13 | const unsigned char LED_OFF = 1; |
okwh | 1:7620606cdb4e | 14 | bool keyPressed; |
okwh | 1:7620606cdb4e | 15 | char response[1]; |
okwh | 1:7620606cdb4e | 16 | |
okwh | 1:7620606cdb4e | 17 | I2C m_i2c(P0_10, P0_11); |
okwh | 1:7620606cdb4e | 18 | |
okwh | 1:7620606cdb4e | 19 | unsigned char i2c_wait(int ms) |
okwh | 1:7620606cdb4e | 20 | { |
okwh | 1:7620606cdb4e | 21 | for(int n=0; n<ms; n+=50) { |
okwh | 1:7620606cdb4e | 22 | //read PCA9672PW |
okwh | 1:7620606cdb4e | 23 | if (m_i2c.read(I2C_ADDR_PCA9672PW, response, 1) == 0) { |
okwh | 1:7620606cdb4e | 24 | //check if push-button pressed (bit #7 = 0) |
okwh | 1:7620606cdb4e | 25 | if ((keyPressed == false) && ((response[0] & 0x80) == 0)) |
okwh | 1:7620606cdb4e | 26 | return 1; |
okwh | 1:7620606cdb4e | 27 | } else { |
okwh | 1:7620606cdb4e | 28 | //error reading from I2C device |
okwh | 1:7620606cdb4e | 29 | while(1) { |
okwh | 1:7620606cdb4e | 30 | for(int i=0; i<10; i++) { |
okwh | 1:7620606cdb4e | 31 | ledBlue = LED_ON; |
okwh | 1:7620606cdb4e | 32 | wait(0.05); |
okwh | 1:7620606cdb4e | 33 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 34 | wait(0.05); |
okwh | 1:7620606cdb4e | 35 | } |
okwh | 1:7620606cdb4e | 36 | wait(1.0); |
okwh | 1:7620606cdb4e | 37 | } |
okwh | 1:7620606cdb4e | 38 | } |
okwh | 1:7620606cdb4e | 39 | wait(0.05); |
okwh | 1:7620606cdb4e | 40 | } |
okwh | 1:7620606cdb4e | 41 | return 0; |
okwh | 1:7620606cdb4e | 42 | } |
dan | 0:7dec7e9ac085 | 43 | |
okwh | 1:7620606cdb4e | 44 | int main() |
okwh | 1:7620606cdb4e | 45 | { |
okwh | 1:7620606cdb4e | 46 | //init RGB-LED to be off |
okwh | 1:7620606cdb4e | 47 | ledRed = LED_OFF; |
okwh | 1:7620606cdb4e | 48 | ledGreen = LED_OFF; |
okwh | 1:7620606cdb4e | 49 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 50 | |
okwh | 1:7620606cdb4e | 51 | //test i2C connection with PCF8591T (1.0.0.1.1.1.1.rw) |
okwh | 1:7620606cdb4e | 52 | const char command1[] = {0x00}; |
okwh | 1:7620606cdb4e | 53 | if (m_i2c.write(I2C_ADDR_PCF8591T, command1, 1) != 0) { |
okwh | 1:7620606cdb4e | 54 | while(1) { |
okwh | 1:7620606cdb4e | 55 | for(int i=0; i<10; i++) { |
okwh | 1:7620606cdb4e | 56 | ledRed = LED_ON; |
okwh | 1:7620606cdb4e | 57 | wait(0.05); |
okwh | 1:7620606cdb4e | 58 | ledRed = LED_OFF; |
okwh | 1:7620606cdb4e | 59 | wait(0.05); |
okwh | 1:7620606cdb4e | 60 | } |
okwh | 1:7620606cdb4e | 61 | wait(1.0); |
okwh | 1:7620606cdb4e | 62 | } |
okwh | 1:7620606cdb4e | 63 | } |
okwh | 1:7620606cdb4e | 64 | |
okwh | 1:7620606cdb4e | 65 | //test i2C connection with PCA9672PW (0.1.0.0.0.1.1.rw) |
okwh | 1:7620606cdb4e | 66 | const char command2[] = {0xff}; |
okwh | 1:7620606cdb4e | 67 | if (m_i2c.write(I2C_ADDR_PCA9672PW, command2, 1) != 0) { |
okwh | 1:7620606cdb4e | 68 | while(1) { |
okwh | 1:7620606cdb4e | 69 | for(int i=0; i<10; i++) { |
okwh | 1:7620606cdb4e | 70 | ledBlue = LED_ON; |
okwh | 1:7620606cdb4e | 71 | wait(0.05); |
okwh | 1:7620606cdb4e | 72 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 73 | wait(0.05); |
okwh | 1:7620606cdb4e | 74 | } |
okwh | 1:7620606cdb4e | 75 | wait(1.0); |
okwh | 1:7620606cdb4e | 76 | } |
okwh | 1:7620606cdb4e | 77 | } |
okwh | 1:7620606cdb4e | 78 | |
okwh | 1:7620606cdb4e | 79 | //startup flash behaviour |
okwh | 1:7620606cdb4e | 80 | for(int i=0; i<10; i++) { |
okwh | 1:7620606cdb4e | 81 | ledGreen = LED_ON; |
okwh | 1:7620606cdb4e | 82 | wait(0.05); |
okwh | 1:7620606cdb4e | 83 | ledGreen = LED_OFF; |
okwh | 1:7620606cdb4e | 84 | wait(0.05); |
okwh | 1:7620606cdb4e | 85 | } |
okwh | 1:7620606cdb4e | 86 | wait(1.0); |
okwh | 1:7620606cdb4e | 87 | |
okwh | 1:7620606cdb4e | 88 | //enter forever loop, change RGB flash patterns with user push-button |
okwh | 1:7620606cdb4e | 89 | keyPressed = false; |
okwh | 1:7620606cdb4e | 90 | i2c_wait(1); |
dan | 0:7dec7e9ac085 | 91 | while(1) { |
okwh | 1:7620606cdb4e | 92 | //read user push-button |
okwh | 1:7620606cdb4e | 93 | #if 1 |
okwh | 1:7620606cdb4e | 94 | i2c_wait(1); |
okwh | 1:7620606cdb4e | 95 | //check if push-button pressed (bit #7 = 0) |
okwh | 1:7620606cdb4e | 96 | if ((keyPressed == false) && ((response[0] & 0x80) == 0)) { |
okwh | 1:7620606cdb4e | 97 | keyPressed = true; |
okwh | 1:7620606cdb4e | 98 | flashPattern++; |
okwh | 1:7620606cdb4e | 99 | } |
okwh | 1:7620606cdb4e | 100 | |
okwh | 1:7620606cdb4e | 101 | //check if push-button released (bit #7 = 1) |
okwh | 1:7620606cdb4e | 102 | else if ((keyPressed == true) && ((response[0] & 0x80) != 0)) |
okwh | 1:7620606cdb4e | 103 | keyPressed = false; |
okwh | 1:7620606cdb4e | 104 | #else |
okwh | 1:7620606cdb4e | 105 | if (m_i2c.read(I2C_ADDR_PCA9672PW, response, 1) == 0) { |
okwh | 1:7620606cdb4e | 106 | //check if push-button pressed (bit #7 = 0) |
okwh | 1:7620606cdb4e | 107 | if ((keyPressed == false) && ((response[0] & 0x80) == 0)) { |
okwh | 1:7620606cdb4e | 108 | keyPressed = true; |
okwh | 1:7620606cdb4e | 109 | flashPattern++; |
okwh | 1:7620606cdb4e | 110 | ledRed = LED_OFF; |
okwh | 1:7620606cdb4e | 111 | ledGreen = LED_OFF; |
okwh | 1:7620606cdb4e | 112 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 113 | } |
okwh | 1:7620606cdb4e | 114 | |
okwh | 1:7620606cdb4e | 115 | //check if push-button released (bit #7 = 1) |
okwh | 1:7620606cdb4e | 116 | else if ((keyPressed == true) && ((response[0] & 0x80) != 0)) |
okwh | 1:7620606cdb4e | 117 | keyPressed = false; |
okwh | 1:7620606cdb4e | 118 | } |
okwh | 1:7620606cdb4e | 119 | //error reading from I2C device |
okwh | 1:7620606cdb4e | 120 | else { |
okwh | 1:7620606cdb4e | 121 | while(1) { |
okwh | 1:7620606cdb4e | 122 | for(int i=0; i<10; i++) { |
okwh | 1:7620606cdb4e | 123 | ledBlue = LED_ON; |
okwh | 1:7620606cdb4e | 124 | wait(0.05); |
okwh | 1:7620606cdb4e | 125 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 126 | wait(0.05); |
okwh | 1:7620606cdb4e | 127 | } |
okwh | 1:7620606cdb4e | 128 | wait(1.0); |
okwh | 1:7620606cdb4e | 129 | } |
okwh | 1:7620606cdb4e | 130 | } |
okwh | 1:7620606cdb4e | 131 | #endif |
okwh | 1:7620606cdb4e | 132 | //flash RGB-LED according to pattern |
okwh | 1:7620606cdb4e | 133 | switch(flashPattern) { |
okwh | 1:7620606cdb4e | 134 | case 0: |
okwh | 1:7620606cdb4e | 135 | ledRed = LED_ON; |
okwh | 1:7620606cdb4e | 136 | if (i2c_wait(80) != 0) break; |
okwh | 1:7620606cdb4e | 137 | ledRed = LED_OFF; |
okwh | 1:7620606cdb4e | 138 | if (i2c_wait(500) != 0) break; |
okwh | 1:7620606cdb4e | 139 | |
okwh | 1:7620606cdb4e | 140 | ledGreen = LED_ON; |
okwh | 1:7620606cdb4e | 141 | if (i2c_wait(15) != 0) break; |
okwh | 1:7620606cdb4e | 142 | ledGreen = LED_OFF; |
okwh | 1:7620606cdb4e | 143 | if (i2c_wait(550) != 0) break; |
okwh | 1:7620606cdb4e | 144 | |
okwh | 1:7620606cdb4e | 145 | ledBlue = LED_ON; |
okwh | 1:7620606cdb4e | 146 | if (i2c_wait(80) != 0) break; |
okwh | 1:7620606cdb4e | 147 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 148 | if (i2c_wait(500) != 0) break; |
okwh | 1:7620606cdb4e | 149 | break; |
okwh | 1:7620606cdb4e | 150 | case 1: |
okwh | 1:7620606cdb4e | 151 | for(float a=0; a<0.01; a+=0.0004) { |
okwh | 1:7620606cdb4e | 152 | ledGreen = LED_ON; |
okwh | 1:7620606cdb4e | 153 | wait(a/2); |
okwh | 1:7620606cdb4e | 154 | ledGreen = LED_OFF; |
okwh | 1:7620606cdb4e | 155 | wait(0.01-a); |
okwh | 1:7620606cdb4e | 156 | } |
okwh | 1:7620606cdb4e | 157 | ledGreen = LED_ON; |
okwh | 1:7620606cdb4e | 158 | if (i2c_wait(1) != 0) break; |
okwh | 1:7620606cdb4e | 159 | for(float a=0.01; a>0.0; a-=0.0002) { |
okwh | 1:7620606cdb4e | 160 | ledGreen = LED_ON; |
okwh | 1:7620606cdb4e | 161 | wait(a/2); |
okwh | 1:7620606cdb4e | 162 | ledGreen = LED_OFF; |
okwh | 1:7620606cdb4e | 163 | wait(0.01-a); |
okwh | 1:7620606cdb4e | 164 | } |
okwh | 1:7620606cdb4e | 165 | if (i2c_wait(400) != 0) break; |
okwh | 1:7620606cdb4e | 166 | break; |
okwh | 1:7620606cdb4e | 167 | case 2: |
okwh | 1:7620606cdb4e | 168 | ledRed = LED_ON; |
okwh | 1:7620606cdb4e | 169 | if (i2c_wait(150) != 0) break; |
okwh | 1:7620606cdb4e | 170 | ledRed = LED_OFF; |
okwh | 1:7620606cdb4e | 171 | if (i2c_wait(150) != 0) break; |
okwh | 1:7620606cdb4e | 172 | |
okwh | 1:7620606cdb4e | 173 | ledGreen = LED_ON; |
okwh | 1:7620606cdb4e | 174 | if (i2c_wait(100) != 0) break; |
okwh | 1:7620606cdb4e | 175 | ledGreen = LED_OFF; |
okwh | 1:7620606cdb4e | 176 | |
okwh | 1:7620606cdb4e | 177 | ledBlue = LED_ON; |
okwh | 1:7620606cdb4e | 178 | if (i2c_wait(150) != 0) break; |
okwh | 1:7620606cdb4e | 179 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 180 | break; |
okwh | 1:7620606cdb4e | 181 | case 3: |
okwh | 1:7620606cdb4e | 182 | ledRed = LED_ON; |
okwh | 1:7620606cdb4e | 183 | ledGreen = LED_ON; |
okwh | 1:7620606cdb4e | 184 | ledBlue = LED_ON; |
okwh | 1:7620606cdb4e | 185 | if (i2c_wait(10) != 0) break; |
okwh | 1:7620606cdb4e | 186 | |
okwh | 1:7620606cdb4e | 187 | ledGreen = LED_OFF; |
okwh | 1:7620606cdb4e | 188 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 189 | ledRed = LED_OFF; |
okwh | 1:7620606cdb4e | 190 | if (i2c_wait(500) != 0) break; |
okwh | 1:7620606cdb4e | 191 | break; |
okwh | 1:7620606cdb4e | 192 | case 4: |
okwh | 1:7620606cdb4e | 193 | for(float a=0; a<0.01; a+=0.0008) { |
okwh | 1:7620606cdb4e | 194 | ledRed = LED_ON; |
okwh | 1:7620606cdb4e | 195 | wait(a); |
okwh | 1:7620606cdb4e | 196 | ledRed = LED_OFF; |
okwh | 1:7620606cdb4e | 197 | wait(0.01-a); |
okwh | 1:7620606cdb4e | 198 | } |
okwh | 1:7620606cdb4e | 199 | ledRed = LED_ON; |
okwh | 1:7620606cdb4e | 200 | if (i2c_wait(1) != 0) break; |
okwh | 1:7620606cdb4e | 201 | for(float a=0; a<0.01; a+=0.0008) { |
okwh | 1:7620606cdb4e | 202 | ledBlue = LED_ON; |
okwh | 1:7620606cdb4e | 203 | wait(a); |
okwh | 1:7620606cdb4e | 204 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 205 | wait(0.01-a); |
okwh | 1:7620606cdb4e | 206 | } |
okwh | 1:7620606cdb4e | 207 | ledBlue = LED_ON; |
okwh | 1:7620606cdb4e | 208 | if (i2c_wait(1) != 0) break; |
okwh | 1:7620606cdb4e | 209 | for(float a=0.01; a>0.0; a-=0.0008) { |
okwh | 1:7620606cdb4e | 210 | ledRed = LED_ON; |
okwh | 1:7620606cdb4e | 211 | wait(a); |
okwh | 1:7620606cdb4e | 212 | ledRed = LED_OFF; |
okwh | 1:7620606cdb4e | 213 | wait(0.01-a); |
okwh | 1:7620606cdb4e | 214 | } |
okwh | 1:7620606cdb4e | 215 | if (i2c_wait(1) != 0) break; |
okwh | 1:7620606cdb4e | 216 | for(float a=0.01; a>0.0; a-=0.0008) { |
okwh | 1:7620606cdb4e | 217 | ledBlue = LED_ON; |
okwh | 1:7620606cdb4e | 218 | wait(a); |
okwh | 1:7620606cdb4e | 219 | ledBlue = LED_OFF; |
okwh | 1:7620606cdb4e | 220 | wait(0.01-a); |
okwh | 1:7620606cdb4e | 221 | } |
okwh | 1:7620606cdb4e | 222 | if (i2c_wait(200) != 0) break; |
okwh | 1:7620606cdb4e | 223 | break; |
okwh | 1:7620606cdb4e | 224 | default: |
okwh | 1:7620606cdb4e | 225 | flashPattern = 0; |
okwh | 1:7620606cdb4e | 226 | break; |
okwh | 1:7620606cdb4e | 227 | } |
dan | 0:7dec7e9ac085 | 228 | } |
dan | 0:7dec7e9ac085 | 229 | } |
okwh | 1:7620606cdb4e | 230 | |
okwh | 1:7620606cdb4e | 231 | #if 0 |
okwh | 1:7620606cdb4e | 232 | //https://github.com/mbedmicro/mbed/tree/master/libraries/mbed/vendor/NXP/capi/LPC812 |
okwh | 1:7620606cdb4e | 233 | P0_0 = 0, |
okwh | 1:7620606cdb4e | 234 | P0_1 = 1, |
okwh | 1:7620606cdb4e | 235 | P0_2 = 2, |
okwh | 1:7620606cdb4e | 236 | P0_3 = 3, |
okwh | 1:7620606cdb4e | 237 | P0_4 = 4, |
okwh | 1:7620606cdb4e | 238 | P0_5 = 5, |
okwh | 1:7620606cdb4e | 239 | P0_6 = 6, |
okwh | 1:7620606cdb4e | 240 | P0_7 = 7, |
okwh | 1:7620606cdb4e | 241 | P0_8 = 8, |
okwh | 1:7620606cdb4e | 242 | P0_9 = 9, |
okwh | 1:7620606cdb4e | 243 | P0_10 = 10, //SDA |
okwh | 1:7620606cdb4e | 244 | P0_11 = 11, //SCL |
okwh | 1:7620606cdb4e | 245 | P0_12 = 12, |
okwh | 1:7620606cdb4e | 246 | P0_13 = 13, |
okwh | 1:7620606cdb4e | 247 | P0_14 = 14, |
okwh | 1:7620606cdb4e | 248 | P0_15 = 15, |
okwh | 1:7620606cdb4e | 249 | P0_16 = 16, |
okwh | 1:7620606cdb4e | 250 | P0_17 = 17, |
okwh | 1:7620606cdb4e | 251 | |
okwh | 1:7620606cdb4e | 252 | D0 = P0_0, |
okwh | 1:7620606cdb4e | 253 | D1 = P0_4, |
okwh | 1:7620606cdb4e | 254 | D2 = P0_6, |
okwh | 1:7620606cdb4e | 255 | D3 = P0_8, |
okwh | 1:7620606cdb4e | 256 | D4 = P0_9, |
okwh | 1:7620606cdb4e | 257 | |
okwh | 1:7620606cdb4e | 258 | D7 = P0_7, |
okwh | 1:7620606cdb4e | 259 | D8 = P0_17, |
okwh | 1:7620606cdb4e | 260 | D9 = P0_16, |
okwh | 1:7620606cdb4e | 261 | D10 = P0_13, |
okwh | 1:7620606cdb4e | 262 | D11 = P0_14, |
okwh | 1:7620606cdb4e | 263 | D12 = P0_15, |
okwh | 1:7620606cdb4e | 264 | D13 = P0_12, |
okwh | 1:7620606cdb4e | 265 | |
okwh | 1:7620606cdb4e | 266 | A4 = P0_10, |
okwh | 1:7620606cdb4e | 267 | A5 = P0_11, |
okwh | 1:7620606cdb4e | 268 | |
okwh | 1:7620606cdb4e | 269 | // LPC800-MAX board |
okwh | 1:7620606cdb4e | 270 | LED_RED = P0_7, |
okwh | 1:7620606cdb4e | 271 | LED_GREEN = P0_17, |
okwh | 1:7620606cdb4e | 272 | LED_BLUE = P0_16, |
okwh | 1:7620606cdb4e | 273 | |
okwh | 1:7620606cdb4e | 274 | // mbed original LED naming |
okwh | 1:7620606cdb4e | 275 | LED1 = LED_BLUE, |
okwh | 1:7620606cdb4e | 276 | LED2 = LED_GREEN, |
okwh | 1:7620606cdb4e | 277 | LED3 = LED_RED, |
okwh | 1:7620606cdb4e | 278 | LED4 = LED_RED, |
okwh | 1:7620606cdb4e | 279 | |
okwh | 1:7620606cdb4e | 280 | // Serial to USB pins |
okwh | 1:7620606cdb4e | 281 | USBTX = P0_6, |
okwh | 1:7620606cdb4e | 282 | USBRX = P0_1, |
okwh | 1:7620606cdb4e | 283 | #endif |