View previous topic :: View next topic |
Author |
Message |
dcorrick
Joined: 22 Apr 2005 Posts: 5
|
Posted: Wed May 11, 2005 11:06 pm Post subject: TLC5615 DAC interface using Software SPI |
|
|
I am attempting to interface a TLC5615 DAC using a Software SPI connection to a Mega8 processor. Problem is that the software SPI sends data LSB first, and I need to send it MSB first. If there any easy way to convert the two bytes, maybe using an EEprom look-up table so the data will be in the right order? Or is there an easy way to make the SPI connection send MSB first? Any Ideas would be appreciated.
Thanks |
|
Back to top |
|
|
rredding
Joined: 25 Mar 2005 Posts: 63 Location: Tacoma WA
|
Posted: Thu May 12, 2005 2:42 am Post subject: |
|
|
Maybe try this. It consumes lots of clock cycles so it will work if you are not in a hurry. I have not tested it so check my syntax. The eeprom method uses far less clock cycles and
I have used that method before.
'Yourdata holds the original data
'Backwards will hold the flipped data
Dim N As Byte
Dim Yourdata As Byte
Dim Backwards As Byte
Dim Position As Byte
Backwards = 0
For N = 0 To 7
Position = 7 - N
If Yourdata.n = 1 Then
Backwards = Bits(position)
End If
Next |
|
Back to top |
|
|
rredding
Joined: 25 Mar 2005 Posts: 63 Location: Tacoma WA
|
Posted: Thu May 12, 2005 2:51 am Post subject: |
|
|
OOPS! That code doesn't work!
Just goes to show, don't post til I've tested!
Backwards needs to be OR'ed with the newly set bit position
Dim Temp As Byte
'Yourdata holds the original data
'Backwards will hold the flipped data
Dim N As Byte
Dim Yourdata As Byte
Dim Backwards As Byte
Dim Position As Byte
Dim Temp As Byte
Backwards = 0
For N = 0 To 7
Position = 7 - N
If Yourdata.n = 1 Then
Temp = Bits(position)
Backwards = Backwards OR Temp
End If
Next
Hope I'm actually helping here.....be warned, I still have not tested it |
|
Back to top |
|
|
rredding
Joined: 25 Mar 2005 Posts: 63 Location: Tacoma WA
|
Posted: Thu May 12, 2005 3:35 pm Post subject: |
|
|
Yup, me again. Have you looked at the "SHIFTOUT" instruction?
That gives the MSB / LSB first option. |
|
Back to top |
|
|
dcorrick
Joined: 22 Apr 2005 Posts: 5
|
Posted: Thu May 12, 2005 11:22 pm Post subject: |
|
|
Thanks for all of your suggestions. I tried the Shiftout command last week, and I couldn't get it to work. I took your original suggestion, and put it in a For Next loop, starting at 0 to 255, and generated the numbers in reverse, and stored them in EEprom, which then makes for a fast lookup. I got it working this afternoon. Thanks again for your help. In case someone else might run into this problem, I will post the program here.
$regfile = "m8def.dat"
'tell the compiler which XTAL was used
$crystal = 4000000
Dim Temp As Byte
'Yourdata holds the original data
'Backwards will hold the flipped data
Dim E As Byte
Dim N As Byte
Dim Yourdata As Byte
Dim B As Byte
Dim C As Byte
Dim A As Byte
Dim Position As Byte
Open "comc.0:38400,8,n,1,inverted" For Output As #2
For E = $00 To $ff
Yourdata = E
B = 0
Temp = 0
For N = 0 To 7
Shift Temp , Left , 1
Position = 7 - N
If Yourdata.n = 1 Then
Temp = Temp Or $01
End If
Next N
B = Temp
Writeeeprom B , E
Readeeprom A , E
Print #2 , "E" ; E ; " NUMBER " ; A
Next E
Close #2
End 'end program |
|
Back to top |
|
|
philm
Joined: 14 Jul 2004 Posts: 138 Location: Australia
|
Posted: Fri May 13, 2005 2:01 am Post subject: |
|
|
Try Code: | dim in_byte As Byte
dim out_byte as byte
declare function swap_bits(inbyte as byte) as byte
do
input "byte ", in_byte
print "Binary "; bin(in_byte)
out_byte = swap_bits (in_byte)
print "Swapped Binary "; bin(out_byte)
loop
function swap_bits(inbyte as byte) as byte
local in_cnt as byte, out_cnt as byte
swap_bits = 0 'by default = 0
out_cnt = 7 'Out bit counter
for in_cnt = 0 to 7 'In bit counter
if inbyte.in_cnt = 1 then set swap_bits.out_cnt
decr out_cnt 'Out bit counts backwards
next in_cnt 'In bit counter counts forwards
end function |
But a better solution is to use the harware SPI |
|
Back to top |
|
|
|