reading input pin

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

reading input pin

4,519 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cseb on Sun Nov 01 01:45:28 MST 2015
Hi,
I have trouble with reading a pin as input. I use the code below. I can only read the pin one time and even if its physical state changes , i can not read the change. In user manual its stated ("if an FIOPIN register is read, its bit(s) masked with 1 in the FIOMASK register will be read as 0 regardless of the physical pin state".)page122, but i can not solve this problem.


static LPC_GPIO_TypeDef (* const LPC_GPIO[5]) = { LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3, LPC_GPIO4  };

uint32_t GPIOGetValue (uint32_t portNum, uint32_t bitPosi)
{
    uint32_t val;
    LPC_GPIO[portNum]->FIOMASK = ~ (1 << bitPosi);
    val = LPC_GPIO[portNum]->FIOPIN;
    val = val >> bitPosi;
    LPC_GPIO[portNum]->FIOMASK = 0x00000000;
    return val;
}
Labels (1)
0 Kudos
Reply
11 Replies

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mysepp on Mon Nov 02 08:47:12 MST 2015
Hi R2D2,

> There's no progress because you don't change your funny code

pay attention: this was a comment by independent reader of the thread, not by initial author of this thread.
It looked like you were missing something, but had not written, what was missing.
But with your detailed explanation following my question, I think it is now clear for the author.
Many thanks for the clarification!

Best regards,

Martin
0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cseb on Mon Nov 02 08:23:14 MST 2015
There is no mistakes in my "funny code". In KEIL code does not work, but in Coocox COIDE it is working. At startup;

SysTick_Config(SystemFrequency/1000 - 1); 
here collapses the input reading ( i think ), but i can not understand "why". Anyway, i will try to solve this.
0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Nov 02 04:43:33 MST 2015

Quote: mysepp
Hi R2D2,
can you post what is missing in your opinion?



What's not clear there  :quest:


