# Math problem in Pic Basic Pro

Discussion in 'Programmer's Corner' started by RobbJohnson, Nov 2, 2011.

1. ### RobbJohnson Thread Starter Active Member

Dec 15, 2009
76
0
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

2. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
Do you want to create a continuous square wave by toggling a pin a software?

3. ### MrChips Moderator

Oct 2, 2009
12,622
3,451
Depending on how accurate you would like your display to be you can try 1000/FQOUT.

4. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,434
1,625
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?

5. ### RobbJohnson Thread Starter Active Member

Dec 15, 2009
76
0
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: Nov 3, 2011
6. ### MrChips Moderator

Oct 2, 2009
12,622
3,451
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:

Code ( (Unknown Language)):
1.
2.
3. Disable
4.
5.  dummy = 1000 * 1000
6.  hertz = DIV32 FQOUT
7.
8. Enable
9.
Use an IF statement to do this only if FQOUT is greater than 31.
This will give you the result in Hz.

Last edited: Nov 3, 2011
7. ### MrChips Moderator

Oct 2, 2009
12,622
3,451
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.

8. ### thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718
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.

9. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,434
1,625
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!

10. ### MrChips Moderator

Oct 2, 2009
12,622
3,451
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.

11. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,434
1,625
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.