![]() |
Forum Index : Microcontroller and PC projects : MSB to LSB conversion ?
Author | Message | ||||
hitsware Guru ![]() Joined: 23/11/2012 Location: United StatesPosts: 535 |
ColorMaximite 4.5 I've bought some of these They work well (as far as a signal out) BUT ! Now I see they want LSB first as opposed to MSB first as MMBasic SPI puts out . Any simple way to convert ? Or am I missing (hopefully :) ) some setting ? |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6220 |
Would this help main:
INPUT x PRINT bitreverse(x, 16) GOTO main FUNCTION bitreverse(num, k) ' reverse bit order ' num = integer to reverse ' k = number of bits (8 or 16) LOCAL n FOR n = 1 TO k IF (num AND 2^(n-1)) > 0 THEN bitreverse =bitreverse+ 2^(k-n) ENDIF NEXT n END FUNCTION It works for 8 or 16 bit numbers (or anything that MMBasic handles as integers) If you have it set to 8 bits and enter a number greater than 255, the result will be strange. Jim VK7JH MMedit |
||||
hitsware Guru ![]() Joined: 23/11/2012 Location: United StatesPosts: 535 |
Ya Mon ![]() Too much beer right now to tell for sure, but that should do it ! Beautifull ![]() |
||||
hitsware Guru ![]() Joined: 23/11/2012 Location: United StatesPosts: 535 |
Thank You TassyJim ! I owe you a beer down by the billybong :) clk=83886 ' pwm to clk in on synth inp=11000 ' SPI to control on synth SetPin 5,2 ' rx for SPI (not used) For p=1 To 3: SetPin p,8: Next p Pin(1)=1 ' data latch playnote: If KeyDown=0 Then GoTo playnote If KeyDown=128 Then clk=clk*1.001 If KeyDown=129 Then clk=clk/1.001 If KeyDown=130 Then inp=inp/1.001 If KeyDown=131 Then inp=inp*1.001 PWM clk,50,50 Pin(1)=0 A= SPI( 5,3,2,bitrev(inp),L,3,16 ) Pin(1)=1 Cls Print " clk =";clk Print " inp =";inp Print " bitrev =";bitrev(inp) Print " freq =";(inp*clk)/(2^23) GoTo playnote End Function bitrev(inp) For n=1 To 16 If (inp And 2^(n-1)) > 0 Then bitrev = bitrev + 2^(16-n) EndIf Next n End Function |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |