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
Signal gen using 12F683
Moderator: Benj
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Signal gen using 12F683
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: : Note got into my mind min - 2Hz. only when re-read noticed 1Hz
For 2 x max frequency:
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
Within interrupt macro I would have:
Note: is NOT equal so output is changed from 0 to 1 then 1 to 0 each time is accessed.
finally within decision brance place
Below the decision (so its not a part of it),just place an output:
Martin
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
For 2 x max frequency:
Code: Select all
6000 (300 x 2)/ 6000 = 10
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
Code: Select all
If TimerTick >= Delay
Code: Select all
EnableOutput = !EnableOutput
Code: Select all
=!
finally within decision brance place
Code: Select all
TimerTick = 0
Code: Select all
EnableOutput>A4
Martin
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Signal gen using 12F683
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
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
Martin