Page 1 of 1

Signal gen using 12F683

Posted: Sat Nov 26, 2016 2:35 pm
by conroydp
Hi,

I would really appreciate any help.

I am trying to generate a square wave signal of 2ms (on/off) using a 12F683. I have a very simple flowchart that only varies the pulse between signals. I need to be able to generate from 1 hz to 300hz only.

I have and ADC on pin 7 of the pic and use that to vary the delay

It works fine on lower frequencies but I find that there are large jumps in the frequency when I approach 200hz.

Here is my flowchart....

I'm sure there is a better way to do it but have racked my brain and not been able to come up with anything better.

Regards
Conroy

Re: Signal gen using 12F683

Posted: Sat Nov 26, 2016 5:51 pm
by medelec35
The way I would do his is to use a timer interrupt running much faster than maximum frequency which will allow better control.
For example for max of 300Hz i would set the timer interrupt at about 6KHz.
Next I would add a variable that counts 1 every time interrupt macro is accessed.
E.g TimerTick = TimerTick + 1
then I would work out how many time the interrupt has to be accessed for 2 x min frequency and 2x max frequency.
For 2 x min frequency:

Code: Select all

6000/(2 x 2) = 1500
: Note got into my mind min - 2Hz. only when re-read noticed 1Hz

For 2 x max frequency:

Code: Select all

 6000 (300 x 2)/ 6000 = 10
Reason for twice frequency is after correct time has elapsed then output is toggled to produce a perfect square wave.

I would then create a calculation to produce 10 when ADC = 0 & 1500 when ADC = 255
E.g

Code: Select all

Delay = ADC * 146 / 25 + 10
Within interrupt macro I would have:

Code: Select all

If TimerTick >= Delay

Code: Select all

EnableOutput = !EnableOutput
Note:

Code: Select all

=! 
is NOT equal so output is changed from 0 to 1 then 1 to 0 each time is accessed.
finally within decision brance place

Code: Select all

TimerTick = 0
Below the decision (so its not a part of it),just place an output:

Code: Select all

EnableOutput>A4
Martin

Re: Signal gen using 12F683

Posted: Sun Nov 27, 2016 11:06 am
by medelec35
To make life easier, you can use timer2 interrupt.
To help you set it to 6KHz, there is a great app posted on these forums.
It produced best settings very quickly.
If you need further help then let me know.

Martin