Page 1 of 1

making a beep sound

Posted: Tue Dec 12, 2006 4:56 pm
by mel
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.

Posted: Tue Dec 12, 2006 5:16 pm
by Mark
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

Posted: Tue Dec 12, 2006 8:18 pm
by mel
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?

Posted: Wed Dec 13, 2006 11:59 am
by Steve
For flowcode V3, use the following within a 'C' code icon to get a 200us delay:

Code: Select all

delay_10us(20);
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.

Posted: Wed Dec 13, 2006 12:01 pm
by Benj
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.

Code: Select all

int i;
int time = 3200;
for (i=0, i<time, i++){};
Then insert an output icon to set a pin to 1

Then insert another C code block with the following code

Code: Select all

int i;
int time = 3200;
for (i=0, i<time, i++){};
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))