CAN测试程序.docx
《CAN测试程序.docx》由会员分享,可在线阅读,更多相关《CAN测试程序.docx(23页珍藏版)》请在冰豆网上搜索。
![CAN测试程序.docx](https://file1.bdocx.com/fileroot1/2023-2/6/558224e6-b8d5-4abc-918d-659f038859a5/558224e6-b8d5-4abc-918d-659f038859a51.gif)
CAN测试程序
1#include"DSP28_Device.h"
2
3
4//---------------------------------------------------------------------------
5//InitECan:
6//---------------------------------------------------------------------------
7//Thisfunctioninitializestoaknownstate.
8//
9voidInitECan(void)
10{
11structECAN_REGSECanaShadow;
12EALLOW;
13ECanaShadow.CANTIOC.all=ECanaRegs.CANTIOC.all;
14ECanaShadow.CANTIOC.bit.TXFUNC=1;
15ECanaRegs.CANTIOC.all=ECanaShadow.CANRIOC.all;
16
17ECanaShadow.CANRIOC.all=ECanaRegs.CANRIOC.all;
18ECanaShadow.CANRIOC.bit.RXFUNC=1;
19ECanaRegs.CANRIOC.all=ECanaShadow.CANRIOC.all;
20EDIS;
21
22ECanaRegs.CANME.all=0;
23
24/*发送邮箱的ID号*/
25ECanaMboxes.MBOX0.MID.all=0x9555AAA0;
26ECanaMboxes.MBOX1.MID.all=0x9555AAA1;
27ECanaMboxes.MBOX2.MID.all=0x9555AAA2;
28ECanaMboxes.MBOX3.MID.all=0x9555AAA3;
29ECanaMboxes.MBOX4.MID.all=0x9555AAA4;
30ECanaMboxes.MBOX5.MID.all=0x9555AAA5;
31ECanaMboxes.MBOX6.MID.all=0x9555AAA6;
32ECanaMboxes.MBOX7.MID.all=0x9555AAA7;
33ECanaMboxes.MBOX8.MID.all=0x9555AAA8;
34ECanaMboxes.MBOX9.MID.all=0x9555AAA9;
35ECanaMboxes.MBOX10.MID.all=0x9555AAAA;
36ECanaMboxes.MBOX11.MID.all=0x9555AAAB;
37ECanaMboxes.MBOX12.MID.all=0x9555AAAC;
38ECanaMboxes.MBOX13.MID.all=0x9555AAAD;
39ECanaMboxes.MBOX14.MID.all=0x9555AAAE;
40ECanaMboxes.MBOX15.MID.all=0x9555AAAF;
41
42/*接收邮箱的ID号*/
43ECanaMboxes.MBOX16.MID.all=0x9555AAA0;
44ECanaMboxes.MBOX17.MID.all=0x9555AAA1;
45ECanaMboxes.MBOX18.MID.all=0x9555AAA2;
46ECanaMboxes.MBOX19.MID.all=0x9555AAA3;
47ECanaMboxes.MBOX20.MID.all=0x9555AAA4;
48ECanaMboxes.MBOX21.MID.all=0x9555AAA5;
49ECanaMboxes.MBOX22.MID.all=0x9555AAA6;
50ECanaMboxes.MBOX23.MID.all=0x9555AAA7;
51ECanaMboxes.MBOX24.MID.all=0x9555AAA8;
52ECanaMboxes.MBOX25.MID.all=0x9555AAA9;
53ECanaMboxes.MBOX26.MID.all=0x9555AAAA;
54ECanaMboxes.MBOX27.MID.all=0x9555AAAB;
55ECanaMboxes.MBOX28.MID.all=0x9555AAAC;
56ECanaMboxes.MBOX29.MID.all=0x9555AAAD;
57ECanaMboxes.MBOX30.MID.all=0x9555AAAE;
58ECanaMboxes.MBOX31.MID.all=0x9555AAAF;
59
60/*0~15isTX,16~31isRX*/
61ECanaRegs.CANMD.all=0xFFFF0000;
62
63/*32个邮箱使能*/
64ECanaRegs.CANME.all=0xFFFFFFFF;
65
66/*数据长度8个BYTE*/
67ECanaMboxes.MBOX0.MCF.bit.DLC=8;
68ECanaMboxes.MBOX1.MCF.bit.DLC=8;
69ECanaMboxes.MBOX2.MCF.bit.DLC=8;
70ECanaMboxes.MBOX3.MCF.bit.DLC=8;
71ECanaMboxes.MBOX4.MCF.bit.DLC=8;
72ECanaMboxes.MBOX5.MCF.bit.DLC=8;
73ECanaMboxes.MBOX6.MCF.bit.DLC=8;
74ECanaMboxes.MBOX7.MCF.bit.DLC=8;
75ECanaMboxes.MBOX8.MCF.bit.DLC=8;
76ECanaMboxes.MBOX9.MCF.bit.DLC=8;
77ECanaMboxes.MBOX10.MCF.bit.DLC=8;
78ECanaMboxes.MBOX11.MCF.bit.DLC=8;
79ECanaMboxes.MBOX12.MCF.bit.DLC=8;
80ECanaMboxes.MBOX13.MCF.bit.DLC=8;
81ECanaMboxes.MBOX14.MCF.bit.DLC=8;
82ECanaMboxes.MBOX15.MCF.bit.DLC=8;
83ECanaMboxes.MBOX16.MCF.bit.DLC=8;
84ECanaMboxes.MBOX17.MCF.bit.DLC=8;
85ECanaMboxes.MBOX18.MCF.bit.DLC=8;
86ECanaMboxes.MBOX19.MCF.bit.DLC=8;
87ECanaMboxes.MBOX20.MCF.bit.DLC=8;
88ECanaMboxes.MBOX21.MCF.bit.DLC=8;
89ECanaMboxes.MBOX22.MCF.bit.DLC=8;
90ECanaMboxes.MBOX23.MCF.bit.DLC=8;
91ECanaMboxes.MBOX24.MCF.bit.DLC=8;
92ECanaMboxes.MBOX25.MCF.bit.DLC=8;
93ECanaMboxes.MBOX26.MCF.bit.DLC=8;
94ECanaMboxes.MBOX27.MCF.bit.DLC=8;
95ECanaMboxes.MBOX28.MCF.bit.DLC=8;
96ECanaMboxes.MBOX29.MCF.bit.DLC=8;
97ECanaMboxes.MBOX30.MCF.bit.DLC=8;
98ECanaMboxes.MBOX31.MCF.bit.DLC=8;
99
100/*没有远方应答帧被请求*/
101ECanaMboxes.MBOX0.MCF.bit.RTR=0;
102ECanaMboxes.MBOX1.MCF.bit.RTR=0;
103ECanaMboxes.MBOX2.MCF.bit.RTR=0;
104ECanaMboxes.MBOX3.MCF.bit.RTR=0;
105ECanaMboxes.MBOX4.MCF.bit.RTR=0;
106ECanaMboxes.MBOX5.MCF.bit.RTR=0;
107ECanaMboxes.MBOX6.MCF.bit.RTR=0;
108ECanaMboxes.MBOX7.MCF.bit.RTR=0;
109ECanaMboxes.MBOX8.MCF.bit.RTR=0;
110ECanaMboxes.MBOX9.MCF.bit.RTR=0;
111ECanaMboxes.MBOX10.MCF.bit.RTR=0;
112ECanaMboxes.MBOX11.MCF.bit.RTR=0;
113ECanaMboxes.MBOX12.MCF.bit.RTR=0;
114ECanaMboxes.MBOX13.MCF.bit.RTR=0;
115ECanaMboxes.MBOX14.MCF.bit.RTR=0;
116ECanaMboxes.MBOX15.MCF.bit.RTR=0;
117ECanaMboxes.MBOX16.MCF.bit.RTR=0;
118ECanaMboxes.MBOX17.MCF.bit.RTR=0;
119ECanaMboxes.MBOX18.MCF.bit.RTR=0;
120ECanaMboxes.MBOX19.MCF.bit.RTR=0;
121ECanaMboxes.MBOX20.MCF.bit.RTR=0;
122ECanaMboxes.MBOX21.MCF.bit.RTR=0;
123ECanaMboxes.MBOX22.MCF.bit.RTR=0;
124ECanaMboxes.MBOX23.MCF.bit.RTR=0;
125ECanaMboxes.MBOX24.MCF.bit.RTR=0;
126ECanaMboxes.MBOX25.MCF.bit.RTR=0;
127ECanaMboxes.MBOX26.MCF.bit.RTR=0;
128ECanaMboxes.MBOX27.MCF.bit.RTR=0;
129ECanaMboxes.MBOX28.MCF.bit.RTR=0;
130ECanaMboxes.MBOX29.MCF.bit.RTR=0;
131ECanaMboxes.MBOX30.MCF.bit.RTR=0;
132ECanaMboxes.MBOX31.MCF.bit.RTR=0;
133
134/*---------设置发送邮箱的初始值------------*/
135
136//设每个发送邮箱的初始值都为十六进制数"FEDCBA9876543210"
137
138ECanaMboxes.MBOX0.MDRL.all=0x76543210;
139ECanaMboxes.MBOX0.MDRH.all=0xFEDCBA98;
140
141ECanaMboxes.MBOX1.MDRL.all=0x76543210;
142ECanaMboxes.MBOX1.MDRH.all=0xFEDCBA98;
143
144ECanaMboxes.MBOX2.MDRL.all=0x76543210;
145ECanaMboxes.MBOX2.MDRH.all=0xFEDCBA98;
146
147ECanaMboxes.MBOX3.MDRL.all=0x76543210;
148ECanaMboxes.MBOX3.MDRH.all=0xFEDCBA98;
149
150ECanaMboxes.MBOX4.MDRL.all=0x76543210;
151ECanaMboxes.MBOX4.MDRH.all=0xFEDCBA98;
152
153ECanaMboxes.MBOX5.MDRL.all=0x76543210;
154ECanaMboxes.MBOX5.MDRH.all=0xFEDCBA98;
155
156ECanaMboxes.MBOX6.MDRL.all=0x76543210;
157ECanaMboxes.MBOX6.MDRH.all=0xFEDCBA98;
158
159ECanaMboxes.MBOX7.MDRL.all=0x76543210;
160ECanaMboxes.MBOX7.MDRH.all=0xFEDCBA98;
161
162ECanaMboxes.MBOX8.MDRL.all=0x76543210;
163ECanaMboxes.MBOX8.MDRH.all=0xFEDCBA98;
164
165ECanaMboxes.MBOX9.MDRL.all=0x76543210;
166ECanaMboxes.MBOX9.MDRH.all=0xFEDCBA98;
167
168ECanaMboxes.MBOX10.MDRL.all=0x76543210;
169ECanaMboxes.MBOX10.MDRH.all=0xFEDCBA98;
170
171ECanaMboxes.MBOX11.MDRL.all=0x76543210;
172ECanaMboxes.MBOX11.MDRH.all=0xFEDCBA98;
173
174ECanaMboxes.MBOX12.MDRL.all=0x76543210;
175ECanaMboxes.MBOX12.MDRH.all=0xFEDCBA98;
176
177ECanaMboxes.MBOX13.MDRL.all=0x76543210;
178ECanaMboxes.MBOX13.MDRH.all=0xFEDCBA98;
179
180ECanaMboxes.MBOX14.MDRL.all=0x76543210;
181ECanaMboxes.MBOX14.MDRH.all=0xFEDCBA98;
182
183ECanaMboxes.MBOX15.MDRL.all=0x76543210;
184ECanaMboxes.MBOX15.MDRH.all=0xFEDCBA98;
185
186//初始化接收邮箱
187
188ECanaMboxes.MBOX16.MDRL.all=0x00000000;
189ECanaMboxes.MBOX16.MDRH.all=0x00000000;
190
191ECanaMboxes.MBOX17.MDRL.all=0x00000000;
192ECanaMboxes.MBOX17.MDRH.all=0x00000000;
193
194ECanaMboxes.MBOX18.MDRL.all=0x00000000;
195ECanaMboxes.MBOX18.MDRH.all=0x00000000;
196
197ECanaMboxes.MBOX19.MDRL.all=0x00000000;
198ECanaMboxes.MBOX19.MDRH.all=0x00000000;
199
200ECanaMboxes.MBOX20.MDRL.all=0x00000000;
201ECanaMboxes.MBOX20.MDRH.all=0x00000000;
202
203ECanaMboxes.MBOX21.MDRL.all=0x00000000;
204ECanaMboxes.MBOX21.MDRH.all=0x00000000;
205
206ECanaMboxes.MBOX22.MDRL.all=0x00000000;
207ECanaMboxes.MBOX22.MDRH.all=0x00000000;
208
209ECanaMboxes.MBOX23.MDRL.all=0x00000000;
210ECanaMboxes.MBOX23.MDRH.all=0x00000000;
211
212ECanaMboxes.MBOX24.MDRL.all=0x00000000;
213ECanaMboxes.MBOX24.MDRH.all=0x00000000;
214
215ECanaMboxes.MBOX25.MDRL.all=0x00000000;
216ECanaMboxes.MBOX25.MDRH.all=0x00000000;
217
218ECanaMboxes.MBOX26.MDRL.all=0x00000000;
219ECanaMboxes.MBOX26.MDRH.all=0x00000000;
220
221ECanaMboxes.MBOX27.MDRL.all=0x00000000;
222ECanaMboxes.MBOX27.MDRH.all=0x00000000;
223
224ECanaMboxes.MBOX28.MDRL.all=0x00000000;
225ECanaMboxes.MBOX28.MDRH.all=0x00000000;
226
227ECanaMboxes.MBOX29.MDRL.all=0x00000000;
228ECanaMboxes.MBOX29.MDRH.all=0x00000000;
229
230ECanaMboxes.MBOX30.MDRL.all=0x00000000;
231ECanaMboxes.MBOX30.MDRH.all=0x00000000;
232
233ECanaMboxes.MBOX31.MDRL.all=0x00000000;
234ECanaMboxes.MBOX31.MDRH.all=0x00000000;
235
236
237
238
239
240EALLOW;
241/*邮箱屏蔽使能*/
242ECanaRegs.CANMIM.all=0xFFFFFFFF;
243ECanaShadow.CANMC.all=ECanaRegs.CANMC.all;
244ECanaShadow.CANMC.bit.CCR=1;
245ECanaRegs.CANMC.all=ECanaShadow.CANMC.all;
246EDIS;
247
248do
249{
250ECanaShadow.CANES.all=ECanaRegs.CANES.all;
251}while(ECanaShadow.CANES.bit.CCE!
=1);
252
253EALLOW;
254/*(BRPREG+1)=10feedsa15MHzCANclock*/
255ECanaShadow.CANBTC.bit.BRP=9;
256/*150/10=15*/
257ECanaShadow.CANBTC.bit.TSEG2=5;
258ECanaShadow.CANBTC.bit.TSEG1=7;
259ECanaRegs.CANBTC.all=ECanaShadow.CANBTC.all;
260
261ECanaShadow.CANMC.all=ECanaRegs.CANMC.all;
262ECanaShadow.CANMC.bit.CCR=0;
263ECanaRegs.CANMC.all=ECanaShadow.CANMC.all;
264EDIS;
265
266do
267{
268ECanaShadow.CANES.all=ECanaRegs.CANES.all;
269}while(ECanaShadow.CANES.bit.CCE!
=0);
270
271EALLOW;
272ECanaShadow.CANMC.all=ECanaRegs.CANMC.all;
273ECanaShadow.CANMC.bit.STM=1;
274ECanaShadow.CANMC.bit.SCM=1;
275ECanaRegs.CANMC.all=ECanaShadow.CANMC.all;
276EDIS;
277
278#ifCAN_INT
279EALLOW;
280ECanaRegs.CANMIM.all=0xFFFFffff;
281ECanaRegs.CANMIL.all=0;
282ECanaRegs.CANGIF0.all=0xFFFFFFFF;
283ECanaRegs.CANGIM.bit.I0EN=1;
284ECan