-1

I have a practice exam question that asks me to read PORTB input logic levels into a single char variable called result. (On a PIC18F252). I had some ideas for this but I haven't got answers to check so I'm not sure whats right. This is what I was thinking at first, but I don't know if calling PORTB in a bitwise OR operation would actually retrieve all the input levels:

void setup(){
   TRISB = 0xFF; //Set all portB pins as inputs
}

void main(){
   char result;
   result |= PORTB;  //OR the value of the PORTB register into the result variable
}

Would this be right? And if not can anyone help me find a better way to do it?

MendelumS
  • 91
  • 1
  • 8
  • 3
    Remember that uninitialized local variables *are* uninitialized. They will have an *indeterminate* value (which might seem as random or garbage). – Some programmer dude Dec 22 '19 at 11:19
  • @visibleman I'm assuming you're saying its wrong but you don't know what would be right? If so, could you explain why you think its wrong? – MendelumS Dec 22 '19 at 12:12
  • @Someprogrammerdude Problem is that in the question it states that result has been initialized earlier in the theoretical program, I only need to write my answer as the actual reading of PORTB. I know I wrote it all here but its more because I often get people being very snappy with me on here if I don't post full code – MendelumS Dec 22 '19 at 12:13

1 Answers1

0

After playing around some more, I've found that it was an error in the question that was causing the problem. result |= PORTB will actually work fine, but result should be an unsigned char, not a char as the question stated. This is because PORTB and unsigned chars both contain an 8-bit value (0-255), but a char handles 7 bit values (-128 to 127).

MendelumS
  • 91
  • 1
  • 8