PDA

View Full Version : Simple questions

ThePenguinMaster
05-26-2010, 10:36 PM
Ok I have what I’m sure is not a difficult question, but I am having difficulty understanding the concept by reading the propeller manual.
What I would like to do is have a byte and simply access one bit from the byte.

An example of this is if you were to have a function that has a single parameter for a byte, and it takes the byte and sets a pin on the prop to a specific position in the byte. This is really just like shifting out a byte through a serial interface. I know there are already objects that will handle serial communication, but what if I wanted to simply check a single bit? The end result will be to send a byte through a pin using SCI, but I would like to have a better understanding on how the byte is shifted and a single bit is output. Looking through the existing objects provides some examples, but it is difficult for me to fully understand how it works. I could simply copy and paste the code, but that won’t get me anywhere in the long run. If there is any reference that you would recommend, I would greatly appreciate it. I have spent some time searching for keywords, but have not turned up many results that fully explain the process.

Mike Green
05-26-2010, 11:06 PM
The reason you're not seeing tutorials on the subject is that this is really basic computer science ... basic bit manipulation. The Propeller (and Spin) has things like logical and, logical or, exclusive or, and bit complementation along with a variety of shift operators and you use these to isolate and manipulate individual bits in a byte, word, or long word. Note that for the I/O control registers (like DIRA, OUTA, INA), Spin allows access to individual bits and groups of bits, but this doesn't work for variables.

Read the descriptions of these bit operators and shift operations in the Propeller Manual:

& logical and
| logical or
^ logical exclusive or
! bit complement

>> shift right
<< shift left

To set a bit in a variable, use "|" (like a := a | %100).
To clear a bit in a variable, use "&" and "!" (like a := a & !%100).
To test a bit, use "&" (like IF a & %100).

ThePenguinMaster
05-27-2010, 12:44 AM
So using what I understand from your examples, I can use a binary mask of 1 to compare with my byte, and simply output the result.

outa[SDA] := (data <-= 1) & 1

This example is what I was looking at and it is clear now. The result of 1 and the data is sent out to the port. This is done in a loop, and only the result of the first bit & 1 is sent. The data byte is also using a bit rotating to move to the next bit (from what I am interpreting.)

Showing your example with the binary value was a good reminder to push me in the right direction here. Seeing "1" and seeing a binary value kind of reminded me that it is a binary comparison.
Yeah, a tutorial isn’t really what I was looking for. I know it is a simple concept, but most of my programming experience is application programming and scripting. Thanks for pointing me in the right direction.

Mike Green
05-27-2010, 12:48 AM
In your particular case, you don't actually have to use the "& 1" because "outa[ SDA ]" is a single bit already. What you've written is still clearer.