Premium Essay

Mccct

In: Computers and Technology

Submitted By Siva66
Words 681
Pages 3
(E) On Speaker
The beep sound is produced by the frequency of the electric signals.
The following shows a way to generate a tone of certain frequency using C program statements.

#include <p18f4520.h>
#include <delays.h> void main ()
{
char i = 0; //declarations
ADCON1 = 0x0f;
TRISE = 11100000;
PORTE = 0; for (i=0;i<500;i++)
{
PORTEbits.RE2 = ~PORTEbits.RE2 // toggled wave
Delay10TCYx(10); //?????
PORTEbits.RE2 = 0;
Delay10TCYx(1);
} for (i=0;i<250;i++)
{
PORTEbits.RE2 = 1;
Delay10TCYx(1);
PORTEbits.RE2 = 0;
Delay10TCYx(4);
}

Here is a way to generate a tone of certain frequency continuously for a certain duration using C program statements.

//------------------- speaker_freq_dur.c ---------------------
//--- Plays a pitch thru a speaker and lights an LED
//- Set up: > Attach a piezo speaker between ground and D6 (pin 12)
// > Wire an LED from D7 (pin 13) thru a 330 ohm resistor to gnd
//---- Here we define a function called “play_pitch” void play_pitch(int freq, int duration){ int i; // Create variable for counting iterations long period; // Create floating point variable long pulse; // Create floating point variable long cycles; // variable for number of cycles (duration) period = 1000000/freq; //Calculate period pulse = (period / 2 ); //Calculate pulse cycles = duration * (freq / 1000.00); for(i = 0; i < cycles; i++){ // Loop for number of cycles digitalWrite(6, HIGH); // Set speaker Pin high delayMicroseconds(pulse); // delay for our calculated duration digitalWrite(6, LOW); // Set speaker Pin low delayMicroseconds(pulse); // delay for our calculated duration } // end loop } // end function definition
//---- Having defined it, now we can use it in the program that follows void setup(){ pinMode(6, OUTPUT); // Define Speaker pin pinMode(7, OUTPUT); // Define LED

Similar Documents