Hi all, I have a Little problem getting my LPC2119 to work as I want it to.
Im running a ET-ARM et-arm stamp lpc2119 with GNU arm compiler in µVision3
Im trying to interface a push button with 2LEDs. Using one LED works but not 2. The compiler does not complain or give me an error.
Here is the code that works, a push button to p0.7 and a LED to p1.18
!
include <LPC21xx.H> // LPC2119 MPU Register int main(void) { PINSEL2 &= ~0x0000000C; // Makesure GPIO1.16-1.31 = GPIO Function, PINSEL0 &= ~0x0000C000; //GPIO P0.7, IODIR1 |=(1<<18); // GPIO 1.18 as output IODIR0 &=~((1<<7)); // Set GPIO0.7 AS input while(1) // Loop Continue { if(!(IOPIN0 & (1<<7))) // When button is pressed { IOSET1 |=(1<<18); } else { IOCLR1 =(1<<18); }
Here is the code that does not work, 2 LEDs on p1.16 and p1.18, and the push button on p0.7
Im trying to set that when the button is pressed LED1 on and LED2 off, when button not pressed then LED2 on and LED2 off but it only works as Before with one LED turning on and off, the second LED never turns off.!! :(. What is it that I'm missing
include <LPC21xx.H> // LPC2119 MPU Register int main(void) { PINSEL2 &= ~0x0000000C; // Makesure GPIO1.16-1.31 = GPIO Function, PINSEL0 &= ~0x0000C000; //GPIO P0.7, IODIR1 |=((1<<16)|(1<<18)); // GPIO p1.18 and p1.16 as output IODIR0 &=~((1<<7)); // Set GPIO0.7 AS input while(1) // Loop Continue { if(!(IOPIN0 & (1<<7))) // When button is pressed { IOSET1 |=(1<<18); IOCLR1 =(1<<16); } else { IOCLR1 =(1<<16); IOCLR1 =(1<<18); } } }
You have one IOSET and three IOCLR. That means you are not treating the two LED:s the same.
Note also that the IOSET/IOCLR registers shouldn't be used with |= since that forces the compiler to do a read/modify/write. They are intended to be written directly to - they have no old content you want to modify.
include <LPC21xx.H> // LPC2119 MPU Register int main(void) { PINSEL2 &= ~0x0000000C; // Makesure GPIO1.16-1.31 = GPIO Function, PINSEL0 &= ~0x0000C000; //GPIO P0.7, IODIR1 |=((1<<16)|(1<<18)); // GPIO p1.18 and p1.16 as output IODIR0 &=~((1<<7)); // Set GPIO0.7 AS input while(1) // Loop Continue { if(!(IOPIN0 & (1<<7))) // When button is pressed { IOSET1 |=(1<<18); IOCLR1 |=(1<<16); } else { IOSET1 |=(1<<16); IOCLR1 |=(1<<18); } } }
But this does not work either.
Hi, thanks for the reply; Sorry, I wrote the second code based on the first one and Im at work and dont have my code in front of me. but you are correct. My code for 2 LEDs is
but this does not work either
Hi Per
I tested what you sed about read/modify/write and I changed the code to do the following.
This code works
if(!(IOPIN0 & (0x00000080))) // When button is pressed => IOPIN0 7 =1; { IOCLR1 = (1<<16)|(1<<18); } else { IOSET1 = (1<<16)|(1<<18); }
and this does not! work
if(!(IOPIN0 & (0x00000080))) // When button is pressed => IOPIN0 7 =1; { IOSET1 = (1<<16)|(1<<18); } else { IOCLR1 = (1<<16)|(1<<18); }
This results in that both LEDs are always on! apparently I can't do a IOSET in my if statement ?! What am I not getting here!