logo资料库

Canopen的C语言实现.pdf

第1页 / 共93页
第2页 / 共93页
第3页 / 共93页
第4页 / 共93页
第5页 / 共93页
第6页 / 共93页
第7页 / 共93页
第8页 / 共93页
资料共93页,剩余部分请下载后查看
MAIN
Timer.h
Timer.c
Sync.h
Sync.c
Sdos.h
sdos.c
sdoc.h
sdoc.c
pdo.h
pdo.c
objdict.h
objdict.c
Objacces.h
Objacces.c
Nmt.h
Nmt.c
Lifegrd.h
Lifegrd.c
Init.h
Init.c
General.h
General.c
Fullcan.h
Fullcan.c
Emgncy.h
Emgncy.c
Def.h
Canop.h
Canop.c
can18xx8.h
can18xx8.c
Basiccan.h
Basiccan.c
Arbracan.h
App.h
App.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\main.c //---------------------------------------------------------------------------- #include #include // CANopen related includes #include #include #include #include #include #include #include #include #include #include // because of the bug-workaround #include #include //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- // Defines //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- // Funktions Prototypen //---------------------------------------------------------------------------- void SetupCanbus(void); void main(void); //---------------------------------------------------------------------------- // SetupCanbus(void) // //---------------------------------------------------------------------------- void SetupCanbus(void) { BYTE i; sInitParameter param; // initialize canopen device param.bNodeID = PORTC & (BYTE)0x7F; // gets the node-id from the dip-switch param.wBaudRateIndex = b125k; param.wProcSpeed = (WORD)FOSC; initConnection( ¶m ); // !!!!!!! should be changed! // dirty hack: canMessageRxBuffer should be initialized by values... but // i couln't manage to get it work. so do it here. for( i=0; i
F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\main.c case INITIALISATION_NODE: initTimer( ); // change automatically into initialisation of the node bStateMachineState = INITIALISATION_NODE; break; // because sending of heartbeat must start just after bootup, lets do that... // the first message has to be the boot-up message (with first data-byte // set to 0, therefore we have to send this heartbeat NOW. initialisation( ); // create the list with all heartbeat-entries... lifeguardInit( ); sendBootUp( ); bStateMachineState = PRE_OPERATIONAL; break; preOperational( ); break; operational( ); break; case STOPPED: stopped( ); break; case PRE_OPERATIONAL: case OPERATIONAL: 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 } } } 2
F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\timer.h 1 /*************************************************************************** 2 timer.h - description 3 ------------------- 4 begin : Fri May 17 2002 5 copyright : (C) 2002 by Raphael Zulliger 6 email : zulli@hsr.ch 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This library is Copyright (c) Raphael Zulliger . * 12 * It is licensed under the GNU Library General Public License (LGPL). * 13 * * 14 ***************************************************************************/ 15 16 17 /*************************************************************************** 18 * * 19 * * 20 ***************************************************************************/ 21 22 #ifndef __timer_h__ 23 #define __timer_h__ 24 25 #include 26 27 /** Initialises the timer. This includes setting up the timer-interrupt, 28 * calculating the timer-interrupt registers, so every millisecond an 29 * interrupt occures. 30 */ 31 void initTimer( void ); 32 33 /** Resets the timer: In the ISR of the timer interrupt we have to set 34 * the timer interrupt registers to the correct value, so the next interrupt 35 * occures after another millisecond. This function sets this registers 36 * to the correct values (calculated by initTimer) 37 */ 38 void resetTimer( void ); 39 40 /** This function returns the correct value of the actual time. If the time 41 * would be read without this function, its possible to get the wrong time, 42 * because the interrupt service routine could be executed during reading 43 * this value, and the LSB of the time value is incremented, so an overflow 44 * of the LSB occrues, and the program read the wrong value.\n 45 * This function calls itself (recursive) if it seams that the ISR was called 46 * during reading the time. 47 * \param time The pointer to the variable thats points to the time that 48 * should be used in the program 49 */ 50 WORD getTime( WORD* time ); 51 52 /** This function sets the correct value of the actual time. If the time 53 * would be written without this function, its possible to set the wrong time, 54 * because the interrupt service routine could be executed during writing 55 * this value.\n 56 * This function calls itself (recursive) if it seams that the ISR was called 57 * during setting the time. 58 * \param time The pointer to the variable thats points to the time that 59 * should be set in the program 60 * \param value The time that should be set. 61 */ 62 void setTime( WORD* time, WORD value ); 63 64 #endif // #define __timer_h__ 65 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\timer.c /*************************************************************************** timer.c - description ------------------- begin : Fri May 17 2002 copyright : (C) 2002 by Raphael Zulliger email : zulli@hsr.ch ***************************************************************************/ /*************************************************************************** * * * This library is Copyright (c) Raphael Zulliger . * * It is licensed under the GNU Library General Public License (LGPL). * * * ***************************************************************************/ #include #include #include #include /* initialized the timer module. the timer-interrupt should occure after every * one millisecond (1ms). */ static BYTE timerLowRegister; static BYTE timerHighRegister; void initTimer( void ) { every fourth (FOsc/4) (external)clock cycle // Enables the Timer0 overflow interrupt // Setze Timer0 interrupt auf hohe Priorität // Enable priority levels on interrupts // Disables all peripheral interrupts // Enable all high priority interrupts // sets the timer clock, so it increments DWORD wCountOfOverflows; //Timer1 Settings: PIE1bits.TMR1IE = (BYTE)0x01; IPR1bits.TMR1IP = (BYTE)0x01; RCONbits.IPEN = (BYTE)0x01; INTCONbits.GIEL = (BYTE)0x00; INTCONbits.GIEH = (BYTE)0x01; T1CONbits.TMR1CS = (BYTE)0x00; T1CONbits.TMR1ON = (BYTE)0x01; // Setting the timer1, so every 1ms an interrupt (overflow) occures // therefore we have to calculate the correct start-value of the counter (timer) // register // theorie: timer increments every FOSC/4. -> count of increments until first // overflow: (1ms/4)*FOSC (e.g. with 4MHz: 1000) wCountOfOverflows = ((WORD)FOSC)/((BYTE)0x04); timerHighRegister = (BYTE)0xFF - (BYTE)( (wCountOfOverflows&(WORD)0xFF00 ) >> (BYTE)0x08 ); timerLowRegister = (BYTE)0xFF - (BYTE)( (wCountOfOverflows&(WORD)0x00FF ) ); // remeber: FOSC is in khz // sets the prescaler to 1:1 (wouldn't be neccessary because the value is 1:1 after reset T1CONbits.T1CKPS0 = (BYTE)0x00; T1CONbits.T1CKPS1 = (BYTE)0x00; // shut off timer1 oscillator T1CONbits.T1OSCEN = (BYTE)0x00; // acces of high- and low-byte by 8bit... (instead of 16bit) T1CONbits.RD16 = (BYTE)0x00; resetTimer( ); } void resetTimer( void ) { TMR1H = timerHighRegister; TMR1L = timerLowRegister; PIR1bits.TMR1IF = (BYTE)0x00; } WORD getTime( WORD* time ) { WORD wReturnValue; WORD wTestTime; wReturnValue = *time; 1 // clear interrupt flag for timer 1
F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\timer.c wTestTime = *time; if( wTestTime != wReturnValue ) { // maybe an overflow occured. call this function again (recursive) wReturnValue = getTime( time ); } } return wReturnValue; void setTime( WORD* time, WORD value ) { *time = value; if( *time != value ) { // it seams that the irq-serviceroutine was called during setting the value, so try again } } setTime( time, value ); 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 2
F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\sync.h 1 /*************************************************************************** 2 sync.h - description 3 ------------------- 4 begin : Fri May 17 2002 5 copyright : (C) 2002 by Raphael Zulliger 6 email : zulli@hsr.ch 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This library is Copyright (c) Raphael Zulliger . * 12 * It is licensed under the GNU Library General Public License (LGPL). * 13 * * 14 ***************************************************************************/ 15 16 /** \file 17 * \brief Functions for sending/receiving sync-messages 18 * 19 * the functions in this file are responsible for sync-related stuff 20 * \warning Not implemented yet. 21 */ 22 23 #ifndef __sync_h__ 24 #define __sync_h__ 25 26 #include 27 28 /** Sends a CANopen sync message. Not supported yet. 29 */ 30 BYTE sendSync(void); 31 32 #endif // __sync_h__ 33 1
F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\sync.c 1 /*************************************************************************** 2 sync.c - description 3 ------------------- 4 begin : Fri May 17 2002 5 copyright : (C) 2002 by Raphael Zulliger 6 email : zulli@hsr.ch 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This library is Copyright (c) Raphael Zulliger . * 12 * It is licensed under the GNU Library General Public License (LGPL). * 13 * * 14 ***************************************************************************/ 15 16 17 #include 18 19 BYTE sendSync( void ) 20 { 21 return 0; 22 } 23 1
F:\Datasheets\Microchip\Applications\CAN_BUS\CAN_OPEN\sdos.h 1 /*************************************************************************** 2 sdos.h - description 3 ------------------- 4 begin : Fri May 17 2002 5 copyright : (C) 2002 by Raphael Zulliger 6 email : zulli@hsr.ch 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This library is Copyright (c) Raphael Zulliger . * 12 * It is licensed under the GNU Library General Public License (LGPL). * 13 * * 14 ***************************************************************************/ 15 16 17 /** \file 18 * \brief Functions for accessing object dictionaries on other nodes 19 * 20 * This functions enables you to access the object dictionary of an 21 * other module. (sdo server). 22 * No such functinality is implemented yet. If you use it, programm it! 23 * \warning Not implemented yet. 24 */ 25 26 27 #ifndef __sdos_h__ 28 #define __sdos_h__ 29 30 31 #endif // __sdos_h__ 32 1
分享到:
收藏