LPC812MAXdemoTest_revPA1

Dependencies:   mbed

Fork of LPC812MAXdemoTest_revPA1 by viniedodo jin

Committer:
viniedodo
Date:
Sun Oct 13 03:42:55 2013 +0000
Revision:
0:c2c6f009e2d1
Child:
1:f7158ed0d3ae
LPC812MAXdemoTest_revPA1

Who changed what in which revision?

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