AnsweredAssumed Answered

How to read Status of INPUT GPIO pin?

Question asked by vikranth Nk on Apr 29, 2018
Latest reply on May 2, 2018 by xiangjun.rong

I am using FRDM-KE04Z dev board with onboard MKE04Z8VFK4 MCU, for developing Bit banging I am able to transmit data through gpio pin but I am unable to receive data from gpio pin, I need to know how to read the status of input GPIO pin?

 

below is my sample code: I am configured PTA3 is OUTPUT gpio and PTA2 Input gpio pin

 

unsigned char UART_Receive(void)
{
unsigned char DataValue = 0;
uint32_t data;


data = GPIO_PinRead(kGPIO_PORTA, 2U);

while((GPIO_PinRead(kGPIO_PORTA, 2U)) == 0);  // this is the right way?

 

PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(OneBitDelay, PIT_SOURCE_CLOCK));
PIT_StartTimer(PIT, kPIT_Chnl_0);
while(!pitIsrFlag);
PIT_StopTimer(PIT, kPIT_Chnl_0);
pitIsrFlag = 0;

PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT((OneBitDelay/2), PIT_SOURCE_CLOCK));
PIT_StartTimer(PIT, kPIT_Chnl_0);
while(!pitIsrFlag);
PIT_StopTimer(PIT, kPIT_Chnl_0);
pitIsrFlag = 0;


PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(OneBitDelay, PIT_SOURCE_CLOCK));


for ( unsigned char i = 0; i < DataBitCount; i++ )
{
if ( (GPIO_PinRead(kGPIO_PORTA, 2U)))  // this is the right way?
{
DataValue += (1<<i);
}

//DelayUs(OneBitDelay);
PIT_StartTimer(PIT, kPIT_Chnl_0);
while(!pitIsrFlag);
PIT_StopTimer(PIT, kPIT_Chnl_0);
pitIsrFlag = 0;
}

// Check for stop bit
if ( (GPIO_PinRead(kGPIO_PORTA, 2U)))  // this is the right way?
{

PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT((OneBitDelay/2), PIT_SOURCE_CLOCK));
PIT_StartTimer(PIT, kPIT_Chnl_0);
while(!pitIsrFlag);
PIT_StopTimer(PIT, kPIT_Chnl_0);
pitIsrFlag = 0;

return DataValue;
}
else //some error occurred !
{
PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT((OneBitDelay/2), PIT_SOURCE_CLOCK));
PIT_StartTimer(PIT, kPIT_Chnl_0);
while(!pitIsrFlag);
PIT_StopTimer(PIT, kPIT_Chnl_0);
pitIsrFlag = 0;
//DelayUs(OneBitDelay/2);
return 0x000;
}
}

 

 

void UART_Transmit(const char DataValue)
{
/* Basic Logic

TX pin is usually high. A high to low bit is the starting bit and
a low to high bit is the ending bit. No parity bit. No flow control.
BitCount is the number of bits to transmit. Data is transmitted LSB first.

*/

// Send Start Bit
//GPIO_PinClear(UART_TX);
GPIO_PinWrite(kGPIO_PORTA, 3U, 0); ////////////////////////////////////

//DelayUs(OneBitDelay);
PIT_StopTimer(PIT, kPIT_Chnl_0);
PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(OneBitDelay, PIT_SOURCE_CLOCK));
PIT_StartTimer(PIT, kPIT_Chnl_0);
while(!pitIsrFlag);
PIT_StopTimer(PIT, kPIT_Chnl_0);
pitIsrFlag = 0;


PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(OneBitDelay, PIT_SOURCE_CLOCK));

for ( unsigned char i = 0; i < DataBitCount; i++ )
{
//Set Data pin according to the DataValue
if( ((DataValue>>i)&0x1) == 0x1 ) //if Bit is high
{
//GPIO_PinSet(UART_TX);
GPIO_PinWrite(kGPIO_PORTA, 3U, 1); ////////////////////////////////////
}
else //if Bit is low
{
//GPIO_PinClear(UART_TX);
GPIO_PinWrite(kGPIO_PORTA, 3U, 0); ////////////////////////////////////
}
PIT_StartTimer(PIT, kPIT_Chnl_0);
while(!pitIsrFlag);
PIT_StopTimer(PIT, kPIT_Chnl_0);
pitIsrFlag = 0;
//DelayUs(OneBitDelay);
}

//Send Stop Bit
//GPIO_PinSet(UART_TX);
GPIO_PinWrite(kGPIO_PORTA, 3U, 1); ////////////////////////////////////

//DelayUs(OneBitDelay);
PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(OneBitDelay, PIT_SOURCE_CLOCK));
PIT_StartTimer(PIT, kPIT_Chnl_0);
while(!pitIsrFlag);
PIT_StopTimer(PIT, kPIT_Chnl_0);
pitIsrFlag = 0;
}

Outcomes