![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
UBR CAN
Fork of CAN_bus_reader_pub by
main.cpp@0:78752077cc25, 2011-11-09 (annotated)
- Committer:
- nadakiti
- Date:
- Wed Nov 09 04:59:01 2011 +0000
- Revision:
- 0:78752077cc25
- Child:
- 1:b57b77b29c7e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nadakiti | 0:78752077cc25 | 1 | // reference : http://mbed.org/users/pangsk/programs/ecu_reader/ |
nadakiti | 0:78752077cc25 | 2 | // Only read CAN-bus data, (not send any data) |
nadakiti | 0:78752077cc25 | 3 | // v1.0 |
nadakiti | 0:78752077cc25 | 4 | |
nadakiti | 0:78752077cc25 | 5 | /* |
nadakiti | 0:78752077cc25 | 6 | |
nadakiti | 0:78752077cc25 | 7 | mbed Can-Bus demo |
nadakiti | 0:78752077cc25 | 8 | |
nadakiti | 0:78752077cc25 | 9 | This program is to demonstrate the CAN-bus capability of the mbed module. |
nadakiti | 0:78752077cc25 | 10 | |
nadakiti | 0:78752077cc25 | 11 | http://www.skpang.co.uk/catalog/product_info.php?products_id=741 |
nadakiti | 0:78752077cc25 | 12 | |
nadakiti | 0:78752077cc25 | 13 | v1.0 July 2010 |
nadakiti | 0:78752077cc25 | 14 | |
nadakiti | 0:78752077cc25 | 15 | ******************************************************************************** |
nadakiti | 0:78752077cc25 | 16 | |
nadakiti | 0:78752077cc25 | 17 | WARNING: Use at your own risk, sadly this software comes with no guarantees. |
nadakiti | 0:78752077cc25 | 18 | This software is provided 'free' and in good faith, but the author does not |
nadakiti | 0:78752077cc25 | 19 | accept liability for any damage arising from its use. |
nadakiti | 0:78752077cc25 | 20 | |
nadakiti | 0:78752077cc25 | 21 | ******************************************************************************** |
nadakiti | 0:78752077cc25 | 22 | |
nadakiti | 0:78752077cc25 | 23 | |
nadakiti | 0:78752077cc25 | 24 | */ |
nadakiti | 0:78752077cc25 | 25 | |
nadakiti | 0:78752077cc25 | 26 | #include "mbed.h" |
nadakiti | 0:78752077cc25 | 27 | #include "ecu_reader.h" |
nadakiti | 0:78752077cc25 | 28 | #include "globals.h" |
nadakiti | 0:78752077cc25 | 29 | #include "TextLCD.h" |
nadakiti | 0:78752077cc25 | 30 | #include "SDFileSystem.h" |
nadakiti | 0:78752077cc25 | 31 | |
nadakiti | 0:78752077cc25 | 32 | //GPS gps(p28, p27); |
nadakiti | 0:78752077cc25 | 33 | TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d0, d1, d2, d3, for starboard orange |
nadakiti | 0:78752077cc25 | 34 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //for starboard orange |
nadakiti | 0:78752077cc25 | 35 | |
nadakiti | 0:78752077cc25 | 36 | /*DigitalIn click(p21); // Joystick inputs |
nadakiti | 0:78752077cc25 | 37 | DigitalIn right(p22); |
nadakiti | 0:78752077cc25 | 38 | DigitalIn down(p23); |
nadakiti | 0:78752077cc25 | 39 | DigitalIn left(p24); |
nadakiti | 0:78752077cc25 | 40 | DigitalIn up(p25);*/ |
nadakiti | 0:78752077cc25 | 41 | |
nadakiti | 0:78752077cc25 | 42 | ecu_reader obdii(CANSPEED_500); //Create object and set CAN speed |
nadakiti | 0:78752077cc25 | 43 | //void gps_demo(void); |
nadakiti | 0:78752077cc25 | 44 | void sd_demo(void); |
nadakiti | 0:78752077cc25 | 45 | //Timer CANTimer; |
nadakiti | 0:78752077cc25 | 46 | Timer CANTimer2; |
nadakiti | 0:78752077cc25 | 47 | |
nadakiti | 0:78752077cc25 | 48 | int main() { |
nadakiti | 0:78752077cc25 | 49 | |
nadakiti | 0:78752077cc25 | 50 | // char buffer[20]; |
nadakiti | 0:78752077cc25 | 51 | |
nadakiti | 0:78752077cc25 | 52 | lcd.locate(0,0); // Set LCD cursor position |
nadakiti | 0:78752077cc25 | 53 | lcd.printf("CAN-Bus demo"); |
nadakiti | 0:78752077cc25 | 54 | |
nadakiti | 0:78752077cc25 | 55 | lcd.locate(0,1); |
nadakiti | 0:78752077cc25 | 56 | lcd.printf("www.skpang.co.uk"); |
nadakiti | 0:78752077cc25 | 57 | pc.baud(460800); |
nadakiti | 0:78752077cc25 | 58 | pc.printf("\n\rCAN-bus demo..."); |
nadakiti | 0:78752077cc25 | 59 | |
nadakiti | 0:78752077cc25 | 60 | wait(3); |
nadakiti | 0:78752077cc25 | 61 | lcd.cls(); |
nadakiti | 0:78752077cc25 | 62 | |
nadakiti | 0:78752077cc25 | 63 | |
nadakiti | 0:78752077cc25 | 64 | /* while(1) // Wait until option is selected by the joystick |
nadakiti | 0:78752077cc25 | 65 | { |
nadakiti | 0:78752077cc25 | 66 | |
nadakiti | 0:78752077cc25 | 67 | if(down == 0) gps_demo(); |
nadakiti | 0:78752077cc25 | 68 | if(left == 0) sd_demo(); |
nadakiti | 0:78752077cc25 | 69 | if(up == 0) break; |
nadakiti | 0:78752077cc25 | 70 | |
nadakiti | 0:78752077cc25 | 71 | }*/ |
nadakiti | 0:78752077cc25 | 72 | |
nadakiti | 0:78752077cc25 | 73 | while(1) { // Main CAN loop |
nadakiti | 0:78752077cc25 | 74 | led2 = 1; |
nadakiti | 0:78752077cc25 | 75 | wait(0.1); |
nadakiti | 0:78752077cc25 | 76 | led2 = 0; |
nadakiti | 0:78752077cc25 | 77 | wait(0.1); |
nadakiti | 0:78752077cc25 | 78 | |
nadakiti | 0:78752077cc25 | 79 | CANTimer2.reset(); |
nadakiti | 0:78752077cc25 | 80 | CANTimer2.start(); |
nadakiti | 0:78752077cc25 | 81 | pc.printf("CANTimer.start\n"); |
nadakiti | 0:78752077cc25 | 82 | |
nadakiti | 0:78752077cc25 | 83 | while (CANTimer2.read_ms() < TIMEOUT) { |
nadakiti | 0:78752077cc25 | 84 | // pc.printf("CANTimer.read_ms(): %dms ", CANTimer2.read_ms()); |
nadakiti | 0:78752077cc25 | 85 | |
nadakiti | 0:78752077cc25 | 86 | // http://mbed.org/cookbook/OBDII-Can-Bus |
nadakiti | 0:78752077cc25 | 87 | |
nadakiti | 0:78752077cc25 | 88 | if (can2.read(can_MsgRx)) { |
nadakiti | 0:78752077cc25 | 89 | pc.printf("Message read\n\r"); |
nadakiti | 0:78752077cc25 | 90 | |
nadakiti | 0:78752077cc25 | 91 | //print message id |
nadakiti | 0:78752077cc25 | 92 | pc.printf("can_MsgRx.id: %x\n\r", can_MsgRx.id); |
nadakiti | 0:78752077cc25 | 93 | |
nadakiti | 0:78752077cc25 | 94 | //print length of message |
nadakiti | 0:78752077cc25 | 95 | pc.printf("Hex: can_MsgRx.len: %x\n\r", can_MsgRx.len); |
nadakiti | 0:78752077cc25 | 96 | |
nadakiti | 0:78752077cc25 | 97 | //print data[2] |
nadakiti | 0:78752077cc25 | 98 | //pc.printf("can_MsgRx.data[2]: %x\n\r", can_MsgRx.data[2]); |
nadakiti | 0:78752077cc25 | 99 | |
nadakiti | 0:78752077cc25 | 100 | for (int i = 0; i < (int)can_MsgRx.len; i++) { |
nadakiti | 0:78752077cc25 | 101 | pc.printf("can_MsgRx.data[%d]: %x\n\r", i, can_MsgRx.data[i]); |
nadakiti | 0:78752077cc25 | 102 | } |
nadakiti | 0:78752077cc25 | 103 | } |
nadakiti | 0:78752077cc25 | 104 | } |
nadakiti | 0:78752077cc25 | 105 | } |
nadakiti | 0:78752077cc25 | 106 | |
nadakiti | 0:78752077cc25 | 107 | |
nadakiti | 0:78752077cc25 | 108 | |
nadakiti | 0:78752077cc25 | 109 | |
nadakiti | 0:78752077cc25 | 110 | |
nadakiti | 0:78752077cc25 | 111 | |
nadakiti | 0:78752077cc25 | 112 | /* if(obdii.request(ENGINE_RPM,buffer) == 1) // Get engine rpm and display on LCD |
nadakiti | 0:78752077cc25 | 113 | { |
nadakiti | 0:78752077cc25 | 114 | lcd.locate(0,0); |
nadakiti | 0:78752077cc25 | 115 | lcd.printf(buffer); |
nadakiti | 0:78752077cc25 | 116 | } |
nadakiti | 0:78752077cc25 | 117 | |
nadakiti | 0:78752077cc25 | 118 | if(obdii.request(ENGINE_COOLANT_TEMP,buffer) == 1) |
nadakiti | 0:78752077cc25 | 119 | { |
nadakiti | 0:78752077cc25 | 120 | lcd.locate(9,0); |
nadakiti | 0:78752077cc25 | 121 | lcd.printf(buffer); |
nadakiti | 0:78752077cc25 | 122 | } |
nadakiti | 0:78752077cc25 | 123 | |
nadakiti | 0:78752077cc25 | 124 | if(obdii.request(VEHICLE_SPEED,buffer) == 1) |
nadakiti | 0:78752077cc25 | 125 | { |
nadakiti | 0:78752077cc25 | 126 | lcd.locate(0,1); |
nadakiti | 0:78752077cc25 | 127 | lcd.printf(buffer); |
nadakiti | 0:78752077cc25 | 128 | } |
nadakiti | 0:78752077cc25 | 129 | |
nadakiti | 0:78752077cc25 | 130 | if(obdii.request(THROTTLE,buffer) ==1 ) |
nadakiti | 0:78752077cc25 | 131 | { |
nadakiti | 0:78752077cc25 | 132 | lcd.locate(9,1); |
nadakiti | 0:78752077cc25 | 133 | lcd.printf(buffer); |
nadakiti | 0:78752077cc25 | 134 | } |
nadakiti | 0:78752077cc25 | 135 | */ |
nadakiti | 0:78752077cc25 | 136 | |
nadakiti | 0:78752077cc25 | 137 | } |
nadakiti | 0:78752077cc25 | 138 | |
nadakiti | 0:78752077cc25 | 139 | /*void gps_demo(void) |
nadakiti | 0:78752077cc25 | 140 | { |
nadakiti | 0:78752077cc25 | 141 | lcd.cls(); |
nadakiti | 0:78752077cc25 | 142 | lcd.printf("GPS demo"); |
nadakiti | 0:78752077cc25 | 143 | lcd.locate(0,1); |
nadakiti | 0:78752077cc25 | 144 | lcd.printf("Waiting for lock"); |
nadakiti | 0:78752077cc25 | 145 | |
nadakiti | 0:78752077cc25 | 146 | wait(3); |
nadakiti | 0:78752077cc25 | 147 | lcd.cls(); |
nadakiti | 0:78752077cc25 | 148 | |
nadakiti | 0:78752077cc25 | 149 | while(1) |
nadakiti | 0:78752077cc25 | 150 | { |
nadakiti | 0:78752077cc25 | 151 | if(gps.sample()) { |
nadakiti | 0:78752077cc25 | 152 | lcd.cls(); |
nadakiti | 0:78752077cc25 | 153 | lcd.printf("Long:%f", gps.longitude); |
nadakiti | 0:78752077cc25 | 154 | lcd.locate(0,1); |
nadakiti | 0:78752077cc25 | 155 | lcd.printf("Lat:%f", gps.latitude); |
nadakiti | 0:78752077cc25 | 156 | pc.printf("I'm at %f, %f\n", gps.longitude, gps.latitude); |
nadakiti | 0:78752077cc25 | 157 | } else { |
nadakiti | 0:78752077cc25 | 158 | pc.printf("Oh Dear! No lock :(\n"); |
nadakiti | 0:78752077cc25 | 159 | lcd.cls(); |
nadakiti | 0:78752077cc25 | 160 | lcd.printf("Waiting for lock"); |
nadakiti | 0:78752077cc25 | 161 | |
nadakiti | 0:78752077cc25 | 162 | } |
nadakiti | 0:78752077cc25 | 163 | } |
nadakiti | 0:78752077cc25 | 164 | |
nadakiti | 0:78752077cc25 | 165 | }*/ |
nadakiti | 0:78752077cc25 | 166 | |
nadakiti | 0:78752077cc25 | 167 | /* |
nadakiti | 0:78752077cc25 | 168 | void sd_demo(void) |
nadakiti | 0:78752077cc25 | 169 | { |
nadakiti | 0:78752077cc25 | 170 | lcd.cls(); |
nadakiti | 0:78752077cc25 | 171 | lcd.printf("SD demo"); |
nadakiti | 0:78752077cc25 | 172 | wait(2); |
nadakiti | 0:78752077cc25 | 173 | lcd.cls(); |
nadakiti | 0:78752077cc25 | 174 | |
nadakiti | 0:78752077cc25 | 175 | FILE *fp = fopen("/sd/sdtest2.txt", "w"); |
nadakiti | 0:78752077cc25 | 176 | if(fp == NULL) { |
nadakiti | 0:78752077cc25 | 177 | lcd.cls(); |
nadakiti | 0:78752077cc25 | 178 | lcd.printf("Could not open file for write\n"); |
nadakiti | 0:78752077cc25 | 179 | } |
nadakiti | 0:78752077cc25 | 180 | fprintf(fp, "Hello fun SD Card World! testing 1234"); |
nadakiti | 0:78752077cc25 | 181 | fclose(fp); |
nadakiti | 0:78752077cc25 | 182 | lcd.locate(0,1); |
nadakiti | 0:78752077cc25 | 183 | lcd.printf("Writtern to SD card"); |
nadakiti | 0:78752077cc25 | 184 | |
nadakiti | 0:78752077cc25 | 185 | while(1) |
nadakiti | 0:78752077cc25 | 186 | { |
nadakiti | 0:78752077cc25 | 187 | led2 = 1; |
nadakiti | 0:78752077cc25 | 188 | wait(0.1); |
nadakiti | 0:78752077cc25 | 189 | led2 = 0; |
nadakiti | 0:78752077cc25 | 190 | wait(0.1); |
nadakiti | 0:78752077cc25 | 191 | |
nadakiti | 0:78752077cc25 | 192 | } |
nadakiti | 0:78752077cc25 | 193 | |
nadakiti | 0:78752077cc25 | 194 | } |
nadakiti | 0:78752077cc25 | 195 | */ |