Dual CANbus monitor and instrumentation cluster

Eagle Schematic and Board design

/media/uploads/TickTock/canaryr5.zip

/media/uploads/TickTock/sch_r4.jpg /media/uploads/TickTock/lcdsch.jpg /media/uploads/TickTock/brd_r4.jpg

Parts List

qtyinstancepart #packagesupplierDescription
1BAT3Vhttp://www.ebay.com/itm/10x-CR2032-SMD-Battery-Holder-for-CR2032-Battery-/180938057979?pt=LH_DefaultDomain_0&hash=item2a20bfa8fbLithium 2032 coin battery holder
4C1-C4ECST1DC106R6032Tantalium capacitor 10uF
3FC1-FC3ZF1-20-01-T-WThttp://www.samtec.com/cable-systems/idc-ffc/ffc/zero-insertion.aspx20 conductor 1mm pitch flex cable connector (optional)
1FJ-20-R-08.00-4http://www.samtec.com/cable-systems/idc-ffc/ffc/zero-insertion.aspx8\" 20 conductor 1mm pitch flex connector, end reversed (optional)
2H1-H4(DON'T populate H1-H4 headers - solder mbed directly)
1H5http://www.ebay.com/itm/221186042943?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l26491x12 .1\" pitch header (optional)
1H62x6 .1\" pitch header (optional)
2IC1,IC2VP230LMDSOP8http://www.ebay.com/itm/130488665247?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649canbus transciever
1IC3LM1117-5VSOT2235V regulator
5JP*2 pin .1\" jumper header
1mbedLPC1768http://www.ebay.com/itm/200830573509?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649mbed uC
2Q1,Q22N2222SOT23General purpose NPN transistor
1R1R392M12063.9K resistor
4R2,R4-R6R102M12061K resistor
1R3R500M120650 Ohm resistor
2TR1-TR5ZJYS81R5-2PL51TG01http://www.digikey.com/product-detail/en/ZJYS81R5-2PL51T-G01/445-2223-1-ND/765232CM Choke
1Z11N5340BGC1702-15http://www.ebay.com/itm/150878122425?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l26496V, 5W Zener Diode
1X1USBhttp://www.ebay.com/itm/New-Vertical-USB-2-0-A-pcb-connector-socket-USB-A-Type-/300553895292?pt=LH_DefaultDomain_0&hash=item45fa687d7cvertical USB connector
2LCD0,LCD1TFThttp://www.mikroe.com/add-on-boards/display/tft-proto/320x240 LCD with touch screen

Assembly

1) LCD Displays

I found ribbon cable is a nice way to organize the wires to the displays. You'll need 12 conductors and I connected them in the following order:

1LED+
2LED-
3RSTB
4MOSI
5SCLK
6CSB
7X+
8X-
9Y+
10Y-
11VDD
12GND

First I connected all the GND connections (IM0, IM1, IM3, & 2 GND). Do not connect the bottom GND until you have the ribbon cable connected. After making all the ribbon cable connections (connecting the GND of the ribbon cable to the bottom GND pad), solder the GND bar from the previous step to the back of the bottom GND connection. Finally, make a connection from the back side 3.3V pin to IM2. Take a break and repeat for the second display. /media/uploads/TickTock/lcdtop.jpg /media/uploads/TickTock/lcdbot.jpg Once the two displays are complete combine all wires except CS0, CS1, X+, X-, Y+, and Y-. Connect X- of the left display to X+ of the right. Similarly connect Y- of the left display to Y+ of the right. Insulate any exposed wires.

2) PCB

Refer to the schematics to place all the components on the board. If you plan to install into the CANary 3D enclosure, DO NOT install the battery holder or the socket for the mbed and, instead, connect two wires to the VB and GND pads nearby. You will have to install the battery holder against the back wall to avoid interfering with the right-hand display and the mbed will have to be directly soldered. I have not found a socket with a low enough profile to fit in the space provided (depth of enclosure is limited by the space behind the center console). Also, I recommend keeping as much lead as possible on the Zener diode (bending it as shown to clear the back wall). Although it is operating well within parameters, the Zener gets quite hot during extended operation and the leads help dissipate the heat and keep it away from the PCB and other components.

/media/uploads/TickTock/pcbtop.jpg /media/uploads/TickTock/pcbbot.jpg

Once the PCB is populated, solder the LCDs to the PCB. CS0 connects to the right display and CS1 connects to the left. /media/uploads/TickTock/brddis.jpg

3) Testing

1)First step is to buzz out all connections from the LCDs to the pins in the main board
2)Next check the touch screen connections. On the main board, place an Ohm meter across X+ and X-. You should read 700 Ohms. Repeat for Y+ and Y-. Then test the resistance from X+ to Y+. With nothing touching the screens, it should read >100K Ohms and <1K when touching either screen.
3)When all connections are checked, solder in the mbed. Download and install the touch2 program http://mbed.org/users/TickTock/code/touch2/ to test the basic operation of the mbed and touch screens.
tips:
Touch screen is sensitive - excess flux on X+,X-,Y+,Y- connection on mbed can result in flakey operation
If touch is not working, double-check the LCD0_CS and LCD1_CS are not swapped. LCD0_CS must connect to the CS of the LCD that has X- & Y- connected to the mbed. LCD1_CS must connect to the CS of the LCD that has X+ & Y+ connected to the mbed.
4)Once touch2 works, it is time to connect to the OBD connector. I highly recommend double checking all connections from the OBD to the PCB with the cable in place before connecting to the Leaf. Buzz out all the pins in the OBS to make sure none are shorting to each other, Check that the 12V goes to the Zener (and nothing else) and the switched 12V to the resistor divider (and nothing else). Test the ground connection properly connects to ground and nothing else.
5)Once you are confident there are no shorts or wrong connections from the OBD connector, take a deep breath and plug it into your leaf. Touch2 program should come up and function. Unplug and install the latest CANary firmware. Test all features before installing into the enclosure (gids, cellpair, menu system, logging) since installing and removing from the enclosure is a PITA.

