Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

lpc1769 input pin reading

Status
Not open for further replies.

eray81

Junior Member level 3
Joined
Jan 1, 2012
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,461
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.

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);			 
				 }
    }
}
 

LPC_GPIO1->FIODIR is used to define the direction of port pins. writing a zero make the port as input and writing a one make the port as output.
LPC_GPIO1->FIODIR =0x0; define the port one as input port.
You can see a good tutorial from the link below.
http://techmasterplus.com/tutorials/arm-gpio.php
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top