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


/************************************************************************** 
MODULE:    IO 
CONTAINS:  IO functions for Atmel CANopen Demo Board 
           Board is a CANgine board - see www.cangine.com 
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> 
 
 
#define T1_RELOAD 89 // Timer 1 reload = 0.05us at 20Mhz 
#define INC_VAL 2 // In timer ISR, joystick values are incremented by this 
 
// The process data from main, is updated in timer ISR 
#ifdef JOYSTICK 
extern WORD data ProcX; 
extern WORD data ProcY; 
#endif 
extern WORD data ProcZ; 
 
// PWM output on LEDs, value 0-16 
BYTE LED_up = 0; 
BYTE LED_down = 0; 
BYTE LED_right = 0; 
BYTE LED_left = 0; 
BYTE LED_ana = 0; 
BYTE cnt = 0; // count from 0 to 64 as comparison for values above 
 
bit mXClr; // detect if both X buttons pressed 
bit mYClr; // detect if both Y buttons pressed 
bit mToggle; // toggle for timer ISR 
 
 
#ifndef ENCODER // Not needed for the encoder example 
/************************************************************************** 
DOES: Byte swapping (big-endian to little-endian conversion 
**************************************************************************/ 
WORD IO_ByteSwap (WORD val) 
{ 
unsigned int rval; 
 
  ((BYTE *) &rval) [0] = ((BYTE *) &val) [1]; 
  ((BYTE *) &rval) [1] = ((BYTE *) &val) [0]; 
 
  return (rval); 
} 
#endif 
 
/************************************************************************** 
DOES: Converts a value from 0 to 0x7FFF to 0,1,2,4,8,16,32,64 
      Needed for the LED fading 
**************************************************************************/ 
BYTE ConvertLEDValue (WORD val) 
{ 
  val &= 0x7FFF; // ensure value range 0 to 0x7FFF 
  val >>= 12; // transform to range 0 to 7 
  if (val == 0) 
    return 0; 
  else 
    return (BYTE) (1 << (val-1)); 
} 
 
 
/************************************************************************** 
DOES: Implements the LED fading / PWM 
**************************************************************************/ 
void SwitchLEDs (void) 
{ 
#ifdef JOYSTICK 
  if (cnt < LED_left) 
    P0_0 = 0; 
  else 
    P0_0 = 1; 
  if (cnt < LED_right) 
    P0_1 = 0; 
  else 
    P0_1 = 1; 
  if (cnt < LED_down) 
    P0_2 = 0; 
  else 
    P0_2 = 1; 
  if (cnt < LED_up) 
    P0_3 = 0; 
  else 
    P0_3 = 1; 
#endif // JOYSTICK 
  if (cnt < LED_ana) 
    P1_7 = 0; 
  else 
    P1_7 = 1; 
  cnt++; 
  if (cnt >= 64) 
    cnt = 0; 
} 
 
 
/************************************************************************** 
DOES: Checks all buttons and the potentiometer 
**************************************************************************/ 
void ReadInputs (void) 
{ 
#ifdef JOYSTICK 
  if (mYClr) // Both buttons were pressed, reset sequence started 
  { // wait until both buttons are released 
    if ((P2_0 == 1) && (P2_1 == 1)) 
	{ 
	  mYClr = 0; 
	} 
  } 
  else 
  { 
    if (P2_0 == 0) // Down button pressed 
    { 
      if (ProcY >= INC_VAL) 
        ProcY -= INC_VAL; 
    } 
    if (P2_1 == 0) // Up button pressed 
    { 
      if (ProcY <= (0xFFFF - INC_VAL)) 
        ProcY += INC_VAL; 
    } 
  } 
  if ((P2_0 == 0) && (P2_1 == 0)) 
  { // Up and Down pressed 
    ProcY = 0x8000; 
    mYClr = 1; 
  } 
 
  if (mXClr) // Both buttons were pressed, reset sequence started 
  { // wait until both buttons are released 
    if ((P2_2 == 1) && (P2_3 == 1)) 
	{ 
	  mXClr = 0; 
	} 
  } 
  else 
  { 
    if (P2_2 == 0) // Left button pressed 
    { 
      if (ProcX >= INC_VAL) 
        ProcX -= INC_VAL; 
    } 
    if (P2_3 == 0) // Right button pressed 
    { 
      if (ProcX <= (0xFFFF - INC_VAL)) 
        ProcX += INC_VAL; 
    } 
  } 
  if ((P2_2 == 0) && (P2_3 == 0)) 
  { // Right and Left pressed 
    ProcX = 0x8000; 
    mXClr = 1; 
  } 
#endif // JOYSTICK 
  if ((ADCON & 0x08) == 0) // Conversion complete? 
  { 
    ProcZ = ((WORD) ADDH) << 8; // Read value from last ADC conversion 
    ADCON |= 0x08; // Start New ADC Conversion 
  } 
  else 
  { 
    ProcZ = (ProcZ & 0xFF00) | 0x0055; 
  } 
} 
 
 
/************************************************************************** 
DOES: Updates the values displayed on the LEDs 
**************************************************************************/ 
void IO_UpdateLEDs (void) 
{ 
WORD data w; 
 
#ifdef JOYSTICK 
  // X - Axis, up and down 
  EA = 0; 
  w = ProcX; 
  EA = 1; 
  if (w < 0x8000) 
  { 
    LED_down = ConvertLEDValue(0x7FFF - w);  
    LED_up = 0; 
  } 
  else 
  { 
    LED_down = 0;  
	LED_up = ConvertLEDValue(w - 0x8000); 
  } 
	 
  // Y - Axis, left and right 
  EA = 0; 
  w = ProcY; 
  EA = 1; 
  if (w < 0x8000) 
  { 
    LED_left = ConvertLEDValue(0x7FFF - w);  
	LED_right = 0; 
  } 
  else 
  { 
    LED_left = 0;  
	LED_right = ConvertLEDValue(w - 0x8000); 
  } 
#endif // JOYSTICK 
 
  // Z - Axis, potentiometer, analog out 
  EA = 0; 
  w = ProcZ; 
  EA = 1; 
  LED_ana = ConvertLEDValue(w >> 1);  
} 
 
 
 
/************************************************************************** 
DOES: Timer Interrupt Service Routine.  
Performs all I/O tasks. 
**************************************************************************/ 
 
void AppTimerISR (void) interrupt 3 
{ 
  mToggle = ~mToggle; 
  if (mToggle) // Only execute this every 2nd call 
  { 
    SwitchLEDs(); 
    ReadInputs(); 
  } 
}