/media/uploads/TickTock/pcbdone.jpg /media/uploads/TickTock/functioning.jpg

4) Enclosure

The 3D printer leaves a lot of powder behind - I used a strong spray of water to get it out of all the cracks. The enclosure comes with a rather rough finish. I recommend convincing yourself you like it, then simply lightly sand then paint before assembly. Sanding is very difficult - the nylon is very nicely fused and doesn't want to sand. I tried sandblasting and that didn't work either. I had some limited success with filler and then sanding, but only on the outside - it is too difficult to sand the face.

5) Final Assembly

Files at this revision

API Documentation at this revision

Comitter:
TickTock
Date:
Mon Apr 22 02:27:37 2013 +0000
Parent:
91:33ab084a184f
Child:
93:c2402e8cd0e2
Commit message:
Reverting to keep latest as last know *tested* revision. logCan interrupt fix work moved to CANaryTest fork.

Changed in this revision

displayModes.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
utility.cpp Show annotated file Show diff for this revision Revisions of this file
utility.h Show annotated file Show diff for this revision Revisions of this file
--- a/displayModes.cpp	Mon Apr 22 02:22:04 2013 +0000
+++ b/displayModes.cpp	Mon Apr 22 02:27:37 2013 +0000
@@ -11,7 +11,7 @@
     tt.background(Yellow);
     if(force) tt.cls(); // Just clear screen if forced - always update display
     tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
