Help with GPIO for new user

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

Help with GPIO for new user

1,223 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cthvx8@hotmail.com on Fri Feb 27 10:58:05 MST 2015
I am just starting out with microcontrollers and I need some help to get me going.  I attempted to write some basic code to turn on and off some LEDs which I connected to pins 0-7 on Port0.  I wanted to alternate them between 10101010 and 01010101 with a delay in between.  It doesn't seem to be working and I was hoping someone could have a look and tell me what I'm doing wrong.

#if defined (__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#endif
#endif

#include <cr_section_macros.h>

// TODO: insert other include files here

// TODO: insert other definitions and declarations here

volatile uint32_t msTicks=0;

// Systick Handler which is called each ms
void SysTick_Handler(void)
{
    msTicks++;
}

// blocks for dlyTicks ms...
static void Delay(uint32_t dlyTicks)
{
    uint32_t curTicks;

    curTicks = msTicks;
    while ((msTicks - curTicks) < dlyTicks);
}
//------------------------------------------------------------------------------------
int main(void) {

#if defined (__USE_LPCOPEN)
#if !defined(NO_BOARD_LIB)
    // Read clock settings and update SystemCoreClock variable
    SystemCoreClockUpdate();
    // Set up and initialize all required blocks and
    // functions related to the board hardware
    Board_Init();
    // Set the LED to the state of "On"
    Board_LED_Set(0, true);
#endif
#endif

    // TODO: insert code here

    //Initialize Port 0
Chip_GPIO_Init(LPC_GPIO_PORT);

//Set Port 0 pins 0-7 to output
Chip_GPIO_SetPortDIROutput(LPC_GPIO_PORT, 0, 0xFF);

Chip_GPIO_SetPortMask(LPC_GPIO_PORT, 0, 0xFF);

SysTick_Config(SystemCoreClock/1000);

while(1){

//Set Port 0 pins to 01010101
Chip_GPIO_SetMaskedPortValue(LPC_GPIO_PORT, 0, 0X55);
Board_LED_Set(0, true);

//Wait 1 second
Delay(1000);

//Set Port 0 pins to 10101010
Chip_GPIO_SetMaskedPortValue(LPC_GPIO_PORT, 0, 0XFF);

Board_LED_Set(0, false);
//Wait 1 second
Delay(1000);

}

    // Force the counter to be placed into memory
    volatile static int i = 0 ;
    // Enter an infinite loop, just incrementing a counter
    while(1) {
        i++ ;

    }
    return 0 ;
}
Labels (1)
0 Kudos
8 Replies

965 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Wed Jun 17 01:07:41 MST 2015
Hint: Before you hijack an old thread you should at least read all the previous replies ...
0 Kudos

965 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kabirbansod on Wed Jun 17 00:54:40 MST 2015
Never mind, it was actually because there was my delay function was not working, the leds were not toggling!
0 Kudos

965 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Jun 16 13:42:30 MST 2015

Quote: kabirbansod
What am i doing wrong?



You are posting code snippets ...
0 Kudos

966 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kabirbansod on Tue Jun 16 13:19:57 MST 2015
I am trying to blink leds, here is how i set up the functions:

#include "LPC13Uxx.h"
#include "gpio.h"

void leds_arch_init(void) {
GPIOSetDir( PORT1,14, 1 );
GPIOSetDir( PORT1,13, 1 );
}

void leds_arch_set(unsigned char leds) {
if(leds & LEDS_GREEN) {
GPIOSetBitValue( 1,13, 1 );
} else {
GPIOSetBitValue( 1,13, 0 );
}

if(leds & LEDS_YELLOW) {
GPIOSetBitValue( 1,14, 1 );
} else {
GPIOSetBitValue( 1,14, 0 );
}

When i try to turn of the LEDs, it doesnt work! What am i doing wrong? While debugging the GPIOSetBitValue() does get called but nothing happens it just goes past " LPC_GPIO->CLR[portNum] = ..."  without turning the LED off.
0 Kudos

966 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Tue Mar 03 01:42:07 MST 2015

Quote: cthvx8@hotmail.com
But I still couldn't get the expected behaviour when I used the "Chip_GPIO_SetMaskedPortValue" function.


You would use the "masked" function or register only under special circumstances, if you want to atomically set a subsets of the port bits to a value that may contain both zeros and ones.

If you want to set only ones or only zeros atomically there are the SET and CLR registers, respectively (and probably corresponding Chip_GPIO_ functions).

BTW, I also find the use of the inverted mask counterintuitive.
0 Kudos

966 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Mar 02 14:04:37 MST 2015

Quote: cthvx8@hotmail.com
What is wrong with the way I set up the masking in my earlier post?



:quest:

UM:

Quote:
9.5.3.6 GPIO masked port pin registers
These registers are similar to the PORT registers, except that the value read is masked by ANDing with the inverted contents of the corresponding MASK register, and writing to one of these registers[color=#f00] only affects output register bits that are enabled by zeros in the corresponding MASK register.[/color]



You are setting MASK register bits, so as result writing to mask port doesn't change the output  :(( 

Sample to change masked bits 2,3,6,8,9 of port 0:
 Chip_GPIO_Init(LPC_GPIO_PORT);

 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 2, (IOCON_FUNC0));
 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 3, (IOCON_FUNC0));
 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 6, (IOCON_FUNC0));
 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 8, (IOCON_FUNC0));
 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 9, (IOCON_FUNC0));

 //Chip_GPIO_SetPortDIROutput(LPC_GPIO_PORT, 0, 0xFC);
 LPC_GPIO_PORT->DIR[0] |= 0xFFC;

 Chip_GPIO_SetPortMask(LPC_GPIO_PORT, 0, [color=#f00]~[/color]((1<<2)|(1<<3)|(1<<6)|(1<<8)|(1<<9)));

 while(1)
 {
  Chip_GPIO_SetMaskedPortValue(LPC_GPIO_PORT, 0, 0XFFF);
  Chip_GPIO_SetMaskedPortValue(LPC_GPIO_PORT, 0, 0X000);
  i++;
 }


Note: Chip_GPIO_SetPortDIROutput isn't working correctly...
0 Kudos

966 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cthvx8@hotmail.com on Mon Mar 02 12:07:23 MST 2015
Ok so I figured out that I had to configure the I/O like so:

    LPC_IOCON->PIO0[0]=1;
    LPC_IOCON->PIO0[1]=0;
    LPC_IOCON->PIO0[2]=0;
    LPC_IOCON->PIO0[3]=0;

But I still couldn't get the expected behaviour when I used the "Chip_GPIO_SetMaskedPortValue" function.  I got it to work when I used the "Chip_GPIO_SetPortValue" function however.  What is wrong with the way I set up the masking in my earlier post?
0 Kudos

966 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Fri Feb 27 13:23:18 MST 2015
Take a look at I/O configuration (chapter 7 in the user manual).
0 Kudos