variable square wave
Moderator: Benj
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
variable square wave
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: variable square wave
Hi Bob,
You're welcome.
Give it a try with the keypad.
If you get stuck we are here to help.
You're welcome.
Give it a try with the keypad.
If you get stuck we are here to help.
Martin
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: variable square wave
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?
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?
Martin
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
hi martin,
now i understand, time to start experimenting, i will let you know how i get on and once again thanks
bob
now i understand, time to start experimenting, i will let you know how i get on and once again thanks
bob
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
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
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
- Attachments
-
- Frequency Generatorv3.fcfx
- (20.45 KiB) Downloaded 282 times
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: variable square wave
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: I will test the code on hardware when i get the chance and see if I can think of other improvements.
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: I will test the code on hardware when i get the chance and see if I can think of other improvements.
Martin
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: variable square wave
Hi Bob,
I have added a simple way of not displaying more then 2000.
I have added a simple way of not displaying more then 2000.
- Attachments
-
- Frequency Generatorv4.fcfx
- (21.17 KiB) Downloaded 273 times
Martin
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: variable square wave
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.
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.
Post the flowchart and I will 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
Martin
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
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
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
- Attachments
-
- Frequency Generatorv4.fcfx
- (22.44 KiB) Downloaded 230 times
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
martin
i have just sorted the clearing side i changed the value of the decision to 5 in the result macro
bob
i have just sorted the clearing side i changed the value of the decision to 5 in the result macro
bob
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: variable square wave
Hi Bob, You will not achieve this with PWM.
The best way will be with a timer interrupt.
The best way will be with a timer interrupt.
Martin
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact:
Re: variable square wave
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 times
- Contact: