Code question...

Hi all,

Well, making progress getting the ARM up and running. I’ve got a stable Yagardo going and a not so stable CrossWorks!

Sooo, I can make my LED’s work! yippeeee!

Now I’m just trying to “read” a pin, so I’m doing a Button test.

volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA;     // pointer to PIO data structure

#define output_low(mask)    (pPIO->PIO_SODR=mask)
#define output_high(mask)   (pPIO->PIO_CODR=mask)
#define input(mask)         (pPIO->PIO_ODSR & mask)

#define HIGH    1
#define LOW     0

int main()
{
    int j;

    LowLevelInit();

    // Set up the LEDs                  
    pPIO->PIO_PER=LED_MASK|SW_MASK;     // bits 17,18 | 19,20
    pPIO->PIO_OER=LED_MASK;             // bits 17, 18
    pPIO->PIO_SODR=LED_MASK;            // bits 17, 18

    // turn off LEDs
    output_low(LED1);
    output_low(LED2);

    while(1)
    {
        if(input(SW1)==HIGH)
        {
            output_high(LED1);
            output_low(LED2);
        }
        else
        {
            output_low(LED1);
            output_high(LED2);
        }
    }

    while(1);

    return 0;
}

It always returns LOW!

Am I reading the port correctly by using pPIO->PIO_ODSR and masking? It’s defined as my “input” method…

Well, there it is!

~Kam (^8*

The line “if(input(SW1)==HIGH)” is always going to evaluate to false unless SW1 happens to be on the zero bit of pPIO->PIO_ODSR as far as I can tell. Think of it as an 8-bit register as an example. If you are testing the MSb, then you would mask it out with 0x80. Then, if the bit was high, you would be left with 0x80, or 0x00 if it was low. Therefore, if you try to test if this result is equal to 1 (which is your define for HIGH), it is never equal, since neither 0x80 or 0x00 are equal to 0x01.

If I were you, I would write the while loop like this:

while(1) 
    { 
        if(input(SW1)!=LOW) 
        { 
            output_high(LED1); 
            output_low(LED2); 
        } 
        else 
        { 
            output_low(LED1); 
            output_high(LED2); 
        } 
    }

I knew that. (:oops:) Just call me Mr. Duh!

Well, still not there…Still not detecting…

BTW, its on a AT91SAM7S256 (Olimex board)

#define output_low(mask)    (pPIO->PIO_SODR=mask)
#define output_high(mask)   (pPIO->PIO_CODR=mask)
#define input(mask)         (pPIO->PIO_ODSR & mask)

#define HIGH    1
#define LOW     0

#define LED1            (1<<17)	
#define LED2            (1<<18)	
#define LED_MASK        (LED1|LED2)

/*-------------------------*/
/* Push Buttons Definition */
/*-------------------------*/

#define SW1        (1<<19)	
#define SW2        (1<<20)	
#define SW_MASK    (SW1|SW2)

int main()
{
    int j;

    LowLevelInit();

    // Set up the LEDs                  
    pPIO->PIO_PER=LED_MASK|SW_MASK;     // bits 17,18 | 19,20
    pPIO->PIO_OER=LED_MASK;             // bits 17, 18
    pPIO->PIO_SODR=LED_MASK;            // bits 17, 18

    // turn off LEDs
    output_low(LED1);
    output_low(LED2);

    while(1)
    {
        if(input(SW1)!=LOW)
        {
            output_high(LED1);
            output_low(LED2);
        }
        else
        {
            output_low(LED1);
            output_high(LED2);
        }
    }

    while(1);

    return 0;
}

In one example, they used used a different pio bit

    if(!((pPio->PIO_PDSR) & BIT19))

and I’m using pio_odsr…

Thoughts?!

~Kam (^8*

Here’s how I do it on an LPC2103:

from lpc2103.h

#define IOPIN (*((volatile unsigned long *) 0xE0028000))

#define SWITCH_PIN 15

if( IOPIN & (1<<SWITCH_PIN) )

sw_state = 1;

else

sw_state = 0;

Finally!

Looking at millions of examples, I noticed a SINLGE line I was missing! AH! :frowning:

    //enable the clock of the PIO
    pPMC->PMC_PCER = 1 << AT91C_ID_PIOA;

That was it! Now I can read “inputs”!

BLA!

~Kam (^8*