📄 gpio.c
字号:
/*****************************************************************************
* gpio.c: GPIO C file for NXP LPC210x Family Microprocessors
*
* Copyright(C) 2006, NXP Semiconductor
* All rights reserved.
*
* History
* 2005.10.01 ver 1.00 Prelimnary version, first Release
*
*****************************************************************************/
#include "LPC210x.h"
#include "type.h"
#include "i2c.h"
#include "gpio.h"
#include "mcu_main.h"
UINT32 IO_State = 0;
UINT32 InputMonitor_Selection = 0;
UINT32 OpenDrain_Selection = 0;
/***********************************************************************
** Function name: IO_Init
** Description: This function initializes the 32 general purpose
** I/O ports to its default values
** Parameter: None
** Returned value: None
************************************************************************/
void IO_Init(void)
{
IODIR |= P0_0_MASKBITPATTERN; // P0.0 (Active-Low) is initialized as High
IOSET = P0_0_MASKBITPATTERN;
/* Save the initial IO states of IO ports */
IO_State = IOPIN;
/* Initialize the input ports for state change monitoring selection status */
InputMonitor_Selection = 0;
/* Initialize the open-drain port selection status */
OpenDrain_Selection = P0_MASKOPENDRAIN;
/* Clear the all other output first, even if direction not set yet */
IOCLR = ~P0_0_MASKBITPATTERN;
} // IO_Init
/***********************************************************************
** Function name: IO_Config
** Description: This function configures the input/output direction
** for the 32 I/O ports of the MCU.
** - When bit is '1', the corresponding port pin
** is set as output port.
** - When bit is '0', the corresponding port pin
** is set as input port.
** Parameter: None
** Returned value: None
************************************************************************/
void IO_Config(void)
{
BYTE i=0;
UINT32 IOTemp = 0, IOValue = 0;
for(i=0; i<4; i++)
{
IOTemp = 0;
IOTemp = I2C0RxBuffer[i+1] << i*8;
IOValue |= IOTemp;
}
IOTemp = IOValue & ~P0_0_MASKBITPATTERN & OpenDrain_Selection;
IOCLR |= IOTemp; // Set logic state as Low first
IODIR |= IOTemp; // Setting open-drain ports as output
IOTemp = IOValue & ~P0_0_MASKBITPATTERN & ~OpenDrain_Selection;
IODIR |= IOTemp; // Setting normal ports as output
IOTemp = IOValue | P0_0_MASKBITPATTERN;
IODIR &= IOTemp; // Setting input ports
IO_State = IOPIN; // Save IO states of IO ports
} // IO_Config
/***********************************************************************
** Function name: IO_ReadStatus
** Description: This function allows the I2C Host to read the I/O
** logic states of the 32 I/Oports on the MCU.
** - The MCU reads and stores the IO states for all
** the 32 IO port pins in the I2C Tx buffer.
** - The IO states stored in the I2C Tx buffer will
** then be transfered back to the I2C master device.
** Parameter: None
** Returned value: None
************************************************************************/
void IO_ReadStatus(void)
{
BYTE i=0;
UINT32 IOValue = 0, IOTemp = 0;
I2C0TxCounter = 4;
IOTemp = ~OpenDrain_Selection | P0_0_MASKBITPATTERN; // Port P0.0 should not be configured as an open-drain port
IOValue = IOPIN & IOTemp;
/* Determine the logic state of open drain outputs by its direction,
since a logic High with pull-up may be below lower limits of logic High */
IOValue |= (~IODIR & OpenDrain_Selection); // If input open-drain port >> Logic High
for(i=0; i<BUFSIZE; i++)
{
if(i<4)
I2C0TxBuffer[i] = (BYTE) ( (IOValue >> (i*8)) & 0xFF );
else
I2C0TxBuffer[i] = 0x00;
}
} // IO_ReadStatus
/***********************************************************************
** Function name: IO_Set
** Description: This function configures the logic state of the
** corresponding port as logic High
** - When bit is '1', the corresponding port pin
** is set as High state.
** - For P0.25, the port pin is set as input when
** the corresponding bit is High.
** - For P0.26, the port pin is set as input when
** the corresponding bit is High.
** Parameter: None
** Returned value: None
************************************************************************/
void IO_Set(void)
{
BYTE i=0;
UINT32 IOTemp=0, IOValue=0;
for(i=0; i<4; i++)
{
IOTemp = 0;
IOTemp = I2C0RxBuffer[i+1] << i*8;
IOValue |= IOTemp;
}
/* Special handling for emulated open-drain ports: */
/* - To set open-drain ports as logic High, configure the
direction of the port as input (let external pull-up do the work) */
IOTemp = IOValue & ~P0_0_MASKBITPATTERN & OpenDrain_Selection;
IODIR &= ~IOTemp;
IOTemp = IOValue & ~P0_0_MASKBITPATTERN & ~OpenDrain_Selection;
IOSET = IOTemp; // Set port state as High
} // IO_Set
/***********************************************************************
** Function name: IO_Clear
** Description: This function configures the logic state of the
** corresponding port as logic Low.
** - When bit is '1', the corresponding port pin
** is set to Low state.
** - When bit is '0', there will be no configuration
** to the logic state of the corresponding port.
** Parameter: None
** Returned value: None
************************************************************************/
void IO_Clear(void)
{
BYTE i=0;
UINT32 IOTemp = 0, IOValue = 0;
for(i=0; i<4; i++)
{
IOTemp = 0;
IOTemp = I2C0RxBuffer[i+1] << i*8;
IOValue |= IOTemp;
}
IOCLR = IOValue & ~P0_0_MASKBITPATTERN; // Set port state as logic Low
/* Special handling for emulated open-drain ports: */
/* - To set open drain ports as logic Low, set the logic level as Low
before configuring the direction of the ports as output */
IOTemp = IOValue & ~P0_0_MASKBITPATTERN & OpenDrain_Selection;
IODIR |= IOTemp;
} // IO_Clear
/***********************************************************************
** Function name: IO_MonitorInput
** Description: This function allows the I2C host to select which
** input ports to monitor for state change event.
**
** - When bit is '1', the corresponding port
** is selected for its input state to be monitored
** - When bit is '0', the corresponding port
** will not be monitored
** Parameter: None
** Returned value: None
************************************************************************/
void IO_MonitorInput(void)
{
BYTE i=0;
InputMonitor_Selection=0;
for(i=0; i<4; i++)
{
InputMonitor_Selection |= I2C0RxBuffer[i+1] << i*8;
}
} // IO_MonitorInput
/***********************************************************************
** Function name: IO_Write
** Description: This function configures the logic state (High/Low)
** of the corresponding ports.
**
** - When bit is '1', the logic state of the
** corresponding port is set as logic High.
** - When bit is '0', the logic state of the
** corresponding port is set as logic Low.
** - For P0.25, the port pin is set as input when
** the corresponding bit is High.
** - For P0.26, the port pin is set as input when
** the corresponding bit is High.
** Parameter: None
** Returned value: None
************************************************************************/
void IO_Write(void)
{
BYTE i=0;
UINT32 IOTemp=0, IOValue=0;
for(i=0; i<4; i++)
{
IOTemp = 0;
IOTemp = I2C0RxBuffer[i+1] << i*8;
IOValue |= IOTemp;
}
/* Special handling for emulated open-drain ports: */
/* 1. To set open-drain ports as logic High, configure the
direction of the port as input (let external pull-up do the work) */
IOTemp = IOValue & ~P0_0_MASKBITPATTERN & OpenDrain_Selection;
IODIR &= ~IOTemp;
/* 2. To set open drain ports as logic Low, set the logic level as Low
before configuring the direction of the ports as output */
IOTemp = ~IOValue & ~P0_0_MASKBITPATTERN & OpenDrain_Selection;
IOCLR = IOTemp;
IODIR |= IOTemp;
IOTemp = IOValue & ~P0_0_MASKBITPATTERN & ~OpenDrain_Selection;
IOSET = IOTemp; // Set port state as High
IOTemp = ~IOValue & ~P0_0_MASKBITPATTERN & ~OpenDrain_Selection;
IOCLR = IOTemp; // Set port state as Low
} // IO_Write
/***********************************************************************
** Function name: IO_OpenDrain
** Description: This function allows the I2C Host to select ports
** for open-drain port handling.
**
** - When bit is '1', the corresponding port is
** selected for open-drain port handling.
** - When bit is '0', the corresponding port is not
** selected for open-drain port handling.
** Parameter: None
** Returned value: None
************************************************************************/
void IO_OpenDrain(void)
{
BYTE i = 0;
OpenDrain_Selection = 0;
for(i=0; i<4; i++)
{
OpenDrain_Selection |= I2C0RxBuffer[i+1] << i*8;
}
OpenDrain_Selection |= P0_MASKOPENDRAIN;
} // IO_OpenDrain
/******************************************************************************
** End Of File
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -