1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Bit rotation

Discussion in 'Oshonsoft' started by SwingeyP, Feb 10, 2014.

  1. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Hello,

    Still working on the AD9850 and now have it working with a fixed frequency.

    I want to break the fout variable into 4 8 bit words.

    My thinking is something like this, (however it doesn't compile) is this the correct way to go about this?

    ad9850_freq = ad9850_mhz + ad9850_khz + hz
    fout = ad9850_freq * 34.3597383 '7 makes it too long!
    'Ok so now we have the frequency we need to break it down to 4 8 bit words.
    For x = 1 To 4
    shifted = 0
    For i = 0 To 7
    shifted = shifted + ShiftRight(fout)
    word_array(x) = shifted
    Next i
    Next x





    Cheers - Paul
     
  2. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    fout is a single. It seems you can't shift a single. fout is only a single because I have to multiply it by 34.3597383
     
  3. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Paul,
    Look at the HW , LW, LB and HB extensions in Oshon

    Dim First as LONG

    Dim Second as WORD
    Dim Third as WORD

    Dim Fourth as BYTE
    Dim Fifth as BYTE
    Dim Sixth as BYTE
    Dim Seven as BYTE

    Second = First.HW
    Third = First.LW

    Forth = Second.HB
    Fifth = Second.LB
    Sixth = Third.HB
    Seventh = Third.LB

    Do you follow that OK.?
    It may work for Single, I cannot confirm that, I dont have FP package

    Eric
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE

    hi,
    This is an example of a USB program, note the ASM: used to shift the Bit pattern.
    Thats another option you could try.

    Code (text):

    'chan0
    SPICSOn
    PORTC.6 = 0
    SPISendBits 8, %00000011
    SPIReceive ch0hi
    SPIReceive ch0lo
    PORTC.6 = 1
    SPICSOff

    ASM:        rrcf ch0hi,F
    ASM:        rrcf ch0lo,F
    ASM:        movlw 0x0f
    ASM:        andwf ch0hi,F

    temp0 = (ch0hi * 256) + ch0lo  'one sample result
    avg0 = avg0 + temp0  'N samples

    Next smpcnt

    temp0lw = avg0.LW
    temp0hw = avg0.HW

    PORTB.2 = 0  'low for 50uSec, high for
    PORTB.3 = 1
    UsbService
    PORTB.3 = 0  'testing only 'scope 15uSec High period

     
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Paul,
    Lets know if you need help with the ASM structure.

    BTW: so that I can offer you better help I have ordered from Vlad the full FP upgraded packages, I should be running it by Wednesday

    E
     
  7. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Hi Eric,

    I bought the full package a while ago when it was on 'special offer' I have all the bits & bobs. I must say I love it. It has really helped me a lot. It just needed a better manual which you sent me. (I should read it properly some time). I had just got to .hb .lb etc when you replied.



    I see the way that yo have done it and it makes sense. I'll give it a try now. Much simpler than loops and arrays all over the place :)

    Cheers - Paul
     
  8. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Ok this is weird.

    fout calculates to 243954128 = 1110 10001010 01110001 11010000

    110 = 14 = E
    10001010 = 138 = 8A
    01110001 = 113 = 71
    11010000 = 208 = D0

    I tried your code and also .3b .4b which I also found in the manual - They both give the same results but I don't understand the results in the watch window.....


    Code (text):
    ad9850_freq = ad9850_mhz + ad9850_khz + hz
    fout = ad9850_freq * 34.3597383  '7 makes it too long!
    'Ok so now we have the frequency we need to break it down to 4 8 bit words.
           
    word1 = fout.LB
    word2 = fout.HB
    word3 = fout.3B
    word4 = fout.4B


    Dim second As Word
    Dim third As Word

    Dim fourth As Byte
    Dim fifth As Byte
    Dim sixth As Byte
    Dim seventh As Byte

    second = fout.HW
    third = fout.LW

    fourth = second.HB
    fifth = second.LB
    sixth = third.HB
    seventh = third.LB
     
    I would have expected to see the values above NOT those as shown in the Watch Variables window.

    BTW I did have that other problem you mentioned with variables not counting properly....

    Cheers - Paul
     

    Attached Files:

  9. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    try this code as a long perfect as a single weird values. Why?

    Code (text):
    Dim fout As Single  'try changing this to a long

    Dim word1 As Byte
    Dim word2 As Byte
    Dim word3 As Byte
    Dim word4 As Byte


    Dim second As Word
    Dim third As Word

    Dim fourth As Byte
    Dim fifth As Byte
    Dim sixth As Byte
    Dim seventh As Byte

    fout = 243954128
    loop:
    word1 = fout.LB
    word2 = fout.HB
    word3 = fout.3B
    word4 = fout.4B

    second = fout.HW
    third = fout.LW

    fourth = second.HB
    fifth = second.LB
    sixth = third.HB
    seventh = third.LB
    Goto loop
     
    This one's a bit of a show stopper :-( If those values as a single are true :-(
     
  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Hi,
    Have now installed and running all latest Oshonsoft packages including FP.
    The Scope also now works!!
    If you could post your very latest code listing I will try it.

    Give me a while to get the hang of the FP etc and I will get back to you

    E
     
  11. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Tried your code as in post #8.

    For a Single, it appears that number is not stored in Binary format, but some other format as you would expect for a FP number.

    The program does break in down into Bytes correctly,but they are not what I would expect.

    I will keep looking at options.
    E
     
  12. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Thanks Eric. Just upgraded to 6.97 and seeing the same thing as you. I tried outputting word1 on a port just to see what the actual output was. It does differ as per the watch variables :-(
    I guess what I need is a long I can multiply by a single :) or perhaps some clever way of converting the single to a long.

    Almost there so close ......

    Thanks for the help and getting the F/P package.

    Cheers - Paul
     
  13. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    Can you explain why you would want to reduce the SINGLE to BYTES.?

    If you could what would be the next step in your program.?
    E
     
  14. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Hi Eric,

    Here's the subroutine. I have commented it for you.

    The fout has to be multiplied by 34.35973837 (I miss the last digit off) hence the single for fout. This number is then broken down to 4 words for the AD9850.

    Code (text):
    convert_freq:

    ad9850_mhz = mhz * 1000000
    ad9850_khz = khz * 1000

    'To set the frequency, the 32 tuning bits are set As:
    '(Frequency x 2^32)/DDS Clock Frequency
    'Where the DDS has a 125Mhz clock, this resolves To
    'Bits = Output Frequency x 34.35973837
    'So To set the Output of the DDS To 7.000Mhz, the tuning bits must
    'be set To 7000000 x 34.35973837 = 240518168 Or E560418 in Hex.

    ad9850_freq = ad9850_mhz + ad9850_khz + hz
    fout = ad9850_freq * 34.3597383  '7 makes it too long!
    'Ok so now we have the frequency we need to break it down to 4 8 bit words.
           
    word1 = fout.LB
    word2 = fout.HB
    word3 = fout.3B
    word4 = fout.4B


    'http://designtools.analog.com/dt/dds/ad9850.html
    'This util gives each word as for freq 7.1mhz @ 125 clk
    'W0 - 80 - 10000000 - 128
    'W1 - E  - 00001110 - 14
    'W2 - 8A - 10001010 - 138
    'W3 - 71 - 01110001 - 113
    'W4 - DE - 11011110 - 222
    Return
    I hope all that makes sense. Word 0 is handled elsewhere and just sets the phase.

    Cheers - Paul
     
  15. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
  16. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Hi Eric.

    Wow that looks complicated. Takes me right back to college days and I don't think I really understood it then.
    I'll have a go at writing a function but this might take longer than the whole process so far - :(

    I'll see what I can do.

    Here's the progress so far with the error, but at least now I know what it is.



    Cheers - Paul
     
  17. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Paul,
    I will look at the EEE 745 conversion today, in the meantime if you download the free version of the Z80 IDE from Oshonsoft its got a EEE745 convert utility that may help you understand the format.
    E
     
  18. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi
    More details....
    I will try more sample values later, but it looks as though you may have to contact Vladimir.?
    E

    Code (text):

    Dim word1 As Long
    Dim word2 As Long

    Dim byte1 As Byte @ 0x60
    Dim byte2 As Byte @ 0x61
    Dim byte3 As Byte @ 0x62
    Dim byte4 As Byte @ 0x63

    'test values in decimal and hex from convert utility

    fout = 243954128  '= 4d 68 a7 1d
    '1 = 3f 80 00 00
    '10 = 41 20 00 00
    '100 = 42 c8 00 00
    '1000= 44 7a 00 00
    '10,000= 46 1c 40 00
    '100,000= 47 c3 50 00
    '1,000,000=49 74 24 00
    '10,000,000= 4b 18 96 80
    '100,000,000=4c be bc 20

    '243954128  = on the Z80 IEEE 754 converter = 4D 68 A7 1D

    '243954128  = hex On a calculator = E8A71D0...  NOTE No SIGN Bit!!!
    'Adding the SIGN Bit gives =  4D 68 A7 1D
    '0100,,1101,,0110,,1000,,1010,,0111,,0001,,1101
    'So the above agree

    'But
    '243954128  =on Watch Window =  9A 68 A7 1D
    'As Bit Pattern =1 001,,1010,,0110,,1000,,1010,,0111,,0001,,1101
    'as Hex= ..................9A.......68......A7...1D

    '1001,,1010,,,,,,,0110,,1000,,1010,,0111,,0001,,1101
    'first Byte appears incorrect.???


    loop1:
    fout = 243954128

    word1 = fout.HW
    word2 = fout.LW

    byte1 = word1.HB
    byte2 = word1.LB

    byte3 = word2.HB
    byte4 = word2.LB

    Goto loop1
     
     
  19. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Hi Eric,

    There seem to be a couple of issues here.

    Firstly, a potential bug in the way the IEEE 754 calculations are done is presenting as you say the first byte incorrectly.
    I did a some calculations on paper last night and after a lot of confusion used an online converter to conclude what you have.

    Secondly, if it is a bug and it gets fixed, I'll still need some conversion to turnit back into the unsigned number. This looks beyond my ability.
    Is there another way I could code the multiply of the single? --> fout = ad9850_freq * 34.3597383 maybe keeping everything as LONGs and working through unit, tens, etc etc?

    Cheers - Paul
     
  20. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    I have spent some time trying to see how Vlad has modified the IEEE745 standard.:banghead:

    fout = ad9850_freq * 34.3597383

    Whats the numeric range ad9850-freq... min max.??

    E
     
  21. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Well it could be 1 (as in 1 hz) to 30,000,000 (as in 30Mhz).

    mhz = 7
    khz = 1
    hz = 0

    I do this to set the initial frequency at 7.1 mhz. However when I expand the project to add a band switch it cold be 1 to 30 as described above.
    At present the project is covering the amateur 40m band 7 -7.2 mhz. I start off at 7.1 as it's in the middle of the band.

    ad9850_mhz = mhz * 1000000
    ad9850_khz = khz * 1000

    ad9850_freq = ad9850_mhz + ad9850_khz + hz
    fout = ad9850_freq * 34.3597383
    I hope this makes sense. I'll post the comlete running code when I get home later today.

    Cheers - Paul
     

Share This Page