source: rtos_arduino/trunk/arduino_lib/libraries/NcesCan/examples/receive_check/receive_check.ino@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 1.6 KB
Line 
1// demo: CAN-BUS Shield, receive data with check mode
2// send data coming to fast, such as less than 10ms, you can use this way
3// loovee, 2014-6-13
4
5
6#include <SPI.h>
7#include "mcp_can.h"
8
9
10// the cs pin of the version after v1.1 is default to D9
11// v0.9b and v1.0 is default D10
12const int SPI_CS_PIN = 9;
13
14MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
15
16void setup()
17{
18 Serial.begin(115200);
19
20START_INIT:
21
22 if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
23 {
24 Serial.println("CAN BUS Shield init ok!");
25 }
26 else
27 {
28 Serial.println("CAN BUS Shield init fail");
29 Serial.println("Init CAN BUS Shield again");
30 delay(100);
31 goto START_INIT;
32 }
33}
34
35
36void loop()
37{
38 unsigned char len = 0;
39 unsigned char buf[8];
40
41 if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
42 {
43 CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
44
45 unsigned long canId = CAN.getCanId();
46
47 Serial.println("-----------------------------");
48 Serial.print("get data from ID: ");
49 Serial.println(canId, HEX);
50
51 for(int i = 0; i<len; i++) // print the data
52 {
53 Serial.print(buf[i]);
54 Serial.print("\t");
55 }
56 Serial.println();
57 }
58}
59
60/*********************************************************************************************************
61 END FILE
62*********************************************************************************************************/
Note: See TracBrowser for help on using the repository browser.