source: rtos_arduino/trunk/examples/NCESCan/r2ca_app.cpp@ 260

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

マクロ名を更新.
実行モデルを変更.

File size: 3.1 KB
Line 
1#include "r2ca.h"
2
3// demo: CAN-BUS Shield, receive data
4#include <SPI.h>
5#include "mcp_can.h"
6#include <stdio.h>
7#define uint8_t unsigned char
8
9const int SPI_CS_PIN = 9;
10#define CAN_INT_PIN 3
11MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
12
13extern void MCP2515_ISR();
14
15/*
16 * CAN Controller : MCP2515
17 * TXB : MCP_TXB0:0, MCP_RXB1:1,MCP_RXB2:2
18 * RXB : MCP_RXB0:0 : Mask MCP_RXM0:0 : Filer MCP_RXF0:0, MCP_RXF1:1
19 * MCP_RXB1:1 : Mask MCP_RXM2:1 : Filer MCP_RXF2:2, MCP_RXF3:3, MCP_RXF4:4, MCP_RXF5:5
20 * Interrupt mask
21 * MCP_TX0IF
22 * MCP_TX1IF
23 * MCP_TX2IF
24 * MCP_RX0IF
25 * MCP_RX1IF
26 */
27
28void setup()
29{
30 Serial.begin(115200);
31 pinMode(13, OUTPUT);
32
33 /*
34 * CAN Initialize
35 */
36 if(CAN.begin(CAN_500KBPS) ==CAN_OK) {
37 Serial.print("can init ok!!\r\n");
38 }
39 else {
40 while(1){
41 Serial.print("Can init fail!!\r\n");
42 digitalWrite(13, HIGH);
43 delay(500);
44 digitalWrite(13, LOW);
45 delay(500);
46 };
47 }
48
49 /*
50 * CAN Recive mabx Init
51 */
52
53 /*
54 * RXB0
55 */
56 CAN.init_Mask(MCP_RXM0, 0x0f);
57 CAN.init_Filter(MCP_RXF0, 0x01);
58 CAN.init_Filter(MCP_RXF1, 0x02);
59 CAN.startReceive(MCP_RXB0);
60
61 /*
62 * RXB1
63 */
64 CAN.init_Mask(MCP_RXM1, 0x0f);
65 CAN.init_Filter(MCP_RXF2, 0x03);
66 CAN.init_Filter(MCP_RXF3, 0x04);
67 CAN.init_Filter(MCP_RXF4, 0x05);
68 CAN.init_Filter(MCP_RXF5, 0x06);
69 CAN.startReceive(MCP_RXB1);
70
71 CAN.enableInterrupt(MCP_RX0IF|MCP_RX1IF);
72 CAN.attachInterrupt(CAN_INT_PIN, MCP2515_ISR);
73
74}
75
76uint8_t Flag_Recv = 0;
77
78void MCP2515_ISR()
79{
80 Flag_Recv = 1;
81}
82
83void loop()
84{
85 uint8_t len = 0;
86 uint8_t buf[8];
87 uint8_t i;
88 uint32_t id;
89
90 if(!Flag_Recv) {
91 delay(1);
92 return;
93 }
94
95 Flag_Recv = 0;
96
97 for(i = 0; i < 2; i++){
98 if(CAN.checkReceive(i) == CAN_NOMSG) {
99 continue;
100 }
101 CAN.readMsg(i, &id, &len, buf); // read data, len: data length, buf: data buf
102 Serial.print("CAN_BUS GET DATA MBX");
103 Serial.println(i);
104 Serial.print("ID = ");Serial.print(id, HEX);
105 Serial.print(", DLC = ");Serial.print(len);
106 Serial.print(", data = ");
107 for(int i = 0; i<len; i++){ // print the data
108 Serial.print(buf[i]);Serial.print("\t");
109 }
110 Serial.println();
111 }
112}
113
114unsigned char stmp[8][8] = {
115 {1, 2, 3, 4, 5, 6, 7, 8},
116 {2, 3, 4, 5, 6, 7, 8, 1},
117 {3, 4, 5, 6, 7, 8, 1, 2},
118 {1, 2, 3, 4, 5, 6, 7, 8},
119 {1, 2, 3, 4, 5, 6, 7, 8},
120 {1, 2, 3, 4, 5, 6, 7, 8},
121 {1, 2, 3, 4, 5, 6, 7, 8},
122 {0, 1, 2, 3, 4, 5, 6, 7}
123};
124
125unsigned char stmp1[8] = {0, 1, 2, 3, 4, 5, 6, 7};
126
127void loop1()
128{
129 int i, e;
130
131 for(e = 0; e < 3; e++){
132 Serial.print("Send can message from mbx ");
133 Serial.println(e);
134 for(i = 1; i <= 8; i++){
135 while(CAN_SENDWAIT == CAN.checkSend(e)){delay(10);};
136 /* mailbox, ID, DLC, DATA */
137 CAN.sendMsg(e, i, i, stmp[i-1]);
138 }
139 }
140
141 delay(5000);
142}
Note: See TracBrowser for help on using the repository browser.