Quote: R2D2
Could be a good idea to read 1 bit 
    val = LPC_GPIO[portNum]->FIOPIN &[color=#f00] (1 << bitPosi)[/color];



And there  :quest:


Quote: R2D2

Clearing MASK bit and reading PIN bit is the usual way to read a PIN (see LPCOpen). If that's not working in your case you have either a hardware problem or a setup problem (which is not shown in your code snippet)...

LPCOpen:

/**
 * @briefGet a GPIO pin state via the GPIO byte register
 * @parampGPIO: The base of GPIO peripheral on the chip
 * @paramport: GPIO Port number where pin is located
 * @parampin: GPIO pin to get state for
 * @returntrue if the GPIO is high, false if low
 * @noteThis function replaces Chip_GPIO_ReadPortBit()
 */
STATIC INLINE bool Chip_GPIO_GetPinState(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
{
return (bool) ((pGPIO[port].PIN >> pin) & 1);
}





Quote: mysepp
...without real progress...



There's no progress because you don't change your funny code:

LPC_GPIO[portNum]->FIOMASK = ...


What's that? Obviously nonsense...

LPC_GPIO[portNum]->FIOMASK = 0x00000000;


Reset the MASK bit of your input pin in your setup...

val = LPC_GPIO[portNum]->FIOPIN;
val = val >> bitPosi;


Again nonsense    You are reading bit 21-31 here  :~

switch(var)
{
 case 0:
desired_counter = 500;
break;
 case 1:
desired_counter = 1000;
break;
}


And last not least: no default case here. If bit 21-31 >1 nothing is happening 



0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mysepp on Mon Nov 02 00:48:09 MST 2015
Hi R2D2,
can you post what is missing in your opinion? Do you want startup code? Vector table?
Project/Make files?
Otherwise this discussion goes on without real progress...
Best regards,
Martin
0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Sun Nov 01 14:59:48 MST 2015

Quote: cseb
where are you looking at?



Your funny code...


Quote: cseb
.. i think you didn't see whole post



I don't think so...
0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cseb on Sun Nov 01 04:11:43 MST 2015
where are you looking at? check my post again, i think you didn't see whole post :)
0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Sun Nov 01 04:09:13 MST 2015

Quote: cseb
My whole code is below.

uint32_t GPIOGetValue (uint32_t portNum, uint32_t bitPosi)
{ 
    uint32_t val;
    LPC_GPIO[portNum]->FIOMASK = ~(1<<bitPosi);
//val = (LPC_GPIO[portNum]->FIOPIN & (1 << bitPosi));
    val = LPC_GPIO[portNum]->FIOPIN;
    val = val >> bitPosi;
    LPC_GPIO[portNum]->FIOMASK = 0x00000000;
    return val;
}



Are you kidding  :quest:
0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cseb on Sun Nov 01 03:59:33 MST 2015
My whole code is below, by the way i have check the hw connections and there is no wrong with the hardware.

#include "LPC17xx.H"     /* LPC17xx definitions                */

 unsigned long time_counter = 0;  
 unsigned long desired_counter = 1000; 
 uint32_t var; 
 void buton_read( void);
 static LPC_GPIO_TypeDef (* const LPC_GPIO[5]) = { LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3, LPC_GPIO4  };
 uint32_t GPIOGetValue (uint32_t portNum, uint32_t bitPosi);
 

int main (void) {                       // Main Program                       
    SystemInit();
    SysTick_Config(SystemFrequency/1000 - 1); // Generate interrupt each 1 ms  

    LPC_SC->PCONP |= 1 << 1; //Power up Timer 0
    LPC_SC->PCLKSEL0 |= 1 << 2; // Clock for timer = CCLK
    LPC_TIM0->MR0 = 72000; // Give a value suitable for the LED blinking frequency based on the clock frequency
    LPC_TIM0->MCR |= 1 << 0; // Interrupt on Match0 compare
    LPC_TIM0->MCR |= 1 << 1; // Reset timer on Match 0.
    LPC_TIM0->TCR |= 1 << 1; // Manually Reset Timer0 ( forced )
    LPC_TIM0->TCR &= ~(1 << 1); // stop resetting the timer.
    NVIC_EnableIRQ(TIMER0_IRQn); // Enable timer interrupt
    LPC_TIM0->TCR |= 1 << 0; // Start timer
   
    LPC_GPIO1->FIODIR |= 1 << 25; // puts P1.25 into output mode. LED is connected to P1.25

    LPC_GPIO1->FIODIR &= ~(1 << 21); // Set buttons as input

    while(1)
    {       
var = GPIOGetValue(1,21);
switch(var)
{
case 0:
desired_counter = 500;
break;
case 1:
desired_counter = 1000;
break;
}
}

}

void TIMER0_IRQHandler(void) 
{ 
 if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt
 {
 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
         
 if(++time_counter == desired_counter)
 {
 time_counter = 0;
 LPC_GPIO1->FIOPIN ^=  (1<< 25); 
 }
    }
}


uint32_t GPIOGetValue (uint32_t portNum, uint32_t bitPosi)
{ 
    uint32_t val;
    LPC_GPIO[portNum]->FIOMASK = ~(1<<bitPosi);
//val = (LPC_GPIO[portNum]->FIOPIN & (1 << bitPosi));
    val = LPC_GPIO[portNum]->FIOPIN;
    val = val >> bitPosi;
    LPC_GPIO[portNum]->FIOMASK = 0x00000000;
    return val;
}
0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Sun Nov 01 03:50:59 MST 2015

Quote: cseb
I have tried this : val = LPC_GPIO[portNum]->FIOPIN & (1 << bitPosi); but it didn't work



Clearing MASK bit and reading PIN bit is the usual way to read a PIN (see LPCOpen). If that's not working in your case you have either a hardware problem or a setup problem (which is not shown in your code snippet)...

LPCOpen:

/**
 * @briefGet a GPIO pin state via the GPIO byte register
 * @parampGPIO: The base of GPIO peripheral on the chip
 * @paramport: GPIO Port number where pin is located
 * @parampin: GPIO pin to get state for
 * @returntrue if the GPIO is high, false if low
 * @noteThis function replaces Chip_GPIO_ReadPortBit()
 */
STATIC INLINE bool Chip_GPIO_GetPinState(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
{
return (bool) ((pGPIO[port].PIN >> pin) & 1);
}
0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cseb on Sun Nov 01 03:34:29 MST 2015
I have tried this : val = LPC_GPIO[portNum]->FIOPIN & (1 << bitPosi); but it didn't work. I can read pin only once, and after the phyisical change (from logic0 to logic1) it behaves as if it is still on logic0 state.
0 Kudos
Reply

3,325 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Sun Nov 01 01:46:40 MST 2015
Could be a good idea to read 1 bit 

    val = LPC_GPIO[portNum]->FIOPIN &[color=#f00] (1 << bitPosi)[/color];


0 Kudos
Reply