How to speed up setting a pin?

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

How to speed up setting a pin?

551 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mhjerde on Fri May 13 23:26:49 MST 2011
I need a fast way to set a pin to the value of an argument (0 or 1). The MCU is an LPC1768. This is what the code looks like now:

    // foo is either 0 or 1

    switch (foo)
    {
        case 0:
            LPC_GPIO0->FIOCLR = (1 << LCD_RS_PIN);    // set low
            break;
        case 1:
            LPC_GPIO0->FIOSET = (1 << LCD_RS_PIN);    // set high
            break;
    }
Is there a way to set the pin directly to the value of foo and avoid an if-else or switch statement? foo is passed as an argument. I feel like I'm missing something embarrassingly obvious!!

Morten
0 Kudos
8 Replies

532 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Mon May 16 00:59:59 MST 2011

Quote: mhjerde
I don't think bitwise OR with 0 will clear the bit. Have to use bitwise AND.



You could be right about this.  It's late and I don't think as well when I'm tired.

I had looked at your site before so I know what the project is about.  It's very interesting.

Good Night/Morning,
Larry
0 Kudos

532 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mhjerde on Mon May 16 00:35:50 MST 2011

Quote: larryvc
Could you try:

// foo is either 0 or 1
  LPC_GPIO0->FIOPIN |= (foo << LCD_RS_PIN);    // set




I don't think bitwise OR with 0 will clear the bit. Have to use bitwise AND.
0 Kudos

532 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mhjerde on Mon May 16 00:24:09 MST 2011
@larryvc Yes, I ran out of both flash and pins on the LPC1343. The ambition grew as project ambitions are prone to. You can see it here: http://riftlabs.com.

Porting from 13xx to 17xx was educational and quite a bit of work, but actually pretty straightforward. Gave me a chance to tidy up the code as well. The nice thing about 17xx is that the drivers and example code I found is way better than for the 13xx.
0 Kudos

532 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Mon May 16 00:13:13 MST 2011

Quote: mhjerde
Hi
Thanks! The thing is that I only want to set and clear a single pin without affecting the rest of the port.
Unless I'm badly mistaken your suggestion will affect all pins on the port
LPC_GPIO0->FIOPIN = (1 << 10);
This will set pin 10 but also clear all other pins on port 0.



Could you try:

// foo is either 0 or 1
  LPC_GPIO0->FIOPIN |= (foo << LCD_RS_PIN);    // set
0 Kudos

532 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mhjerde on Mon May 16 00:07:06 MST 2011
Hi
Thanks! The thing is that I only want to set and clear a single pin without affecting the rest of the port.
Unless I'm badly mistaken your suggestion will affect all pins on the port
LPC_GPIO0->FIOPIN = (1 << 10);
This will set pin 10 but also clear all other pins on port 0.

The two ways I know to set or clear a pin are
LPC_GPIO0->FIOPIN |= (1 << 10); //or
LPC_GPIO0->FIOSET = (1 << 10);

and 

LPC_GPIO0->FIOPIN &= ~(1 << 10);  //or
LPC_GPIO0->FIOCLR = (1 << 10);
I can't find a way to directly set pin 10 to 1 or 0. I could maybe mask the other pins. I'll have to figure out if mask/unmask is faster or slower than using the  switch statement.
0 Kudos

532 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Sun May 15 23:41:17 MST 2011
I thought I had seen you asking about this before for the LPC1343.:)

http://knowledgebase.nxp.com/showthread.php?t=809

Did you run out of flash and have to move to the LPC1768?
0 Kudos

532 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Sat May 14 08:25:50 MST 2011

Quote: Zero
It's a little bit smaller :) and faster :)



and smarter :)

I was up past 1:00 am trying to remember how to do that and where I had used it before.  You posted at 1:37 am local time, just after I conked out.:rolleyes:
0 Kudos

532 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat May 14 01:37:12 MST 2011
Try:

// foo is either 0 or 1
  LPC_GPIO0->FIOPIN = (foo << LCD_RS_PIN);    // set 
It's a little bit smaller and faster

Quote:

     switch (foo)
0x000001c6 <main+66>:  ldr   r3, [r7, #12]
0x000001c8 <main+68>:  str   r3, [r7, #4]
0x000001ca <main+70>:  ldr   r3, [r7, #4]
0x000001cc <main+72>:  cmp   r3, #0
0x000001ce <main+74>:  beq.n 0x1d8 <main+84>
0x000001d0 <main+76>:  ldr   r3, [r7, #4]
0x000001d2 <main+78>:  cmp   r3, #1
0x000001d4 <main+80>:  beq.n 0x1e8 <main+100>
0x000001d6 <main+82>:  b.n   0x1aa <main+38>
             LPC_GPIO0->FIOCLR = (1 << LCD_RS_PIN);    // set low
0x000001d8 <main+84>:  movw  r3, #49152    ; 0xc000
0x000001dc <main+88>:  movt  r3, #8201    ; 0x2009
0x000001e0 <main+92>:  mov.w r2, #2097152    ; 0x200000
0x000001e4 <main+96>:  str   r2, [r3, #28]
0x000001e6 <main+98>:  b.n   0x1aa <main+38>
             LPC_GPIO0->FIOSET = (1 << LCD_RS_PIN);    // set high
0x000001e8 <main+100>: movw  r3, #49152    ; 0xc000
0x000001ec <main+104>: movt  r3, #8201    ; 0x2009
0x000001f0 <main+108>: mov.w r2, #2097152    ; 0x200000
0x000001f4 <main+112>: str   r2, [r3, #24]
0x000001f6 <main+114>: b.n   0x1aa <main+38>


Quote:

LPC_GPIO0->FIOPIN = (foo << LCD_RS_PIN);    // set low
0x000001da <main+86>:  movw  r2, #49152    ; 0xc000
0x000001de <main+90>:  movt  r2, #8201    ; 0x2009
0x000001e2 <main+94>:  ldr   r3, [r7, #4]
0x000001e4 <main+96>:  mov.w r3, r3, lsl #21
0x000001e8 <main+100>: str   r3, [r2, #20]
0x000001ea <main+102>: b.n   0x1aa <main+38>

0 Kudos