Bit conversion


Closed Thread
Results 1 to 12 of 12

Thread: Bit conversion

  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default Bit conversion

    HI,

    i am geting error in compiling my program. I think its because I am doing a subtracion with a binary number and a decimal number

    C:\PBP>pbp -p16f876a lcd3
    PicBasic Pro Compiler 2.46, (c) 1998, 2005 microEngineering Labs, Inc.
    All Rights Reserved.
    PM Assembler 4.07, Copyright (c) 1995, 2004 microEngineering Labs, Inc.
    Error PBPPIC14.LIB 2048 : [202] Illegal Character 'æ'
    Error PBPPIC14.LIB 2064 : [202] Illegal Character 'æ'
    *** 2 Errors ***

    here is the code:

    if right > left Then 'left and right are from sensor reading
    temp =right-left
    temp= temp/right
    temp= temp * 254
    HPWM 1,255-temp,1000
    HPWM 2,255,1000

    k

  2. #2
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    my question is how do i convert my binary value to a decimal so i can work with it.

    k

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Hi lerameur,

    In your code piece here, first you are missing ENDIF. For now say it is there, then;

    Your variables are all decimal already.


    Also, say that Right = 5 and Left = 2.

    Then,

    IF Right > Left statement will be executed; then

    Temp = Right - Left
    (Temp = 3)
    Temp = Temp/Right
    (Temp = 3/5)
    (Temp = 0)
    Temp = Temp*254
    (Temp = 0)


    In this case, you will get TEMP as zero, because you will always be dividing a small number by a bigger number.


    Also, if you run your code alone, it will not give you any errors.

    Code:
    IF Right > Left THEN 'left and right are from sensor reading
        Temp =Right-Left
        Temp= Temp/Right
        Temp= Temp * 254
        HPWM 1,255-temp,1000
        HPWM 2,255,1000 
    ENDIF
    
    'No errors !!


    Thus, you should check the rest of your code. It seems that you have no problem with your IF statement.

    -------------------------------
    Last edited by sayzer; - 4th November 2006 at 16:22.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    ok but how do i get the 0.6 from 3/5
    maybe (3/5)*254 ?
    i do not want zero

    also,

    I removed these lines and it now compiles:
    I thought I needed them...

    'DEFINE CCP1_REG PORTC ‘ Hpwm 1 pin port, RIGHT
    'DEFINE CCP1_BIT 2 ‘ Hpwm 1 pin bit
    'DEFINE CCP2_REG PORTC ‘ Hpwm 2 pin port, LEFT
    'DEFINE CCP2_BIT 1 ‘ Hpwm 2 pin bit
    'DEFINE HPWM1_TIMER 1
    'DEFINE HPWM2_TIMER 1

    k

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    why not 30/5 ?

    OR (or i'm right) 3//5?

    'DEFINE CCP1_REG PORTC ‘ Hpwm 1 pin port, RIGHT
    'DEFINE CCP1_BIT 2 ‘ Hpwm 1 pin bit
    'DEFINE CCP2_REG PORTC ‘ Hpwm 2 pin port, LEFT
    'DEFINE CCP2_BIT 1 ‘ Hpwm 2 pin bit
    There's a Huge difference between a apostroph ' and the other thing
    Last edited by mister_e; - 4th November 2006 at 18:25.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Floating point is not supported by PBP lerameur. Thus you can not use parenthesis; still zero.

    There are still ways to go around it though, take mister_e's example.

    Multiplying by 10 could work but
    if the difference is too big then need to multiply by 100.
    and then if the difference is very big, word size variable may be, then multiply by 1000 shall work.

    Ex:
    Right = 240 and Left = 235;
    then Temp=5.
    Now, 5/240 or 50/240 will not work.
    You will need to multiply by 100 and have 500/240.

    Thus you need to multiply temp by 10 or 100 or 1000 based on your values.



    -----------------------------
    -------------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  7. #7
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Argg

    ''''''

    thanks
    Mister_e

  8. #8
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    PBP do not take signed intergers

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Lerameur,

    Multiplications with negative numbers works but if you need division you need to do a little "manual" work:
    Code:
    Sign VAR Word       'This will hold the sign of our value.
    Left VAR Word
    Right VAR Word
    Temp VAR Word
    
    Left = 100
    Right = 200
    
    Temp = Left - Right     'Temp is now -100 (or 65436)
    
    If Temp.15 = 1 then    'Highest bit set means value is negative
       Sign = -1
    Else
       Sign = 1
    Endif
    
    'We cant divide a negative number so we have to use the ABS operator.
    Temp = ABS(Temp) / 2    'Temp is now 50 (positve)
    
    Temp = Temp * Sign       'Re-apply sign, Temp is now -50 (or 65486)
    /Henrik Olsson.

  10. #10
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    hi Henrik,

    I am not sure how lerameur will implement this information into his problem.


    ---------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  11. #11
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Sorry...

    Hi Sayzer,
    He said PBP don't 'take' signed integers and I showed him a way to get around it. I guess I didn't understand the problem, still don't then....

    Sorry if I made more confusing.

    /Henrik Olsson.

  12. #12
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    NP Henrik,

    His problem is with floating point issue that PBP does not support.

    As you said he still needs to do some manual work.



    He will take care of his problem with a few more code lines.
    -----------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Doubt with interrupt on change
    By lugo.p in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th March 2010, 15:22
  3. Sleep Mode
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th March 2008, 10:31
  4. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 00:55
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts