www.pudn.com > MicroCANOPEN.rar > main.c, change:2003-05-28,size:3513b


/************************************************************************** 
MODULE:    MAIN - Encoder 
CONTAINS:  CANopen encoder implementation 
COPYRIGHT: Embedded Systems Academy, Inc. 2003. 
           All rights reserved. www.microcanopen.com 
           This software was written in accordance to the guidelines at 
		   www.esacademy.com/software/softwarestyleguide.pdf 
DISCLAIM:  Read and understand our disclaimer before using this code! 
           www.esacademy.com/disclaim.htm 
LICENSE:   Users that have purchased a license for PCANopenMagic 
           (www.esacademy.com/software/pcanopenmagic) 
           may use this code in commercial projects. 
           Otherwise only educational use is acceptable. 
VERSION:   1.00, Pf/Aa/Ck 28-MAY-03 
--------------------------------------------------------------------------- 
HISTORY:   1.00, Pf 07-OCT-02, First Published Version 
***************************************************************************/  
 
#include "mco.h" 
#include "mcohw.h" 
#include <Reg51cc01.h> 
 
 
// IO functions, buttons and LEDs are implemented in module io.c 
extern void IO_UpdateLEDs (void); 
 
#ifdef USE_LED 
// CAN Run and Err LED 
extern BYTE data gRLED; // Current pattern on run led 
extern BYTE data gELED; // Current pattern on error led 
#endif 
 
// Process Data to CANopen stack 
long data Position = 0; // Goes into TPDO1 
 
// Process Data, application copy 
// These are updated in io.c by the timer interrupt service routine 
WORD data ProcZ; 
// ProcX and ProcY not used in this example... 
 
 
// MicroCANopen Call-back function for fatal error 
void MCOUSER_FatalError (WORD ErrCode) 
{ 
  gELED = LED_BLINK; 
  gRLED = LED_OFF; 
  while (ErrCode != 0) 
  { 
  } 
} 
 
 
// MicroCANopen Call-back function to reset application 
void MCOUSER_ResetApplication (void) 
{ 
  EA = 0; // Disable all interrupts 
  WDTPRG = 0; // Minimize timer count for fastest response (about 10ms) 
  WDTRST = 0x1E; // Sequence to start WatchDog 
  WDTRST = 0xE1; 
  while (1) 
  { // Wait for watchdog to hit 
  } 
} 
 
 
// MicroCANopen Call-back function to reset communication 
void MCOUSER_ResetCommunication (void) 
{ 
  EA = 0; 
  MCO_Init(125,OD_NODEID,OD_HEARTBEAT); // 125kbit, Node ID, heartbeat 
 
#if NR_OF_TPDOS > 0 
  MCO_InitTPDO(1,0,100,0,4,(BYTE *) &Position);     
  // TPDO1, default ID (0x180+nodeID), 100 event, 0ms inhibit, 4 bytes 
  // Transmit trigger: 100ms event time 
#endif 
} 
 
void main (void) 
{ 
#ifdef USE_LED 
  gELED = LED_ON; 
  gRLED = LED_OFF; 
#endif 
 
  // Initialize Timer 1 interrupt 
  TR1  =  0;     // timer 1: stop  
  TMOD |= 0x20;  // mode 2  
  TH1  =  0x80;  // first run-time not important 
  TL1  =  0x80; 
  TR1  =  1;     // timer 1: start  
  ET1  =  1;     // enable timer 1 int  
 
  // Initialize ADC 
  ADCF  = 1;     // Enable P1.0 to be used 
  ADCON = 0x20;  // Set bit 5, clear all others 
  ADCON |= 0x08; // Start a conversion 
   
  // Init Process Data 
  ProcZ = 0x8000; 
 
  // Reset/Initialize CANopen communication 
  MCOUSER_ResetCommunication(); 
 
  EA = 1; // End of initialization, Enable all interrupts 
 
#ifdef USE_LED 
  gELED = LED_ON; 
  gRLED = LED_BLINK; 
#endif 
 
while(1) 
  { 
 
    EA = 0; // Disable interrupts for data consistency 
    Position = ProcZ;  
	Position <<= 8; 
    EA = 1; 
 
	// Updated LED display values 
    IO_UpdateLEDs(); // Uses ProcX, ProcY, ProcZ 
 
    // Operate on CANopen protocol stack 
    MCO_ProcessStack(); 
  } // end of while(1) 
}