Hello all
I have downloaded flowcode today, and have been trying various things, but I am having a problem .I only have a PIC 16F84A, so any answers must be able to run on this particular chip. I want to make a bleep sound, using a piezoelectric sounder; this is the type without an internal drive circuit. So what I need is a square wave output of approximately 4 kHz, and I can’t see anything in the help section that tells you how to do this. Also is their way to make a low battery warning using flowcode with this chip, when using batteries to power a circuit?
Any help would be gratefully accepted.
Many thanks!
Mel.
making a beep sound
-
- Posts: 209
- Joined: Thu Oct 19, 2006 11:46 am
- Location: Bakewell, UK
- Has thanked: 20 times
- Been thanked: 16 times
- Contact:
If I remember correctly a directly connected piezo element makes a sound on a positive going transition. 4kHz i 4000 times as second and given a 1:1 mark to space ratio we need two delays of 125us between changes on an output.
So try coding
Loop 255 times (= beep for 16th second, for example)
Port B Out "1" (or whatever pin you want)
Delay 125us
Port B Out "0"
Delay 125us
Repeat loop
The delays will need to be in a "C" icon as the minimum is otherwise 1ms (the code for this is elsewhere on the forum). Alternatively use NOP insructions in a C icon, but I suggest only if you have a slow clock or want a high frequency.)
If it where me I would compromise and use
Port B Out "1" (or whatever pin you want)
Delay 1ms
Port B Out "0"
Repeat loop
As the tranducer only needs the pulse, not its width (within reason) then this code should give a 1kHz output, which should be well audible.
Hope this helps
So try coding
Loop 255 times (= beep for 16th second, for example)
Port B Out "1" (or whatever pin you want)
Delay 125us
Port B Out "0"
Delay 125us
Repeat loop
The delays will need to be in a "C" icon as the minimum is otherwise 1ms (the code for this is elsewhere on the forum). Alternatively use NOP insructions in a C icon, but I suggest only if you have a slow clock or want a high frequency.)
If it where me I would compromise and use
Port B Out "1" (or whatever pin you want)
Delay 1ms
Port B Out "0"
Repeat loop
As the tranducer only needs the pulse, not its width (within reason) then this code should give a 1kHz output, which should be well audible.
Hope this helps
Go with the Flow.
Hello Mark
Thank you for getting back to me so quickly. I have tried what you said and with the delays set at 1 millisecond, it is just enough to get the sounder to make a very faint noise. I have checked the sounder using a 555 timer at 5 Volts, and to get any reasonable sound output requires a frequency of at least 2.5 kHz, so it looks like I will need to do as you say, and write the delays in C. Here in lies the first hurdle, I have no idea how to do this. I have done as you suggested and looked elsewhere on the forum for the code, but cannot find it. Would you be good enough to point me to it please?
Thank you for getting back to me so quickly. I have tried what you said and with the delays set at 1 millisecond, it is just enough to get the sounder to make a very faint noise. I have checked the sounder using a 555 timer at 5 Volts, and to get any reasonable sound output requires a frequency of at least 2.5 kHz, so it looks like I will need to do as you say, and write the delays in C. Here in lies the first hurdle, I have no idea how to do this. I have done as you suggested and looked elsewhere on the forum for the code, but cannot find it. Would you be good enough to point me to it please?
- Steve
- Matrix Staff
- Posts: 3427
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
For flowcode V3, use the following within a 'C' code icon to get a 200us delay:
There is an equivalent "delay_us()" function but this is likely to fail (depending on the clockspeed).
For v2 users, there is no "delay_10us()" function, but the "delay_us()" function does not fail to compile: when it cannot produce an exact delay, it tries its best.
If you want a more exact delay, you might have to resort to using the 'C' "nop()" function to produce a 4/osc second delay (for a 20MHz crystal, the delay for 1 nop call would be 0.2us).
When using your own 'C' code, you will usually need to put a semicolon at the end of each line.
Code: Select all
delay_10us(20);
For v2 users, there is no "delay_10us()" function, but the "delay_us()" function does not fail to compile: when it cannot produce an exact delay, it tries its best.
If you want a more exact delay, you might have to resort to using the 'C' "nop()" function to produce a 4/osc second delay (for a 20MHz crystal, the delay for 1 nop call would be 0.2us).
When using your own 'C' code, you will usually need to put a semicolon at the end of each line.
- 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:
Hi Mel
Heres a good example to play around with.
Start off with a While 1.
Inside the while 1 insert a C code icon and input the following code.
Then insert an output icon to set a pin to 1
Then insert another C code block with the following code
Then insert an output icon to set the output pin back to 0
This will toggle a pin on and off to create a square wave with a frequency equal to
1 / (time * instruction frequency)
or
1 / (time * (Ossilator Frequency / 4))
Heres a good example to play around with.
Start off with a While 1.
Inside the while 1 insert a C code icon and input the following code.
Code: Select all
int i;
int time = 3200;
for (i=0, i<time, i++){};
Then insert another C code block with the following code
Code: Select all
int i;
int time = 3200;
for (i=0, i<time, i++){};
This will toggle a pin on and off to create a square wave with a frequency equal to
1 / (time * instruction frequency)
or
1 / (time * (Ossilator Frequency / 4))
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