PDA

View Full Version : PicBasic´s Math



srspinho
- 24th March 2004, 19:50
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)

Melanie
- 24th March 2004, 20:35
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

srspinho
- 25th March 2004, 11:24
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

Melanie
- 25th March 2004, 12:37
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

srspinho
- 21st July 2004, 20:43
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

carl_schell
- 22nd July 2004, 08:37
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

Melanie
- 22nd July 2004, 10:26
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

srspinho
- 23rd July 2004, 15:04
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