CAN总线硬件连接及程序(7页).doc
《CAN总线硬件连接及程序(7页).doc》由会员分享,可在线阅读,更多相关《CAN总线硬件连接及程序(7页).doc(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CAN 总线 硬件 连接 程序
限制150内