Allans code
Dependencies: RPG SerLCDv25 CascadedTLC5940 mbed
main.cpp@0:3f24fed55710, 2013-04-05 (annotated)
- Committer:
- Mach5
- Date:
- Fri Apr 05 16:06:14 2013 +0000
- Revision:
- 0:3f24fed55710
project code 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mach5 | 0:3f24fed55710 | 1 | #include "mbed.h" |
Mach5 | 0:3f24fed55710 | 2 | #include "TLC5940.h" |
Mach5 | 0:3f24fed55710 | 3 | #include "SerLCDv25.h" |
Mach5 | 0:3f24fed55710 | 4 | #include "RPG.h" |
Mach5 | 0:3f24fed55710 | 5 | |
Mach5 | 0:3f24fed55710 | 6 | SPI spi (p5, NC, p7);//pin5 is sin/ nc/p7 is sclk |
Mach5 | 0:3f24fed55710 | 7 | DigitalOut dcprg (p6);//see p10 |
Mach5 | 0:3f24fed55710 | 8 | DigitalOut xlat (p8); |
Mach5 | 0:3f24fed55710 | 9 | DigitalOut blank (p9); |
Mach5 | 0:3f24fed55710 | 10 | DigitalOut vprg (p10);//this might need to be swapped with dcprg cause pins may be backwards |
Mach5 | 0:3f24fed55710 | 11 | |
Mach5 | 0:3f24fed55710 | 12 | SPI adcspi (NC, p12, p13);//adc |
Mach5 | 0:3f24fed55710 | 13 | DigitalOut cs(p14);//chipselect |
Mach5 | 0:3f24fed55710 | 14 | PwmOut gsclk (p21);//constantly gives clock to grey scale clock |
Mach5 | 0:3f24fed55710 | 15 | Serial lcdspi(p28, NC);//do not need pin 27 |
Mach5 | 0:3f24fed55710 | 16 | //Constructor(Channel A input pin, Channel B input pin, Pushbutton input pin) |
Mach5 | 0:3f24fed55710 | 17 | RPG encoder1 (p27,p29,p30);//encoder top left |
Mach5 | 0:3f24fed55710 | 18 | RPG encoder2 (p26,p25,p24);//encoder middle right |
Mach5 | 0:3f24fed55710 | 19 | //RPG encoder3 (p22,p23,p11);//Left connected to check batterylevels |
Mach5 | 0:3f24fed55710 | 20 | AnalogIn vbatt(p20);//analog input span of 0 to check for battery levels |
Mach5 | 0:3f24fed55710 | 21 | InterruptIn interpulsea(p22); |
Mach5 | 0:3f24fed55710 | 22 | InterruptIn interpulseb(p23); |
Mach5 | 0:3f24fed55710 | 23 | InterruptIn inter(p11);//pb interrupt |
Mach5 | 0:3f24fed55710 | 24 | |
Mach5 | 0:3f24fed55710 | 25 | DigitalOut led1(LED1); |
Mach5 | 0:3f24fed55710 | 26 | DigitalOut led2(LED2); |
Mach5 | 0:3f24fed55710 | 27 | DigitalOut led3(LED3); |
Mach5 | 0:3f24fed55710 | 28 | DigitalOut led4(LED4); |
Mach5 | 0:3f24fed55710 | 29 | |
Mach5 | 0:3f24fed55710 | 30 | |
Mach5 | 0:3f24fed55710 | 31 | int ic_number = 3; //change depending on ics you have |
Mach5 | 0:3f24fed55710 | 32 | unsigned short leds [48];//number of channels you will have 16*number of ics |
Mach5 | 0:3f24fed55710 | 33 | |
Mach5 | 0:3f24fed55710 | 34 | BusOut mux(p15, p16, p17, p18); |
Mach5 | 0:3f24fed55710 | 35 | //A0 |
Mach5 | 0:3f24fed55710 | 36 | //mux |
Mach5 | 0:3f24fed55710 | 37 | //mux |
Mach5 | 0:3f24fed55710 | 38 | //a3 |
Mach5 | 0:3f24fed55710 | 39 | DigitalOut muxenable(p19); |
Mach5 | 0:3f24fed55710 | 40 | //EN |
Mach5 | 0:3f24fed55710 | 41 | void startup(void); |
Mach5 | 0:3f24fed55710 | 42 | void checkbattery(void); |
Mach5 | 0:3f24fed55710 | 43 | void ledstartup(void); |
Mach5 | 0:3f24fed55710 | 44 | void ledtest(void); |
Mach5 | 0:3f24fed55710 | 45 | |
Mach5 | 0:3f24fed55710 | 46 | |
Mach5 | 0:3f24fed55710 | 47 | |
Mach5 | 0:3f24fed55710 | 48 | |
Mach5 | 0:3f24fed55710 | 49 | int main() //main start function =============================================================<<<<<<<<<<<<<<<<<<<<<<<<< |
Mach5 | 0:3f24fed55710 | 50 | { |
Mach5 | 0:3f24fed55710 | 51 | |
Mach5 | 0:3f24fed55710 | 52 | startup(); |
Mach5 | 0:3f24fed55710 | 53 | setup (spi, vprg, xlat, blank, dcprg, gsclk, ic_number);//setsup led |
Mach5 | 0:3f24fed55710 | 54 | ledstartup();//test all leds in sequence 0-15 in R then G then B |
Mach5 | 0:3f24fed55710 | 55 | //padpush(15,0xf0,0x0f,0x01); |
Mach5 | 0:3f24fed55710 | 56 | led1 =0; |
Mach5 | 0:3f24fed55710 | 57 | led2 =0; |
Mach5 | 0:3f24fed55710 | 58 | led3 =0; |
Mach5 | 0:3f24fed55710 | 59 | led4 =0; |
Mach5 | 0:3f24fed55710 | 60 | inter.fall(&ledstartup); |
Mach5 | 0:3f24fed55710 | 61 | interpulsea.fall(&ledtest); |
Mach5 | 0:3f24fed55710 | 62 | interpulseb.fall(&ledtest); |
Mach5 | 0:3f24fed55710 | 63 | while(1) // MAIN LOOOP ====================================================================<<<<<<<<<<<<<<<<<<< |
Mach5 | 0:3f24fed55710 | 64 | { |
Mach5 | 0:3f24fed55710 | 65 | muxenable = 1; |
Mach5 | 0:3f24fed55710 | 66 | for (int m = 0; m <=0xf; m++) |
Mach5 | 0:3f24fed55710 | 67 | { |
Mach5 | 0:3f24fed55710 | 68 | mux = m; |
Mach5 | 0:3f24fed55710 | 69 | //if adc > something do something |
Mach5 | 0:3f24fed55710 | 70 | } |
Mach5 | 0:3f24fed55710 | 71 | padpush(0x0,0x10,0xff,0x01); |
Mach5 | 0:3f24fed55710 | 72 | led4=!led4; |
Mach5 | 0:3f24fed55710 | 73 | update_led (spi, xlat, blank, (unsigned short*)leds); |
Mach5 | 0:3f24fed55710 | 74 | |
Mach5 | 0:3f24fed55710 | 75 | |
Mach5 | 0:3f24fed55710 | 76 | |
Mach5 | 0:3f24fed55710 | 77 | // update_led (spi, xlat, blank, (unsigned short*)leds); |
Mach5 | 0:3f24fed55710 | 78 | |
Mach5 | 0:3f24fed55710 | 79 | }//endofwhile |
Mach5 | 0:3f24fed55710 | 80 | }//endofmain |
Mach5 | 0:3f24fed55710 | 81 | |
Mach5 | 0:3f24fed55710 | 82 | |
Mach5 | 0:3f24fed55710 | 83 | |
Mach5 | 0:3f24fed55710 | 84 | |
Mach5 | 0:3f24fed55710 | 85 | |
Mach5 | 0:3f24fed55710 | 86 | |
Mach5 | 0:3f24fed55710 | 87 | |
Mach5 | 0:3f24fed55710 | 88 | |
Mach5 | 0:3f24fed55710 | 89 | |
Mach5 | 0:3f24fed55710 | 90 | |
Mach5 | 0:3f24fed55710 | 91 | //LCD FUNCTIONS================================================================================ |
Mach5 | 0:3f24fed55710 | 92 | |
Mach5 | 0:3f24fed55710 | 93 | void clear(void) {//clear lcd display |
Mach5 | 0:3f24fed55710 | 94 | lcdspi.putc(0xFE); |
Mach5 | 0:3f24fed55710 | 95 | lcdspi.putc(0x01); |
Mach5 | 0:3f24fed55710 | 96 | } |
Mach5 | 0:3f24fed55710 | 97 | |
Mach5 | 0:3f24fed55710 | 98 | |
Mach5 | 0:3f24fed55710 | 99 | void move_cursor(int line, int pos) {//move to a certain part of the lcd |
Mach5 | 0:3f24fed55710 | 100 | int cp; |
Mach5 | 0:3f24fed55710 | 101 | cp = pos + (line * 64) +128; |
Mach5 | 0:3f24fed55710 | 102 | lcdspi.putc(0xFE); |
Mach5 | 0:3f24fed55710 | 103 | lcdspi.putc(cp); |
Mach5 | 0:3f24fed55710 | 104 | } |
Mach5 | 0:3f24fed55710 | 105 | |
Mach5 | 0:3f24fed55710 | 106 | |
Mach5 | 0:3f24fed55710 | 107 | |
Mach5 | 0:3f24fed55710 | 108 | //Allan's Functions =================================================================== |
Mach5 | 0:3f24fed55710 | 109 | void startup(void) |
Mach5 | 0:3f24fed55710 | 110 | { |
Mach5 | 0:3f24fed55710 | 111 | for (int a = 0; a<3;a++) |
Mach5 | 0:3f24fed55710 | 112 | { |
Mach5 | 0:3f24fed55710 | 113 | clear(); |
Mach5 | 0:3f24fed55710 | 114 | move_cursor(0,0); |
Mach5 | 0:3f24fed55710 | 115 | lcdspi.printf("Initializing"); |
Mach5 | 0:3f24fed55710 | 116 | wait(0.2); |
Mach5 | 0:3f24fed55710 | 117 | lcdspi.printf("."); |
Mach5 | 0:3f24fed55710 | 118 | wait(0.2); |
Mach5 | 0:3f24fed55710 | 119 | lcdspi.printf("."); |
Mach5 | 0:3f24fed55710 | 120 | wait(0.2); |
Mach5 | 0:3f24fed55710 | 121 | lcdspi.printf("."); |
Mach5 | 0:3f24fed55710 | 122 | wait(0.2); |
Mach5 | 0:3f24fed55710 | 123 | } |
Mach5 | 0:3f24fed55710 | 124 | checkbattery();//in initialize, check battery levels |
Mach5 | 0:3f24fed55710 | 125 | } |
Mach5 | 0:3f24fed55710 | 126 | |
Mach5 | 0:3f24fed55710 | 127 | void checkbattery(void) |
Mach5 | 0:3f24fed55710 | 128 | { |
Mach5 | 0:3f24fed55710 | 129 | float batterylevel = vbatt*100; |
Mach5 | 0:3f24fed55710 | 130 | |
Mach5 | 0:3f24fed55710 | 131 | //Check Battery Levels |
Mach5 | 0:3f24fed55710 | 132 | led1 = (vbatt > 0.2) ? 1 : 0; |
Mach5 | 0:3f24fed55710 | 133 | led2 = (vbatt > 0.4) ? 1 : 0; |
Mach5 | 0:3f24fed55710 | 134 | led3 = (vbatt > 0.6) ? 1 : 0; |
Mach5 | 0:3f24fed55710 | 135 | led4 = (vbatt > 0.8) ? 1 : 0; |
Mach5 | 0:3f24fed55710 | 136 | if (vbatt< 0.2) { |
Mach5 | 0:3f24fed55710 | 137 | clear(); |
Mach5 | 0:3f24fed55710 | 138 | move_cursor(0,0); |
Mach5 | 0:3f24fed55710 | 139 | lcdspi.printf("WARNING"); |
Mach5 | 0:3f24fed55710 | 140 | move_cursor(1,0); |
Mach5 | 0:3f24fed55710 | 141 | lcdspi.printf("LOW BATTERY"); |
Mach5 | 0:3f24fed55710 | 142 | wait(2);} |
Mach5 | 0:3f24fed55710 | 143 | else |
Mach5 | 0:3f24fed55710 | 144 | { |
Mach5 | 0:3f24fed55710 | 145 | clear(); |
Mach5 | 0:3f24fed55710 | 146 | move_cursor(0,0); |
Mach5 | 0:3f24fed55710 | 147 | lcdspi.printf("Battery Level"); |
Mach5 | 0:3f24fed55710 | 148 | move_cursor(1,0); |
Mach5 | 0:3f24fed55710 | 149 | lcdspi.printf(" %.0f%%",batterylevel);} |
Mach5 | 0:3f24fed55710 | 150 | wait(2); |
Mach5 | 0:3f24fed55710 | 151 | |
Mach5 | 0:3f24fed55710 | 152 | } |
Mach5 | 0:3f24fed55710 | 153 | //CheckallLEDS on startup |
Mach5 | 0:3f24fed55710 | 154 | void ledstartup(void) |
Mach5 | 0:3f24fed55710 | 155 | { |
Mach5 | 0:3f24fed55710 | 156 | clear(); |
Mach5 | 0:3f24fed55710 | 157 | move_cursor(0,0); |
Mach5 | 0:3f24fed55710 | 158 | lcdspi.printf("LED TEST"); |
Mach5 | 0:3f24fed55710 | 159 | for (int t = 0; t<=48;t++) //will check all leds and colors R G B to ensure there are no shorts |
Mach5 | 0:3f24fed55710 | 160 | { |
Mach5 | 0:3f24fed55710 | 161 | clearall(); |
Mach5 | 0:3f24fed55710 | 162 | leds[t] = 0xff; |
Mach5 | 0:3f24fed55710 | 163 | update_led (spi, xlat, blank, (unsigned short*)leds); |
Mach5 | 0:3f24fed55710 | 164 | wait(0.02); |
Mach5 | 0:3f24fed55710 | 165 | |
Mach5 | 0:3f24fed55710 | 166 | } |
Mach5 | 0:3f24fed55710 | 167 | clear(); |
Mach5 | 0:3f24fed55710 | 168 | } |
Mach5 | 0:3f24fed55710 | 169 | |
Mach5 | 0:3f24fed55710 | 170 | void ledtest(void) |
Mach5 | 0:3f24fed55710 | 171 | { |
Mach5 | 0:3f24fed55710 | 172 | |
Mach5 | 0:3f24fed55710 | 173 | } |
Mach5 | 0:3f24fed55710 | 174 | |
Mach5 | 0:3f24fed55710 | 175 | |
Mach5 | 0:3f24fed55710 | 176 | //TLC5940 Pushbutton Functions============================================ |
Mach5 | 0:3f24fed55710 | 177 | //Writes to 1 button and allows you to control 3 colours |
Mach5 | 0:3f24fed55710 | 178 | //k = 0-15 indicates which button you want to select |
Mach5 | 0:3f24fed55710 | 179 | //r g b = value between 0x0-0xff. 0xff = full brightness |
Mach5 | 0:3f24fed55710 | 180 | void padpush (int k,int r,int g,int b) |
Mach5 | 0:3f24fed55710 | 181 | { |
Mach5 | 0:3f24fed55710 | 182 | //k=0x1-0xfff |
Mach5 | 0:3f24fed55710 | 183 | leds[k] = r; |
Mach5 | 0:3f24fed55710 | 184 | leds[k+16] = g; |
Mach5 | 0:3f24fed55710 | 185 | leds[k+32] = b; |
Mach5 | 0:3f24fed55710 | 186 | } |
Mach5 | 0:3f24fed55710 | 187 | //Turn all LEDS on pads off |
Mach5 | 0:3f24fed55710 | 188 | void clearall(void) |
Mach5 | 0:3f24fed55710 | 189 | { |
Mach5 | 0:3f24fed55710 | 190 | for (int i = 0; i<=(ic_number*16-1);i++) |
Mach5 | 0:3f24fed55710 | 191 | { |
Mach5 | 0:3f24fed55710 | 192 | leds [i] =0; |
Mach5 | 0:3f24fed55710 | 193 | } |
Mach5 | 0:3f24fed55710 | 194 | } |