Piezo Sounder accuracy
Moderator: Benj
Piezo Sounder accuracy
I am creating a new project based loosely on an earlier project where I had issues getting the frequency and timing to work with any degree of accuracy so I thought I would start with this.
With it complied and running in a pcb I am getting a 640Hz (set for 1000Hz) tone with a 1.5 second duration, the delay count loop gives me a 6 second delay instead of 5 but strangely the 1 second o/p on C1 is bang on within a few milliseconds.
I wasn't expecting high accuracy on the piezo but it is so far out I am thinking there is something wrong with either the component or my flowcode.
I am using Picoscope for measurements.
Thanks
Ian
With it complied and running in a pcb I am getting a 640Hz (set for 1000Hz) tone with a 1.5 second duration, the delay count loop gives me a 6 second delay instead of 5 but strangely the 1 second o/p on C1 is bang on within a few milliseconds.
I wasn't expecting high accuracy on the piezo but it is so far out I am thinking there is something wrong with either the component or my flowcode.
I am using Picoscope for measurements.
Thanks
Ian
- Attachments
-
- sounder_timing.fcfx
- (9.46 KiB) Downloaded 173 times
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Piezo Sounder accuracy
Hi Ian,
The piezo component is likely using a software loop which will generally always be a bit inaccurate.
A way you could do it much more accuratley would be to use a timer interrupt to generate the output frequency. You could then use a second timer interrupt to monitor the passing of time and use this to switch on and off the piezo output.
Let us know if you want an example.
The piezo component is likely using a software loop which will generally always be a bit inaccurate.
A way you could do it much more accuratley would be to use a timer interrupt to generate the output frequency. You could then use a second timer interrupt to monitor the passing of time and use this to switch on and off the piezo output.
Let us know if you want an example.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Piezo Sounder accuracy
Thank you for your reply Ben, an example would be a great starting point.
Since posting i have been experimenting with timer interrupts instead of using the loop-delay-count method for the pause between the sounder outputs, the calculated v the actual timings have been interesting so there is much to learn!
Ian
Since posting i have been experimenting with timer interrupts instead of using the loop-delay-count method for the pause between the sounder outputs, the calculated v the actual timings have been interesting so there is much to learn!
Ian
-
- 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: Piezo Sounder accuracy
Hi Ian,
Are you able to choose a different target device?
A newer one that has timer2 interrupt, it will make life easier for you.
I can create a quick tutorial on how to generate a given frequency.
Do you want to stick with 14 pins?
Are you able to choose a different target device?
A newer one that has timer2 interrupt, it will make life easier for you.
I can create a quick tutorial on how to generate a given frequency.
Do you want to stick with 14 pins?
Martin
Re: Piezo Sounder accuracy
Hi Martin
I hadn't really thought about how many pins I will need as I was only thinking about getting the output frequency and timing sorted first within the confines of my existing board and it is confining so time to move on! So yes a good idea to upgrade with more timers, I will still need to use a couple of pins for IOC. At least 20 pins would be good as I would rather have some left over than run out with project creep.
Edit:
Sorry Martin -I only half answered you, yes a tutorial would be fantastic as I think I have interrupts sussed but no experience of how to get a tune out of them.
Thanks
Ian
I hadn't really thought about how many pins I will need as I was only thinking about getting the output frequency and timing sorted first within the confines of my existing board and it is confining so time to move on! So yes a good idea to upgrade with more timers, I will still need to use a couple of pins for IOC. At least 20 pins would be good as I would rather have some left over than run out with project creep.
Edit:
Sorry Martin -I only half answered you, yes a tutorial would be fantastic as I think I have interrupts sussed but no experience of how to get a tune out of them.
Thanks
Ian
-
- 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: Piezo Sounder accuracy
Hi Ian,
What I would do if I was you is go to the Main Flowcode 8 site
Scroll down to Program your embedded devices:
Change to Show 100 entries.
Click on the Up arrow of the Pins section.
Go to page 2 and scroll down to the 20 pin microcontrollers.
Select a suitable microcontroller based on functions, memory etc.
E.g
18F13K50
18F13K22
18F14K50
16F18344
16F18345
Maybe best to double check on the price at say mouser, RS components, farnell etc.
When you get a new microcontroller, I will then take you through step by step on generating a set frequency.
There are two methods.
The timer 2 (or other similar interrupt) & PWM component
What I would do if I was you is go to the Main Flowcode 8 site
Scroll down to Program your embedded devices:
Change to Show 100 entries.
Click on the Up arrow of the Pins section.
Go to page 2 and scroll down to the 20 pin microcontrollers.
Select a suitable microcontroller based on functions, memory etc.
E.g
18F13K50
18F13K22
18F14K50
16F18344
16F18345
Maybe best to double check on the price at say mouser, RS components, farnell etc.
When you get a new microcontroller, I will then take you through step by step on generating a set frequency.
There are two methods.
The timer 2 (or other similar interrupt) & PWM component
Martin
Re: Piezo Sounder accuracy
Hi Martiive not seen that page before on Matrix, a bewildering choice!
I can bypass that bit short term as I have a Pic18F27J13 already set up on a board i bought as a kickstarter project by Mike Hibbet a few years back, i have shoehorned a 40mhz xtal on it to drive an oled display so it's not slow. I can port the code across when i know exactly what i need.
Pic attached.
Thanks
Ian
I can bypass that bit short term as I have a Pic18F27J13 already set up on a board i bought as a kickstarter project by Mike Hibbet a few years back, i have shoehorned a 40mhz xtal on it to drive an oled display so it's not slow. I can port the code across when i know exactly what i need.
Pic attached.
Thanks
Ian
- Attachments
-
- 20200630_150034.jpg (158.66 KiB) Viewed 5237 times
-
- 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: Piezo Sounder accuracy
This is how I would generate a 1000Hz square wave using timer 2 interrupt.
1. Use as fast as practical clock speed, then modify configuration settings.
2. Check everything is set correctly by running 1 second flash test.
Scroll down the page for basic configuration settings.
3. Download Timer 2 Calculator from here
It's a spreadsheet, so will need excel, Libra office calc etc.
4. Enter Clock frequency and double the required frequency.
Reason for double is every time the timer interrupt is accessed the output will toggle.
output on then off = 1 complete cycle
For this example:
Clock frequency = 40MHz
Timer 2 Req Value = 2000Hz
Results: 5. Drag the timer interrupt to the start of main. It should reside outside the main look and is only accessed once.
Double click on the interrupt & Select Timer 2 from the Interrupt on:
Select Properties, Enter any of the results so long as its for 2000Hz.
We will choose Prescaler 1:4, Postscaler 1:10 & Rollover 125.
6. Select Create New Macro, Name it to something meaningful like Timer2ISR.
No Spaces are allowed.
Select OK, then OK & Edit Macro.
Drag a calculation box within Timer2ISR.
Select drop down next to <Add new> under the Global variables, select Add new.
For Initial value enter 0.
Select Bool. (results will only be 1 or 0)
Name variable to something meaningful e.g Toggle, select OK.
Click within Calculation: area, double click or drag Toggle (under Variables)
then enter =! then drag Toggle variable again.
So you should have:
Drag an output icon to below the calculation icon, double click it, change Port: and Select down arrow for the Variable or value:
Drag or double click Toggle variable.
Select Single Bit:and select the required port bit from the drop down arrow.
Select OK: Select The Main macro i.e Main(Flow) tab
Just below the timer interrupt add a Loop by dragging it.
Add a small delay within the loop, e.g 50ms: If all is set up as above, you should see a 1KHz square wave on the selected pin.
Note: the star next to icons just mean I have not save the flowchart.
1. Use as fast as practical clock speed, then modify configuration settings.
2. Check everything is set correctly by running 1 second flash test.
Scroll down the page for basic configuration settings.
3. Download Timer 2 Calculator from here
It's a spreadsheet, so will need excel, Libra office calc etc.
4. Enter Clock frequency and double the required frequency.
Reason for double is every time the timer interrupt is accessed the output will toggle.
output on then off = 1 complete cycle
For this example:
Clock frequency = 40MHz
Timer 2 Req Value = 2000Hz
Results: 5. Drag the timer interrupt to the start of main. It should reside outside the main look and is only accessed once.
Double click on the interrupt & Select Timer 2 from the Interrupt on:
Select Properties, Enter any of the results so long as its for 2000Hz.
We will choose Prescaler 1:4, Postscaler 1:10 & Rollover 125.
6. Select Create New Macro, Name it to something meaningful like Timer2ISR.
No Spaces are allowed.
Select OK, then OK & Edit Macro.
Drag a calculation box within Timer2ISR.
Select drop down next to <Add new> under the Global variables, select Add new.
For Initial value enter 0.
Select Bool. (results will only be 1 or 0)
Name variable to something meaningful e.g Toggle, select OK.
Click within Calculation: area, double click or drag Toggle (under Variables)
then enter =! then drag Toggle variable again.
So you should have:
Code: Select all
Toggle =! Toggle
Drag or double click Toggle variable.
Select Single Bit:and select the required port bit from the drop down arrow.
Select OK: Select The Main macro i.e Main(Flow) tab
Just below the timer interrupt add a Loop by dragging it.
Add a small delay within the loop, e.g 50ms: If all is set up as above, you should see a 1KHz square wave on the selected pin.
Note: the star next to icons just mean I have not save the flowchart.
Martin
Re: Piezo Sounder accuracy
Hi Martin
Thank you so much for this, it makes a great platform to start from. A couple of things, there always are (!) my output frequency was 100Hz so I changed the post scaler to 1:1 and got 1.003 KHz which is pretty bang on, I had confidence that the config was correct until I added the 1 sec pulse test and found it to be 10 secs which explains all.
I have attached the file in as there is an error in the configuration, I have gone though just about every option to find a change but there is no change for better or worse.
The other is that my Excel 2010 reported some unreadable content in the calculator and attempted to fix it which resulted in a completely blank worksheet. I have some screenshots if you would like to see them.
Many thanks
Ian
Thank you so much for this, it makes a great platform to start from. A couple of things, there always are (!) my output frequency was 100Hz so I changed the post scaler to 1:1 and got 1.003 KHz which is pretty bang on, I had confidence that the config was correct until I added the 1 sec pulse test and found it to be 10 secs which explains all.
I have attached the file in as there is an error in the configuration, I have gone though just about every option to find a change but there is no change for better or worse.
The other is that my Excel 2010 reported some unreadable content in the calculator and attempted to fix it which resulted in a completely blank worksheet. I have some screenshots if you would like to see them.
Many thanks
Ian
- Attachments
-
- 1Khz_sounder.fcfx
- (20.39 KiB) Downloaded 164 times
-
- 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: Piezo Sounder accuracy
Hi Ian,
you're welcome.
There are two options.
1. leave PLL enabled but change clock speed to 96MHz
You will also need to change timer 2 settings accordingly.
2. Disable PLL and leave clock speed at 40MHz
Attached is the spreadsheet saved in a different format.
Had to compress it due to large file size for the forums.
you're welcome.
My guess is because PLL is enabled it's forcing clock frequency to 96MHz so USB will work.
There are two options.
1. leave PLL enabled but change clock speed to 96MHz
You will also need to change timer 2 settings accordingly.
2. Disable PLL and leave clock speed at 40MHz
Thank you for that, you are the first to report it.
Attached is the spreadsheet saved in a different format.
Had to compress it due to large file size for the forums.
- Attachments
-
- Timer 2 Calculator V2.zip
- (906.57 KiB) Downloaded 165 times
Martin
Re: Piezo Sounder accuracy
Hi Martin
Got there in the end, the timings are spot on now but I will adjust the configuration settings some more to see what breaks it.
The timer calculator now loads without a hitch - pure genius!
I have been playing with gating the output on and off at 1Hz, probably crude but it works and its even possible to leave the output high or low depending on whether the counts are odd or even. Next step is to change the fixed values to variables but wondering how much code you can have in the Macro without it having a significant effect on the overall running, or best to always to do as much as possible in the main loop?
Thanks for your help
Ian
Got there in the end, the timings are spot on now but I will adjust the configuration settings some more to see what breaks it.
The timer calculator now loads without a hitch - pure genius!
I have been playing with gating the output on and off at 1Hz, probably crude but it works and its even possible to leave the output high or low depending on whether the counts are odd or even. Next step is to change the fixed values to variables but wondering how much code you can have in the Macro without it having a significant effect on the overall running, or best to always to do as much as possible in the main loop?
Thanks for your help
Ian
- Attachments
-
- 1Khz_sounder.fcfx
- (21.18 KiB) Downloaded 161 times
-
- 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: Piezo Sounder accuracy
Hi Ian,
Glad you got there in the end.
Glad you got there in the end.
check out this post, and the post down contains a flowchart they may be of interest.
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: Piezo Sounder accuracy
It is, but that's what I enjoy.
It's even better when you can get the hardware to do wanted it to.
Have fun.
I have attached how I would toggle the alarm on/off every second.
It's even better when you can get the hardware to do wanted it to.
Have fun.
I have attached how I would toggle the alarm on/off every second.
- Attachments
-
- 1Khz_sounder v2.fcfx
- (21.51 KiB) Downloaded 107 times
Martin
Re: Piezo Sounder accuracy
Thanks Martin- that is neat.
I have seen you use that last decision in other flowcodes "If Enable ?" thinking it looks incomplete but its works!
Regards
Ian
I have seen you use that last decision in other flowcodes "If Enable ?" thinking it looks incomplete but its works!
Regards
Ian
-
- 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: Piezo Sounder accuracy
If you have a variable with no expression, then condition will be true for all values except 0.
So you could look at if: Variable as being the same as If: Varaible <>0 or If: Varaible =! 0
So you could look at if: Variable as being the same as If: Varaible <>0 or If: Varaible =! 0
Martin
Re: Piezo Sounder accuracy
Oh yes that has compiled to an accurate signal!
I can now play with some variables for different outputs.
Don't know whether I have read it wrong but on the Project options I have to set the "PLL Configuration Enable Bit" to Enable if I am not using it. On another project when I was using the PLL at 96Mhz to drive a display it had to be set to Disable.
Its often a challenge with a new target to get them to behave and this has taken a lot of learning, Flowcode is dangerously addictive!
Thanks for your help
Ian
I can now play with some variables for different outputs.
Don't know whether I have read it wrong but on the Project options I have to set the "PLL Configuration Enable Bit" to Enable if I am not using it. On another project when I was using the PLL at 96Mhz to drive a display it had to be set to Disable.
Its often a challenge with a new target to get them to behave and this has taken a lot of learning, Flowcode is dangerously addictive!
Thanks for your help
Ian
- Attachments
-
- 1Khz_sounder v2.fcfx
- (21.23 KiB) Downloaded 105 times
-
- 1Khz sounder.pdf
- (526.08 KiB) Downloaded 110 times