您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > Canopen-C代码
F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\main.c12//----------------------------------------------------------------------------34#includep18c658.h5#includedelays.h67//CANopenrelatedincludes8#includedef.h9#includeinit.h10#includecan18xx8.h11#includepdo.h12#includeobjdict.h13#includeobjacces.h14#includelifegrd.h15#includetimer.h16#includenmt.h1718#includeapp.h1920//becauseofthebug-workaround21#includegeneral.h22#includecanop.h2324//----------------------------------------------------------------------------2526//----------------------------------------------------------------------------27//Defines28//----------------------------------------------------------------------------293031//----------------------------------------------------------------------------32//FunktionsPrototypen33//----------------------------------------------------------------------------34voidSetupCanbus(void);35voidmain(void);363738394041//----------------------------------------------------------------------------42//SetupCanbus(void)43//44//----------------------------------------------------------------------------45voidSetupCanbus(void)46{47BYTEi;4849sInitParameterparam;5051//initializecanopendevice52param.bNodeID=PORTC&(BYTE)0x7F;//getsthenode-idfromthedip-switch53param.wBaudRateIndex=b125k;54param.wProcSpeed=(WORD)FOSC;55initConnection(¶m);5657//!!!!!!!shouldbechanged!58//dirtyhack:canMessageRxBuffershouldbeinitializedbyvalues...but59//icouln'tmanagetogetitwork.sodoithere.60for(i=0;iRX_CAN_BUFFER_SIZE;i++)61{62canMessageRxBuffer[i].valid=FALSE;63}64}65666768//----------------------------------------------------------------------------69//Mainroutine70//----------------------------------------------------------------------------71voidmain(void)72{73//startstatemachine74bStateMachineState=INITIALISATION_COMMUNICATION;7576while(1)77{78switch(bStateMachineState)79{80caseINITIALISATION_COMMUNICATION:81//initcanbus82SetupCanbus();1F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\main.c83initTimer();84//changeautomaticallyintoinitialisationofthenode85bStateMachineState=INITIALISATION_NODE;86break;87caseINITIALISATION_NODE:88//becausesendingofheartbeatmuststartjustafterbootup,letsdothat...89//thefirstmessagehastobetheboot-upmessage(withfirstdata-byte90//setto0,thereforewehavetosendthisheartbeatNOW.91initialisation();92//createthelistwithallheartbeat-entries...93lifeguardInit();94sendBootUp();95bStateMachineState=PRE_OPERATIONAL;96break;97casePRE_OPERATIONAL:98preOperational();99break;100caseOPERATIONAL:101operational();102break;103caseSTOPPED:104stopped();105break;106}107}108}1091101111121131142F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\timer.h1/***************************************************************************2timer.h-description3-------------------4begin:FriMay1720025copyright:(C)2002byRaphaelZulliger6email:zulli@hsr.ch7***************************************************************************/89/***************************************************************************10**11*ThislibraryisCopyright(c)RaphaelZulligerzulli@gmx.net.*12*ItislicensedundertheGNULibraryGeneralPublicLicense(LGPL).*13**14***************************************************************************/151617/***************************************************************************18**19**20***************************************************************************/2122#ifndef__timer_h__23#define__timer_h__2425#includegeneral.h2627/**Initialisesthetimer.Thisincludessettingupthetimer-interrupt,28*calculatingthetimer-interruptregisters,soeverymillisecondan29*interruptoccures.30*/31voidinitTimer(void);3233/**Resetsthetimer:IntheISRofthetimerinterruptwehavetoset34*thetimerinterruptregisterstothecorrectvalue,sothenextinterrupt35*occuresafteranothermillisecond.Thisfunctionsetsthisregisters36*tothecorrectvalues(calculatedbyinitTimer)37*/38voidresetTimer(void);3940/**Thisfunctionreturnsthecorrectvalueoftheactualtime.Ifthetime41*wouldbereadwithoutthisfunction,itspossibletogetthewrongtime,42*becausetheinterruptserviceroutinecouldbeexecutedduringreading43*thisvalue,andtheLSBofthetimevalueisincremented,soanoverflow44*oftheLSBoccrues,andtheprogramreadthewrongvalue.\n45*Thisfunctioncallsitself(recursive)ifitseamsthattheISRwascalled46*duringreadingthetime.47*\paramtimeThepointertothevariablethatspointstothetimethat48*shouldbeusedintheprogram49*/50WORDgetTime(WORD*time);5152/**Thisfunctionsetsthecorrectvalueoftheactualtime.Ifthetime53*wouldbewrittenwithoutthisfunction,itspossibletosetthewrongtime,54*becausetheinterruptserviceroutinecouldbeexecutedduringwriting55*thisvalue.\n56*Thisfunctioncallsitself(recursive)ifitseamsthattheISRwascalled57*duringsettingthetime.58*\paramtimeThepointertothevariablethatspointstothetimethat59*shouldbesetintheprogram60*\paramvalueThetimethatshouldbeset.61*/62voidsetTime(WORD*time,WORDvalue);6364#endif//#define__timer_h__651F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\
本文标题:Canopen-C代码
链接地址:https://www.777doc.com/doc-6311938 .html