![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
canbus rpm test
main.cpp@0:2e70b895129c, 2010-09-29 (annotated)
- Committer:
- guigra
- Date:
- Wed Sep 29 20:40:07 2010 +0000
- Revision:
- 0:2e70b895129c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
guigra | 0:2e70b895129c | 1 | #include "mbed.h" |
guigra | 0:2e70b895129c | 2 | #include "TextLCD.h" |
guigra | 0:2e70b895129c | 3 | //leemos el can a 500 kb/s y lo enviamos por el usb |
guigra | 0:2e70b895129c | 4 | |
guigra | 0:2e70b895129c | 5 | DigitalOut led1(LED1); |
guigra | 0:2e70b895129c | 6 | DigitalOut led2(LED2); |
guigra | 0:2e70b895129c | 7 | DigitalOut led3(LED3); |
guigra | 0:2e70b895129c | 8 | DigitalOut led4(LED4); |
guigra | 0:2e70b895129c | 9 | Serial pc(USBTX, USBRX); // tx, rx |
guigra | 0:2e70b895129c | 10 | CAN can2(p30,p29); |
guigra | 0:2e70b895129c | 11 | CANMessage can_MsgRx; |
guigra | 0:2e70b895129c | 12 | |
guigra | 0:2e70b895129c | 13 | TextLCD lcd(p18,p19,p20,p17,p16,p15,p14); |
guigra | 0:2e70b895129c | 14 | static char error=0; |
guigra | 0:2e70b895129c | 15 | int main() { |
guigra | 0:2e70b895129c | 16 | char buffer[20]; |
guigra | 0:2e70b895129c | 17 | int engine_data; |
guigra | 0:2e70b895129c | 18 | //float |
guigra | 0:2e70b895129c | 19 | lcd.locate(0,0); |
guigra | 0:2e70b895129c | 20 | lcd.printf("C3 PICASSO v2"); |
guigra | 0:2e70b895129c | 21 | |
guigra | 0:2e70b895129c | 22 | lcd.locate(0,1); |
guigra | 0:2e70b895129c | 23 | lcd.printf("RICARDO GUILLEN"); |
guigra | 0:2e70b895129c | 24 | |
guigra | 0:2e70b895129c | 25 | |
guigra | 0:2e70b895129c | 26 | |
guigra | 0:2e70b895129c | 27 | wait(3); |
guigra | 0:2e70b895129c | 28 | lcd.cls(); |
guigra | 0:2e70b895129c | 29 | |
guigra | 0:2e70b895129c | 30 | //inicializamos el can a 500kb/s |
guigra | 0:2e70b895129c | 31 | can2.frequency(500000); |
guigra | 0:2e70b895129c | 32 | |
guigra | 0:2e70b895129c | 33 | //bulce principal |
guigra | 0:2e70b895129c | 34 | while (1) { |
guigra | 0:2e70b895129c | 35 | |
guigra | 0:2e70b895129c | 36 | if (can2.read(can_MsgRx)) { |
guigra | 0:2e70b895129c | 37 | |
guigra | 0:2e70b895129c | 38 | |
guigra | 0:2e70b895129c | 39 | |
guigra | 0:2e70b895129c | 40 | |
guigra | 0:2e70b895129c | 41 | |
guigra | 0:2e70b895129c | 42 | switch(can_MsgRx.id){ |
guigra | 0:2e70b895129c | 43 | case 0x208: |
guigra | 0:2e70b895129c | 44 | //engine_data = ((can_MsgRx.data[0]*256) + can_MsgRx.data[1])/4; |
guigra | 0:2e70b895129c | 45 | // sprintf(buffer,"%d rpm ",(int) engine_data); |
guigra | 0:2e70b895129c | 46 | // sprintf(buffer,"%d rpm ",can_MsgRx.id); |
guigra | 0:2e70b895129c | 47 | //unsigned long a=array[0]|(array[1]<<8); |
guigra | 0:2e70b895129c | 48 | /* engine_data=(can_MsgRx.data[0]|(can_MsgRx.data[1]<<8)/10); |
guigra | 0:2e70b895129c | 49 | lcd.locate(0,0); |
guigra | 0:2e70b895129c | 50 | lcd.printf(buffer); |
guigra | 0:2e70b895129c | 51 | lcd.printf("%d rpm %x%x ",(int) engine_data,can_MsgRx.data[0],can_MsgRx.data[1]); |
guigra | 0:2e70b895129c | 52 | */ |
guigra | 0:2e70b895129c | 53 | |
guigra | 0:2e70b895129c | 54 | //(s[ 0 ] << 8) +s[ 1 ] |
guigra | 0:2e70b895129c | 55 | /* engine_data=(can_MsgRx.data[0]<<8 + (can_MsgRx.data[1])/10); |
guigra | 0:2e70b895129c | 56 | sprintf(buffer,"0000%x%x",can_MsgRx.data[0],can_MsgRx.data[1]); |
guigra | 0:2e70b895129c | 57 | lcd.locate(0,0); |
guigra | 0:2e70b895129c | 58 | // lcd.printf(buffer); |
guigra | 0:2e70b895129c | 59 | lcd.printf("%d rpm %x%x %s",(int) engine_data,can_MsgRx.data[0],can_MsgRx.data[1],buffer);*/ |
guigra | 0:2e70b895129c | 60 | |
guigra | 0:2e70b895129c | 61 | unsigned char b[4]; |
guigra | 0:2e70b895129c | 62 | b[0]=0x00; |
guigra | 0:2e70b895129c | 63 | b[1]=0x00; |
guigra | 0:2e70b895129c | 64 | b[2]=can_MsgRx.data[0]; |
guigra | 0:2e70b895129c | 65 | b[3]=can_MsgRx.data[1]; |
guigra | 0:2e70b895129c | 66 | int valor= (b[0] << 24) + ((b[1] & 0xFF) << 16)+ ((b[2] & 0xFF) << 8)+ (b[3] & 0xFF); |
guigra | 0:2e70b895129c | 67 | lcd.locate(0,0); |
guigra | 0:2e70b895129c | 68 | // lcd.printf(buffer); |
guigra | 0:2e70b895129c | 69 | lcd.printf("%d rpm ", (valor/10)); |
guigra | 0:2e70b895129c | 70 | |
guigra | 0:2e70b895129c | 71 | |
guigra | 0:2e70b895129c | 72 | break; |
guigra | 0:2e70b895129c | 73 | //default: |
guigra | 0:2e70b895129c | 74 | //lcd.locate(0,1); |
guigra | 0:2e70b895129c | 75 | //lcd.printf("default:%x",can_MsgRx.id); |
guigra | 0:2e70b895129c | 76 | } |
guigra | 0:2e70b895129c | 77 | |
guigra | 0:2e70b895129c | 78 | // pc.printf("id: 0x%x data:", can_MsgRx.id); |
guigra | 0:2e70b895129c | 79 | pc.printf("%x,",can_MsgRx.id); |
guigra | 0:2e70b895129c | 80 | for (char i=0; i<can_MsgRx.len; i++) { |
guigra | 0:2e70b895129c | 81 | if(i==can_MsgRx.len-1){ |
guigra | 0:2e70b895129c | 82 | pc.printf("%x", can_MsgRx.data[i]); |
guigra | 0:2e70b895129c | 83 | }else{ |
guigra | 0:2e70b895129c | 84 | pc.printf("%x,", can_MsgRx.data[i]); |
guigra | 0:2e70b895129c | 85 | |
guigra | 0:2e70b895129c | 86 | } |
guigra | 0:2e70b895129c | 87 | } |
guigra | 0:2e70b895129c | 88 | pc.printf("\n\r"); |
guigra | 0:2e70b895129c | 89 | // mensaje ok, |
guigra | 0:2e70b895129c | 90 | // led3 = !led3; |
guigra | 0:2e70b895129c | 91 | led3=1; |
guigra | 0:2e70b895129c | 92 | led4=0; |
guigra | 0:2e70b895129c | 93 | |
guigra | 0:2e70b895129c | 94 | |
guigra | 0:2e70b895129c | 95 | |
guigra | 0:2e70b895129c | 96 | |
guigra | 0:2e70b895129c | 97 | |
guigra | 0:2e70b895129c | 98 | |
guigra | 0:2e70b895129c | 99 | |
guigra | 0:2e70b895129c | 100 | |
guigra | 0:2e70b895129c | 101 | |
guigra | 0:2e70b895129c | 102 | |
guigra | 0:2e70b895129c | 103 | |
guigra | 0:2e70b895129c | 104 | |
guigra | 0:2e70b895129c | 105 | |
guigra | 0:2e70b895129c | 106 | |
guigra | 0:2e70b895129c | 107 | }else{ |
guigra | 0:2e70b895129c | 108 | if(error==254){ |
guigra | 0:2e70b895129c | 109 | // pc.printf("Errores: %x last: %x \n\r",error, can2.rderror()); |
guigra | 0:2e70b895129c | 110 | error=0; |
guigra | 0:2e70b895129c | 111 | } |
guigra | 0:2e70b895129c | 112 | |
guigra | 0:2e70b895129c | 113 | error++; |
guigra | 0:2e70b895129c | 114 | // led4 = !led4; |
guigra | 0:2e70b895129c | 115 | led4=1; |
guigra | 0:2e70b895129c | 116 | led3=0; |
guigra | 0:2e70b895129c | 117 | |
guigra | 0:2e70b895129c | 118 | } |
guigra | 0:2e70b895129c | 119 | } |
guigra | 0:2e70b895129c | 120 | |
guigra | 0:2e70b895129c | 121 | } |