Page 1 of 1

variable square wave

Posted: Wed May 01, 2019 9:06 pm
by siliconchip
hi all,
i would like to develope a square wave frequency that i can vary with a keypad from 0 to around 2KHz,and im thinking of using a pic 16f 690 or 887, trouble is ive no idea how to generate a square wave let alone make it variable im willing to put the work in but need pointers ive trolled through this site but cant find anything to start me off, or even if this achievable any help would be greatly appreciated thanks in advance

bob

Re: variable square wave

Posted: Wed May 01, 2019 10:30 pm
by medelec35
Hi Bob,
This may be of interest?

Re: variable square wave

Posted: Thu May 02, 2019 6:54 pm
by siliconchip
hi martin,
as always thanks for the reply, i will dissect the program in the link and see what i can come up with, ideally rather than 25Hz steps i would like to input a value on a keypad from 0 to 2000Hz and maybe hit the # key to enter as you mentioned, this will be my aim plus having some fun learning PWM

bob

Re: variable square wave

Posted: Thu May 02, 2019 10:10 pm
by medelec35
Hi Bob,
You're welcome.
Give it a try with the keypad.
If you get stuck we are here to help.

Re: variable square wave

Posted: Sat May 04, 2019 10:50 am
by siliconchip
hi martin,
im a little confused with the following calculation,

PWMFrequency = (PWMFrequency * 39 / 99) * 25 + 500


i know that the 25+500 is the 25hz step plus starting at 500 hz but i cant figure out the first part *39/99 ??

cheers bob

Re: variable square wave

Posted: Sat May 04, 2019 12:41 pm
by medelec35
hi Bob,
If using a keypad the frequency will be direct entry, therefore you won't require the formula.

The *39/99 just takes the ADC 0 to 255 and converts it from 0 to 100.
That's what makes the 25Hz steps possible if using a pot.
For example if the pot is on 0:
Frequency = 0 x 39 / 99 * 25 + 500 = 500

If the pot is on 255:
Frequency = 255 x 39 / 99 * 25 + 500 = 3000
You won't get the same result if using a calculator.
That is because with a microcontroller the result is always rounded down.
so if ADC was 10.
Instead of 10 x 39 / 99 * 25 + 500 = 598
It will actually be
10 x 39 / 99 = 3
3 x 25 + 500 = 575

See how microcontroller maths using integers, can give different than expected results?

Re: variable square wave

Posted: Sat May 04, 2019 1:11 pm
by siliconchip
hi martin,
now i understand, time to start experimenting, i will let you know how i get on and once again thanks

bob

Re: variable square wave

Posted: Sat May 04, 2019 7:18 pm
by siliconchip
hi,
ive played with the code and added a few bits, ie a keypad, the idea is i only want a max of 2000hz so only 4 digits can be selected tap in a value and press the * key to enter then press the # to clear its not perfect and i would appreciate simplifying my code and making it more reliable, i also think i
need to change the pwm to reflect my chosen frequency's ?? any input appreciated, cheers

bob

Re: variable square wave

Posted: Sun May 05, 2019 4:04 pm
by medelec35
Hi Bob the issue I would see is the keypad is scanned very quickly.
I key is held down for more than is necessary then multiple numbers will be registered.
e.g if just want a single 1 then the result could be 111
Try changing the flowchart to:
Keypad mod.png
(49.47 KiB) Downloaded 3652 times
I will test the code on hardware when i get the chance and see if I can think of other improvements.

Re: variable square wave

Posted: Sun May 05, 2019 5:26 pm
by siliconchip
hi martin,
although only 4 digits can be displayed this could be 9999,i only want a max of 2000 Hz so i need to try and overcome this so no more than 2K can be displayed, as always thank you

bob

Re: variable square wave

Posted: Sun May 05, 2019 8:53 pm
by medelec35
Hi Bob,
I have added a simple way of not displaying more then 2000.

Re: variable square wave

Posted: Tue May 07, 2019 5:11 pm
by siliconchip
hi martin
ive tried the program on hardware using my EB006 board and although there is a problem with key 10 clearing the number on the lcd if 4 digits are selected its working well, however ive tried to change some settings with the clock speed and pwm to try to light a led on C2 to see if i can flash it at 1Hz but ive been unsuccessful

bob

Re: variable square wave

Posted: Tue May 07, 2019 6:22 pm
by medelec35
Hi Bob,
I have not looked at the clearing side as I did not know you had an issue with it prior to your last message.
I will take a look at it for you.
siliconchip wrote:try to light a led on C2 to see if i can flash it at 1Hz but ive been unsuccessful
Post the flowchart and I will look at it for you.

Re: variable square wave

Posted: Tue May 07, 2019 7:46 pm
by siliconchip
hi martin
im not to bothered about the clearing side at present this will give me something to work through, the idea is put in a value 1-4 digits,hit the star for enter and this should give the same value square wave then hit the hash to clear, ive added a led to C2 hoping to get it to flash at 1 Hz ive tried to mess with the clock speed and pwm settings but to no avail also tried connecting the scope but got no output,im not sure how to show that the right frequency once selected is actually there

bob

Re: variable square wave

Posted: Tue May 07, 2019 7:53 pm
by siliconchip
martin
i have just sorted the clearing side i changed the value of the decision to 5 in the result macro :)

bob

Re: variable square wave

Posted: Tue May 14, 2019 7:09 pm
by siliconchip
hi all
could anyone point me in the right direction in how to get the frequency down to 1Hz if selected ive tried to alter the prescaler and clock speed but ive been unsuccessful

bob

Re: variable square wave

Posted: Tue May 14, 2019 7:24 pm
by medelec35
Hi Bob, You will not achieve this with PWM.
The best way will be with a timer interrupt.

Re: variable square wave

Posted: Tue May 14, 2019 9:43 pm
by siliconchip
Hi Martin
Thanks for the reply, that's a shame I was hoping I had nearly cracked it as for timer interups I'm really none the wiser plus is it still possible to use the keypad to select a frequency with interupts as my original idea was 0 to 2KHz

Bob

Re: variable square wave

Posted: Tue May 14, 2019 10:21 pm
by medelec35
Hi Bob,
I will see if its plausible 0 to 2 KHz via timer interrupt.

Re: variable square wave

Posted: Wed May 15, 2019 11:56 am
by siliconchip
hi martin
thanks i would appreciate that

bob