CAN总线硬件连接及程序

上传人:da****ge 文档编号:135430367 上传时间:2022-08-15 格式:DOC 页数:10 大小:164.35KB
收藏 版权申诉 举报 下载
CAN总线硬件连接及程序_第1页
第1页 / 共10页
CAN总线硬件连接及程序_第2页
第2页 / 共10页
CAN总线硬件连接及程序_第3页
第3页 / 共10页
资源描述:

《CAN总线硬件连接及程序》由会员分享,可在线阅读,更多相关《CAN总线硬件连接及程序(10页珍藏版)》请在装配图网上搜索。

1、1. /F04x_CAN1.c 2. /- 3. / 4. / 5. /DEVICE:C8051F040 6. / 7. /AUTHOR:LS 8. / 9. /TOOLS:KeilC-compilerandSiliconLabsIDE 10. / 11. / 12. /CAN1.candCAN2.careasimpleexampleofconfiguringaCANnetworkto 13. /transmitandreceivedataonaCANnetwork,andhowtomoveinformationto 14. /andfromCANRAMmessageobjects.EachC

2、8051F040-TBCANnodeisconfigured 15. /tosendamessagewhenitsP3.7buttonisdepressed/released,witha0x11 16. /toindicatethebuttonispushed,and0x00whenreleased.Eachnodealsohas 17. /amessageobjectconfiguredtoreceivemessages.TheC8051teststhe 18. /receiveddataandwillturnon/offthetargetboardsLED.Whenonetarget 19

3、. /isloadedwithCAN2.candtheotherisloadedwithCAN1.c,onetarget 20. /boardspush-buttonwillcontroltheothertargetboardsLED,establishing 21. /asimplecontrollinkviatheCANbusandcanbeobserveddirectlyonthe 22. /targetboards. 23. / 24. 25. / 26. /Includes 27. / 28. 29. #include/SFRdeclarations 30. 31. /CANProt

4、ocolRegisterIndexforCAN0ADR,fromTABLE18.1oftheC8051F040 32. /datasheet 33. / 34. #defineCANCTRL0x00/ControlRegister 35. #defineCANSTAT0x01/Statusregister 36. #defineERRCNT0x02/ErrorCounterRegister 37. #defineBITREG0x03/BitTimingRegister 38. #defineINTREG0x04/InterruptLowByteRegister 39. #defineCANTS

5、TR0x05/Testregister 40. #defineBRPEXT0x06/BRPExtensionRegister 41. / 42. /IF1InterfaceRegisters 43. / 44. #defineIF1CMDRQST0x08/IF1CommandRestRegister 45. #defineIF1CMDMSK0x09/IF1CommandMaskRegister 46. #defineIF1MSK10x0A/IF1Mask1Register 47. #defineIF1MSK20x0B/IF1Mask2Register 48. #defineIF1ARB10x0

6、C/IF1Arbitration1Register 49. #defineIF1ARB20x0D/IF1Arbitration2Register 50. #defineIF1MSGC0x0E/IF1MessageControlRegister 51. #defineIF1DATA10x0F/IF1DataA1Register 52. #defineIF1DATA20x10/IF1DataA2Register 53. #defineIF1DATB10x11/IF1DataB1Register 54. #defineIF1DATB20x12/IF1DataB2Register 55. / 56.

7、/IF2InterfaceRegisters 57. / 58. #defineIF2CMDRQST0x20/IF2CommandRestRegister 59. #defineIF2CMDMSK0x21/IF2CommandMaskRegister 60. #defineIF2MSK10x22/IF2Mask1Register 61. #defineIF2MSK20x23/IF2Mask2Register 62. #defineIF2ARB10x24/IF2Arbitration1Register 63. #defineIF2ARB20x25/IF2Arbitration2Register

8、64. #defineIF2MSGC0x26/IF2MessageControlRegister 65. #defineIF2DATA10x27/IF2DataA1Register 66. #defineIF2DATA20x28/IF2DataA2Register 67. #defineIF2DATB10x29/IF2DataB1Register 68. #defineIF2DATB20x2A/IF2DataB2Register 69. / 70. /MessageHandlerRegisters 71. / 72. #defineTRANSREQ10x40/TransmissionRest1

9、Register 73. #defineTRANSREQ20x41/TransmissionRest2Register 74. 75. #defineNEWDAT10x48/NewData1Register 76. #defineNEWDAT20x49/NewData2Register 77. 78. #defineINTPEND10x50/InterruptPending1Register 79. #defineINTPEND20x51/InterruptPending2Register 80. 81. #defineMSGVAL10x58/MessageValid1Register 82.

10、 #defineMSGVAL20x59/MessageValid2Register 83. 84. / 85. /GlobalVariables 86. / 87. charMsgNum;88. charstatus;89. inti;90. intMOTwoIndex=0;91. intMOOneIndex=0;92. intStatusCopy;93. intRXbuffer4;94. intTXbuffer8;95. intMsgIntNum;96. intTemperature;97. sbitBUTTON=P37;98. sbitLED=P16;99. sfr16CAN0DAT=0x

