Microcontroller C language programming: Playing Happy Birthday song

Publisher:好的哎Latest update time:2013-06-27 Source: 21icKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/* Name: Play music

Description: The program plays the Happy Birthday song while it is running. No timer interrupt is used, and all frequencies are completely realized by delay.

*/

#include

#define uchar unsigned char

#define uint unsigned int

sbit BEEP=P3^7;

//Happy Birthday song note frequency table, different frequencies are determined by different delays

uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,

212,212,106,126,159,169,190,119,119,126,159,142,159,0};

//Happy Birthday song beat chart, the beat determines the playing length of each note

uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,

9,3,12,12,12,12,12,9,3,12,12,12,24,0};

//Delay

void DelayMS(uint x)

{

uchar t;

while(x--) for(t=0;t<120;t++);

}

//Play function

void PlayMusic()

{


 


uint i=0,j,k;


while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)

{ //Play each note, SONG_LONG is the beat length

for(j=0;j

{

BEEP=~BEEP;

//SONG_TONE delay table determines the frequency of each note

for(k=0;k

}

DelayMS(10);

i++;

       }

}[page]

void main()

{

BEEP=0;

while(1)

{

PlayMusic(); //Play Happy Birthday

DelayMS(500); //Pause for a while after playing

}

}

Microcontroller C language programming: Playing Happy Birthday song

Keywords:MCU Reference address:Microcontroller C language programming: Playing Happy Birthday song

Previous article:MCU C language programming: external INT0 interrupt control LED
Next article:MCU C language programming: key pronunciation

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

Using low-cost MCU's UART to drive smart cards
The growing need for security and enhanced functionality in the banking, identification and telecommunications markets has increased the use of smart cards worldwide, while at the same time reducing the use of less secure magnetic stripe cards. However, the hardware and firmware development of appropriate communicat
[Microcontroller]
Using low-cost MCU's UART to drive smart cards
Application of C language bit field structure in PIC microcontroller
    When I need to use flag bits in my program, I thought I could define the bits the same way as Keil51, but I kept getting errors. Later, I learned that the MC18 compiler does not support bit definition (the PICC18 compiler does). Here is a solution: bit field structure.     When storing some information, it doe
[Microcontroller]
MCU multi-channel AD acquisition source program (8 channels) STC15w series
12864 is a serial connection. AD0~AD4 is connected to 5V power supply. AD5~AD7 is connected to GND. STC15W408AS comes with 8 channels of 10-bit AD   . The program only uses the upper eight bits. The range is 255. The program contains 10-bit AD acquisition. 1024. The code has been commented. I am a novice. Please corr
[Microcontroller]
MCU multi-channel AD acquisition source program (8 channels) STC15w series
51 MCU IO port expansion - 74HC165 chip parallel to serial experiment
We all know that there are two major types of communication: serial and parallel. The biggest advantage of serial is that it occupies less bus, but the transmission rate is low; parallel is just the opposite, occupying more bus and high transmission rate. There are many such chips on the market, some are serial input
[Microcontroller]
51 MCU IO port expansion - 74HC165 chip parallel to serial experiment
Smart meter design based on LPC1114 microcontroller and ATT7053 chip
ATT7053AU is a single-phase multi-function metering chip (EMU) with SPI. The working range of the chip is 3.0 ~ 3.6V. The crystal oscillator used is 5.5296MHz. 1.att7053 circuit diagram 2.att7053  PCB diagram 3. Finished finished board 4.LPC1114 microcontroller and ATT7053 use SPI communication method. The sof
[Microcontroller]
Smart meter design based on LPC1114 microcontroller and ATT7053 chip
51 MCU register group settings
Everyone knows that the 51 MCU has four groups of registers R0-R7. In the past year, when I was communicating with novice friends, I found that many friends often had problems using the register group. Although this is not a difficult problem, if an error occurs, it will also cause serious consequences.     First, let
[Microcontroller]
Interface Design between LCD Display and PIC Microcontroller
1 Introduction In the design of intelligent instruments, it is usually necessary to display the measurement and control information. Due to the power consumption, volume and other conditions of the instrument, general CRT displays are often not suitable. Although semiconductor digital tubes can display numbers a
[Microcontroller]
Interface Design between LCD Display and PIC Microcontroller
Design of PWM Special Signal Generator Based on AT89S51 Single Chip Microcomputer
1 Introduction As one of the most widely used methods of nondestructive testing, eddy current nondestructive testing has the advantages of simple sensor structure, high sensitivity, large measurement range, no influence from oil and other media, strong anti-interference ability, etc. It has been widely used
[Microcontroller]
Design of PWM Special Signal Generator Based on AT89S51 Single Chip Microcomputer
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号