MCU music player C language program

Publisher:cangartLatest update time:2012-08-10 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include 
#define uchar unsigned char
#define uint unsigned int
 
sbit speaker = P1^7;
uchar t0h,t0l,time;
//-----------------------------------------
//The MCU crystal oscillator uses 11.0592MHz
// Frequency-half-cycle data table high eight bits This software saves a total of 28 frequency data of four octaves
uchar code FREQH[] = {0x01, //No note when 0
    0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //Bass 1234567
    0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i
    0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, // high pitch 234567
    0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //Super treble 1234567
//----------------------------------------
// Frequency-half-cycle data table lower eight bits
uchar code FREQL[] = {0x01, //No note when 0
    0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //Bass 1234567
    0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i
    0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, ​​// high pitch 234567
    0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //Super high pitch 1234567
//-----------------------------------------
//A note has three numbers. The first one is the note number, the middle one is the octave, and the last one is the duration (in 1/4 beats).
//6, 2, 6 represent: 6, middle note, 6 1/4 beats;
//5, 2, 2 represent: 5, middle note, 2 1/4 beats;
//3, 2, 4 represent: 3, middle note, 4 1/4 beats;
//5, 2, 4 represent: 5, middle note, 4 1/4 beats;
//1, 3, 4 represent: 1, high note, 4 1/4 beats;
//Bass is 1, midrange is 2, treble is 3, and super treble is 4;
///////////////////////////////////////////////////// ////////
/************************There is only one good mother in the world************************/
uchar code sszymmh[] = {
 6,2,6, 5,2,2, 3,2,4, 5,2,4,
 1,3,4, 6,2,2, 5,2,2, 6,2,8,
 3,2,4, 5,2,2, 6,2,2, 5,2,4,
 3,2,4, 1,2,2, 6,1,2, 5,2,2,
 3,2,2, 2,2,8, 2,2,6, 3,2,2,
 5,2,4, 5,2,2, 6,2,2, 3,2,4,
 2,2,4, 1,2,8, 5,2,6, 3,2,2,
 2,2,2, 1,2,2, 6,1,2, 1,2,2,
 5,1,16, 0,0,0};
/**********************Two mice*************************/
uchar code TwoMouse[]={
 3,2,2, 6,2,2, 7,2,2, 5,2,2,
 3,2,2, 6,2,2, 7,2,2, 5,2,2,
 7,2,2, 1,3,2, 2,3,4,
 7,2,2, 1,3,2, 2,3,4,
 2,3,1, 3,3,1, 2,3,1, 1,3,1, 7,2,2, 5,2,2,
 2,3,1, 3,3,1, 2,3,1, 1,3,1, 7,2,2, 5,2,2,
 6,2,2, 2,2,2, 5,2,4, 6,2,2, 2,2,2, 5,2,4,
 0,0,0};
/***********************Two butterflies**********************/
uchar code TwoButterfly[]={
 3,2,2, 2,2,2, 3,2,6, 3,2,2, 3,2,2, 2,2,2,
 1,2,6, 6,1,2, 1,2,2, 2,2,4, 2,2,2, 3,2,2,
 2,2,2, 1,2,2, 6,1,2, 1,2,2, 5,1,12,3,2,2,
 2,2,2, 3,2,8, 3,2,2, 2,2,2, 3,2,2, 3,2,2,
 1,2,12,6,1,2, 1,2,2, 2,2,4, 2,2,2, 3,2,2,
 2,2,2, 1,2,2, 6,1,2, 1,2,2, 2,2,12,3,2,2,
 2,2,2, 3,2,8, 3,2,2, 2,2,2, 3,2,2, 2,2,2,
 1,2,12,6,1,2, 1,2,2, 2,2,4, 2,2,2, 3,2,2,
 2,2,2, 1,2,2, 6,1,2, 1,2,2, 5,2,12,3,2,2,
 5,2,2, 5,2,8, 5,2,2, 5,2,2, 6,2,2, 5,2,2,
 3,2,12,2,2,2, 3,2,2, 2,2,4, 2,2,2, 3,2,2,
 2,2,2, 1,2,2, 6,1,2, 6,1,1, 1,2,1, 1,2,1,
 1,2,1, 1,2,12,0,0,4, 5,2,2, 5,2,2, 6,2,2,
 1,3,2, 7,2,2, 7,2,2, 6,2,2, 3,2,2, 2,2,2,
 2,2,2, 3,2,2, 3,2,8, 3,2,2, 3,2,2,
 5,2,2, 6,2,4, 6,2,6, 6,1,2, 3,2,2, 2,2,2,
 2,2,12,3,2,2, 5,2,2, 5,2,2, 3,2,2, 5,2,4,
 1,3,4, 7,2,2, 6,2,2, 7,2,2, 3,2,4,
 6,2,2, 6,2,2, 7,2,2, 6,2,2, 5,2,2,
 3,2,2, 2,2,4, 3,2,2, 2,2,2, 3,2,2, 5,2,6,
 5,2,2, 5,2,2, 6,2,2, 6,2,2, 5,2,2,
 3,2,2, 2,2,4, 5,1,2, 5,1,2, 6,1,2, 1,2,12,
 0,0,0};
/**********************happy birthday************************/
uchar code Birthday[]={
 5,1,2, 5,1,2, 6,1,4, 5,1,4, 1,2,4, 7,1,8,
 5,1,2, 5,1,2, 6,1,4, 5,1,4, 2,2,4, 1,2,8,
 5,1,2, 5,1,2, 5,2,4, 3,2,4, 1,2,4, 7,1,4,
 6,1,4, 4,2,2, 4,2,2, 3,2,4, 1,2,4, 2,2,4,
 1,2,4, 0,0,0};
 
///////////////////////////////////////////////////// //////
//------------------------------------------------ -----
void t0int() interrupt 1 //T0 interrupt program, control the tone of pronunciation
{
    TR0 = 0; //Close T0 first
    speaker = ~speaker; //output square wave, pronunciation
    TH0 = t0h; //The next interruption time, this time, controls the pitch
    TL0 = t0l;
    TR0 = 1; //Start T0
}
//-----------------------------------------
void delay(uchar t) //delay program, control the pronunciation time length 120ms (1/4 beat)
{
 uchar a,b,c;
    while(t--) //Quadruple loop, total delay t 1/4 beats
 {         
     for(c=193;c>0;c--)
         for(b=114;b>0;b--)
             for(a=1;a>0;a--);
 }
  //During the delay period, you can enter T0 interrupt to pronounce
    TR0 = 0; //Turn off T0, stop pronunciation
}
//-----------------------------------------
void singachar() //play a note
{
 TR0=0;
    TH0 = t0h; //Control tone
    TL0 = t0l;
    TR0 = 1; //Start T0, and T0 outputs square wave to pronounce
    delay(time); //Control the time length, i.e. the beat
}
//-----------------------------------------
//**************Play a song***********************************//
void song(uchar *str)
{
 uchar k,i;
 i = 0;
    time = 1;
    while(time)
 {
        k = str[i] + 7 * (str[i + 1]); //The i-th is a note, the i+1th is the octave
        t0h = FREQH[k]; //Read the frequency value from the data table
        t0l = FREQL[k]; //Actually, it is the time length of the timing
        time = str[i + 2]; //Read the time length value beat time
        i += 3;
        singachar();
 }
}
void main(void)
{
    TMOD = 0x01; //Set T0 timing working mode 1
    ET0 = 1; //Open T0 interrupt
    EA = 1; // Enable CPU interrupt
    while(1)
 {
  song(TwoMouse);
  delay(20);
  song(sszymmh);
  delay(20);
// song(TwoButterfly);
  song(Birthday);
  delay(20);
 }
                 //Play a note
}

/****************Music score beats, high and low notes***********************************
Beat: Two horizontal bars below indicate a quarter beat; one horizontal bar below indicates a half beat; a dot at the end indicates adding half the beat of the note;
2 indicates one beat, 2- indicates two beats; 2--- indicates four beats
Pitch: A dot above indicates a high note, a dot below indicates a low note, usually indicating a middle note;
set the beat speed to 125 beats/minute, then 2.08/beat, two beats are 960ms, one beat is 0.48s, and 1/4 beat is 120ms
*************************************************************/

Keywords:MCU Reference address:MCU music player C language program

Previous article:Power-off protection and watchdog reset C51 program
Next article:Gateway to serial communication based on TCP/IP

Recommended ReadingLatest update time:2024-11-16 22:46

Design of MP3 music player based on single chip microcomputer
1. Hardware Solution This design uses a single-chip microcomputer as the core controller to design a music player, which mainly completes the task of extracting and playing audio file data stored in the TF card. The player is similar to the music player software of a personal computer. It can identify and extract WMA
[Microcontroller]
Design of MP3 music player based on single chip microcomputer
Multi-channel nuclear radiation detection data acquisition system based on AVR single chip microcomputer and CH375
The system uses AVR single-chip microcomputer, high-speed devices and USB interface technology, with fast acquisition speed, short dead time, high counting rate and large communication volume. The advantages of USB such as plug-and-play, strong versatility, easy expansion and high reliability improve the performance o
[Microcontroller]
Multi-channel nuclear radiation detection data acquisition system based on AVR single chip microcomputer and CH375
Implementation of wireless temperature monitoring system based on single chip microcomputer
With the development of information science and microelectronics technology, temperature monitoring can be automated and intelligentized by using modern technology. The multi-channel wireless temperature monitoring system is designed towards this goal. This design requires the use of a single-chip microcomputer and
[Microcontroller]
Implementation of wireless temperature monitoring system based on single chip microcomputer
Optimization Method of Single Chip Microcomputer C Language Program Code
1. Choose the right algorithm and data structure You should be familiar with algorithmic language and know the advantages and disadvantages of various algorithms. For specific information, please refer to the corresponding reference materials, which are introduced in many computer books. Replacing the slower sequentia
[Microcontroller]
Understand the role of single chip resistor in one article
  Reading the resistance value of the resistor array   In a three-digit number, the first and second digits from left to right are valid digits, and the third digit represents the first two digits multiplied by 10 to the power of N (in Ω). If there is a decimal point in the resistance value, it is represented by "R" a
[Microcontroller]
Understand the role of single chip resistor in one article
A brief discussion on PIC microcontroller software development skills
    A single-chip microcomputer is a small but complete computer system that is integrated into a circuit chip. It uses ultra-large-scale integrated circuit technology to integrate the central processing unit CPU with data processing capabilities, random access memory RAM, read-only memory ROM, multiple I/O ports and i
[Microcontroller]
Ultra-low power security MCU based on enhanced 80C51 core - RJM8L003 series
The RJM8L003 series is an ultra-low-power secure MCU based on the enhanced 80C51 core, specially designed for battery-powered IoT terminals. Rich and complete functional safety features, built-in hardware true random number generator. Integrated 12-bit high-precision successive approximation ADC, which complies with t
[Microcontroller]
Knowledge is required for microcontroller learning
In fact, electronic engineers put a bunch of devices together, inject ideas (programs), complete the functions that these devices cannot complete when separated, and make a finished product. The higher the skills required, the more complex the functions, the lower the cost, and the greater the demand for the correspon
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号