Turning off a single GPIO pin on ARM9 (LPC3141)

88 views Asked by At

I am currently studying on a LPC3141 development board. I am trying to turn off a single GPIO pin while leaving the others in the same states as they were. My problem is that I can turn them on individually but when i want to turn off just one pit it makes a "bus reset" and turns them all off. I cannot figure out why does it reset all of them when I use bit shifting. Here is an example of my code that does this:

#define PINS (*((volatile unsigned int *)0x130031C0))
#define MODE0 (*((volatile unsigned int *)0x130031D0)) 
#define MODE0_SET (*((volatile unsigned int *)0x130031D4))
#define MODE0_RESET (*((volatile unsigned int *)0x130031D8))

#define MODE1 (*((volatile unsigned int *)0x130031E0))
#define MODE1_SET (*((volatile unsigned int *)0x130031E4))
#define MODE1_RESET (*((volatile unsigned int *)0x130031E8))

void delay (void);

void c_entry(void){

    //Prg gpio pins (glej user manual str 312-318
    //Bit manipulation (spremenim samo 1 bit v registru inne celega)
    MODE1 = MODE1 | (0x1 << 6); 
    MODE1 = MODE1 | (0x1 << 8);

    while(1){
        MODE0 = MODE0 | (0x1 << 6);
        MODE0 = MODE0 | (0x1 << 8);
        delay();
        MODE1 = MODE1 | (0x1 << 6);
        MODE1 = MODE1 | (0x1 << 8);
        MODE0 = MODE0 & !(0b1000000);
        delay();
    }
}

void delay (void){
    volatile int stej = 1000000;
    while(stej){
    stej = stej - 1;
}
1

There are 1 answers

8
Paul R On

You're using the wrong operator when you want to clear a bit - you want the bitwise complement operator ~, not the logical NOT operator !.

Note: bitwise operators, as their name implies, operate on individual bits within a value, whereas logical operators treat a value as a single true/false quantity (0 = false, everything else = true). Bitwise operators: &, |, ^, ~. Logical operators: &&, ||, !.

So for example your line:

MODE0 = MODE0 & !(0b1000000);

should be:

MODE0 = MODE0 & ~(0b1000000);

or more succinctly/consistently:

MODE0 &= ~(0x1 << 6);