-    for(int i=0; i<19; ++i){
+    for(int i=0; i<19; i++){
         msg = lastMsg[i+indexOffset];
         printf("%03x : %02x %02x %02x %02x %02x %02x %02x %02x    \n",msg.id,msg.data[0],msg.data[1],msg.data[2],msg.data[3],msg.data[4],msg.data[5],msg.data[6],msg.data[7]);
     }
@@ -40,7 +40,7 @@
         if(msgChanged[j]>0){
             msg = lastMsg[j];
             printf("%03x : %02x %02x %02x %02x %02x %02x %02x %02x    \n",msg.id,msg.data[0],msg.data[1],msg.data[2],msg.data[3],msg.data[4],msg.data[5],msg.data[6],msg.data[7]);
-            ++i;
+            i++;
         }// if changed
     }while(i<19&&j<99);
     if((sMode==1)&&showButtons){
@@ -62,7 +62,7 @@
         tt.cls();
         tt.locate(0,6);
         tt.set_font((unsigned char*) Arial12x12);
-        for(int i=0; i<19; ++i){
+        for(int i=0; i<19; i++){
             printf("%s",displayLog[displayLoc]);
             displayLoc=displayLoc>17?0:displayLoc+1;
         }
@@ -252,7 +252,7 @@
     }
 
     // plot bar graph for each wheel pressure
-    for (i=0; i<4; ++i){
+    for (i=0; i<4; i++){
         if (msg.data[i]<239) {
             if (msg.data[i]>lastPressure[i]){
                 tt.fillrect(10+40*i,239-msg.data[i],40+40*i,239,Red);
@@ -322,7 +322,7 @@
         max=0;
         min=9999;
         avg=0;
-        for(i=0; i<96; ++i){
+        for(i=0; i<96; i++){
            bd=(battData[i*2+3]<<8)+battData[i*2+4];
            avg+=bd;
             if(bd>max) max=bd;
@@ -340,7 +340,7 @@
         printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %02dC %02dC %02dC %02dC\n\n",
                     max,min,avg,jv, battData[224+5],battData[224+8],battData[224+11],battData[224+14]);
         tt.locate(0,36);
-        for(i=0; i<16; ++i){
+        for(i=0; i<16; i++){
             printf("%02d-%02d : %04d %04d %04d %04d %04d %04d\n",
                 i*6+1,i*6+6,
                 (battData[i*12+3]<<8)+battData[i*12+4],(battData[i*12+5]<<8)+battData[i*12+6],
@@ -351,7 +351,7 @@
         tt.rect(8+1*41,16,40+1*41,28,Yellow);
         //tt.rect(8+2*41,16,40+2*41,28,White);
         tt.rect(8+3*41,16,40+3*41,28,Red);
-        for(i=0; i<96; ++i){
+        for(i=0; i<96; i++){
             bd=(battData[i*2+3]<<8)+battData[i*2+4];
             if(bd>0){
                 if(bd==max) tt.rect(58+(i%6)*41,34+(int)(i/6)*12,90+(i%6)*41,46+(int)(i/6)*12,Green);
@@ -431,7 +431,7 @@
         avg=0;
        
         // calc each cell-pair voltage, find max and min
-        for(i=0; i<96; ++i){
+        for(i=0; i<96; i++){
            bd=(battData[i*2+3]<<8)+battData[i*2+4];
            nBar[i] = bd; // init to bar height
            avg+=bd;
@@ -470,7 +470,7 @@
             max = min + 95/4 ; // for small values
             avg = ( max + min ) / 2;
             iBarValMax = max - min ; // zero to N
-            for(int i=0; i<96; ++i) {
+            for(int i=0; i<96; i++) {
               //nBar[i] = i*2 + min ; // test tall values
               nBar[i] = i/4 + min ; // test small values
             }
@@ -480,7 +480,7 @@
         if( nBarScale < 0.1 ) nBarScale = 0.1 ;
     
         // do the Bar-height scaling
-        for(int i=0; i<96; ++i){
+        for(int i=0; i<96; i++){
           nBar[i] -= min ; // now, 0 to N = iBinValMax
           nBar[i] *= nBarScale ; // scale, as needed
         }
@@ -503,7 +503,7 @@
         
         int xPos = xWinMin + 2 ; // start one from the left
          
-        for( int i=0; i<96; ++i) {
+        for( int i=0; i<96; i++) {
             height = nBar[i] ;
             if( height > 100 ) height = 100 ; // clip tops
             
@@ -551,7 +551,7 @@
         max=0;
         min=9999;
         avg=0;
-        for(i=0; i<96; ++i){
+        for(i=0; i<96; i++){
            bd=(battData[i*2+3]<<8)+battData[i*2+4];
            avg+=bd;
             if(bd>max) max=bd;
@@ -584,15 +584,15 @@
         if( iBinValMax > iBinIndxMax ) iBinValMax = iBinIndxMax ;
     
         // clean the bins
-        for(int i=0; i<=iBinIndxMax; ++i) {
+        for(int i=0; i<=iBinIndxMax; i++) {
           nBin[i] = 0;
         }
         
         // do the bin counting
-        for(int i=0; i<96; ++i){
+        for(int i=0; i<96; i++){
           bd=(battData[i*2+3]<<8)+battData[i*2+4] - min ;
           if( bd > iBinValMax ) bd = iBinValMax ;
-          ++nBin[bd] ;
+          nBin[bd] ++ ;
         }
         
         //----------------
@@ -602,7 +602,7 @@
             max = 50 ;
             avg = ( max + min ) / 2;
             iBinValMax = max - min ;
-            for(int i=0; i<=(iBinValMax/2); ++i) {
+            for(int i=0; i<=(iBinValMax/2); i++) {
               nBin[i] = i ;
               nBin[iBinValMax-i] = i ;
             }
@@ -622,7 +622,7 @@
         int xPos = (xWinMin + xWinMax) / 2 ; 
         xPos -= (avg-min) * (nBarWidth + nBarSpace) ;
         
-        for( int i=0; i<=iBinValMax; ++i) {
+        for( int i=0; i<=iBinValMax; i++) {
             height = 4 * nBin[i] ;
             if( height > 100 ) height = 100 ; // clip tops
             
@@ -832,7 +832,7 @@
     }
     if(force||lgids!=gids){ // update Y axis when kWh changes
         tt.set_font((unsigned char*) Arial12x12);
-        for(i=0;i<10;++i){
+        for(i=0;i<10;i++){
             y=200-i*20;
             tt.locate(10,y-8);
             if (showMiles){
@@ -845,7 +845,7 @@
         lgids=gids;    
     }
     if(updateDTE||force){
-        for(i=0;i<10;++i){
+        for(i=0;i<10;i++){
             y=200-i*20;
             tt.line(40,y,158,y,DarkGrey);
         }
@@ -888,7 +888,7 @@
         tt.fillcircle(lx,leff[0],radius,Navy);
         tt.fillcircle(lx,ly,radius,color);
 
-        for(i=1;i<toVal;++i){
+        for(i=1;i<toVal;i++){
             x=50+i*6;
             y=mpkWh[i]*20;
             if(y<200) {
@@ -1197,7 +1197,7 @@
         lastPressure[3] = 200;
     }
     // plot bar graph for each wheel pressure
-    for (i=0; i<4; ++i){
+    for (i=0; i<4; i++){
         if (msg.data[i]<239) {
             if (msg.data[i]>lastPressure[i]){
                 tt.fillrect(10+40*i,239-msg.data[i],40+40*i,239,Red);
--- a/main.cpp	Mon Apr 22 02:22:04 2013 +0000
+++ b/main.cpp	Mon Apr 22 02:27:37 2013 +0000
@@ -51,7 +51,7 @@
 // gg - revStr is used in 2 places
 // gg - and is easy to edit here
 // gg - added ZeroSecTick and revStr
-char revStr[7] = "90";
+char revStr[7] = "92";
 
 bool debugMode = false;
 bool logEn = false, logOpen = false; 
@@ -145,7 +145,7 @@
     timer.start() ;
     RTC_Init(); // start the RTC Interrupts that sync the timer
     struct tm t; // pointer to a static tm structure
-    NVIC_SetPriority(TIMER3_IRQn, 3); //set ticker priority
+    NVIC_SetPriority(TIMER3_IRQn, 1); //set ticker priority
     NVIC_SetPriority(CAN_IRQn, 2); //higher than can (so RTC sync works)
 
     seconds = time(NULL);
@@ -160,11 +160,11 @@
     }
     t = *localtime(&seconds) ;
     strftime(sTemp, 32, "%a %m/%d/%Y %X\n", &t);
-    printMsg(sTemp); // record RTC
+    logMsg(sTemp); // record RTC
     
     // revision
     sprintf(sTemp,"CANary firmware rev%s\n", revStr); // gg - for Logging the revision
-    printMsg(sTemp); // revision
+    logMsg(sTemp); // revision
 
     // Look for new binary on thumbdrive
     // Can't make this work right now since USB doesn't attach the right timestamp (so new binary isn't loaded)
@@ -172,11 +172,11 @@
     lastDMode[whichTouched]=99;//force refresh
     if (cfile!=NULL){ //found a new binary on the thumbdrive so copy it over
         sprintf(sTemp,"New binary found.\n");
-        printMsg(sTemp); // new binary
+        logMsg(sTemp); // new binary
         file = fopen("/local/CANary.bin", "wb");
         if (file==NULL){ //failed to open destination
             sprintf(sTemp,"Unable to open destination file.\n");
-            printMsg(sTemp); // cannot open CANary.bin
+            logMsg(sTemp); // cannot open CANary.bin
         } else {
             tt.set_display(2);
             tt.foreground(White);
@@ -206,7 +206,7 @@
     //read efficiency history data
     cfile = fopen("/local/ehist.cny", "r");
     if (cfile!=NULL){ // found a efficiency history file
-        for(i=0;i<39;++i){
+        for(i=0;i<39;i++){
             if(!feof(cfile)){
                 fscanf(cfile,"%f %f\r\n",&mph[i],&kW[i]);
                 mpkWh[i]=mph[i]/kW[i];
@@ -214,11 +214,11 @@
         }
         fclose(cfile);
         sprintf(sTemp,"History Loaded.\n");
-        printMsg(sTemp); // History loaded
+        logMsg(sTemp); // History loaded
     } else { // create initial file
         sprintf(sTemp,"History not found.  Created.\n");
-        printMsg(sTemp); // history not found, created
-        for(i=0;i<39;++i){
+        logMsg(sTemp); // history not found, created
+        for(i=0;i<39;i++){
             // Pre-load with 4 mpkWh @ 40 mph
             mph[i]=40*timeConstant[i];
             kW[i]=10*timeConstant[i];
@@ -251,24 +251,24 @@
                 t = *localtime(&seconds) ;
                 strftime(fileName, 32, "/usb/%m%d%H%M.alc", &t); //mmddhhmm.alc
                 //sprintf(sTemp,"Using file %s\n",fileName);
-                //printMsg(sTemp); // using alc file ...
+                //logMsg(sTemp); // using alc file ...
                 file = fopen(fileName, "ab");
                 lastDMode[whichTouched]=99;//force refresh
                 if(file==NULL){
                     sprintf(sTemp,"\nUnable to open %s\n\n\n\n",fileName);
-                    printMsg(sTemp); // cannot open alc file
+                    logMsg(sTemp); // cannot open alc file
                     logEn=false;
                     spkr.beep(1000,0.25);
                 } else {
                     logOpen = true;
                     readPointer=writePointer;
                     sprintf(sTemp,"Starting Can Log %s\n",fileName);
-                    printMsg(sTemp); // starting alc log file 
+                    logMsg(sTemp); // starting alc log file 
                     
                     logTS(); // Date Time at start
-                    logEventMsg("Starting"); // Log startup msg for testing
+                    logErrMsg("Starting"); // Log startup msg for testing
                     sprintf(sTemp,"Cr%s",revStr);
-                    logEventMsg(sTemp); // gg - log firmware version
+                    logErrMsg(sTemp); // gg - log firmware version
                     
                     spkr.beep(2000,0.25);
                 }
@@ -280,12 +280,12 @@
                 if (file == NULL) {
                     logOpen = false;
                     sprintf(sTemp,"Failed to append log file.\n");
-                    printMsg(sTemp); // failed to append 
+                    logMsg(sTemp); // failed to append 
                     spkr.beep(1000,0.25);
                     logEn=false;
                 } else {
                     while (readPointer != writePointer) {
-                        for (j = 0; j<13; ++j){
+                        for (j = 0; j<13; j++){
                             fprintf(file,"%c",writeBuffer[readPointer][j]);
                         }
                         if(++readPointer >= maxBufLen){
@@ -297,7 +297,7 @@
             } // if > 1/16 full, canbus has stopped, or logging stopped
             if (!logEn) {
                 sprintf(sTemp,"Stopping Can Log %s\n",fileName);
-                printMsg(sTemp); // stopping alc log file 
+                logMsg(sTemp); // stopping alc log file 
                 fclose(file);
                 logOpen=false;
                 pointerSep=0;
@@ -310,7 +310,7 @@
             seconds = time(NULL);
             t = *localtime(&seconds) ;
             strftime(sTemp, 40, "Sleeping: %a %m/%d/%Y %X\n", &t);
-            printMsg(sTemp); // sleeping date time
+            logMsg(sTemp); // sleeping date time
             updateDisplay(0); //Added for turbo3 who has a display override and wants to see the sleep message before going to sleep
             updateDisplay(1);
             //LPC_RTC->CIIR=0x00; // block RTC interrupts
@@ -333,13 +333,13 @@
             seconds = time(NULL);
             t = *localtime(&seconds) ;
             strftime(sTemp, 40, "Waking: %a %m/%d/%Y %X\n", &t);
-            printMsg(sTemp); // wakeup date time
+            logMsg(sTemp); // wakeup date time
             if (time(NULL)>(secs+1800)) {
                 logOpen = false; // Start new file if asleep for more than 30 minutes
                 if (secsNoTouch>100) secsNoTouch = 100; // also mostly reset user Idle counter
             } else if (logOpen){ // insert timestamp on each wake if logging enabled (disabled for now)
                 file = fopen(fileName, "ab");
-                logEventMsg("WakingUp"); // gg - use messeges
+                logErrMsg("WakingUp"); // gg - use messeges
                 logTS(); // Date-Time at wakeup
             }
         } // if idle
@@ -366,7 +366,7 @@
                 sMode = 1;
             }
             //sprintf(sTemp,"%d,%d ",lastTouch.x,lastTouch.y);
-            //printMsg(sTemp); // touch x,y - for debug
+            //logMsg(sTemp); // touch x,y - for debug
             touched = false; // clear interrupt flag
         }
         //---------------
@@ -380,7 +380,7 @@
                 secsNoTouch +=2; // increment to prevent double touch
                 sMode = 1;
                 //sprintf(sTemp,"button %d %d,%d %d\n",i,buttonX(lastTouch.x,3),buttonY(lastTouch.y,3),lastTouch.x);
-                //printMsg(sTemp); // button parms - for debug
+                //logMsg(sTemp); // button parms - for debug
                 switch (sMode) {
                     case 0: // no select
                         break;
@@ -420,7 +420,7 @@
                             //-----------------------------------------------
                             case 10: // 1,0 (col left of center,top row) on screen 0 or 1
                                 if (dMode[whichTouched]==changedScreen) {
-                                    for(j=0;j<100;++j) msgChanged[j]=0; // clear changed data
+                                    for(j=0;j<100;j++) msgChanged[j]=0; // clear changed data
                                     lastDMode[whichTouched]=99;//force refresh
                                 } else if (dMode[whichTouched] == indexScreen) { // gg - index
                                     sMode=0;
@@ -457,7 +457,7 @@
                                     sMode=0;
                                 } else if (dMode[whichTouched]==config1Screen) {
                                     sprintf(sTemp,"Saving config file.\n");
-                                    printMsg(sTemp); // saving config
+                                    logMsg(sTemp); // saving config
                                     saveConfig();
                                     spkr.beep(2000,0.25);
                                 } else if (dMode[whichTouched]==playbackScreen) { // faster
@@ -487,7 +487,7 @@
                                     lastDMode[whichTouched ^ 1]=99; // repaint other screen (^ = XOR)
                                     
                                     //sprintf(sTemp,"Changed Skin to %d.\n");
-                                    //printMsg(sTemp); /// changed skin - for debug
+                                    //logMsg(sTemp); /// changed skin - for debug
                                     //saveConfig();
                                     //spkr.beep(2000,0.25);
                                     
@@ -539,21 +539,21 @@
                                             lastDMode[whichTouched]=99;//force refresh
                                             if(file==NULL){
                                                 sprintf(sTemp,"Unable to open /usb/playback.alc\n");
-                                                printMsg(sTemp); // no playback.alc
+                                                logMsg(sTemp); // no playback.alc
                                                 spkr.beep(1000,0.25);
                                             } else {
                                                 playbackOpen = true;
                                                 playbackEn=true;
                                                 playback.attach(&playbackISR,playbackInt);
                                                 sprintf(sTemp,"Starting playback\n");
-                                                printMsg(sTemp); // start playback
+                                                logMsg(sTemp); // start playback
                                                 spkr.beep(2000,0.25);
                                                 can1.attach(&doNothing);// Stop recieving CAN data
                                                 can2.attach(&doNothing);
                                             }
                                         } else {
                                             sprintf(sTemp,"Must stop logging first\n");
-                                            printMsg(sTemp); // Must stop logging first
+                                            logMsg(sTemp); // Must stop logging first
                                         }
                                     } else {
                                         playback.detach();
@@ -676,14 +676,13 @@
         }
 
         if(tick){ // Executes once a second
-            headlights = (lastMsg[indexLastMsg[0x358]].data[1]&0x80)?true:false;  // headlight/turn signal indicator
             accV=floor(mon12V*scale12V*10+0.5)/10; //Round to nearest 10th
             accOn=(accV>5)?true:false;
             if(laccOn&&!accOn){ // Car turned off
                 //write efficiency history data
                 cfile = fopen("/local/ehist.cny", "w");
                 if (cfile!=NULL){ // found a efficiency history file
-                    for(i=0;i<39;++i){
+                    for(i=0;i<39;i++){
                         fprintf(cfile,"%f %f\r\n",mph[i],kW[i]);
                     }
                     fclose(cfile);
@@ -692,7 +691,7 @@
             laccOn=accOn;
             if(!accOn&&!logEn&&userIdle&&!playbackEn){
                 //sprintf(sTemp,"Display Off %4.2f\n",accV);
-                //printMsg(sTemp); // display off - for debug
+                //logMsg(sTemp); // display off - for debug
                 
                 dled = 0; // turn off display if car off and logging disabled and no user activity
             }else if(!headlights){
@@ -727,7 +726,7 @@
             mWs_x4=0;
             numWsamples=0;
             if(accOn||playbackEn){
-                for(i=1;i<39;++i){
+                for(i=1;i<39;i++){
                     average=mph[i]/timeConstant[i];
                     mph[i]-=average;
                     mph[i]+=mph[0];
@@ -746,7 +745,7 @@
             if(logCP)
                 logPackVoltages(); // Turbo3, only call
             tick=false;
-        }// tick
+        }
 
         display=display<1?display+1:0; // toggle display
         updateDisplay(display);
@@ -760,7 +759,7 @@
         
         if(step){ // playback
             if(playbackOpen&&playbackEn){
-                for(i=0;i<120;++i){
+                for(i=0;i<120;i++){
                     if(!feof(file)){
                         fscanf(file,"%5c%8c",&header,&data);
                         logCan(header[0],CANMessage(0x7ff&((header[4]<<8)+header[3]), data, 8)); // Playback
--- a/utility.cpp	Mon Apr 22 02:22:04 2013 +0000
+++ b/utility.cpp	Mon Apr 22 02:27:37 2013 +0000
@@ -17,11 +17,11 @@
     LPC_RTC->ILR=0x00; // set up the RTC interrupts
     LPC_RTC->CIIR=0x01; // interrupts each second
     LPC_RTC->CCR = 0x01;  // Clock enable
-    NVIC_SetPriority( RTC_IRQn, 1 );
+    //NVIC_SetPriority( RTC_IRQn, 10 );
     NVIC_EnableIRQ( RTC_IRQn );
 }
 
-void printMsg (char *msg) {
+void logMsg (char *msg) {
     strcpy(displayLog[displayLoc],msg);
     displayLoc=displayLoc>17?0:displayLoc+1;
 }
@@ -48,8 +48,10 @@
     static unsigned short nLost = 0; // gg - overrun
  
     char sTemp[40];    
-    unsigned char changed, i;
-    signed short packV, packA;
+    unsigned char changed;
+    unsigned short i; // was unsigned char
+    signed short packV;
+    signed short packA;
     signed long imWs_x4;
     unsigned short ts;
 
@@ -89,7 +91,7 @@
                         writeBuffer[tempWritePointer][3]=0xfe; // MsgID, low byte
                         writeBuffer[tempWritePointer][4]=0xff; // Len nibble, and MsgID high nibble
                         // lay in the "Lost0002" text
-                        for(i=5;i<13;++i){ 
+                        for(i=5;i<13;i++){ 
                             writeBuffer[tempWritePointer][i]= strLost[i-5];
                         }
                     } else {
@@ -99,7 +101,7 @@
                         
                         // lay the new count into the comment
                         sprintf(strLost,"%04d",nLost);
-                        for(i=9;i<13;++i){ 
+                        for(i=9;i<13;i++){ 
                             writeBuffer[tempWritePointer][i]= strLost[i-9];
                         }
                     }
@@ -113,7 +115,7 @@
                     writeBuffer[writePointer][3]=canRXmsg.id&0xff; // MsgID, low byte
                     char sLen = canRXmsg.len ;
                     writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(sLen<<4); // Len nibble, and MsgID high nibble
-                    for(i=0;i<8;++i){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?)
+                    for(i=0;i<8;i++){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?)
                         if(i<sLen) 
                             writeBuffer[writePointer][i+5]=canRXmsg.data[i];
                         else // i>=sLen
@@ -135,7 +137,7 @@
                     if( nLost > 0 ) {
                         // We previously lost messages that did not get into the buffer
                         sprintf(sTemp,"-- Lost %d Messages.\n", nLost);
-                        printMsg(sTemp); // write buffer overrun
+                        logMsg(sTemp); // write buffer overrun
                         //spkr.beep(500,0.25);
                         
                         nLost = 0 ;
@@ -144,49 +146,45 @@
                 }
             }
         }
-    } else { // not debugMode - keep code short
+    }else{ // not debugMode - keep code short
         if(logOpen){
-            int localWritePointer = writePointer++; // create local copy to make logCan reentrant
-            // note that the static variables above do not prevent safe reentry
-            // since they are only used for msgId<0x800 which will never interrupt
-            // another msgId<0x800 (both CANbusses are same priority)
-            if (writePointer >= maxBufLen) {
-                writePointer = 0;
-                led3 = !led3;
-            }
-            if (localWritePointer >= maxBufLen) { //Have to test local, too, just in case interrupted just after increment (got double incremented)
-                localWritePointer = 0;
-            }
-            ts=getTimeStamp(); // only use
-            writeBuffer[localWritePointer][0]=mType;
-            writeBuffer[localWritePointer][1]=(ts&0xff00)>>8;
-            writeBuffer[localWritePointer][2]=(ts&0x00ff);
-            writeBuffer[localWritePointer][3]=canRXmsg.id&0xff;
-            writeBuffer[localWritePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
-            for(i=5;i<13;++i){ // Is there a better way to do this? (writeBuffer[localWritePointer][i]=canRXmsg.data?)
-                writeBuffer[localWritePointer][i]=canRXmsg.data[i-5];
-            }
-            if (writePointer==readPointer) {
-                // Just caught up to read pointer
-                sprintf(sTemp,"Write buffer overrun.\n");
-                printMsg(sTemp); // write buffer overrun
-                spkr.beep(500,0.25);
+            if(canRXmsg.id>0) {
+                ts=getTimeStamp(); // only use
+                writeBuffer[writePointer][0]=mType;
+                writeBuffer[writePointer][1]=(ts&0xff00)>>8;
+                writeBuffer[writePointer][2]=(ts&0x00ff);
+                writeBuffer[writePointer][3]=canRXmsg.id&0xff;
+                writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
+                for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?)
+                    writeBuffer[writePointer][i]=canRXmsg.data[i-5];
+                }
+                if (++writePointer >= maxBufLen) {
+                    writePointer = 0;
+                    led3 = !led3;
+                }
+                if (writePointer==readPointer) {
+                    // Just overwrote an entry that hasn't been sent to thumbdrive
+                    sprintf(sTemp,"Write buffer overrun.\n");
+                    logMsg(sTemp); // write buffer overrun
+                    spkr.beep(500,0.25);
+                }
             }
         }
     }
 
     if(canRXmsg.id<0x800){ // Block FFE and FFF messages
-
         if(indexLastMsg[canRXmsg.id]==0) { //Check if no entry
+            //ii=ii<99?ii+1:0; // Should never wrap - less than 100 different messages ever used
             if(ii<99) {
+                //indexLastMsg[canRXmsg.id]=ii; //Create entry if first message
                 indexLastMsg[canRXmsg.id]=++ii; //Create entry for first MsgID occurance
                 // ii max is 99 here
             } else {
                 // the ii array is full, more than 100 MsgIDs found
                 if(ii==99) {
-                    ++ii; // step to 100 to log only one error
+                    ii++; // step to 100 to log only one error
                     sprintf(sTemp,"MsgID buffer overrun.\n");
-                    printMsg(sTemp); // write buffer overrun
+                    logMsg(sTemp); // write buffer overrun
                 }
             }
         }
@@ -195,7 +193,7 @@
         if(dMode[0]==changedScreen||dMode[1]==changedScreen){// Skip if not using (for execution speed)
             changed=msgChanged[indexLastMsg[canRXmsg.id]];
             // This is cleared in the main loop when reset button is touched
-            for(i=0;i<8;++i){
+            for(i=0;i<8;i++){
                 if(lastMsg[indexLastMsg[canRXmsg.id]].data[i]!=canRXmsg.data[i]){
                     changed |= 1<<i;
                 }
@@ -207,32 +205,35 @@
         
         //-------------------
         //Miscellaneous on-recieve operations below
-        if((mType==1)&&(canRXmsg.id==0x7bb)){ // is battery data?  Need to store all responses
+        if((mType==2)&&(canRXmsg.id==0x358)){ // headlight/turn signal indicator
+            headlights = (canRXmsg.data[1]&0x80)?true:false;
+            
+        }else if((mType==1)&&(canRXmsg.id==0x7bb)){ // is battery data?  Need to store all responses
             if(canRXmsg.data[0]<0x20){
                 if(canRXmsg.data[3]==2){//Group 2 = cellpair data
                     bdi=BatDataBaseG2; // index offset for CP data (uses 00 - 1C)
                     sprintf(sTemp,"  Getting cell pair data\n");
-                    printMsg(sTemp);
+                    logMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==4){//Group 4 = temperature data
                     bdi=BatDataBaseG4; // index offset for Temperature data (uses 20 - 22)
                     sprintf(sTemp,"  Getting temperature data\n");
-                    printMsg(sTemp);
+                    logMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==1){//Group 1 data
                     bdi=BatDataBaseG1; // index offset for Group 1 data (uses 20 - 22)
                     sprintf(sTemp,"Getting Group 1 data\n");
-                    printMsg(sTemp);
+                    logMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==3){//Group 3 data
                     bdi=BatDataBaseG3; // index offset for Group 3 data (uses 20 - 22)
                     sprintf(sTemp,"  Getting Group 3 data\n");
-                    printMsg(sTemp);
+                    logMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==5){//Group 5 data
                     bdi=BatDataBaseG5; // index offset for Group 5 data (uses 20 - 22)
                     sprintf(sTemp,"  Getting Group 5 data\n");
-                    printMsg(sTemp);
+                    logMsg(sTemp);
                     
                 }else bdi=0xff; // ignore other messages (for now)
                 lasti=0;
@@ -274,7 +275,7 @@
             imWs_x4 *= -packA; // milliWattseconds*4
             if (!((imotorRPM<2)&&(imWs_x4<0))){ //Ignore if charging from wall
                 mWs_x4 += imWs_x4; // total mWs_x4
-                ++numWsamples;
+                numWsamples++;
             }
         }else if((mType==1)&&(canRXmsg.id==0x1da)){ //Motor Speed
             imotorRPM=((canRXmsg.data[4]<<8)|(canRXmsg.data[5]));
@@ -282,7 +283,7 @@
                 imotorRPM=-imotorRPM;
             }
             motorRPM+=imotorRPM;
-            ++numSsamples;
+            numSsamples++;
         }
     }
 }
@@ -306,16 +307,16 @@
     logCan(0,tsMsg); // Date-Time
 }
 
-void logEventMsg (char * eventMsg) {
+void logErrMsg (char * errMsg) {
     // log CAN-Do 8-character Pseudo Message
     CANMessage tsMsg;
     tsMsg.id=0xffe; // pseudo Message to CAN-Do log
     tsMsg.len=0xf;
-    int iMsgLen = strlen(eventMsg);
+    int iMsgLen = strlen(errMsg);
     // 8 character message compatible with CAN-Do
-    for(int i=0; i<8; ++i){
+    for(int i=0; i<8; i++){
       tsMsg.data[i]=' '; 
-      if( i < iMsgLen ) tsMsg.data[i]=eventMsg[i];
+      if( i < iMsgLen ) tsMsg.data[i]=errMsg[i];
     }
     logCan(0,tsMsg); // FFE Comment Message
 }
@@ -323,37 +324,37 @@
 void sendReq() {
     static char data[8] = {0x02, 0x21, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff};
     if(reqMsgCnt<99){
-        switch (++reqMsgCnt){
-            case 1:
+        switch (reqMsgCnt){
+            case 0:
                 can1.monitor(false); // set to active mode
                 can1SleepMode = 0; // enable TX
                 data[0]=0x02; //change to request group 1
                 data[1]=0x21;
                 data[2]=0x01;
                 break;
-            case 7: // group 1 has 6 frames
+            case 6: // group 1 has 6 frames
                 can1.monitor(false); // set to active mode
                 can1SleepMode = 0; // enable TX
                 data[0]=0x02; //change to request group 2 (cp data)
                 data[1]=0x21;
                 data[2]=0x02;
                 break;
-            case 36: // group 2 has 29 frames
+            case 35: // group 2 has 29 frames
                 data[0]=0x02; //change to request group 3
                 data[1]=0x21;
                 data[2]=0x03;
                 break;
-            case 41: // group 3 has 5 frames
+            case 40: // group 3 has 5 frames
                 data[0]=0x02; //change to request group 4 (temperature)
                 data[1]=0x21;
                 data[2]=0x04;
                 break;
-            case 44: // group 4 has 3 frames
+            case 43: // group 4 has 3 frames
                 data[0]=0x02; //change to request group 5
                 data[1]=0x21;
                 data[2]=0x05;
                 break;
-            case 55: // group 5 has 11 frames
+            case 54: // group 5 has 11 frames
                 reqMsgCnt = 99;
                 can1SleepMode = 1; // disable TX
                 can1.monitor(true); // set to snoop mode
@@ -364,9 +365,56 @@
                 data[2]=0x00;
         }
         can1.write(CANMessage(0x79b, data, 8));
+        reqMsgCnt++;
     }
 }
 
+/*void sendCPreq() {
+    char i;
+    char data[8] = {0x02, 0x21, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff};
+    can1.monitor(false); // set to active mode
+    can1SleepMode = 0; // enable TX
+    can1.write(CANMessage(0x79b, data, 8));
+    
+    if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
+    
+    logCan(1,CANMessage(0x79b, data, 8)); // Group 2 Request on EV
+    data[0]=0x30; //change to request next line message
+    data[1]=0x01;
+    data[2]=0x00;
+    for(i=0;i<29;i++){
+        wait_ms(16); //wait 16ms
+        can1.write(CANMessage(0x79b, data, 8));
+    }
+    can1SleepMode = 1; // disable TX
+    can1.monitor(true); // set to snoop mode
+}
+
+void sendTreq() {
+    char i;
+    char data[8] = {0x02, 0x21, 0x04, 0xff, 0xff, 0xff, 0xff, 0xff};
+    can1.monitor(false); // set to active mode
+    can1SleepMode = 0; // enable TX
+    can1.write(CANMessage(0x79b, data, 8));
+    
+    if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
+    
+    logCan(1,CANMessage(0x79b, data, 8)); // Group 4 request on EV
+    data[0]=0x30; //change to request next line message
+    data[1]=0x01;
+    data[2]=0x00;
+    for(i=0;i<3;i++){
+        wait_ms(16); //wait 16ms
+        can1.write(CANMessage(0x79b, data, 8));
+    }
+    can1SleepMode = 1; // disable TX
+    can1.monitor(true); // set to snoop mode
+}
+
+void autoPollISR() {  //This is the ticker ISR for auto-polling
+    pollCP=true;    //Set a flag to do in main loop instead of here
+}                   //since ticker blocks other interrupts*/
+
 void autoPollISR(){
     reqMsgCnt = 0; //reset message counter
     msgReq.attach(&sendReq,0.025);
@@ -448,9 +496,9 @@
     cfile = fopen("/local/config.txt", "r");
     if (cfile==NULL){ // if doesn't exist --> create
         sprintf(sTemp,"No config file found.\n");
-        printMsg(sTemp); // no config file
+        logMsg(sTemp); // no config file
         sprintf(sTemp,"Calibrating touch screen.\n");
-        printMsg(sTemp); // calibrating
+        logMsg(sTemp); // calibrating
         //tt.setcal(5570, 34030, 80, 108, 33700, 5780, 82, 108, 32500);// bypass calibration using my values
         tt.calibrate();   // run touchscreen calibration routine
         // NOTE: calibrates screen 1 first, then screen 0.
@@ -494,10 +542,10 @@
         if(ff<4){//If not latest format, save as latest format
             saveConfig();
             sprintf(sTemp,"Config file format updated.\n");
-            printMsg(sTemp); // config forat updates
+            logMsg(sTemp); // config forat updates
         }
         sprintf(sTemp,"Config file loaded.\n");
-        printMsg(sTemp); // config file loaded
+        logMsg(sTemp); // config file loaded
     }
 }
 
@@ -581,7 +629,7 @@
     max=0;
     min=9999;
     avg=0;
-    for(i=0; i<96; ++i) {
+    for(i=0; i<96; i++) {
         bd=(battData[i*2+3]<<8)+battData[i*2+4];
         avg+=bd;
         if(bd>max) max=bd;
@@ -603,7 +651,7 @@
         // BatDataBaseG4 * 7 = 224        
         fprintf(bfile,"%d,%d,%d,%d,",(battData[224+ 3]<<8)+battData[224+ 4],battData[224+ 5],(battData[224+ 6]<<8)+battData[224+ 7],battData[224+ 8]);
         fprintf(bfile,"%d,%d,%d,%d", (battData[224+ 9]<<8)+battData[224+10],battData[224+11],(battData[224+12]<<8)+battData[224+13],battData[224+14]);
-        for(i=0; i<96; ++i) {
+        for(i=0; i<96; i++) {
             bd=(battData[i*2+3]<<8)+battData[i*2+4];
             fprintf(bfile,",%d",bd);
         }
--- a/utility.h	Mon Apr 22 02:22:04 2013 +0000
+++ b/utility.h	Mon Apr 22 02:27:37 2013 +0000
@@ -58,12 +58,14 @@
     void mbed_reset();
     void RTC_IRQHandler();
     void RTC_Init (void);    
-    void printMsg (char *msg);
+    void logMsg (char *msg);
     void touch_ISR();
     unsigned short getTimeStamp();
     void logCan (char mType, CANMessage canRXmsg);
     void logTS ();    
-    void logEventMsg (char * eventMsg); // gg - messeges
+    void logErrMsg (char * errMsg); // gg - messeges
+    //void sendCPreq();    
+    //void sendTreq();    
     void sendReq();    
     void autoPollISR();
     void playbackISR();