Can't multiply by 2!


Closed Thread
Results 1 to 3 of 3
  1. #1
    jswayze's Avatar
    jswayze Guest

    Default Can't multiply by 2!

    I'm at a loss, and I think I need some edumacation on pbp's math system.

    I'm trying to do a simple ADC voltage reading. The voltage source is a ~8 volt battery, so I made a voltage divider. With my meter I read 4.1 after the divider (which is the input to the PIC.)

    Here is my code (the important parts):
    Code:
        Quanta con 1274 
    
        ADCIN 0, ADval ' Read A/D on RA0
        ADval = (ADval*10) */ Quanta
        ADval1 = ADval * 2
        
        SEROUT2 PORTB.1,Baud,[dec ADval dig 4,".",dec4 ADval," Vdc",$0D,$0A]
        SEROUT2 PORTB.1,Baud,[dec ADval1 dig 4,".",dec4 ADval1," Vdc ADVal1",$0D,$0A]
    My results are:
    Code:
    4.1056 Vdc
    1.6576 Vdc ADVal1
    Obviously I'm doing something right as I'm getting my 4.1 volt measurement. My problem is trying to multiply it by 2 (as you can see...)

    I also tried ADval1=ADval + ADval and ADval=ADval << 2, both resulting in wierd numbers.

    I suppose I'm just not getting the whole floating point thing with pbp. Can someone enlighten me?

    Thanks,

    Jeff

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    4.1056 Vdc is actually 41,056.

    41,056 * 2 = 82,112 which is larger than a word sized variable can hold.

    82,112 - 65536 = 16,576 which is why you're seeing 1.6576 returned in AdVal1.

    Try something like ADval1 = (ADval/10) * 2, and you should be back in the ball game.

    This will return 8,211 so --

    SEROUT2 PORTB.1,Baud,[dec ADval1 dig 3,".",dec3 ADval1," Vdc ADVal1",$0D,$0A]
    Last edited by Bruce; - 29th October 2004 at 08:09.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    jswayze's Avatar
    jswayze Guest


    Did you find this post helpful? Yes | No

    Default

    Ahhh... that helps greatly. Thanks!!!

    Jeff

Similar Threads

  1. How to drive the Vinculum VDIP1 in UART mode
    By BrianT in forum Code Examples
    Replies: 41
    Last Post: - 23rd May 2013, 12:34
  2. Replies: 4
    Last Post: - 15th April 2009, 01:54
  3. USB-FTDI[UM232R] with PIC16f877a
    By bjox in forum USB
    Replies: 1
    Last Post: - 23rd February 2008, 22:40
  4. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  5. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27

Members who have read this thread : 0

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