I am having trouble using the Pins Config Tools to make GPIO pin working in input mode.
The board I use is FRDM-K22F.
Here is what I did:
1) I created a simple "Hello World" project by following the MCUXpresso_IDE_User_Guide.pdf.
2) Use the Pins config tools to configure PTA4 as an input pin and PTC3 as an output pin. I gave am identifier 'TestIn' to PTA4, and 'TestOut' to PTC3. The tools generated the following code in the BOARD_initPins(void) (inside the pin_mux.h), which seem to be correct:
void BOARD_InitPins(void)
{
/* Port A Clock Gate Control: Clock enabled */
CLOCK_EnableClock(kCLOCK_PortA);
/* Port C Clock Gate Control: Clock enabled */
CLOCK_EnableClock(kCLOCK_PortC);
gpio_pin_config_t TestIn_config = {
.pinDirection = kGPIO_DigitalInput,
.outputLogic = 0U
};
/* Initialize GPIO functionality on pin PTA4 (pin 26) */
GPIO_PinInit(BOARD_TestIn_GPIO, BOARD_TestIn_PIN, &TestIn_config);
gpio_pin_config_t TestOut_config = {
.pinDirection = kGPIO_DigitalOutput,
.outputLogic = 0U
};
/* Initialize GPIO functionality on pin PTC3 (pin 46) */
GPIO_PinInit(BOARD_TestOut_GPIO, BOARD_TestOut_PIN, &TestOut_config);
/* PORTA4 (pin 26) is configured as PTA4 */
PORT_SetPinMux(BOARD_TestIn_PORT, BOARD_TestIn_PIN, kPORT_MuxAsGpio);
/* PORTC3 (pin 46) is configured as PTC3 */
PORT_SetPinMux(BOARD_TestOut_PORT, BOARD_TestOut_PIN, kPORT_MuxAsGpio);
}
2) I added the following code to the board.h file:
#define TestOut_ON() \
GPIO_PortSet(BOARD_TestOut_GPIO, 1U << BOARD_TestOut_PIN) /*!< Set TestOut pin */
#define TestOut_OFF() \
GPIO_PortClear(BOARD_TestOut_GPIO, 1U << BOARD_TestOut_PIN) /*!< Clear TestOut pin */
#define Read_TestIn() \
GPIO_ReadPinInput(BOARD_TestIn_GPIO, 1U << BOARD_TestIn_PIN) /*!< Read TestIn pin */
3) I added a few test lines in the main() procedure. The main() looks like this:
int main(void) {
uint32_t test_val;
/* Init board hardware. */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
/* Init FSL debug console. */
BOARD_InitDebugConsole();
PRINTF("Hello World\n");
/* Force the counter to be placed into memory. */
volatile static int i = 0 ;
/* Enter an infinite loop, just incrementing a counter. */
TestOut_ON();
TestOut_OFF();
test_val = Read_TestIn();
PRINTF("input: %d\n",test_val);
while(1) {
i++ ;
}
return 0 ;
}
4) I compiled the project and stepped through the code using the debug mode. Before running the code, I had PTA4 connected to a 3.3V input. I can see the output pin PTC3 worked correctly by switching to HIGH and LOW as it stepped through the TestOut_ON() and TestOUT_OFF(), but when I stepped through the "test_val = Read_TestIn()", the test_val returned is always 0. I tried to use different pins, but no success.
If anyone see anything wrong I did, please leave a message. Thank you very much for your help.
#
已解决! 转到解答。
Hello Gang,
I have checked your code , I'm confused why does it left shift one bit at below:
If you change it to the below code , this meaning read the PTA4 input pin :
#define Read_TestIn() \
GPIO_ReadPinInput(BOARD_TestIn_GPIO, BOARD_TestIn_PIN) /*!< Read TestIn pin */
the result is :
Hope it helps,
Have a great day,
TIC
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hello GANG FENG,
Do you choose the right pin on FRDM-K22 board ?
PTA4 is J1-10 pin on board. The pin is high by defult even if we connet nothing signal with it.
I test on my side with MCUXpresso IDE, it can work well .
Which IDE do you used ? You can send your whole project to me , I will check it on my side .
Hope it helps,
Have a great day,
TIC
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hello Gang,
I have checked your code , I'm confused why does it left shift one bit at below:
If you change it to the below code , this meaning read the PTA4 input pin :
#define Read_TestIn() \
GPIO_ReadPinInput(BOARD_TestIn_GPIO, BOARD_TestIn_PIN) /*!< Read TestIn pin */
the result is :
Hope it helps,
Have a great day,
TIC
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------