CAN总线硬件连接及程序.doc

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

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

1、-作者xxxx-日期xxxxCAN总线硬件连接及程序【精品文档】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. /a

2、ndfromCANRAMmessageobjects.EachC8051F040-TBCANnodeisconfigured 15. /tosendamessagewhenitsP3.7buttonisdepressed/released,witha0x11 16. /toindicatethebuttonispushed,and0x00whenreleased.Eachnodealsohas 17. /amessageobjectconfiguredtoreceivemessages.TheC8051teststhe 18. /receiveddataandwillturnon/offthe

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

4、SFRdeclarations 30. 31. /CANProtocolRegisterIndexforCAN0ADR,fromTABLE18.1oftheC8051F040 32. /datasheet 33. / 34. #defineCANCTRL0x00/ControlRegister 35. #defineCANSTAT0x01/Statusregister 36. #defineERRCNT0x02/ErrorCounterRegister 37. #defineBITREG0x03/BitTimingRegister 38. #defineINTREG0x04/Interrupt

5、LowByteRegister 39. #defineCANTSTR0x05/Testregister 40. #defineBRPEXT0x06/BRPExtensionRegister 41. / 42. /IF1InterfaceRegisters 43. / 44. #defineIF1CMDRQST0x08/IF1CommandRestRegister 45. #defineIF1CMDMSK0x09/IF1CommandMaskRegister 46. #defineIF1MSK10x0A/IF1Mask1Register 47. #defineIF1MSK20x0B/IF1Mas

6、k2Register 48. #defineIF1ARB10x0C/IF1Arbitration1Register 49. #defineIF1ARB20x0D/IF1Arbitration2Register 50. #defineIF1MSGC0x0E/IF1MessageControlRegister 51. #defineIF1DATA10x0F/IF1DataA1Register 52. #defineIF1DATA20x10/IF1DataA2Register 53. #defineIF1DATB10x11/IF1DataB1Register 54. #defineIF1DATB20

7、x12/IF1DataB2Register 55. / 56. /IF2InterfaceRegisters 57. / 58. #defineIF2CMDRQST0x20/IF2CommandRestRegister 59. #defineIF2CMDMSK0x21/IF2CommandMaskRegister 60. #defineIF2MSK10x22/IF2Mask1Register 61. #defineIF2MSK20x23/IF2Mask2Register 62. #defineIF2ARB10x24/IF2Arbitration1Register 63. #defineIF2A

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

9、eTRANSREQ10x40/TransmissionRest1Register 73. #defineTRANSREQ20x41/TransmissionRest2Register 74. 75. #defineNEWDAT10x48/NewData1Register 76. #defineNEWDAT20x49/NewData2Register 77. 78. #defineINTPEND10x50/InterruptPending1Register 79. #defineINTPEND20x51/InterruptPending2Register 80. 81. #defineMSGVA

10、L10x58/MessageValid1Register 82. #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.

11、 sbitLED=P16;99. sfr16CAN0DAT=0xD8;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(charMsgN

12、um);113. voidtransmit_turn_LED_OFF(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. /disablewatc

13、hdogtimer 128. WDTCN=0xde;129. WDTCN=0xad;130. 131. /configurePortI/O 132. config_IO();133. 134. /switchtoexternaloscillator 135. external_osc();136. 137. 138. / 139. /ConfigureCANcommunications 140. / 141. /IF1usedforprocedurescallesbymainprogram 142. /IF2usedforinterruptserviceprocedurereceive_dat

14、a 143. / 144. /MessageObjectassignments: 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

15、. init_msg_object_RX(0x01);157. 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(0x

16、02);173. else174. while(BUTTON=1)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

17、. OSCXCN=0x77;/startexternaloscillator;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;/Por

18、tSFRsonConfigurationpage 199. XBR3=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. 2

19、12. SFRPAGE=CAN0_PAGE;213. CAN0ADR=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=2tq28

20、0. 281. TSEG1=(Prop_Seg+Phase_Seg1-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.7246%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

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

当前位置:首页 > 教育专区 > 高考资料

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

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