Advertisement
Guest User

canbus_arduino_sketch.ino

a guest
Apr 22nd, 2013
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.69 KB | None | 0 0
  1. #include "CAN_test.h"
  2. // include the SPI library:
  3. #include <SPI.h>
  4.  
  5. int count = 0;
  6.  
  7. uint8_t id_count[4]={150, 180, 190, 200};
  8.  
  9. void setup() {
  10.   Serial.begin(9600);
  11.   Serial.println("\t\t[[ SETUP ]]");
  12.   // set the slaveSelectPin as an output:
  13.   pinMode (slaveSelectPin, OUTPUT);
  14.   // initialize SPI:
  15.   SPI.begin();
  16.   SPI.setDataMode(SPI_MODE0);
  17.  
  18.   // Configure MCP2515
  19.   spiReset();
  20.  
  21.   // Set bitrate and speed
  22.   spiWrite(0x28, 0b00000010); // CNF3 <= PHSEG21
  23.   spiWrite(0x29, 0b10010000); // CNF2 <= PHSEG11 | BTLMODE
  24.   spiWrite(0x2A, 0b00000000); // CNF1 <= Speed Same as Occilator
  25.  
  26.   // Enable interrupts
  27.   //spiWrite(CANINTE, 0b00000011); // CANINTE <= Enable inters for 1 and 2
  28.   spiWrite(CANINTE, 0b00000011); // CANINTE <= Enable inters for 1 and 2
  29.  
  30.   if (spiRead(CANINTE) != 3) {
  31.     Serial.print("Error Setting Interrupts: ");
  32.     Serial.println(spiRead(CANINTE), BIN);
  33.     while(1);
  34.   }
  35.  
  36.   // Deactivate ouputs
  37.   spiWrite(0x0C, 0b00000000); // deactivate the RXnBF Pins
  38.  
  39.   // Set inputs
  40.   spiWrite(0x0D, 0b00000000); // set TXnRTS as inputs
  41.  
  42.   // turn off filters => receive any message
  43.   spiWrite(0x60, 0b01100000); // RXB0CTRL <= RXM1 | RXM0
  44.   spiWrite(0x70, 0b01100000); // RXB0CTRL <= RXM1 | RXM0
  45.  
  46.   Serial.print("Status: ");
  47.   spiRead(0x0F,1);
  48.   Serial.println("");
  49.  
  50.  
  51.  
  52.   // ========================================
  53.   // !!!!!!!   CHANGE BACK TO NORMAL !!!!!!!!
  54.   spiWrite(0x0F, startupMode); // CANCNTRL <= Normal Mode
  55.   // !!!!!!!   CHANGE BACK TO NORMAL !!!!!!!!
  56.   // ========================================
  57.  
  58.   if (spiRead(0x0F) != startupMode) {
  59.     Serial.print("Error configuring MCP2515\nCANCTRL: ");
  60.     Serial.println(spiRead(0x0F));
  61.     while(1);
  62.   } else if (startupMode == NORMAL_MODE)
  63.     Serial.println("MCP2515 Configured In NORMAL_MODE...");
  64.   else if (startupMode == CONFIG_MODE)
  65.     Serial.println("MCP2515 Configured In CONFIG_MODE...");
  66.   else if (startupMode == LOOPBACK_MODE)
  67.     Serial.println("MCP2515 Configured In LOOPBACK_MODE...");
  68.   else
  69.     Serial.println("MCP2515 Configured Correctly...");
  70. }
  71.  
  72. void loop() {
  73.   // Test
  74.  
  75.   //spiRead(0x0F, 1);
  76.   //spiWrite(0x0F, 0x80);
  77.   //Serial.print(">>");
  78.   //delay(500);
  79.   //spiRead(0x0F, 1);
  80.   //Serial.println(" ");
  81.   //spiReset();
  82.   //delay(500);
  83.  
  84.  
  85.   // Send
  86.  
  87.  
  88. for(int i=0; i<4; i++)
  89. {
  90.  
  91.   spiReset();
  92.   delay(500);
  93.  
  94.   tCAN msg;
  95.   msg.id = id_count[i];
  96.   msg.length = 8;
  97.   msg.buff[0] = count;
  98.   msg.buff[1] = count+1;
  99.   msg.buff[2] = count+2;
  100.   msg.buff[3] = count+3;
  101.   msg.buff[4] = count+4;
  102.   msg.buff[5] = count+5;
  103.   msg.buff[6] = 0;
  104.   msg.buff[7] = 0;
  105.  
  106.  
  107.  
  108.   if (sendMessage(&msg)) {
  109.     Serial.print("Sent: ");
  110.     Serial.println(count, DEC);
  111.   }
  112.   count++;
  113.  }
  114.  
  115.   delay(1000);
  116.  
  117.   // Receive
  118.   //tCAN rec;
  119.   //if (recMessage(&rec)) {
  120.   //  Serial.print("\tReceived: ");
  121.   //  Serial.println(rec.buff[0], DEC);
  122.   //}  
  123.   //delay(500);
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. }
  132.  
  133. void spiReset() {
  134.   Serial.println("\t\t[[ RESETTTING ]]");
  135.   // take the SS pin low to select the chip:
  136.   digitalWrite(slaveSelectPin,LOW);
  137.   //  send in the address and value via SPI:
  138.   SPI.transfer(RESET_COM);
  139.   // take the SS pin high to de-select the chip:
  140.   digitalWrite(slaveSelectPin,HIGH);
  141. }
  142.  
  143. void spiWrite(int address, int value) {
  144.   // take the SS pin low to select the chip:
  145.   digitalWrite(slaveSelectPin,LOW);
  146.   //  send in the address and value via SPI:
  147.   SPI.transfer(WRITE_COM);
  148.   SPI.transfer(address);
  149.   SPI.transfer(value);
  150.   // take the SS pin high to de-select the chip:
  151.   digitalWrite(slaveSelectPin,HIGH);
  152. }
  153.  
  154. char spiWriteContInit(int address) {
  155.   SPI.transfer(WRITE_COM);
  156.   SPI.transfer(address);
  157. }
  158.  
  159. char spiWriteCont(int data) {
  160.   return SPI.transfer(data);
  161. }
  162.  
  163. int spiRead(int address, int verbose) {
  164.   // take the SS pin low to select the chip:
  165.   digitalWrite(slaveSelectPin,LOW);
  166.   //  send in the address and value via SPI:
  167.   SPI.transfer(READ_COM);
  168.   SPI.transfer(address);
  169.   int data = SPI.transfer(0x00);
  170.   // take the SS pin high to de-select the chip:
  171.   digitalWrite(slaveSelectPin,HIGH);  
  172.   if (verbose) {
  173.     Serial.print(data, BIN);
  174.     Serial.print(", ");
  175.   }
  176.   return data;
  177. }
  178.  
  179. char spiReadContInit(int address) {
  180.   SPI.transfer(READ_COM);
  181.   SPI.transfer(SPI_WRITE_TX | address);
  182. }
  183.  
  184. char spiReadCont() {
  185.   return SPI.transfer(0x00);
  186. }
  187.  
  188. char getStatus() {
  189.   digitalWrite(slaveSelectPin,LOW);
  190.   SPI.transfer(STATUS_COM);
  191.   int data = SPI.transfer(0x00);
  192.   digitalWrite(slaveSelectPin,HIGH);  
  193.   return data;
  194. }
  195.  
  196. char getRxStatus() {
  197.   digitalWrite(slaveSelectPin,LOW);
  198.   SPI.transfer(STATUS_RX_COM);
  199.   int data = SPI.transfer(0x00);
  200.   digitalWrite(slaveSelectPin,HIGH);  
  201.   return data;
  202. }
  203.  
  204. /*
  205.  * Returns 1 if RX1 is full
  206.  * Returns 2 if RX2 is full
  207.  * Returns 3 if both RX1 and RX2 are full
  208.  * Zero if no message
  209.  */
  210. int hasMessage(void) {
  211.   return getStatus() & 3; // CANINTF
  212. }
  213.  
  214. /*
  215.  * Returns the RX buffere which was read
  216.  * Returns 0 if the was an error.
  217.  */
  218. int recMessage(tCAN *msg) {
  219.   int stat = getRxStatus(); // SPI_RX_STATUS
  220.   int addr = 0;
  221.   if (stat & (1 << 6))
  222.     addr = SPI_READ_RX;
  223.   else if (stat & (1 << 7))
  224.     addr = SPI_READ_RX | 4;
  225.   else
  226.     return 0;
  227.   digitalWrite(slaveSelectPin,LOW);
  228.   spiReadContInit(0x90);
  229.   msg->id = spiReadCont() << 3;
  230.   msg->id |= spiReadCont() >> 5;
  231.  
  232.   spiReadCont();
  233.   spiReadCont();
  234.  
  235.   msg->length = spiReadCont();
  236.   //  message->header.rtr = (bit_is_set(status, 3)) ? 1 : 0; // FROM mcp2515.c
  237.   int i;
  238.   for (i=0;i<msg->length;i++)
  239.     msg->buff[i] = spiReadCont();
  240.   digitalWrite(slaveSelectPin,HIGH);
  241.   delay(2);
  242.   // Clear Interrupt Flag
  243.   spiWrite(CANINTF, stat&(0b11111100|(stat&(1<<6)?0xFE:0xFD)));
  244.   return 1;
  245. }
  246.  
  247. int sendMessage(tCAN *msg) {
  248.   int stat = getStatus(); // SPI_STATUS
  249.   int addr = 0;
  250.   if (!(stat & (1 << 2)))
  251.     addr = 0x00;
  252.   else if (!(stat & (1 << 4)))
  253.     addr = 0x02;
  254.   else if (!(stat & (1 << 6)))
  255.     addr = 0x04;
  256.   else
  257.     // all buffer used => could not send message
  258.     return 0;
  259.   digitalWrite(slaveSelectPin,LOW);
  260.   //spiWriteContInit(addr);
  261.   spiWriteCont(SPI_WRITE_TX | addr);
  262.   //spiWriteCont(msg->id << 3);
  263.   //spiWriteCont(msg->id >> 5);
  264.   spiWriteCont(msg->id >> 3);
  265.   spiWriteCont(msg->id << 5);
  266.  
  267.   spiWriteCont(0);
  268.   spiWriteCont(0);
  269.  
  270.   // a rtr-frame has a length, but contains no data
  271.  
  272.   spiWriteCont(msg->length);
  273.   //  message->header.rtr = (bit_is_set(status, 3)) ? 1 : 0; // FROM mcp2515.c
  274.  
  275.   for (int i =0;i<msg->length;i++)
  276.     spiWriteCont(msg->buff[i]);
  277.  
  278.   digitalWrite(slaveSelectPin,HIGH);
  279.   delay(1);
  280.   addr = (addr == 0) ? 1 : addr;
  281.   digitalWrite(slaveSelectPin,LOW);
  282.   SPI.transfer(SPI_RTS | addr);
  283.   digitalWrite(slaveSelectPin,HIGH);
  284.   return addr;
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement