Ok, I want to be able to send a number between 0 and 255 from the RA port to set the RC and/ or RB ports. I know you cant do it directly because the RA port is 4 bit where RB and RC are 8 bit.
Hmmm, that is not going to be easy.... Probably you would need to use a shift register to hold the value.
Check out this page http://www.allaboutcircuits.com/vol_4/chpt_12/4.html
Attached is a picture using only 3 outputs to create 8 outputs.
If your using SX/B you can use SHIFTOUT to set the shift register.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
If you need to receive 8 parallel bits and you only have 4 pins open, then I too would use a shift-register -- my choice in this case would be a 74HC165 (needs three pins and can be daisy-chained for more inputs).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ Jon Williams Applications Engineer, Parallax
You are thinking in reverse here. I want to "read in" 4 bits (0 - 15 at a time)·and output on either the RC or RB register. If the oppisite were true, I would use a shift register.
Your original question was "How to I input 8 bits with just four pins" (I'm paraphrasing).· You can in fact do that with a 74HC165 -- it is a parallel-in, serial-out shift register.· If you're using SX/B then you can read the 74HC165 with SHIFTIN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ Jon Williams Applications Engineer, Parallax
I have data coming from an a Parallal port. It is coming four bits at a time. Four bits can only possibly be 0 to 15. If I were to come out with an eight bit solution, I would have to add one four bit parallalel transmision to another. Lets say those values are 8 and 12. The parallel port outputs the number 8. The microcontroller picks it up and waits for the next nibble. Once it is sent, the microcontroller captures it. After this is done, the microcontroller adds the two values together. The process is repeated again if the nibble changes.
That is an entirely different scenario from your first post. How does the SX know when the nibble changes? What if the eight bit value contains the same value in both nibbles?· If they're always different, does the high or low nibble come first?
Posting clear questions and not changing your request in the middle of the thread will get you quicker answers ... and save me and others a lot of wasted typing.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ Jon Williams Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 10/13/2005 3:10:30 AM GMT
Instead of, "Lets say I have data coming from an external source, such as a Parallal port or dip switches," why not just say exactly what you want to hook up to the RA port and then see if people can help you. Waving your hands around the problem isn't making it any clearer for those trying to assist you. If you have a parallel port, then provide details such as :
What the device is that actually has the port in question. Is it a PC? An embedded processor?
What handshake mechanism there is to let the SX know that data is ready and that a nibble has been retrieved.
If it's not a parallel port, then don't mention parallel ports at all. If it's a DIP switch, then say it's a DIP switch. In that case, you still need to tell folks what sort of handshake or sync mechanism there is to let the SX know that data is ready.
If there is no mechanism, then all you are trying to do is read an 8 bit DIP switch with a 4 bit port. In that case you're going to need some external hardware to assist. Period. There is no magic to getting a four bit port to read an eight bit DIP swtich - it's just a matter of buffering the data and presenting it as a pair of nibbles, but you stil need an extra bit or two to load the buffer and switch between nibbles.
It happens sometimes, I think something is clear where others do not. Trust me, ive seen worse.
This may work. Tell the SX to capture the first nibble, wait a bit, then recieve the next. That way the computer parallel port is responsible for sending the second signal within the wait period. That way, if the second signal is the same, the comp will not change it. The microcontroller goes into a loop. When it is time to recieve another signal, a break code is given (lets say the number 2). After the parallel port puts 2 out, the loop is broken, a delay is given to give the computer time to change from the number 2 to the next nibble. Here is the general code, dont mind any errors, just approximation.
tris_b = 0
nib1 var byte
nib2 var byte
result var byte
start:
do 'signal microcontroller to begin recieving first nibble
pause 25
loop until ra = 2
pause 100 ' wait for PC to change signals from 2 to whatever
nib1 = ra
pause 100 ' wait for PC to change signals to nibble 2 value
nib2 = ra
'compute result
result = nib1 + nib2
rb = result
pause 100 ' give computer time to change from whatever to zero, this prevents an unwanted
'loop break if ra is still 2 (nibble 2 is 2)
Did you not say that you wanted to receive two nibbles and reassemble them as a byte?· Well, then, adding them together won't work.·You might consider assembling the byte like this:
· theByte = RA················' get high·nibble · SWAP theByte················' move to high position· · PAUSE NibDelay··············' wait for low nibble · temp = RA···················' read the low nible · theByte = theByte | temp··· ' assemble with high
But as Peter pointed out, without some kind of specific notification from the external device that the nibble has changed you could end up with errors.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ Jon Williams Applications Engineer, Parallax
It sounds like you will still need a way for the external device to tell the SX that each new data nybble is ready.
You could send two data bits at a time, and use a third bit as clock, or a "data ready" bit. The SX would wait for the clock bit to go high. On the first transition to high, then data is ready. The SX would shift its result byte left twice, then read the two data bits and OR them into the result byte. Then wait for the clock bit to go low. Do this four times and the SX will then have a complete byte.
You might need a way to synchronize the start of each byte transmission, depending on your specific application. I've done something like this where I used time for synchronization - once the SX receives a clock, it expects the next three clocks to arrive within milliseconds, otherwise it resets.
How about adapting an async method for encoding the the signal for when data should be clocked in.
Doing this has a set of problems because you need to be able to distinguish between the timing nibbles and the data nibbles which may be identical, the only method I can think of is having two timing nibbles, a stop nibble and a start nibble.
The stop nibble will be the·NOT of the last data nibble, the start nibble could be any value, but lets assume it is 0.
You will look for the transition of the last data nibble of the previous frame to the stop nibble by looking for the transition from a value to to its inverse, then you time the amount of time that passes till the nibble turns to 0 (let's call that value t), now the first data nibble of the frame could also be a 0, but that doesn't matter because you already got your time per nibble.
So you wait a value of t+t/2 from the end of the stop nibble, this places you in the middle of the first data nibble, you read that value into a buffer byte·then·SWAP it to the high nibble of·the buffer. Then you wait an additional time t, and now your in the middle of the second data nibble, you OR port a into your buffer, and now the data is in your buffer byte. At some time less than t/2 later you enter a tight loop looking for the next stop nibble and start the whole process over.
Now when the·communication first starts you may end up out of sync if the·first data nibble is the inverse of the start nibble or the second data nibble is the inverse of the first data nibble, but the first data sequence this isn't the case you will be synced and stay synced.· If you make the start nibble the same as the last data nibble of the previous frame (NOT(NOT(last data nibble)) you can reduce the likelyhood of syncing to the wrong portion of the data because your looking for a "nibble NOT(nibble) nibble" sequence which will occur only rarely when it isn't the real McCoy.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ ·1+1=10
Post Edited (Paul Baker) : 10/13/2005 4:07:39 PM GMT
Thank you all for your help. I did know until now that you had to get fancy to reassemble a byte. My bianary addition skills are extremely rusty and thats what threw me off. I am also sorry for causing you inconvience by vague posts. That was partly because I did not know what to really say at first.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
A friend of mine was a street bum for halloween.
Comments
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
Available now... SX-Video OSD module $59.95 www.sxvm.com
"Save your money. Pay with cash."
·
Thanks
Check out this page http://www.allaboutcircuits.com/vol_4/chpt_12/4.html
Attached is a picture using only 3 outputs to create 8 outputs.
If your using SX/B you can use SHIFTOUT to set the shift register.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
Available now... SX-Video OSD module $59.95 www.sxvm.com
"Save your money. Pay with cash."
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (kingneb) : 10/13/2005 4:07:13 AM GMT
Posting clear questions and not changing your request in the middle of the thread will get you quicker answers ... and save me and others a lot of wasted typing.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 10/13/2005 3:10:30 AM GMT
Instead of, "Lets say I have data coming from an external source, such as a Parallal port or dip switches," why not just say exactly what you want to hook up to the RA port and then see if people can help you. Waving your hands around the problem isn't making it any clearer for those trying to assist you. If you have a parallel port, then provide details such as :
What the device is that actually has the port in question. Is it a PC? An embedded processor?
What handshake mechanism there is to let the SX know that data is ready and that a nibble has been retrieved.
If it's not a parallel port, then don't mention parallel ports at all. If it's a DIP switch, then say it's a DIP switch. In that case, you still need to tell folks what sort of handshake or sync mechanism there is to let the SX know that data is ready.
If there is no mechanism, then all you are trying to do is read an 8 bit DIP switch with a 4 bit port. In that case you're going to need some external hardware to assist. Period. There is no magic to getting a four bit port to read an eight bit DIP swtich - it's just a matter of buffering the data and presenting it as a pair of nibbles, but you stil need an extra bit or two to load the buffer and switch between nibbles.
Thanks, PeterM
This may work. Tell the SX to capture the first nibble, wait a bit, then recieve the next. That way the computer parallel port is responsible for sending the second signal within the wait period. That way, if the second signal is the same, the comp will not change it. The microcontroller goes into a loop. When it is time to recieve another signal, a break code is given (lets say the number 2). After the parallel port puts 2 out, the loop is broken, a delay is given to give the computer time to change from the number 2 to the next nibble. Here is the general code, dont mind any errors, just approximation.
tris_b = 0
nib1 var byte
nib2 var byte
result var byte
start:
do 'signal microcontroller to begin recieving first nibble
pause 25
loop until ra = 2
pause 100 ' wait for PC to change signals from 2 to whatever
nib1 = ra
pause 100 ' wait for PC to change signals to nibble 2 value
nib2 = ra
'compute result
result = nib1 + nib2
rb = result
pause 100 ' give computer time to change from whatever to zero, this prevents an unwanted
'loop break if ra is still 2 (nibble 2 is 2)
goto start:
· theByte = RA················' get high·nibble
· SWAP theByte················' move to high position·
· PAUSE NibDelay··············' wait for low nibble
· temp = RA···················' read the low nible
· theByte = theByte | temp··· ' assemble with high
But as Peter pointed out, without some kind of specific notification from the external device that the nibble has changed you could end up with errors.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Perhaps you've seen worse descriptions, but that does not excuse you of helping the forum out with a better effort.
Believe me, the amount of other people's effort will bear a direct relationship to the amount you are putting in yourself.
As for me, I can't be bothered.
Good luck.
Peter (pjv)
You could send two data bits at a time, and use a third bit as clock, or a "data ready" bit. The SX would wait for the clock bit to go high. On the first transition to high, then data is ready. The SX would shift its result byte left twice, then read the two data bits and OR them into the result byte. Then wait for the clock bit to go low. Do this four times and the SX will then have a complete byte.
You might need a way to synchronize the start of each byte transmission, depending on your specific application. I've done something like this where I used time for synchronization - once the SX receives a clock, it expects the next three clocks to arrive within milliseconds, otherwise it resets.
David
Doing this has a set of problems because you need to be able to distinguish between the timing nibbles and the data nibbles which may be identical, the only method I can think of is having two timing nibbles, a stop nibble and a start nibble.
The stop nibble will be the·NOT of the last data nibble, the start nibble could be any value, but lets assume it is 0.
You will look for the transition of the last data nibble of the previous frame to the stop nibble by looking for the transition from a value to to its inverse, then you time the amount of time that passes till the nibble turns to 0 (let's call that value t), now the first data nibble of the frame could also be a 0, but that doesn't matter because you already got your time per nibble.
So you wait a value of t+t/2 from the end of the stop nibble, this places you in the middle of the first data nibble, you read that value into a buffer byte·then·SWAP it to the high nibble of·the buffer. Then you wait an additional time t, and now your in the middle of the second data nibble, you OR port a into your buffer, and now the data is in your buffer byte. At some time less than t/2 later you enter a tight loop looking for the next stop nibble and start the whole process over.
Now when the·communication first starts you may end up out of sync if the·first data nibble is the inverse of the start nibble or the second data nibble is the inverse of the first data nibble, but the first data sequence this isn't the case you will be synced and stay synced.· If you make the start nibble the same as the last data nibble of the previous frame (NOT(NOT(last data nibble)) you can reduce the likelyhood of syncing to the wrong portion of the data because your looking for a "nibble NOT(nibble) nibble" sequence which will occur only rarely when it isn't the real McCoy.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
Post Edited (Paul Baker) : 10/13/2005 4:07:39 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
A friend of mine was a street bum for halloween.