11、D8;100. 101. 102. 103. / 104. /FunctionPROTOTYPES 105. / 106. 107. /InitializeMessageObject 108. voidclear_msg_objects(void);109. voidinit_msg_object_TX(charMsgNum);110. voidinit_msg_object_RX(charMsgNum);111. voidstart_CAN(void);112. voidtransmit_turn_LED_ON(charMsgNum);113. voidtransmit_turn_LED_O

12、FF(charMsgNum);114. voidreceive_data(charMsgNum);115. voidexternal_osc(void);116. voidconfig_IO(void);117. voidflash_LED(void);118. voidtest_reg_write(chartest);119. voidstop_CAN(void);120. 121. 122. / 123. /MAINRoutine 124. / 125. voidmain(void)126. 127. /disablewatchdogtimer 128. WDTCN=0xde;129. W

13、DTCN=0xad;130. 131. /configurePortI/O 132. config_IO();133. 134. /switchtoexternaloscillator 135. external_osc();136. 137. 138. / 139. /ConfigureCANcommunications 140. / 141. /IF1usedforprocedurescallesbymainprogram 142. /IF2usedforinterruptserviceprocedurereceive_data 143. / 144. /MessageObjectassi

14、gnments: 145. /0x02:UsedtotransmitcommandstotoggleitsLED,arbitrationnumber1 146. / 147. / 148. 149. /ClearCANRAM 150. clear_msg_objects();151. 152. /Initializemessageobjecttotransmitdata 153. init_msg_object_TX(0x02);154. 155. /Initializemessageobjecttoreceivedata 156. init_msg_object_RX(0x01);157.

15、158. /EnableCANinterruptsinCIP-51 159. EIE2=0x20;160. 161. /FunctioncalltostartCAN 162. start_CAN();163. 164. /Globalenable8051interrupts 165. EA=1;166. 167. /Loopandwaitforinterrupts 168. while(1)169. 170. if(BUTTON=0)171. while(BUTTON=0)172. transmit_turn_LED_OFF(0x02);173. else174. while(BUTTON=1

16、)175. transmit_turn_LED_ON(0x02);176. 177. 178. 179. 180. / 181. /SetupC8051F040 182. / 183. 184. /Switchtoexternaloscillator 185. voidexternal_osc(void)186. 187. intn;/localvariableusedindelayFORloop. 188. SFRPAGE=CONFIG_PAGE;/switchtoconfigpagetoconfigoscillator 189. OSCXCN=0x77;/startexternalosci

17、llator;22.1MHzCrystal 190. /systemclockis22.1MHz/2=11.05MHz 191. for(n=0;n255;n+);/delayabout1ms 192. while(OSCXCN&0x80)=0);/waitforoscillatortostabilize 193. CLKSEL|=0x01;/switchtoexternaloscillator 194. 195. 196. voidconfig_IO(void)197. 198. SFRPAGE=CONFIG_PAGE;/PortSFRsonConfigurationpage 199. XB

18、R3=0x80;/ConfigureCANTXpin(CTX)aspush-pulldigitaloutput 200. P1MDOUT|=0x40;/ConfigureP1.6aspush-pulltodriveLED 201. XBR2=0x40;/EnableCrossbar/lowports 202. 203. 204. / 205. /CANFunctions 206. / 207. 208. 209. /ClearMessageObjects 210. voidclear_msg_objects(void)211. 212. SFRPAGE=CAN0_PAGE;213. CAN0A

