PicBasicīs Math


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2003
    Location
    Sao Paulo - Brazil
    Posts
    92

    Question PicBasicīs Math

    Hi friends,

    after some problems, I have (finally), my Carīs speedometer working fine (thanks Tim Box for your tips).

    But, I have another problem related to PicBasicīs Math.

    When I finish the trip cruise, I have to calculate the average speed.

    I have the distance in hundreds of meters and the time traveled in seconds.

    Iīm using this formula :

    VM = ((Dist * 36)/Time) * 10


    But, with this method I won't be able to calculate average speed for distances greater than 182 Km (or 1820 hundreds of meters)
    because the Dist*36 will be greater than 65535.

    Does anyone know how to handle this problem ?

    tahnk you

    Sérgio (Brazil)

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


    Did you find this post helpful? Yes | No

    Default

    VM = ((Dist * 36)/Time) * 10

    No problem... your calaculation equates to...

    VM=Dist *360/Time

    So just write it as...

    TempB=360
    TempA=Dist * TempB
    VM=DIV32 Time

    where all the variables used are words...

    Dist var Word
    TempA var word
    TempB var Word
    Time var Word
    VM var Word

    The Dist * TempB produces a 32 bit answer which you cannot directly access, but by running immediately the DIV32 you return a 16-bit answer. See section 4.17.8 in the PBP manual.

    Melanie

  3. #3
    Join Date
    Nov 2003
    Location
    Sao Paulo - Brazil
    Posts
    92


    Did you find this post helpful? Yes | No

    Smile

    Thhank you Melanie,

    Iīt's working fine now.

    As the manual says, I had to disable the interrupts to get it working fine.
    With the interrupts enabled sometimes I just read 65535 as a result.

    May I ask something else ?

    I'm using Timer0 and Timer1 to count my pulses in this same project.
    I would like to move to a 16F877 and get access to some analog ports.

    My doubt is : Can I use ADCIN with interrupts or should I disable them to read the analog port ?
    The PBP manual donīt say I need to disable interrupts on 16F87x series. Is it right ?

    Thank you again .

    Sérgio

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    AD conversion on all PIC's is done in hardware completely independently. You can have interrupts shooting off all over the place without worry.

    At the end of the AD conversion, you can either check the DONE flag to see your result is ready to be picked up, or have the AD generate an interrupt for you to tell you it's done, or have it all done for you by ADCIN.

    Personally, I find ADCIN a handicap especially if you have interrupts you don't want to miss. It's easier to access the registers and do all the ADC work directly. If you can't figure it for yourself, come back to me and I'll show you how.

    Melanie

  5. #5
    Join Date
    Nov 2003
    Location
    Sao Paulo - Brazil
    Posts
    92


    Did you find this post helpful? Yes | No

    Question

    Hi Melanie,

    some months ago you told me that we can access the ADC result via Registers instead of using the ADCIN command.

    Now, the first version of my on-board computer is working very fine. But I would like to improve it with some new functions, like Gas consumption (is this word correct ?), Battery status and Lambda sensor.

    Could you explain me how can I read those adc channels without ADCIN command ?

    Thank you very much !

    Regards.

    Sérgio

  6. #6
    carl_schell's Avatar
    carl_schell Guest


    Did you find this post helpful? Yes | No

    Arrow PicBasicīs Math

    Sérgio:

    In response to:

    "Could you explain me how can I read those adc channels without ADCIN command ?

    I know you asked Melanie, but thought I'd chime in and give you a hand. Plus it is good practice for me. Hope it helps:

    *****The code is attached*******

    I tried both versions and got the same A to D result on my thermistor connected to AN0.

    Best of luck to you my friend,

    Carl
    Novi, Michigan, USA
    www.schellelectronics.com
    Attached Files Attached Files
    Last edited by carl_schell; - 22nd July 2004 at 07:40.

  7. #7
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    To use PBP but without ADCIN, you first have to set-up your ADC Registers... I'll walk you through this with your PIC16F877, but for any PIC the proceedure is essentially the same...

    1. Get the Datasheet

    Check what Registers are used for ADC... ADCON0, ADCON1, ADRESH, ADRESL. The last two are just for reading the results.

    2. Decide 8-Bit mode or 10-bit mode?

    If you want 8-bits, then the result is going to be LEFT justified, and read from ADRESH only. The lower bits in ADRESL are ignored. If you want 16-bits then the result is going to be in both registers. You want RIGHT justification and ADRESH becomes the Highbyte of your resultant word, and ADRESL is the Lowbyte.

    3. Set-Up the Registers.

    First set-up which ADC's you're going to be using, what voltage reference, 8 or 16 bit mode, and TRIS bits. Let's say we're only going to use AN0/RA0 and everything else is Digital with our Voltage Reference being Vdd and Vss. Let's also say we're going to use 10-bit mode... (check Datasheet 11.2 to see what I've done)...

    TRISA=%11111111
    TRISE=%11111111
    ADCON1=%10001110

    Now I've set TRISA and TRISE both to INPUT's, but they can be INPUT or OUTPUT however you wish. Only Bit 0 of TRISA must be an INPUT (which is the ADC pin we're playing with).

    We also need a variable to play with... since we're going to use 10-bit ADC, we'll need a Word...

    MyWord var WORD

    Next we configure the remaining Bits of ADCON0... Here we decide on the clock source for the ADC, the channel we're going to use, and we're going to switch-on the ADC so it's all ready for when we need it... This actually can all be done at Read-time... so straight into our ReadADC routine...

    ADCON0=%01000001
    ' Set Fosc, Select Channel, Turn-On A/D - Check Datasheet 11.1
    Pauseus 50
    ' Wait for channel to setup
    ADCON0.2 = 1
    ' Start conversion
    While ADCON0.2=1:Wend
    ' Wait for conversion
    MyWord.Highbyte=ADRESH
    MyWord.Lowbyte=ADRESL
    ' Read result from ADC into variable

    There's a few alternatives you can try... if you switch-on the ADC when you initialise your PIC, you can dispose of the 50uS pause and jump straight into the read routine by setting the ADCON0.2 start Flag. That might just save you a few uS at each conversion if you really need them. However, if you're switching between multiple inputs (rather than fixed at just one input as in our example), it's better to keep the 50uS delay to allow everything to settle and the sampling capacitor time to charge to the new input value.

    So, you can see there are very few lines of actual code. Set the registers up, start the conversion by setting the ADCON0.2 Flag, and when the flag drops you pick up yor result. It really doesn't get any easier than this.

    Melanie

  8. #8
    Join Date
    Nov 2003
    Location
    Sao Paulo - Brazil
    Posts
    92


    Did you find this post helpful? Yes | No

    Default

    Hi carl_schell and Melanie,

    thank you very much for your replies.

    I will test them in this weekend.

    Thank you again.

    Regards.

    Sérgio

Similar Threads

  1. Resolution integer math assistance?
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th January 2010, 03:01
  2. Line Graph, math problem...
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th May 2009, 04:20
  3. Pulsin Math question
    By ruijc in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 2nd April 2008, 16:15
  4. PBPL Math...new math takes more cycles...Always?
    By skimask in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th February 2008, 10:22
  5. not quite understanding the MATH function
    By Rhatidbwoy in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th January 2006, 20:20

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