Shop OBEX P1 Docs P2 Docs Learn Events
Working with numbers larger than 255 — Parallax Forums

Working with numbers larger than 255

kingnebkingneb Posts: 65
edited 2005-10-09 00:15 in General Discussion
I want to read a nibble (4 bits on the ra channel)·off the RA·channel on the sx28.· I want to multiply that nibble value by 100.· I want to also subtract that value from 1000.· I know there is a trick for this type of operation.· how do you do it?

Thanks,
kingneb·

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-07 21:48
    Hello,

    ·· There are plenty of Math routines on www.sxlist.com for doing up to 32-bit math I believe.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • kingnebkingneb Posts: 65
    edited 2005-10-07 23:26
    You can do it by using two bytes to store the 16 bit value.· The arethmetic is not hard at all.· What I want to do is this.

    1.· Read a nibble

    2.· Multiply that nibble by 100 (this is for pulse width modulation, calculating the high delay and the low delay) to get the high duration.

    3.· Subtract the nibble by 1000 to get the low duration

    Here is the Picbasic code:

    high_dur var word
    low_dur var word
     
    here:
     
    high_dur = ra * 100 'multiply the ra channel by 100 to get high duration
    low_dur = 1000 - high_dur 'get low duration
     
    high rb.0
    pause_us high_dur
    low rb.0
    pause_us low_dur
     
    goto here
    

    Thanks, kingneb
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-10-08 14:15
    Im gathering that the nibble is BCD because your equations produce a negative value for values > 10. I think there is a simpler method of doing the delays than figuring out the number of cycles for high and low periods. Since you are only dealing in multiples of 100 cycles, why not have a delay of 100 cycles (actually a little less to account for the overhead of doing this method) then iteratively loop for how many units of 100 cycle delays?

    for the high duration you iterate ra 100 cycle delays,
    for the low duration you iterate 10-ra 100 cycle delays (by doing 'mov w,#10' followed by 'sub w,ra')

    though you should probably have ra buffered in the event that ra is changed externally between the use of ra when calculating the high duration and the low duration.

    Use Gunther's·link in your other post to generate the code for the 100 cycle delay.

    The nice thing about this method is you keep all the math as 8 bit values, reducing computational overhead.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 10/8/2005 2:18:00 PM GMT
  • kingnebkingneb Posts: 65
    edited 2005-10-09 00:15
    The nibble is read off PC a parallel port. There are·11 posible settings (speeds) for the PWM. Zero means off and Ten means full speed. This is the code generated for a 100 cycle delay. I am puzzled because I cannot see how to calculate the values that go into a loop like this.

    d1 DS 1

    ;98 cycles
    mov w, #$19
    mov d1, w
    Delay_0:
    decsz d1
    jmp Delay_0

    ;2 cycles
    nop
    nop

    Post Edited (kingneb) : 10/9/2005 12:18:18 AM GMT
Sign In or Register to comment.