This is mainly achieved by using timer 1CTC mode, starting iv_TIMER 1_COMPA interrupt, and then getting the flat rate parameters of a song. I found a program, as follows;
/****************************************
* Using the I/O port of M 16 single chip microcomputer to generate simulated music *
* Function: Use I/O port to generate analog music *
* Clock frequency: external 7.3728M *
* Design: Tonghe *
* revision date: April 2007, 13 *
* compilation environment: ICC-AVR6.3 1 *
* Experimental environment: M 16 learning board of Afa *
* Sound: Passive buzzer is connected to PD5 *
* Result: The test passed *
****************************************/
# include & ltIOM 16v . h & gt;
# include & ltmacros.h & gt
#include "wo_ni.h" // contains music files.
#include "delay.h" // delay file
# Define uchar unsigned characters
# Define uint unsigned integer
/************************************
* Port initialization function *
************************************/
void port_init(void)
{
PORTD = 0x20// Buzzer interface initialized to high.
DDRD = 0x 20; //Buzzer interface output.
}
/**********************************
* Timer 1 Interrupt entry function: generate square wave sound *
**********************************/
#pragma interrupt handler timer 1:iv _ timer 1_COMPA
Voidtimer1(void)//timer1interrupt entry.
{
portd^=( 1<; & lt5); //produce a square wave, sound.
}
/***********************************
* Music production function *
* Function: output a square wave with frequency x and delay of y milliseconds *
* range: x: 100-20000 Hz, 0: no sound *
* y:0-65536 ms *
***********************************/
Invalid sound (uint i)
{
uint x;
x = I; //Get the frequency data of music
If (x! =0)
{
x = 3686400/x; //data calculation
ocr 1A = x; //Comparison value assignment
tccr 1B = 0x 09; //Allow comparison of inverted outputs
}
other
{
tccr 1B = 0x 00; //The frequency is 0, static and silent.
}
}
/******************************
* Main functions *
******************************/
Invalid master (invalid)
{
uchar I = 0;
port _ init();
tccr 1A = 0x 40; //Configure T/C 1 as CTC mode, and OC 1A generates music in reverse.
TIMSK | =( 1 & lt; & ltocie 1A); //Turn on compare match interrupt
SEI(); //Open host interrupt
while( 1)
{
tccr 1B = 0x 09; //Turn on comparison matching
while(music_data[i][ 1]! =0) // Wait for the last note.
{
sound(music _ data[I][0]); //output comments
delay _ ms(music _ data[I][ 1]); //Beat delay
tccr 1B = 0x 00; //Stop after playing a note.
PORTD | =( 1 & lt; & lt5); //Turn off the buzzer to protect the horn.
i++; //Prepare a note.
delay _ ms(5); //Play
}
tccr 1B = 0x 00;
PORTD | =( 1 & lt; & lt5); //Turn off the buzzer to protect the horn.
delay _ ms(4000);
I = 0;
}
}
////Header file 2
flash unsigned int music _ data[][2]=
{
{587,800},{698,800},{466, 1600},{523,800},{587,800},{349, 1600},
{466,800},{523,800},{587,800},{698,800},{523,3200},{587,800},
{698,800},{466, 1600},{523,800},{587,800},{392, 1600},{523,800},
{349,800},{523,800},{587,800},{466, 1600},{784, 1600},{698, 1600},
{784, 1600},{466, 1600},{587,800},{392,800},{587, 1200},{698,400},
{523,3200},{587,800},{698,800},{466, 1600},{523,800},{587,800},
{392, 1600},{523,800},{349,800},{523,800},{587,800},{466,3200},
{ 0, 0}
};
#pragma data: data
//header file 3
/****************************************
* function name: void delay_us(uchar i) *
* Function: Delay function *
* Crystal frequency: 7.3728 MHz *
****************************************/
Void delay_us (unsigned integer I)
{
When (I-)
{
NOP();
NOP();
}
}
/****************************************
* function name: void delay_ms(uint i) *
* Function: Delay function *
* Crystal frequency: 7.3728 MHz *
* Entry parameter: 1 ms *
****************************************/
Void delay_ms (unsigned integer I)
{
Unsigned int a;;
for(; Me; I-)
{
for(a = 1052; a; Answer-)
{; }
}
}
Combine these programs by yourself and connect PD5 to the buzzer interface. It will be wonderful.
Contact with questions