Advertisement
Guest User

Arduino Sparkfun CAN BUS Shield Vehicle Speed

a guest
Feb 27th, 2017
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. /*
  2. This is for use with Sparkfun's CAN Bus Shield: https://www.sparkfun.com/products/10039
  3. Library located in Sparkfun CAN Bus Shield Library on Github: https://github.com/sparkfun/SparkFun_CAN-Bus_Arduino_Library
  4. */
  5.  
  6. #include <Canbus.h>
  7. #include <defaults.h>
  8. #include <global.h>
  9. #include <mcp2515.h>
  10. #include <mcp2515_defs.h>
  11.  
  12. void setup() {
  13. Serial.begin(9600);
  14. Serial.println("CAN BUS Vehicle Speed");
  15. delay(1000);
  16.  
  17. if(Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
  18. Serial.println("CAN Init ok");
  19. else
  20. Serial.println("Can't init CAN");
  21.  
  22. delay(1000);
  23. }
  24.  
  25. void loop(){
  26. //FIRST ---- Send PID Request for vehicle speed
  27. tCAN message;
  28. float engine_data;
  29. char *buffer;
  30.  
  31. message.id = 0x7DF; //PID REQUEST message.id =0x7DF
  32. message.header.rtr = 0;
  33. message.header.length = 8;
  34. message.data[0] = 0x02;
  35. message.data[1] = 0x01;
  36. message.data[2] = 0x0D; //vehicle speed PID
  37. message.data[3] = 0x00;
  38. message.data[4] = 0x00;
  39. message.data[5] = 0x00;
  40. message.data[6] = 0x00;
  41. message.data[7] = 0x00;
  42.  
  43. mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
  44. mcp2515_send_message(&message);
  45.  
  46. delay(200); //delay 0.2 seconds, but may not be needed
  47.  
  48. //SECOND ---- Filter for PID REPLY with message.id =0x7E8 and message.data[2] =0x0D
  49. //tCAN message;
  50. if (mcp2515_check_message())
  51. {
  52. if (mcp2515_get_message(&message))
  53. {
  54. if(message.id == 0x7E8 and message.data[2] == 0x0D) //Filter
  55. {
  56. engine_data = message.data[3];
  57. sprintf(buffer,"%d km ",(int) engine_data);
  58. }
  59. }
  60. }
  61. delay(1000);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement