ARM LPC2000 GPIO problem

I have a problem reading input pins on the LPC2138.
I used the following assignment in the Keil uVision3 and got the following error:
CODE FRAGMENT
unsigned int Key_0=IOPIN1& 0x00080000; // Read input pin P1.24

ERROR
error: KeyPad.c(36): error: #28: expression must have a constant value.

What could be the problem? How do I read an input (IOPIN) and store the value?

Parents
  • You showed too little code - we can' see the scope rule for your variable. Note that for C, you can't declare new variables anywhere in the code.

    C allows the variable declaration to assign a constant value, i.e. a compile-time-computable value in the variable declaration.

    unsigned my_variable = 0;
    

    Separate your code into:

    unsigned Key_0;
    
    Key_0 = xxx;
    

    Another thing: Your constant is 0x00080000. Your comment says P1.24. First off, you seem to have miss-placed a zero. But the next problem is that the pin names are zero-relative, so pin 24 would represent 0x01000000. Your constant relates to P1.19.

    How about using (1u << 24) if you want to access pin 24? The advantage is of course that you might know which pin you test, without a comment.

    Even better would be to create a named constant, telling what P1.24 really is.

    if (IOPIN1 & IO1_KEY_0) {
    }
    

    But are you sure that your variable should be named Key_0? Do you have one wire for each key, or are the keys placed in a grid?

    Finally: Your header says GPIO problem. But in reality you have a compilation problem. Your problem isn't reading the GPIO, but to get the compiler to accept your source code.

Reply
  • You showed too little code - we can' see the scope rule for your variable. Note that for C, you can't declare new variables anywhere in the code.

    C allows the variable declaration to assign a constant value, i.e. a compile-time-computable value in the variable declaration.

    unsigned my_variable = 0;
    

    Separate your code into:

    unsigned Key_0;
    
    Key_0 = xxx;
    

    Another thing: Your constant is 0x00080000. Your comment says P1.24. First off, you seem to have miss-placed a zero. But the next problem is that the pin names are zero-relative, so pin 24 would represent 0x01000000. Your constant relates to P1.19.

    How about using (1u << 24) if you want to access pin 24? The advantage is of course that you might know which pin you test, without a comment.

    Even better would be to create a named constant, telling what P1.24 really is.

    if (IOPIN1 & IO1_KEY_0) {
    }
    

    But are you sure that your variable should be named Key_0? Do you have one wire for each key, or are the keys placed in a grid?

    Finally: Your header says GPIO problem. But in reality you have a compilation problem. Your problem isn't reading the GPIO, but to get the compiler to accept your source code.

Children
More questions in this forum