eray81
Junior Member level 3
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.
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.
Code:
#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;
}
}
}
uint32_t GPIOGetValue (uint32_t portNum, uint32_t bitPosi)
{
uint32_t val;
LPC_GPIO[portNum]->FIOMASK = ~(1<FIOPIN & (1 << bitPosi));
val = LPC_GPIO[portNum]->FIOPIN;
val = val >> bitPosi;
LPC_GPIO[portNum]->FIOMASK = 0x00000000;
return val;
}
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);
}
}
}