I摘要基于STM32的CAN总线通信设计分散式工业控制系统作为工业控制领域里最具活力的分支,依靠着计算机软硬件技术和集成电路技术的进步,在工业控制领域取得了迅速的发展。控制的多元化,系统的分散化都对系统的可靠性和灵活性提出了更高的要求。典型的分散式控制系统的组成部分包括:现场设备,接口与计算设备以及通信设备。无论是工业自动化领域,还是医疗仪器,交通建筑领域,对工业控制系统的通信的要求都也越来越高。控制器局域网(CAN—ControllerAreaNetwork)是由BOSCH公司率先推出的一种多主机局域网,它成功的满足了系统对灵活性和可靠性的高要求,迅速成为公认的最具前途的现场总线之一。起初,汽车中多个控制器和测试仪器间的数据交换越来越复杂,引发了对一种多主机数据总线的需求,该总线要能很好的满足控制器间数据交换的要求,让每个节点都可以作为主机通信,CAN总线技术因此发展起来。CAN总线的通信介质可以是双绞线、同轴电缆或光导纤维[4]。CAN总线技术摒弃了传统的站地址编码,通信速率可达1Mb/s。通过对数据块的编码使网络内节点数理论上不受限。CAN协议所具有的CRC检验功能保证了数据通信的可靠性。数据段长度最多为8个字节,在保证了控制命令等长度要求又不占用过长的总线时间,系统通信的实时性能提高。CAN协议所具有的位仲裁机制可以保证实现总线分配时,避免两个站在同时发送数据时所形成的冲突碰撞。由于CAN总线协议的高可靠性和灵活性,目前基于各种控制芯片下的CAN通信系统层出不穷,这些开发出来的系统被广泛应用于检测,监视,记录各种信号,如汽车通信系统中,温度湿度检测系统等等,都在实际应用中取得了稳定可靠的效果。本文介绍了基于STM32的CAN总线通信设计,在众多32位微处理器中,STM32系列是基于专为嵌入式应用而设计的ARMCortex-M3内核,有着性能出众,外设丰富的优越性能,被广泛应用于工业及消费类电子产品生产中。使用STM32作为主控芯片构建高性能的CAN总线通信系统,将更有利于满足生产实践中的高可靠性要求。本文对CAN总线技术做了比较详细的介绍,对基于STM32控制下的CAN通信进行了设计与分析,对每一步工作进行了记录和总结,本文的主要工作有以下内容:一、CAN总线技术介绍本文的第一步工作就是对CAN总线技术进行全面细致的研究和学习。对CAN总线II技术的发展和特点做了总结。详细介绍了CAN协议的层结构,报文格式,错误和优先级仲裁等概念。二、CAN通信的总体设计提出系统的设计要求,构建基于STM32的CAN通信设计的基本框架和实现功能,包括系统的结构框架,硬件设计以及软件设计部分。系统硬件设计部分包括:STM32主控芯片的性能和各个CAN通信模块功能介绍,系统硬件结构设计等。系统软件设计部分包括:系统程序开发环境介绍,系统初始化过程,主程序设计流程,CAN模块初始化等。三、调试与总结对STM32控制器的调试,主要进行了以下几点的调试:系统的初始化和消息的接收和发送等。调试确保了控制器节点间能够正常通信。调试和实验后,基于STM32的CAN总线通信工作基本正常,运行结果符合最初设计目标和预期效果,这个系统设计实现的完成作为CAN总线技术在工业控制领域成功应用的很好实例,同时也为CAN总线协议在实际应用领域增添了依据。关键词:CAN总线通信、STM32、MCP2515、嵌入式系统、现场总线IIIAbstractTheDesignofCANBusCommunicationbasedonSTM32Distributedindustrialcontrolsystemisoneofthemostdynamicpartinthefieldofindustrialcontrol.Dependingonthefastdevelopedtechnologyofcomputerhardwareandsoftwareandprogressofintegratedcircuittechnique,Distributedindustrialcontrolsystemhasachievedrapiddevelopmentinthefieldofindustrialcontrolarea.Thediversityofcontrolanddiversificationofsystemenhancethedemandonsystemreliabilityandflexibility.Atypicaldistributedcontrolsystemincludesfielddevices,interface,computingequipments,andcommunicationsequipments.Communicationdemandofindustrialcontrolsystemisbecomingmoreandmoreimportant,nomatterinindustrialautomationfield,ormedicalinstrumentsandTransportationconstructionfield.CAN—ControllerAreaNetworkisamulti-masternetworkwhichcreatedbyBOSCH.Causeitsatisfiedhighrequirementsofthesystemontheflexibilityandreliability,itquicklybecomesthemostpromisingbusinpublic.FirstCANwascreatedtosolvethedataexchangeproblembetweencontrollersandtestinstruments.CANhasmanymasters,providesthepossibilitythateachnodecommunicatewithothersasahost.Communicationmediumcanbetwistedpair,coaxialcableandopticalfiber.ThebiggestcharacteristicoftheCANprotocolisencodingcommunicationdatablock,abolishingthetraditionalstationaddresscoding,itscommunicationratecanbeupto1MB/s.Sothatwecanmakethenetworknodenumbernotlimitedtheoretically.CRCcheckfunctionofCANensuresthereliabilityofdatacommunication.Thelengthofdataisuptoeightdatabytes.Whileguaranteedthelengthofcontrolcommand,italsoensuresimprovedthereal-timecommunicationandwon'ttaketoolongbustimeatthesametime.ThearbitrationmechanismofCANprotocolguaranteestoavoidclashwhentwounitssendmassagesatthesametimewhenallotsthebus.DuetothehighreliabilityandflexibilityofCANbusprotocol,avarietyofcontrolchipsofCANcommunicationsystememergeinendlessly.Thedevelopedsystemsarewidelyusedindetection,monitoring,recordingallkindsofsignals.Suchasautomobilecommunicationsystem,temperaturehumiditytestsystem,etc.Theirstableandreliableresultshavebeenachievedinpractice.ThispaperintroducesthedesignofCANbuscommunicationsystembasedonSTM32.Inthenumerous32-bitmicroprocessors,STM32IVseriesisspeciallydesignedforembeddedapplicationsbasedonARMCortex-M3withtheperformance,ofrichsuperiorperipheralandwidelyusedinindustrialandconsumerelectronicsproducts.UsingSTM32asmaincontrolchiptobuildhigh-performance,CANbuscommunicationsystemwillbemoreconducivetomeettherequirementsofhighreliabilityintheproductionpractice.Inthispaper,theCANbustechnologyisintroducedindetail,CANbuscommunicationsystembasedonSTM32isdesignedandanalysised.Recordingandsummarizingtheworkofeachstep,inthispaper,themainworkhasthefollowingcontents:1.IntroducetheCANbustechnologyThefirststepofthisarticleistoworkontheCANbustechnologytoconductacomprehensiveandmeticulousresearchandlearningandmadeasummaryofthecharacteristicsofCANbustechnologydevelopment.IntroducedthelayerstructureoftheCANprotocol,packetformat,errorandpriorityarbitration.2.ThedesignofCANcommunicationsystemPutforwardthedesignrequirementsofthesystem.TobuildthebasicframeworkofCANcommunicationsystembasedonSTM32,includingthesystemstructure,hardwaredesignandsoftwaredesignpart.Thesystemhardwaredesignpartincludes:STM32mastercontrolchipperformanceandtheintroductionofCANcommunicationmodulefunctions.Systemsoftwaredesignincludes:Systemapplicationdevelopmentenvironment,thesysteminitializationprocess,themainprogramdesignprocess,theCANmoduleinitializationandsoon.3.DebuggingandsummaryDebuggingofmastercontrollerSTM32,mainlyincludesthemaincontrollernodeinitializationandmessagereceivingandsending.Debuggingthesystem,mainlyinordert