# Math problem in Pic Basic Pro

#### RobbJohnson

Joined Dec 15, 2009
76
In my program I used

PULSIN PORTA.3,1,FQOUT 'width of the high side (2uS resolution)
FQOUT=FQOUT*4 'width of one complete cycle

Now I have the number of Microseconds of the square wave signal.
Using Interger Math (PicBasic Pro) how can I calculate the frequncy so i can display it. (1/FQOUT obviously won't work)

Robb

#### t06afre

Joined May 11, 2009
5,934
Do you want to create a continuous square wave by toggling a pin a software?

#### MrChips

Joined Oct 2, 2009
21,869
Depending on how accurate you would like your display to be you can try 1000/FQOUT.

#### ErnieM

Joined Apr 24, 2011
8,055
It's been quite some time since I used PIC Basic Pro so I forget, what is the largest variable it can use? Was the largest variable a 16 bit word?

Also, what range of either frequency or period do you need to cover?

#### RobbJohnson

Joined Dec 15, 2009
76
Thanks Mr. chips
Came to that conclusion also

1,000/FQOUT= Frequency in KHz

I thank that is going to work fine.

I an using an xr2206 to generate a signal
and a digital pot to change freq and sweep
and a microcontroller to control everything and display freq.

Breadboard working good. Using interrupts to capture buttons.

thanks again

Last edited:

#### MrChips

Joined Oct 2, 2009
21,869
The 1000/FQOUT was just a suggestion to get you started. There are other solutions to improve your resolution.

For example, you could use 10000/FQOUT and that will give you an extra digit. You just have to do some extra footwork with PIC BASIC PRO to insert the decimal point in the right place.

The next step is to play with the DIV32 instruction:

Rich (BB code):
Disable

dummy = 1000 * 1000
hertz = DIV32 FQOUT

Enable
Use an IF statement to do this only if FQOUT is greater than 31.
This will give you the result in Hz.

Last edited:

#### MrChips

Joined Oct 2, 2009
21,869
Another solution is to measure the frequency directly over a given period (e.g. 1 second) using a pulse counter (if the PIC has one) or interrupts.

PIC BASIC PRO is OK for getting started with PICs and when execution time is not critical. For serious work you want to move on to C and ASM.

#### thatoneguy

Joined Feb 19, 2009
6,359
If you can access the CCP and timer0, you'll get a lot more accurate results, if you aren't currently. Isn't PIC BASIC Pro the BS2 commands in compiler format?

If so, you don't get to use some of the nicer parts of PICs (counters/timers and interrupts). I haven't looked into PICBasic in a long time.

#### ErnieM

Joined Apr 24, 2011
8,055
If you can access the CCP and timer0, you'll get a lot more accurate results, if you aren't currently. Isn't PIC BASIC Pro the BS2 commands in compiler format?

If so, you don't get to use some of the nicer parts of PICs (counters/timers and interrupts). I haven't looked into PICBasic in a long time.
Been quite some time since I left that compiler behind too, but as I was doing some A2D conversions with it you can access the full resister set.

My main issue with it was a bad week tracking down some bad code that I wrote (from some poor documentation) that while it compiled clean lead to non-functioning code. Damn it, bad code should issue an error, not a hex file!

#### MrChips

Joined Oct 2, 2009
21,869
With some serious thoughts into the matter, one usually arrives at the question, "Is it better to measure period or frequency?"

That would depend on your frequency range of interest and what kind of resolution do you require. For a typical range from 1Hz to 1MHz, a ballpark answer would be, for frequencies below 1000Hz, measure period. For frequencies above 1000Hz, measure frequency.

#### ErnieM

Joined Apr 24, 2011
8,055
With some serious thoughts into the matter, one usually arrives at the question, "Is it better to measure period or frequency?"

That would depend on your frequency range of interest and what kind of resolution do you require. For a typical range from 1Hz to 1MHz, a ballpark answer would be, for frequencies below 1000Hz, measure period. For frequencies above 1000Hz, measure frequency.
We could keep on debating that, but I think the OP solved his problems back in post #5.

Your numbers are probably a good general starting guide, but every specific problem is different.