19、DR=IF1CMDMSK;/PointtoCommandMaskRegister1 214. CAN0DATL=0xFF;/SetdirectiontoWRITEallIFregisterstoMsgObj 215. for(i=1;i=400ns).275. Sync_Seg=1tq276. 277. Phase_seg1+Phase_Seg2=(11-6)tq=5tq278. Phase_seg1Phase_seg1=2tqandPhase_Seg2=3tq279. SJW=(min(Phase_Seg1,4)tq=2tq280. 281. TSEG1=(Prop_Seg+Phase_Se

20、g1-1)=6282. TSEG2=(Phase_Seg2-1)=2283. SJW_p=(SJW-1)=1284. 285. BitTimingRegister=BRP+SJW_p*0x0040=TSEG1*0x0100+TSEG2*0x1000=2640286. 287. Clocktolerancedf:288. 289. A:dfmin(Phase_Seg1,Phase_Seg2)/(2*(13*bit_time-Phase_Seg2)290. B:dfSJW/(20*bit_time)291. 292. A:df2/(2*(13*11-3)=1/(141-3)=1/138=0.724

21、6%293. B:df2/(20*11)=1/110=0.9091%294. 295. Actualclocktoleranceis0.7246%-0.5381%=0.1865%(noproblemforquartz)296. */297. 298. SFRPAGE=CAN0_PAGE;299. CAN0CN|=0x41;/ConfigurationChangeEnableCCEandINIT 300. CAN0ADR=BITREG;/PointtoBitTimingregister 301. CAN0DAT=0x2640;/seeabove 302. 303. CAN0ADR=IF1CMDM

22、SK;/PointtoCommandMask1 304. CAN0DAT=0x0087;/ConfigforTX:WRITEtoCANRAM,writedatabytes, 305. /setTXrqst/NewDat,clrIntPnd 306. 307. /RX-IF2operationmayinterruptTX-IF1operation 308. CAN0ADR=IF2CMDMSK;/PointtoCommandMask2 309. CAN0DATL=0x1F;/ConfigforRX:READCANRAM,readdatabytes, 310. /clrNewDatandIntPnd

23、 311. CAN0CN|=0x06;/GlobalInt.EnableIEandSIE 312. CAN0CN&=0x41;/ClearCCEandINITbits,startsCANstatemachine 313. 314. 315. /TransmitCANframetoturnothernodesLEDON 316. voidtransmit_turn_LED_ON(charMsgNum)317. 318. SFRPAGE=CAN0_PAGE;/IF1alreadysetupforTX 319. CAN0ADR=IF1CMDMSK;/PointtoCommandMask1 320.

24、CAN0DAT=0x0087;/ConfigtoWRITEtoCANRAM,writedatabytes, 321. /setTXrqst/NewDat,ClrIntPnd 322. CAN0ADR=IF1DATA1;/Pointto1stbyteofDataField 323. CAN0DATL=0x11;/OnessignalstoturnLEDslightONindataA1field 324. CAN0ADR=IF1CMDRQST;/PointtoCommandRequestReg. 325. CAN0DATL=MsgNum;/MovenewdataforTXtoMsgObjMsgNu

25、m 326. 327. 328. /TransmitCANFrametoturnothernodesLEDOFF 329. voidtransmit_turn_LED_OFF(charMsgNum)330. 331. SFRPAGE=CAN0_PAGE;/IF1alreadysetupforTX 332. CAN0ADR=IF1DATA1;/Pointto1stbyteofDataField 333. CAN0DATL=0x00;/ZerosignalstoturnLEDslightONinDataA1field 334. CAN0ADR=IF1CMDRQST;/PointtoCommandR

26、equestReg. 335. CAN0DATL=MsgNum;/MovenewdataforTXtoMsgObjMsgNum 336. 337. 338. 339. /ReceiveDatafromtheIF2buffer 340. voidreceive_data(charMsgNum)341. 342. charvirtual_button;343. SFRPAGE=CAN0_PAGE;/IF1alreadysetupforRX 344. CAN0ADR=IF2CMDRQST;/PointtoCommandRequestReg. 345. CAN0DATL=MsgNum;/Movenew

27、dataforRXfromMsgObjMsgNum 346. /Movenewdatatoa 347. CAN0ADR=IF2DATA1;/Pointto1stbyteofDataField 348. 349. virtual_button=CAN0DATL;350. if(virtual_button=0x11)/OnesissignalfromothernodetoturnLEDON 351. LED=1;352. elseLED=0;/OtherwiseturnLEDOFF(messagewasones) 353. 354. 355. / 356. /InterruptServiceRo

28、utine 357. / 358. voidISRname(void)interrupt19359. 360. status=CAN0STA;361. if(status&0x10)!=0)362. /RxOkisset,interruptcausedbyreception 363. CAN0STA=(CAN0STA&0xEF)|0x07;/ResetRxOk,setLECtoNoChange 364. /*readmessagenumberfromCANINTREG*/365. receive_data(0x01);/Uptonow,wehaveonlyoneRXmessage 366. 3

29、67. if(status&0x08)!=0)368. /TxOkisset,interruptcausedbytransmision 369. CAN0STA=(CAN0STA&0xF7)|0x07;/ResetTxOk,setLECtoNoChange 370. 371. if(status&0x07)!=0)&(status&0x07)!=7)372. /Errorinterrupt,LECchanged 373. /*errorhandling?*/374. CAN0STA=CAN0STA|0x07;/SetLECtoNoChange 375. 376. 377. /- / F04x_

30、CAN1.c /- / / / DEVICE: C8051F040 / / AUTHOR: LS / / TOOLS: Keil C-compiler and Silicon Labs IDE / / / CAN1.c and CAN2.c are a simple example of configuring a CAN network to / transmit and receive data on a CAN network, and how to move information to / and from CAN RAM message objects. Each C8051F04

31、0-TB CAN node is configured / to send a message when its P3.7 button is depressed/released, with a 0x11 / to indicate the button is pushed, and 0x00 when released. Each node also has / a message object configured to receive messages. The C8051 tests the / received data and will turn on/off the target boards LED. When one target / is loaded with CAN2.c and the other is loaded with CAN1.c, one target / boards push-button will control the other target boards LED, establishing / a simple control link via the CAN bus and can be observed directly on the / target boards. / /

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!