⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 digio.c

📁 AVR平台下的CanOpen协议桟源码包括应用
💻 C
📖 第 1 页 / 共 2 页
字号:

      /* Refresh the DDR */
      DIGOUT2_DDR |= DigOutMask2;
      /* Get current output setting (leaving all other bits zero) */
      portdata     = DIGOUT2_PORT & DigOutMask2;
    }
  return portdata;
}

/* ------------------------------------------------------------------------ */

void digout_pdo( BYTE dlc, BYTE *pdo_data )
{
  if( dlc >= DIGIO_OUTPORT_CNT )
    {
      /* Set digital outputs according to databyte(s) in PDO */ 
      digout_set_port( 1, pdo_data[0] );
      digout_set_port( 2, pdo_data[1] );
    }
  else
    {
      if( dlc == 1 )
	{
	  /* Set digital outputs according to databyte in PDO */ 
	  digout_set_port( 1, pdo_data[0] );
	}
      else
	{
	  /* DLC is zero ?... */

	  /* CANopen Error Code 0x8210: protocol error
	     (PDO not processed due to length error), see CiA DS301 pg 9-38 */
	  can_write_emergency( 0x10, 0x82, 1,
			       0, 0, 0, ERRREG_COMMUNICATION );
	}
    }
}

/* ------------------------------------------------------------------------ */

BOOL digout_set_init( BOOL init_hi )
{
  if( init_hi > 1 ) return FALSE;
  if( init_hi ) DigOutInitHi = TRUE;
  else DigOutInitHi = FALSE;

#ifdef __VARS_IN_EEPROM__
  if( eeprom_read( EE_DIGOUTINIT ) != DigOutInitHi )
    eeprom_write( EE_DIGOUTINIT, DigOutInitHi );
#endif /* __VARS_IN_EEPROM__ */
  return TRUE;
}

/* ------------------------------------------------------------------------ */

BOOL digout_get_init( void )
{
#ifdef __VARS_IN_EEPROM__
  DigOutInitHi = eeprom_read( EE_DIGOUTINIT );
#endif /* __VARS_IN_EEPROM__ */

  return DigOutInitHi;
}

/* ------------------------------------------------------------------------ */

void digout_set_mask( BYTE port_no, BYTE digout_mask )
{
  /* Set digital output mask according to databyte */
  if( port_no == 2 )
    {
      DigOutMask2 = digout_mask;

      /* Apply new output setting (outputs itself not initialized) */
      DIGOUT2_DDR = DigOutMask2;

      /* Sharing the port with the inputs... */
      DigInMask2  = ~DigOutMask2;

      /* Digital input port: use pull-ups (=1) */
      DIGIN2_PORT |= DigInMask2;

#ifdef __VARS_IN_EEPROM__
      if( eeprom_read( EE_DIGOUTMASK2 ) != DigOutMask2 )
	eeprom_write( EE_DIGOUTMASK2, DigOutMask2 );
#endif /* __VARS_IN_EEPROM__ */
    }
}

/* ------------------------------------------------------------------------ */

BYTE digout_get_mask( BYTE port_no )
{
#ifdef __VARS_IN_EEPROM__
  DigOutMask2 = eeprom_read( EE_DIGOUTMASK2 );
#endif /* __VARS_IN_EEPROM__ */

  if( port_no == 1 ) return 0xFF;
  if( port_no == 2 ) return DigOutMask2;

  return 0x00;
}

/* ------------------------------------------------------------------------ */

void digin_set_debounce( BYTE debounce_cntr )
{
  DigInDebouncePolls = debounce_cntr;

  /* Maximum is 0xFE */
  if( debounce_cntr == 0xFF ) DigInDebouncePolls = 0xFE;

#ifdef __VARS_IN_EEPROM__
  if( eeprom_read( EE_DIGINDEBOUNCEPOLLS ) != DigInDebouncePolls )
    eeprom_write( EE_DIGINDEBOUNCEPOLLS, DigInDebouncePolls );
#endif /* __VARS_IN_EEPROM__ */
}

/* ------------------------------------------------------------------------ */

BYTE digin_get_debounce( void )
{
#ifdef __VARS_IN_EEPROM__
  DigInDebouncePolls = eeprom_read( EE_DIGINDEBOUNCEPOLLS );
#endif /* __VARS_IN_EEPROM__ */

  return DigInDebouncePolls;
}

/* ------------------------------------------------------------------------ */

BOOL digin_set_intrpt_ena( BOOL intrpt_ena )
{
  if( intrpt_ena > 1 ) return FALSE;
  if( intrpt_ena ) DigInIntrptEna = TRUE;
  else DigInIntrptEna = FALSE;

#ifdef __VARS_IN_EEPROM__
  if( eeprom_read( EE_DIGININTRPTENA ) != DigInIntrptEna )
    eeprom_write( EE_DIGININTRPTENA, DigInIntrptEna );
#endif /* __VARS_IN_EEPROM__ */
  return TRUE;
}

/* ------------------------------------------------------------------------ */

BOOL digin_get_intrpt_ena( void )
{
#ifdef __VARS_IN_EEPROM__
  DigInIntrptEna = eeprom_read( EE_DIGININTRPTENA );
#endif /* __VARS_IN_EEPROM__ */

  return DigInIntrptEna;
}

