This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Greetings,
I am a student in Electrical Engineering at University of Central Florida and I am currently studying Embedded Systems. For my lab, we are required to run through a series of prewritten example codes in both C and assembly. I am understanding (at least I hope I am) the basics of the assembly instruction set. My problem is this: every time I come back to my code after a day or so hiatus, the hex values that Port1 reads out are different. Sometimes, for the switches both being open (not pressed), the P1IN value in the register window reads 0x03, yet other times it will be 0x0B or even 0x8something. One day my code will work, but the next day, the exact same code will no longer work until I check the register window for the new hex values and change my code accordingly. I'm not sure what is causing these values to change every time I come back to my code. I have asked my professor and he has no idea what I am talking about. Neither does anybody in my class. Any ideas why it is doing this?
Seth Rhodes said:I have asked my professor and he has no idea what I am talking about. Neither does anybody in my class.
And neither do I :)
Could you upload the program you are talking about and explain at which location in the code the problem occurs?
Dennis
Read SCBA004: Implications of Slow or Floating CMOS Inputs.
You probably forgot a pull-up or -down resistor on your inputs, or have configured unused pins on the port as inputs.
I am going to guess that your two switches are connected to bits 0 and 1, and that the remaining bits of the port are unconnected. I am also going to guess that you have configured all bits of the port as inputs and that the unused ones are thus floating.
When you read the port, bits 0 and 1 reflect the value of you switches (always 1 when the switch is open and 0 when it is pressed if the switch closes to ground), and the remaining bits are random because the inputs are floating.
To determine the state of the switch, you need to just look at the bits in question and ignore the other bits, rather than do a comparison on the entire value read from the port.
if (~P1IN & 0x01) {
// the first switch is pressed
}
if (~P1IN & 0x02) {
// the second switch is pressed
}
I hate this craptastic editor.
if (~P1IN & 0x01) { // the first switch is pressed } if (~P1IN & 0x02) { // the second switch is pressed }
It sets all the bits to inputs; 0, 1 and the other six that are not connected to your switches.
Your original complaint was that sometimes you read 0x03 and other times, with the switches in the same state, you read 0x0B. Those two values have exactly the same values for bits 0 and 1, it is the other bits that differ: 0x03 = 00000011, 0x0B = 00001011. Bits 0 and 1 (the least-significant bits, on the right-hand end of the binary representation) are 1 in both cases. It is other bits that are different.
My guess is that you have not wired up the other six bits, and that they are thus floating, and that you therefore read random values from them
The correct approach is that, when you wish to test the value of a particular bit, use a bitwise operator. For example (P1IN & 0x01) gives the value of just bit 0, ignoring the others, and (P1IN & 0x02) does the same for bit 1.
**Attention** This is a public forum