That's a reasonably complex task.I have figure out till to get the input pulses in the time frame 1s but facing issue to interface the buzzer so that it makes the sound at that frequency
yes it is my project for microcontroller course. Upto 85 percent accuracy is tolerableThat's a reasonably complex task.
What is the purpose of doing this?
What is the desired frequency accuracy of the reproduced sound?
Is it homework?
It's unclear to me what, exactly, is meant by, "I have figure out till to get the input pulses in the time frame 1s".I have figure out till to get the input pulses in the time frame 1s but facing issue to interface the buzzer so that it makes the sound at that frequency
It's unclear to me what, exactly, is meant by, "I have figure out till to get the input pulses in the time frame 1s".
Are you saying that there is some input coming in and that you need to monitor it for one second and determine what the frequency of that signal is?
If so, what do you know about that signal (i.e., what assumptions can you make)? Is it supposed to be a steady, pure, sinusoid? Or can it be music? Or what?
That sounds like a very tractable task. Break it into two pieces. They can be tackled in any order. First on is to figure out how to count the pulses that you see in one second and to store that into a register (or other memory location). The second is to take the value in a register (or some other memory location) and output a signal at the corresponding frequency to the buzzer.Like i am giving a input signal from the function generator to the timer pin working as a counter and counting the pulses of it in duration of 1 sec which means it is the frequency of the input signal and now i have the frequency now my further task is to send that frequency to the buzzer to play at that frequency which i have connected to P1.0
if possible can you please check this codeThat sounds like a very tractable task. Break it into two pieces. They can be tackled in any order. First on is to figure out how to count the pulses that you see in one second and to store that into a register (or other memory location). The second is to take the value in a register (or some other memory location) and output a signal at the corresponding frequency to the buzzer.
#include <reg51.h>
sbit rs = P2 ^ 7;
sbit rw = P2 ^ 6;
sbit en = P2 ^ 5;
sbit buzzer = P1 ^ 0; // Define buzzer on pin P1.0
void delay(unsigned int);
void cmdwrt(unsigned char);
void datawrt(unsigned char);
void generate_buzzer_sound(unsigned long int);
void cmdwrt(unsigned char x) {
P0 = x;
rs = 0;
rw = 0;
en = 1;
delay(1);
en = 0;
}
void datawrt(unsigned char y) {
P0 = y;
rs = 1;
rw = 0;
en = 1;
delay(1);
en = 0;
}
void delay(unsigned int z) {
unsigned int p;
for (p = 0; p < z; p++) {
TMOD = 0x10; // Timer 1 in Mode 1 (16-bit timer mode)
TH1 = 0xFC; // Load timer high byte
TL1 = 0x66; // Load timer low byte
TR1 = 1; // Start Timer 1
while (TF1 == 0); // Wait for overflow
TF1 = 0; // Clear overflow flag
TR1 = 0; // Stop Timer 1
}
}
void generate_buzzer_sound(unsigned long int frequency) {
unsigned int delay_period;
if (frequency < 50) // Lower limit for sound generation
frequency = 50;
if (frequency > 5000) // Upper limit for audible range
frequency = 5000;
// Calculate delay period for buzzer
delay_period = 50000 / frequency;
// Generate square wave on buzzer pin
buzzer = 1;
delay(delay_period);
buzzer = 0;
delay(delay_period);
}
void main(void) {
unsigned long int pulses;
unsigned char i;
unsigned char cmd[] = {0x38, 0x01, 0x06, 0x0C, 0x82};
unsigned char msg[] = {"Freq: "};
unsigned char msg2[] = {" Hz"};
for (i = 0; i < 5; i++) {
cmdwrt(cmd[i]);
delay(1);
}
while (1) {
// Configure Timer 0 for counting external pulses on T0 (P3.4)
TMOD = 0x05; // Timer 0 in Mode 1 (16-bit counter mode)
TL0 = 0; // Clear Timer 0 low byte
TH0 = 0; // Clear Timer 0 high byte
TR0 = 1; // Start Timer 0
delay(100); // Wait for 100 ms using Timer 1
TR0 = 0; // Stop Timer 0
// Calculate pulses from TH0 and TL0
pulses = (TH0 << 8) | TL0;
pulses = pulses * 10; // Convert to Hz (since we measured for 0.1 seconds)
// Clear LCD and display frequency
cmdwrt(0x01); // Clear display
delay(1);
for (i = 0; msg[i] != '\0'; i++) {
datawrt(msg[i]);
}
// Display frequency on LCD
datawrt((pulses / 10000) % 10 + '0');
datawrt((pulses / 1000) % 10 + '0');
datawrt((pulses / 100) % 10 + '0');
datawrt((pulses / 10) % 10 + '0');
datawrt((pulses % 10) + '0');
for (i = 0; msg2[i] != '\0'; i++) {
datawrt(msg2[i]);
}
// Generate sound based on the measured frequency
generate_buzzer_sound(pulses);
delay(1000); // Delay before next measurement using Timer 1
}
}
No, I'm not going to check through a couple hundred lines of code to figure out if it is correct or not. The fact that you have this much code and don't know what, if any, problems there might be with it says that you went about things all wrong. Incremental development. Break the problem into small pieces and implement each piece and test it (and fix it) before moving on to the next piece. Otherwise, you are left with hundreds of lines of code with no idea as to where a problem might be.if possible can you please check this code
Code:#include <reg51.h> sbit rs = P2 ^ 7; sbit rw = P2 ^ 6; sbit en = P2 ^ 5; sbit buzzer = P1 ^ 0; // Define buzzer on pin P1.0 void delay(unsigned int); void cmdwrt(unsigned char); void datawrt(unsigned char); void generate_buzzer_sound(unsigned long int); void cmdwrt(unsigned char x) { P0 = x; rs = 0; rw = 0; en = 1; delay(1); en = 0; } void datawrt(unsigned char y) { P0 = y; rs = 1; rw = 0; en = 1; delay(1); en = 0; } void delay(unsigned int z) { unsigned int p; for (p = 0; p < z; p++) { TMOD = 0x10; // Timer 1 in Mode 1 (16-bit timer mode) TH1 = 0xFC; // Load timer high byte TL1 = 0x66; // Load timer low byte TR1 = 1; // Start Timer 1 while (TF1 == 0); // Wait for overflow TF1 = 0; // Clear overflow flag TR1 = 0; // Stop Timer 1 } } void generate_buzzer_sound(unsigned long int frequency) { unsigned int delay_period; if (frequency < 50) // Lower limit for sound generation frequency = 50; if (frequency > 5000) // Upper limit for audible range frequency = 5000; // Calculate delay period for buzzer delay_period = 50000 / frequency; // Generate square wave on buzzer pin buzzer = 1; delay(delay_period); buzzer = 0; delay(delay_period); } void main(void) { unsigned long int pulses; unsigned char i; unsigned char cmd[] = {0x38, 0x01, 0x06, 0x0C, 0x82}; unsigned char msg[] = {"Freq: "}; unsigned char msg2[] = {" Hz"}; for (i = 0; i < 5; i++) { cmdwrt(cmd[i]); delay(1); } while (1) { // Configure Timer 0 for counting external pulses on T0 (P3.4) TMOD = 0x05; // Timer 0 in Mode 1 (16-bit counter mode) TL0 = 0; // Clear Timer 0 low byte TH0 = 0; // Clear Timer 0 high byte TR0 = 1; // Start Timer 0 delay(100); // Wait for 100 ms using Timer 1 TR0 = 0; // Stop Timer 0 // Calculate pulses from TH0 and TL0 pulses = (TH0 << 8) | TL0; pulses = pulses * 10; // Convert to Hz (since we measured for 0.1 seconds) // Clear LCD and display frequency cmdwrt(0x01); // Clear display delay(1); for (i = 0; msg[i] != '\0'; i++) { datawrt(msg[i]); } // Display frequency on LCD datawrt((pulses / 10000) % 10 + '0'); datawrt((pulses / 1000) % 10 + '0'); datawrt((pulses / 100) % 10 + '0'); datawrt((pulses / 10) % 10 + '0'); datawrt((pulses % 10) + '0'); for (i = 0; msg2[i] != '\0'; i++) { datawrt(msg2[i]); } // Generate sound based on the measured frequency generate_buzzer_sound(pulses); delay(1000); // Delay before next measurement using Timer 1 } }