CAN总线硬件连接及程序(7页).doc

上传人:1595****071 文档编号:35396836 上传时间:2022-08-21 格式:DOC 页数:7 大小:226KB
返回 下载 相关 举报
CAN总线硬件连接及程序(7页).doc_第1页
第1页 / 共7页
CAN总线硬件连接及程序(7页).doc_第2页
第2页 / 共7页
点击查看更多>>
资源描述

《CAN总线硬件连接及程序(7页).doc》由会员分享,可在线阅读,更多相关《CAN总线硬件连接及程序(7页).doc(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、-1.2.3.4. CAN总线硬件连接及程序-第 7 页5. /F04x_CAN1.c 6. /DEVICE:C8051F040 7. /AUTHOR:LS 8. /TOOLS:KeilC-compilerandSiliconLabsIDE 9. /CAN1.candCAN2.careasimpleexampleofconfiguringaCANnetworkto 10. /transmitandreceivedataonaCANnetwork,andhowtomoveinformationto 11. /andfromCANRAMmessageobjects.EachC8051F040-TB

2、CANnodeisconfigured 12. /tosendamessagewhenitsP3.7buttonisdepressed/released,witha0x11 13. /toindicatethebuttonispushed,and0x00whenreleased.Eachnodealsohas 14. /amessageobjectconfiguredtoreceivemessages.TheC8051teststhe 15. /receiveddataandwillturnon/offthetargetboardsLED.Whenonetarget 16. /isloaded

3、withCAN2.candtheotherisloadedwithCAN1.c,onetarget 17. /boardspush-buttonwillcontroltheothertargetboardsLED,establishing 18. /asimplecontrollinkviatheCANbusandcanbeobserveddirectlyonthe 19. /targetboards. 20. /Includes 21. #include/SFRdeclarations 22. /CANProtocolRegisterIndexforCAN0ADR,fromTABLE18.1

4、oftheC8051F040 23. /datasheet 24. #defineCANCTRL0x00/ControlRegister 25. #defineCANSTAT0x01/Statusregister 26. #defineERRCNT0x02/ErrorCounterRegister 27. #defineBITREG0x03/BitTimingRegister 28. #defineINTREG0x04/InterruptLowByteRegister 29. #defineCANTSTR0x05/Testregister 30. #defineBRPEXT0x06/BRPEx

5、tensionRegister 31. /IF1InterfaceRegisters 32. #defineIF1CMDRQST0x08/IF1CommandRestRegister 33. #defineIF1CMDMSK0x09/IF1CommandMaskRegister 34. #defineIF1MSK10x0A/IF1Mask1Register 35. #defineIF1MSK20x0B/IF1Mask2Register 36. #defineIF1ARB10x0C/IF1Arbitration1Register 37. #defineIF1ARB20x0D/IF1Arbitra

6、tion2Register 38. #defineIF1MSGC0x0E/IF1MessageControlRegister 39. #defineIF1DATA10x0F/IF1DataA1Register 40. #defineIF1DATA20x10/IF1DataA2Register 41. #defineIF1DATB10x11/IF1DataB1Register 42. #defineIF1DATB20x12/IF1DataB2Register 43. /IF2InterfaceRegisters 44. #defineIF2CMDRQST0x20/IF2CommandRestRe

7、gister 45. #defineIF2CMDMSK0x21/IF2CommandMaskRegister 46. #defineIF2MSK10x22/IF2Mask1Register 47. #defineIF2MSK20x23/IF2Mask2Register 48. #defineIF2ARB10x24/IF2Arbitration1Register 49. #defineIF2ARB20x25/IF2Arbitration2Register 50. #defineIF2MSGC0x26/IF2MessageControlRegister 51. #defineIF2DATA10x2

8、7/IF2DataA1Register 52. #defineIF2DATA20x28/IF2DataA2Register 53. #defineIF2DATB10x29/IF2DataB1Register 54. #defineIF2DATB20x2A/IF2DataB2Register 55. /MessageHandlerRegisters 56. #defineTRANSREQ10x40/TransmissionRest1Register 57. #defineTRANSREQ20x41/TransmissionRest2Register 58. #defineNEWDAT10x48/

9、NewData1Register 59. #defineNEWDAT20x49/NewData2Register 60. #defineINTPEND10x50/InterruptPending1Register 61. #defineINTPEND20x51/InterruptPending2Register 62. #defineMSGVAL10x58/MessageValid1Register 63. #defineMSGVAL20x59/MessageValid2Register 64. /GlobalVariables 65. charMsgNum;66. charstatus;67

10、. inti;68. intMOTwoIndex=0;69. intMOOneIndex=0;70. intStatusCopy;71. intRXbuffer4;72. intTXbuffer8;73. intMsgIntNum;74. intTemperature;75. sbitBUTTON=P37;76. sbitLED=P16;77. sfr16CAN0DAT=0xD8;78. /FunctionPROTOTYPES 79. /InitializeMessageObject 80. voidclear_msg_objects(void);81. voidinit_msg_object

11、_TX(charMsgNum);82. voidinit_msg_object_RX(charMsgNum);83. voidstart_CAN(void);84. voidtransmit_turn_LED_ON(charMsgNum);85. voidtransmit_turn_LED_OFF(charMsgNum);86. voidreceive_data(charMsgNum);87. voidexternal_osc(void);88. voidconfig_IO(void);89. voidflash_LED(void);90. voidtest_reg_write(chartes

12、t);91. voidstop_CAN(void);92. /MAINRoutine 93. voidmain(void)94. /disablewatchdogtimer 95. WDTCN=0xde;96. WDTCN=0xad;97. /configurePortI/O 98. config_IO();99. /switchtoexternaloscillator 100. external_osc();101. /ConfigureCANcommunications 102. /IF1usedforprocedurescallesbymainprogram 103. /IF2usedf

13、orinterruptserviceprocedurereceive_data 104. /MessageObjectassignments: 105. /0x02:UsedtotransmitcommandstotoggleitsLED,arbitrationnumber1 106. /ClearCANRAM 107. clear_msg_objects();108. /Initializemessageobjecttotransmitdata 109. init_msg_object_TX(0x02);110. /Initializemessageobjecttoreceivedata 1

14、11. init_msg_object_RX(0x01);112. /EnableCANinterruptsinCIP-51 113. EIE2=0x20;114. /FunctioncalltostartCAN 115. start_CAN();116. /Globalenable8051interrupts 117. EA=1;118. /Loopandwaitforinterrupts 119. while(1)120. if(BUTTON=0)121. while(BUTTON=0)122. transmit_turn_LED_OFF(0x02);123. else124. while

15、(BUTTON=1)125. transmit_turn_LED_ON(0x02);126. /SetupC8051F040 127. /Switchtoexternaloscillator 128. voidexternal_osc(void)129. intn;/localvariableusedindelayFORloop. 130. SFRPAGE=CONFIG_PAGE;/switchtoconfigpagetoconfigoscillator 131. OSCXCN=0x77;/startexternaloscillator;22.1MHzCrystal 132. /systemc

16、lockis22.1MHz/2=11.05MHz 133. for(n=0;n255;n+);/delayabout1ms 134. while(OSCXCN&0x80)=0);/waitforoscillatortostabilize 135. CLKSEL|=0x01;/switchtoexternaloscillator 136. voidconfig_IO(void)137. SFRPAGE=CONFIG_PAGE;/PortSFRsonConfigurationpage 138. XBR3=0x80;/ConfigureCANTXpin(CTX)aspush-pulldigitalo

17、utput 139. P1MDOUT|=0x40;/ConfigureP1.6aspush-pulltodriveLED 140. XBR2=0x40;/EnableCrossbar/lowports 141. /CANFunctions 142. /ClearMessageObjects 143. voidclear_msg_objects(void)144. SFRPAGE=CAN0_PAGE;145. CAN0ADR=IF1CMDMSK;/PointtoCommandMaskRegister1 146. CAN0DATL=0xFF;/SetdirectiontoWRITEallIFreg

18、isterstoMsgObj 147. for(i=1;i=400ns).192. Sync_Seg=1tq193. Phase_seg1+Phase_Seg2=(11-6)tq=5tq194. Phase_seg1Phase_seg1=2tqandPhase_Seg2=3tq195. SJW=(min(Phase_Seg1,4)tq=2tq196. TSEG1=(Prop_Seg+Phase_Seg1-1)=6197. TSEG2=(Phase_Seg2-1)=2198. SJW_p=(SJW-1)=1199. BitTimingRegister=BRP+SJW_p*0x0040=TSEG1

19、*0x0100+TSEG2*0x1000=2640200. Clocktolerancedf:201. A:dfmin(Phase_Seg1,Phase_Seg2)/(2*(13*bit_time-Phase_Seg2)202. B:dfSJW/(20*bit_time)203. A:df2/(2*(13*11-3)=1/(141-3)=1/138=0.7246%204. B:df2/(20*11)=1/110=0.9091%205. Actualclocktoleranceis0.7246%-0.5381%=0.1865%(noproblemforquartz)206. SFRPAGE=CA

20、N0_PAGE;207. CAN0CN|=0x41;/ConfigurationChangeEnableCCEandINIT 208. CAN0ADR=BITREG;/PointtoBitTimingregister 209. CAN0DAT=0x2640;/seeabove 210. CAN0ADR=IF1CMDMSK;/PointtoCommandMask1 211. CAN0DAT=0x0087;/ConfigforTX:WRITEtoCANRAM,writedatabytes, 212. /setTXrqst/NewDat,clrIntPnd 213. /RX-IF2operation

21、mayinterruptTX-IF1operation 214. CAN0ADR=IF2CMDMSK;/PointtoCommandMask2 215. CAN0DATL=0x1F;/ConfigforRX:READCANRAM,readdatabytes, 216. /clrNewDatandIntPnd 217. CAN0CN|=0x06;/GlobalInt.EnableIEandSIE 218. CAN0CN&=0x41;/ClearCCEandINITbits,startsCANstatemachine 219. /TransmitCANframetoturnothernodesLE

22、DON 220. voidtransmit_turn_LED_ON(charMsgNum)221. SFRPAGE=CAN0_PAGE;/IF1alreadysetupforTX 222. CAN0ADR=IF1CMDMSK;/PointtoCommandMask1 223. CAN0DAT=0x0087;/ConfigtoWRITEtoCANRAM,writedatabytes, 224. /setTXrqst/NewDat,ClrIntPnd 225. CAN0ADR=IF1DATA1;/Pointto1stbyteofDataField 226. CAN0DATL=0x11;/Oness

23、ignalstoturnLEDslightONindataA1field 227. CAN0ADR=IF1CMDRQST;/PointtoCommandRequestReg. 228. CAN0DATL=MsgNum;/MovenewdataforTXtoMsgObjMsgNum 229. /TransmitCANFrametoturnothernodesLEDOFF 230. voidtransmit_turn_LED_OFF(charMsgNum)231. SFRPAGE=CAN0_PAGE;/IF1alreadysetupforTX 232. CAN0ADR=IF1DATA1;/Poin

24、tto1stbyteofDataField 233. CAN0DATL=0x00;/ZerosignalstoturnLEDslightONinDataA1field 234. CAN0ADR=IF1CMDRQST;/PointtoCommandRequestReg. 235. CAN0DATL=MsgNum;/MovenewdataforTXtoMsgObjMsgNum 236. /ReceiveDatafromtheIF2buffer 237. voidreceive_data(charMsgNum)238. charvirtual_button;239. SFRPAGE=CAN0_PAG

25、E;/IF1alreadysetupforRX 240. CAN0ADR=IF2CMDRQST;/PointtoCommandRequestReg. 241. CAN0DATL=MsgNum;/MovenewdataforRXfromMsgObjMsgNum 242. /Movenewdatatoa 243. CAN0ADR=IF2DATA1;/Pointto1stbyteofDataField 244. virtual_button=CAN0DATL;245. if(virtual_button=0x11)/OnesissignalfromothernodetoturnLEDON 246.

26、LED=1;247. elseLED=0;/OtherwiseturnLEDOFF(messagewasones) 248. /InterruptServiceRoutine 249. voidISRname(void)interrupt19250. status=CAN0STA;251. if(status&0x10)!=0)252. /RxOkisset,interruptcausedbyreception 253. CAN0STA=(CAN0STA&0xEF)|0x07;/ResetRxOk,setLECtoNoChange 254. /*readmessagenumberfromCANINTREG*/255. receive_d

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 单元课程

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

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