/* ------------------------------------------------------------------------ */

void digin_set_intrpt_mask( BYTE port_no, BYTE intrpt_mask )
{
  /* Set interrupt mask according to databyte */
  if( port_no == 1 ) DigInIntrptMask1 = intrpt_mask;
  if( port_no == 2 ) DigInIntrptMask2 = intrpt_mask;

#ifdef __VARS_IN_EEPROM__
  if( eeprom_read( EE_DIGININTRPTMASK1 ) != DigInIntrptMask1 )
    eeprom_write( EE_DIGININTRPTMASK1, DigInIntrptMask1 );
  if( eeprom_read( EE_DIGININTRPTMASK2 ) != DigInIntrptMask2 )
    eeprom_write( EE_DIGININTRPTMASK2, DigInIntrptMask2 );
#endif /* __VARS_IN_EEPROM__ */
}

/* ------------------------------------------------------------------------ */

BYTE digin_get_intrpt_mask( BYTE port_no )
{
#ifdef __VARS_IN_EEPROM__
  DigInIntrptMask1 = eeprom_read( EE_DIGININTRPTMASK1 );
  DigInIntrptMask2 = eeprom_read( EE_DIGININTRPTMASK2 );
#endif /* __VARS_IN_EEPROM__ */

  if( port_no == 1 ) return DigInIntrptMask1;
  if( port_no == 2 ) return DigInIntrptMask2;

  return 0x00;
}

/* ------------------------------------------------------------------------ */

#define DIGIO_STORE_SIZE 6

/* ------------------------------------------------------------------------ */

BOOL digio_store_config( void )
{
  BYTE block[DIGIO_STORE_SIZE];

#ifdef __VARS_IN_EEPROM__
  DigInDebouncePolls = eeprom_read( EE_DIGINDEBOUNCEPOLLS );
  DigInIntrptEna     = eeprom_read( EE_DIGININTRPTENA );
  DigOutInitHi       = eeprom_read( EE_DIGOUTINIT );
  DigOutMask2        = eeprom_read( EE_DIGOUTMASK2 );
  DigInIntrptMask1   = eeprom_read( EE_DIGININTRPTMASK1 );
  DigInIntrptMask2   = eeprom_read( EE_DIGININTRPTMASK2 );
#endif /* __VARS_IN_EEPROM__ */

  block[0] = DigInDebouncePolls;
  block[1] = DigInIntrptEna;
  block[2] = DigOutInitHi;
  block[3] = DigOutMask2;
  block[4] = DigInIntrptMask1;
  block[5] = DigInIntrptMask2;

  return( store_write_block( STORE_DIGIO, DIGIO_STORE_SIZE, block ) );
}

/* ------------------------------------------------------------------------ */

static void digio_load_config( void )
{
  BYTE block[DIGIO_STORE_SIZE];

  /* Read the configuration from EEPROM, if any */
  if( store_read_block( STORE_DIGIO, DIGIO_STORE_SIZE, block ) )
    {
      DigInDebouncePolls = block[0];
      DigInIntrptEna     = block[1];
      DigOutInitHi       = block[2];
      DigOutMask2        = block[3];
      DigInIntrptMask1   = block[4];
      DigInIntrptMask2   = block[5];
    }
  else
    {
      /* No valid parameters in EEPROM: use defaults */
      DigInDebouncePolls = DIGIN_DEBOUNCE_POLLS_DFLT;
      DigInIntrptEna     = FALSE;
      DigOutInitHi       = TRUE;
      DigOutMask2        = 0xFF; /* 2nd Port is output by default */
      DigInIntrptMask1   = 0xFF;
      DigInIntrptMask2   = 0xFF;
    }
#ifdef __ADC_AVR__
  DigInIntrptEna = FALSE;
#endif

#ifdef __VARS_IN_EEPROM__
  /* Create working copies of configuration globals in EEPROM */
  if( eeprom_read( EE_DIGINDEBOUNCEPOLLS ) != DigInDebouncePolls )
    eeprom_write( EE_DIGINDEBOUNCEPOLLS, DigInDebouncePolls );
  if( eeprom_read( EE_DIGININTRPTENA ) != DigInIntrptEna )
    eeprom_write( EE_DIGININTRPTENA, DigInIntrptEna );
  if( eeprom_read( EE_DIGOUTINIT ) != DigOutInitHi )
    eeprom_write( EE_DIGOUTINIT, DigOutInitHi );
  if( eeprom_read( EE_DIGOUTMASK2 ) != DigOutMask2 )
    eeprom_write( EE_DIGOUTMASK2, DigOutMask2 );
  if( eeprom_read( EE_DIGININTRPTMASK1 ) != DigInIntrptMask1 )
    eeprom_write( EE_DIGININTRPTMASK1, DigInIntrptMask1 );
  if( eeprom_read( EE_DIGININTRPTMASK2 ) != DigInIntrptMask2 )
    eeprom_write( EE_DIGININTRPTMASK2, DigInIntrptMask2 );
#endif /* __VARS_IN_EEPROM__ */
}

/* ------------------------------------------------------------------------ */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -