《单片机硬件CAN通信的应用.docx》由会员分享,可在线阅读,更多相关《单片机硬件CAN通信的应用.docx(16页珍藏版)》请在三一办公上搜索。
1、CAN的应用非常广泛,现在很多单片机都内部集成了CAN控制器.今天结合瑞萨RA2单片机的CAN控制器模块,给大家描述一下其应用案例。瑞萨RA21.1系列MCU筒介RA21.1产品组基于Arm*Corte-M23核心(现今Arm-Cortex-M系列中功耗最低的CPU).这款产品采用优化的制程和瑞萨电子的低功耗工艺技术,是业界一流水平的超低功耗微控制器.RA21.1产品组能够支持1.6V至5.SV宽电压工作,CPU时钟频率最高48MHz,且运行模式电澹和待机模式电流更低.RA21.1产品群配备了增强型电容式触推感应单元(CTSU2)、CAN控制器局域网总线,串行通信接口、高精度模拟电路和定时器。
2、产品封装从48引脚到100弓I脚.RA21.148MHz32-BitArmCortex-M23CoreHMemoryCodeF1.ash(256kB.128kB)SRAM(16k)PantySRAM(16kB)ECCDataF1.ash(8kB)11Ana1.ogueCJTimers12bitADC(19ch)12OitDAC(Ich)1.owPowerAna1.ogComparator(2c)GPT32-bWDTTempecaturGSensor(CommunicationCANx1I2C2SCIx5SPIx2SystemSafety控制叁同域网CAN模块而介SysTickDTCMu1.ti
3、p1.eC1.ocksOnCpOsaRatorHOCO(24.32.48.64MHz).1.OCO(32kHz).I1.OCO(15kKz)1.owPoworModesE1.CPortFunctionSe1.ectRTCMonxxyProtectionISRAMParityChoECCinSRAMPTC1.OCkFrequencyAccuracyMeasurerrCRCCa1.cu1.atorIWDTDataOperatKXiCircF1.ashAreaProtectiADCSeifTest控制器局域网(CAN)模块使用基于消息的协议在电磁暝声应用中的多个从机和主机之间接收和传输数据.该模块符
4、合ISOI1.898-1(CAN2.0ACAN20B)标准,最多支持32个邮箱,可配置为普通邮箱和FIFO模式下的发送或接收。支持标准(11位)和扩展(29位)消息格式.CAN模块需要额外的外部CAN收发器.注意本项:CAN需要外部速晶体作为时钟源,使用CAN模块前需要先配置好外部高速时钟.CAN模块参数规格TU27.1CANBfMCifksbont(1o2ParamMfSm*1inOetararvttrata1.SO118fi1crraftIcr*i*Cte*dIVX1.at1.cMWrrmB13DM*w*k*11mFrEWM岬SMWECAN.8MKrIfCI*CANCMtxrcUeMQM皿
5、EqaI)”,wqmo0w Ncrniaimode:32mt&mM9eMrcen1.9Mbtofar4hc*mMMerroec4eft 0w24mMbcmncw04fWrw119amb*xtfwrIrVHrMIPQMrtvoegton.athrrrw119*mout*31o*omtM0abfK.or*ywxMdID,crmecCH*;,.I!.,. StoctaMtMtoR0MrMeM(urMdmMgMRMttM)andovnunmotmMEM9MMd) Rocco0rterperjor,CCHabIoro(11MUJA4CMiMKffVMUt*MU1cmfarwyXMMt1.OWm)M3OS
6、Mdb*5BIOICfmeeytoor*yr1.ndanSC.0r*7anddID.ormadO P10wvvnMA4trvnMonVm ftv*da1.f11MgrnctoA MoaodBOf1.KiIibMd4tmMK)oraaKwnuntreryfromIMbuk1tm9*Wtacn0wM AatemMcvcqe(CANMmodeenbuMf1.eMry Awnacr*c*gofCANMrtmocn kVMMfQoCANImAmodeMughsdtam TrwwBcnto11ordMM*ghtvWfWmEgavf*oMtriCw-XioQut*11yImo-XACXexrzCC-.C*T
7、0EACKcMrMteremx 0ttMownnfrorpMM.MrgTb.Urrp1.xcboouwq1.bcto1.e4.2ert.nMWvpio(HHM11ct*jnceoSuaofttodrtamtwuctRCfCCOccwdkpo*MVJMQf1.Thf1.MB0*MfU01IreS*Xf4*O*Wp9CfOrc*JO*OMOMkWMCAMUOKtfrvc IMeMWrnO4 S*trtWknpb) rcd11.yamicceckRA21.1CAN通信应用例程使用官方右studi。开发工具创建RA21.1工程,并添加CAN外设模块底层应用.9JW1V031R.BDr.SCAN-GMM
8、tiM5*I0MAUCww*SKMjCewcnq.opotgt.wo.c*3Hy1.g.CQC。OoOM6C配2SCAN模块参数(包括通信速率、引脚死因等)。9-CAN5)arr*1cvrq.fCAMQ.4VCxrwrJNrv*CWwdC*xkSoweXIEOwtdOrrwHeWcdeCMTWXrXwMK*3x*oMMVAUo9me*WdWtr*j4rf4eWtB,*X*KecmHfONwbO03(mmMU4r(WMMx)SH(MCUPwbo1215OoufMrj16HOoupMtW2JOsJPM9Z42GfourMrfbo51OpurCAN应用叁考代码/wait#defineWAIT_TIM
9、E(500U)timeva1.ue/mai1.#defineCAN_MAI1.BOX_NUMBER_TX(OU)boxnumber/CANtransmission/CANreceivestatus/CANerrorstatus/CANtransmit/CANreceiveWdefineCAN_MAI1.BOX_NUMBER_RX(IU)#defineCAN_FRAME_TRANSM1.1.DATA_BYTES(8U)/data1.engthSdefineZERO(OU)*Privateg1.oba1.variab1.es*/*F1.ags,setfromCa1.1.backfunction*/
10、staticvo1.ati1.eb1.b_can_tx=fa1.se;statusstaticvo1.ati1.eboo1.b_can_rx=fa1.se;staticvo1.ati1.eboo1.b_can_err=fa1.se;*CANframesfortxandrx*/staticcan_frame_tg_can_tx_frame;framestaticcan_frame_tg_can_rx_frame;framevoidha1.-entry(void)*TODO:addyourowncodehere/fsp_err_terr=FSP,SUCCESS;uint32ttimeout/tim
11、eoutuint8jcan_tx_msgCAN_FRAME_TRANSMIT_DATA_BYTES0,1,2,3,4,5,6,7;uint8jcan_rx_msgCAN_FRAME_TRANSMIT_DATA_BYTES=0;*Initia1.izestheCGCmodu1.e.*/err=R_CGC_Open(&g_cgcO_ctr1.,&g_cgcO_cfg);*Hand1.eanyerrors.Thisfunctionshou1.dbedefinedbytheuser./assert(FSP_SUCCESS=err);*StarttheCGJC1.oCK_MA1.NJDSC.7err=R
12、_CGC_C1.ockStart(&g_cgcO_ctr1.,CGC_C1.OCK_MAIN_OSC,NU1.1.);assert(FSP_SUCCESS=err);*Initia1.izeCANmodu1.e*/err=R_CAN_Open(&g_can_ctr1.,&g_can_cfg);*Errortrap*/if(FSP_SUCCESS!=err)g_can_tx_frame.id=CAN_MAI1.80X_NUMBER_TX;g_can_tx_frame.type=CAN_FRAME_TYPE_DATA;g_can_tx_frame.data_1.ength_code=CAN_FRA
13、ME_TRANSMIT_DATA_BYTES;*copythetxdataframewithTX_MSG/memcpy(uint8_t,)&g_can_tx_frame.dataZERO,(uint8_t*)&can_tx_msg(ZERO,CAN_FRAME_TRANSMIT_DATA_BYTES);err=R_CAN_Write(&g_can_ctr1.,CAN_MAI1.BOX_NUMBER_TX,&g_can_tx_frame);*Errortrap*/if(FSP_SUCCESS!=err)(err=R_CAN_C1.ose(&g_can_ctr1.);if(FSP_SUCCESS!
14、=err)(_asm(BKPTW0n);whi1.e(1.)*checkifreceivef1.agisset*/if(true=b_can_rx)(*Resetf1.agbit*/b_can_rx=fa1.se;g_can_rx_frame.data(CAN_FRAME_TRANSMIT_DATA_BYTES-1.=g_can_rx_frame.id;*Transmittherxdataframeasacknow1.edgingthedatatransferissuccessfu1.*/err=R_CAN_Write&g_can_ctr1.,CAN_MAI1.BOX_NUMBER_TX,&g
15、_can_rx_frame);*Errortrap*/if(FSP_SUCCESS!=err)(err=R_CAN_C1.ose(&g_can_ctr1.);if(FSP_SUCCESS!=err)*waitfortransmitf1.agbittoset*/whi1.e(true!=b_can_tx)&(time_out-);if(0=time-out)(_asm(BKPT0n);*Resetf1.agbit*/b_can_tx=fa1.se;voidcan_ca1.1.backcan_ca1.1.back_args_t*p_arg$)(switch(p-args-event)(caseCA
16、N_EVENT_TX_COMP1.ETE:(/setf1.agbitb_can_tx=true;break;caseCAN_EVENT_RX_COMP1.ETE:b_can_rx=true;/copythereceiveddatatorx-framememcpy(&g_can_rx_frame,p_args-p_frame,sizeofcan_frame_t);break;caseCAN_EVENT_MAI1.BOX_MESSAGE_1.OST:/overwrite/overrunerroreventcaseCAN_EVENT_8US_RECOVERY:eventcaseCAN_EVENT_E
17、RR_8US_0FF:caseCAN_EVENT_ERR_PASSIVE:caseCAN_EVENT_ERR_WARNING:event(b_can_err=true;/Busrecoveryerror/errorBusOffevent/errorpassiveevent/errorwarning/setf1.agbitbreak;CAN通信实测殴证RA21.1芯片内部只有CAN控制器,需要与外部CAN设备通信时,还需要外接CAN收发器。CAN收发器位:有Iffi理IS使用USB-CAN工具测试CAN通信Q1.HANToo1.*.11UMCANa21W*OJOM9.*:VJJS01.9Mtf-
18、QXMMtCiM三*SMeSgOTWS9)X,MU3三三MIXttwSS?nVMt11*i3000O1.MM000MMCgwcxeUnwdCB3”KHMAiMt1.MV?g*YftStMI0nnCiMV,它今9WStN1.011三esOmme6:*tmottoWt1.i1.”。3M149nmQvIICWc114M.1由1420B4CtaUim3“29eMB无k1.1420)K4Q*1MU4114202X?JtA1.14mCkiaiM3141无XK4a14a4MBJtU1.14ZD44M019At1.CMIy无142DMC0b1X4KA1.O488888BB88B388:BSSBSBBBSS88
19、83888:38S88888BB88B88MB88B88BB3BB881.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.*a9三=三=2三三三三EM=黑黑蓼RZRiVeHFovMai1.box0-3GroupMai1.boxIDMai1.boxIDMode7Mai1.boxTypeMai1.box0TypeMai1.box1TypeMai1.box2TypeMai1.box3TypeMai1.boxFrameTypeMai1.box0-3GroupMaskTransmitMai1.boxReceiveMai1.bOXReceiveMai1.boxReceiveMai1.boxOxifffffffb.禁止MaSk功能后,可接收所有ID的数据.gcanCAN(rcan)SettingsAPIInfonReceiveFIFOvMai1.box0-3Group Mai1.boxID Mai1.boxIDModevMai1.boxTypeMaiIboxOTypeMai1.box1TypeMai1.box2TypeMai1.box3Type Mai1.boxFrameTypeMai1.box03GroupMask、Mai1.hn*4-7GrminTransmitMai1.boxReceiveMai1.boxReceiveMai1.boxReceiveMai1.boxOxO