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


/************************************************************************** 
MODULE:    MAIN - Joystick 
CONTAINS:  CANopen joystick 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); 
extern WORD IO_ByteSwap (WORD val); 
 
#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 
BYTE data MSB_Values; // Goes into TPDO1 
WORD data Axis[3]; // Goes into TPDO2 
 
// Process Data, application copy 
// These are updated in io.c by the timer interrupt service routine 
BYTE data ProcB; 
WORD data ProcX, ProcY, ProcZ; 
 
 
// 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,0,50,1,&MSB_Values);     
  // TPDO1, default ID (0x180+nodeID), 0 event, 50ms inhibit, 1 byte 
  // Transmit trigger: COS (change-of-state) with 50ms inhibit time 
#endif 
   
#if NR_OF_TPDOS > 1 
  MCO_InitTPDO(2,0,100,0,6,(BYTE *) &(Axis[0]));  
  // TPDO2, default ID (0x280+nodeID), 100ms event, 0ms inhibit, 6 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 
  ProcX = 0x8000; 
  ProcY = 0x8000; 
  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 
	// Copy MSB of each axis to buttons bits 4,5,6 
    Axis[0] = IO_ByteSwap(ProcX); 
    Axis[1] = IO_ByteSwap(ProcY); 
    Axis[2] = IO_ByteSwap(ProcZ); 
    ProcB   = (BYTE) ( ((ProcX & 0x8000) >> 11) | 
                       ((ProcY & 0x8000) >> 10) | 
			           ((ProcZ & 0x8000) >>  9) ); 
	MSB_Values = ProcB; 
    EA = 1; 
 
	// Updated LED display values 
    IO_UpdateLEDs(); // Uses ProcX, ProcY, ProcZ 
 
    // Operate on CANopen protocol stack 
    MCO_ProcessStack(); 
  } // end of while(1) 
}