68HCS12 I/O problems

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

68HCS12 I/O problems

903 Views
jonathankoh
Contributor I

Iam using the HCS12 micro controller. The following program that I have takes the input from an on board keypad and displays letters to an on board LCD depending on which key is pressed (I only attached the portion of the code dealing with the inputs / outputs, i can attach the rest if need be but its easier on the eyes this way).

 

What I would like to do is to get rid of the keypad interface being used as inputs, and use a sensor that I have which sends active low signals though 3 separate pins instead. That way in stead of having to push the keypad each time, whenever the sensor reads a low signal (either PortA1, 2 or 3) it will display the correct letter.

 

The init_keypad function sets PortA0-PortA3 as inputs. In the getkey function im not sure how to change the if statements to read PortA0-PortA3 individually and check for the active low inputs.

 

 

 

 

void init_keypad(void){
    DDRA = 0xf0;  // PA7-PA4 output; PA3-PA0 input
    DDRB = 0xf0;
    PUCR |= 1;   // PUPAE =1 (enable pull-up on all PORTA inputs)
}

char getkey(void){
    PORTA = 0xe0;       // selects row 0    
    if ((PORTA & 1) == 0){
    cmdwrt(0x01);
      delay(20);
      if ((PORTA & 1) == 0) return('L');//true if '1' key still active after 20 ms
    }
    PORTA = 0xd0;
    if ((PORTA & 1) == 0){
    cmdwrt(0x01);
      delay(20);
      if ((PORTA & 1) == 0) return('S');//true if '1' key still active after 20 ms
    }
    PORTA = 0xb0; 
    if ((PORTA & 1) == 0){
    cmdwrt(0x01);
      delay(20);
      if ((PORTA & 1) == 0) return('R');//true if '1' key still active after 20 ms
    }

 

 

Labels (1)
0 Kudos
8 Replies

648 Views
Lundin
Senior Contributor IV

This seems rather off-topic, since your question seems to be about fundamental C programming and not Freescale MCUs.

To check if a bit is set, you write  if( (PORTA & (1<<n)) > 0).

To check if a bit is clear, you write if( (PORTA & (1<<n)) == 0).

Where n is the bit number, in your case from 0 to 3.

0 Kudos

648 Views
colinhoward
Contributor I

Daniel, out of curiosity, why do you use > 0 for testing what is really an inequality? Isn't the use of != 0 more intuitive?

Thanks, Colin

0 Kudos

648 Views
Lundin
Senior Contributor IV

Either > or == would be fine, I don't think there is a particular advantage of either form. The important part is that the result of the operation which is passed to the if statement is "essentially boolean", that is, it is the result of one of the operators ! == != > < >= <= && ||. Code like if(PORTA & (1<<n)) where the result is "essentially integer", is often considered poor/sloppy style. It is banned by MISRA-C, for example.

0 Kudos

648 Views
colinhoward
Contributor I

Thanks for replying Daniel.  For me the question of "> 0" vs "!= 0" is really one of readability and consistency.

I agree that in the example of doing a bit wise AND,  > 0 is can be used just as effectively as != 0 (as long as you are only testing for 1 bit) however it comes down to maintaining a consistent way of dealing with the boolean conversion process.  In the bad old days a zero value was false and non zero was true.  So if we "convert" the now MISRA illegal if(i) to if(i > 0) then we have essentially defined false to be <= 0.  If we convert if(i) to if(i != 0) the original intent of the code is maintained. 

I reserve > and < for what I would consider threshold comparisons of analog data - not evaluating if a bit is set or clear.

It may just be personal preferance but it is one I will bring up in code reviews everytime ;-)

0 Kudos

648 Views
RadekS
NXP Employee
NXP Employee

If you using default mc9s12xxx.h file (project in CW), you can use standard names for pins:

PORTA_PA0

PORTA_PA1

For details: click right mouse button on “PORTA” and go to “Go to macro declaration of PORTA” (project must be already compiled). You will see declaration in mc9s12xxx.h file…


0 Kudos

648 Views
Lundin
Senior Contributor IV

The CW headers are hardly "standard", as they rely on undefined- and implementation-defined behavior of bit fields. Personally I prefer to use standard C, as specified in ISO 9899.

There is not even anything "Freescale standard" about the the name PORTA_PA0. No such bit mask name exists in the manual, which must be the canon for bit mask naming.

0 Kudos

648 Views
colinhoward
Contributor I

Daniel, I'm not sure if you missed my original reply into this topic, but out of curiosity, why do you use > 0 for testing what is really an inequality? Isn't the use of != 0 more intuitive?

Thanks, Colin

  

0 Kudos

648 Views
jonathankoh
Contributor I

After re-reading my post it seems a little confusing. 

Im trying to set 3 port pins as inputs (already in the above code) but I dont know how to read the individual port pins.

0 Kudos