canbus rpm test

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 //leemos el can a 500 kb/s y lo enviamos por el usb
00004 
00005 DigitalOut led1(LED1);
00006 DigitalOut led2(LED2);
00007 DigitalOut led3(LED3);
00008 DigitalOut led4(LED4);
00009 Serial pc(USBTX, USBRX); // tx, rx
00010 CAN can2(p30,p29);
00011 CANMessage can_MsgRx;
00012 
00013 TextLCD lcd(p18,p19,p20,p17,p16,p15,p14); 
00014 static  char error=0;
00015 int main() {
00016 char buffer[20];
00017  int engine_data;
00018  //float
00019  lcd.locate(0,0);
00020     lcd.printf("C3 PICASSO v2");
00021     
00022     lcd.locate(0,1);
00023     lcd.printf("RICARDO GUILLEN");
00024        
00025    
00026     
00027     wait(3);
00028     lcd.cls();
00029 
00030     //inicializamos el can a 500kb/s
00031     can2.frequency(500000);
00032 
00033     //bulce principal
00034     while (1) {
00035     
00036         if (can2.read(can_MsgRx)) {
00037         
00038         
00039         
00040         
00041         
00042          switch(can_MsgRx.id){
00043             case 0x208:
00044             //engine_data =  ((can_MsgRx.data[0]*256) + can_MsgRx.data[1])/4;
00045             // sprintf(buffer,"%d rpm ",(int) engine_data);
00046             // sprintf(buffer,"%d rpm ",can_MsgRx.id);
00047             //unsigned long a=array[0]|(array[1]<<8);
00048            /* engine_data=(can_MsgRx.data[0]|(can_MsgRx.data[1]<<8)/10);
00049             lcd.locate(0,0);
00050             lcd.printf(buffer);
00051             lcd.printf("%d rpm %x%x ",(int) engine_data,can_MsgRx.data[0],can_MsgRx.data[1]);
00052             */
00053             
00054             //(s[ 0 ] << 8) +s[ 1 ]
00055            /* engine_data=(can_MsgRx.data[0]<<8 + (can_MsgRx.data[1])/10);
00056             sprintf(buffer,"0000%x%x",can_MsgRx.data[0],can_MsgRx.data[1]);
00057             lcd.locate(0,0);
00058             // lcd.printf(buffer);
00059             lcd.printf("%d rpm %x%x %s",(int) engine_data,can_MsgRx.data[0],can_MsgRx.data[1],buffer);*/
00060             
00061             unsigned char b[4];
00062             b[0]=0x00;
00063             b[1]=0x00;
00064             b[2]=can_MsgRx.data[0];
00065             b[3]=can_MsgRx.data[1];
00066            int valor= (b[0] << 24) + ((b[1] & 0xFF) << 16)+ ((b[2] & 0xFF) << 8)+ (b[3] & 0xFF);
00067            lcd.locate(0,0);
00068             // lcd.printf(buffer);
00069             lcd.printf("%d rpm ", (valor/10));
00070            
00071             
00072              break;
00073             //default:
00074             //lcd.locate(0,1);
00075             //lcd.printf("default:%x",can_MsgRx.id);
00076             }
00077                 
00078            // pc.printf("id: 0x%x data:", can_MsgRx.id);
00079            pc.printf("%x,",can_MsgRx.id);
00080             for (char i=0; i<can_MsgRx.len; i++) {
00081             if(i==can_MsgRx.len-1){
00082                     pc.printf("%x", can_MsgRx.data[i]);
00083             }else{
00084                         pc.printf("%x,", can_MsgRx.data[i]);
00085                       
00086             }
00087             }
00088             pc.printf("\n\r");
00089             // mensaje ok, 
00090            // led3 = !led3;
00091             led3=1;
00092             led4=0;
00093             
00094            
00095             
00096            
00097             
00098             
00099             
00100             
00101             
00102             
00103            
00104             
00105             
00106         
00107         }else{
00108             if(error==254){
00109               // pc.printf("Errores: %x last: %x \n\r",error, can2.rderror()); 
00110                error=0;            
00111            }
00112                    
00113              error++;
00114             // led4 = !led4;
00115              led4=1;
00116              led3=0;
00117         
00118         }
00119     }
00120    
00121 }