this code is supposed to read the state of a digital input pin via a pushbutton and output the state to an LED. i.e. when input is high, LED is on and vice versa Since the pushbutton is connected to pull-up resistor, When the pushbutton is pressed the input is supposed to read a LOW and vice versa.
My code:
#include "board.h"
#include <stdio.h>
//setting pointers
#define Port0 ((LPC_GPIO_T *) 0x50000000) //Port 0
#define IOCON ((LPC_IOCON_T *) 0x40044000) //IO configuration
int main(void)
{
/* Initialize pins */
Port0->DIR &= ~((1 << 1)); //PIO0_1 input - onboard switch (unpressed state is pulled-up)
Port0->DIR |= (1<<7); //PIO0_7 output - onboard LED
//Pin configuration
IOCON->REG[IOCON_PIO0_7] &= 0x0 << 3; //No addition pin function
IOCON->REG[IOCON_PIO0_1] &= 0x0 << 3; // "
Port0->DATA[1<<7] &= ~(1<<7); // output initially low
while (1) {
if((Port0->DATA[1<<1]) & (1<<1)) //When input is high
{
Port0->DATA[1<<7] |= (1<<7); //drive PIO0_7 High
}
else
{
Port0->DATA[1<<7] &= ~(1<<7); //Drive PIO0_7 Low
}
}
return 0;
}
When this part of the code is executed PIO0_7 remains remains low unless the button is pressed..However isn't it meant to work the opposite way since switch is pulled-up? I also double checked this with the voltmeter.
I tried changing
if((Port0->DATA[1<<1]) & (1<<1)) //When input is high
to
if(!(Port0->DATA[1<<1]) & (1<<1)) //When input is Low
The LED output remains High, even when the button is pressed.