📄 gpio.c
字号:
//
//*****************************************************************************
long
GPIOPinRead(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Return the pin value(s).
//
return(HWREG(ulPort + (GPIO_O_DATA + (ucPins << 2))));
}
//*****************************************************************************
//
//! Writes a value at the specified pins of the selected GPIO port.
//!
//! \param ulPort base address of the selected GPIO port
//! \param ucPins bit-packed representation of the specified pins
//! \param ucVal value to write to the specified pins
//!
//! Writes the corresponding bit values to the output pins specified
//! by \e ucPins. Writing to a pin configured as an input pin has no
//! effect.
//!
//! The pins are specified using a bit-packed byte, where each bit that is
//! set identifies the pin to be accessed, and where bit 0 of the byte
//! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, etc.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinWrite(unsigned long ulPort, unsigned char ucPins, unsigned char ucVal)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Write the pins.
//
HWREG(ulPort + (GPIO_O_DATA + (ucPins << 2))) = ucVal;
}
//*****************************************************************************
//
//! Configures pin(s) for use as an analog comparator input.
//!
//! \param ulPort base address of the selected GPIO port
//! \param ucPins bit-packed representation of the specified pins
//!
//! The analog comparator input pins must be properly configured for the analog
//! comparator to function correctly. This function provides the proper
//! configuration for those pins.
//!
//! \note This cannot be used to turn any pin into an analog comparator input;
//! it only configures an analog comparator pin for proper operation.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinTypeComparator(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Make the pin(s) be inputs.
//
GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_IN);
//
// Set the pad(s) for analog operation.
//
GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_ANALOG);
}
//*****************************************************************************
//
//! Configures pin(s) for use by the I2C peripheral.
//!
//! \param ulPort base address of the selected GPIO port
//! \param ucPins bit-packed representation of the specified pins
//!
//! The I2C pins must be properly configured for the I2C peripheral to function
//! correctly. This function provides the proper configuration for those pins.
//!
//! \note This cannot be used to turn any pin into an I2C pin; it only
//! configures an I2C pin for proper operation.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinTypeI2C(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Make the pin(s) be peripheral controlled.
//
GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
//
// Set the pad(s) for open-drain operation with a weak pull-up.
//
GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD_WPU);
}
//*****************************************************************************
//
//! Configures pin(s) for use by the PWM peripheral.
//!
//! \param ulPort base address of the selected GPIO port
//! \param ucPins bit-packed representation of the specified pins
//!
//! The PWM pins must be properly configured for the PWM peripheral to function
//! correctly. This function provides a typical configuration for those pins;
//! other configurations may work as well depending upon the board setup (for
//! example, using the on-chip pull-ups).
//!
//! \note This cannot be used to turn any pin into a PWM pin; it only
//! configures a PWM pin for proper operation.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinTypePWM(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Make the pin(s) be peripheral controlled.
//
GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
//
// Set the pad(s) for standard push-pull operation.
//
GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
}
//*****************************************************************************
//
//! Configures pin(s) for use by the QEI peripheral.
//!
//! \param ulPort base address of the selected GPIO port
//! \param ucPins bit-packed representation of the specified pins
//!
//! The QEI pins must be properly configured for the QEI peripheral to function
//! correctly. This function provides a typical configuration for those pins;
//! other configurations may work as well depending upon the board setup (for
//! example, not using the on-chip pull-ups).
//!
//! \note This cannot be used to turn any pin into a QEI pin; it only
//! configures a QEI pin for proper operation.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinTypeQEI(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Make the pin(s) be peripheral controlled.
//
GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
//
// Set the pad(s) for standard push-pull operation with a weak pull-up.
//
GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
}
//*****************************************************************************
//
//! Configures pin(s) for use by the SSI peripheral.
//!
//! \param ulPort base address of the selected GPIO port
//! \param ucPins bit-packed representation of the specified pins
//!
//! The SSI pins must be properly configured for the SSI peripheral to function
//! correctly. This function provides a typical configuration for those pins;
//! other configurations may work as well depending upon the board setup (for
//! example, using the on-chip pull-ups).
//!
//! \note This cannot be used to turn any pin into a SSI pin; it only
//! configures a SSI pin for proper operation.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinTypeSSI(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Make the pin(s) be peripheral controlled.
//
GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
//
// Set the pad(s) for standard push-pull operation.
//
GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
}
//*****************************************************************************
//
//! Configures pin(s) for use by the Timer peripheral.
//!
//! \param ulPort base address of the selected GPIO port
//! \param ucPins bit-packed representation of the specified pins
//!
//! The CCP pins must be properly configured for the timer peripheral to
//! function correctly. This function provides a typical configuration for
//! those pins; other configurations may work as well depending upon the board
//! setup (for example, using the on-chip pull-ups).
//!
//! \note This cannot be used to turn any pin into a timer pin; it only
//! configures a timer pin for proper operation.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinTypeTimer(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Make the pin(s) be peripheral controlled.
//
GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
//
// Set the pad(s) for standard push-pull operation.
//
GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
}
//*****************************************************************************
//
//! Configures pin(s) for use by the UART peripheral.
//!
//! \param ulPort base address of the selected GPIO port
//! \param ucPins bit-packed representation of the specified pins
//!
//! The UART pins must be properly configured for the UART peripheral to
//! function correctly. This function provides a typical configuration for
//! those pins; other configurations may work as well depending upon the board
//! setup (for example, using the on-chip pull-ups).
//!
//! \note This cannot be used to turn any pin into a UART pin; it only
//! configures a UART pin for proper operation.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinTypeUART(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Make the pin(s) be peripheral controlled.
//
GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
//
// Set the pad(s) for standard push-pull operation.
//
GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
}
//*****************************************************************************
//
//! Configures pin(s) for use as a CAN device.
//!
//! \param ulPort base address of the selected GPIO port
//! \param ucPins bit-packed representation of the specified pins
//!
//! The CAN pins must be properly configured for the CAN peripherals to
//! function correctly. This function provides a typical configuration for
//! those pins; other configurations may work as well depending upon the board
//! setup (for example, using the on-chip pull-ups).
//!
//! \note This cannot be used to turn any pin into a CAN pin(s); it only
//! configures a CAN pin(s) for proper operation.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinTypeCAN(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Make the pin(s) be inputs.
//
GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
//
// Set the pad(s) for analog operation.
//
GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -