The principle of music programming based on single chip microcomputer

Publisher:mmsg3814Latest update time:2018-03-09 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    Using a single-chip microcomputer (or single-board computer) to play music is probably one of the issues that radio enthusiasts are interested in. This article starts with the basic experiment of single-chip microcomputers, discusses the design principles of music programs, and gives specific examples for reference.

    Basic pronunciation experiment of single chip microcomputer

    We know that the frequency spectrum of sound ranges from about tens to thousands of hertz. If we can use a program to control the "high" or low level of a certain line of a single machine, a rectangular wave of a certain frequency can be generated on the line, and a speaker can emit a sound of a certain frequency. If we use a delay program to control the duration of the "high" and "low" levels, we can change the output frequency and thus change the tone.

 

    For example, to generate a 200Hz audio signal, connect the speaker as shown in Figure 1 (if it is a temporary experiment, the speaker can also be directly connected to the P1 port line). The experimental procedure is:

    Among them, subroutine DEL is a delay subroutine. When R3 is 1, the delay time is about 20us. R3 stores the delay constant. For 200HZ audio, its period is 1/200 second, that is, 5ms. In this way, when the duration of the high or low level of P1.4 is 2.5ms, that is, when the time constant of R3 is 2500/20=125 (7DH), a 200HZ tone can be emitted. Type the above program into the learning machine, and continuously modify the constant of R3 to feel the change of tone. In the music, each note corresponds to a certain frequency. Table 1 gives the frequency of each note in C key and its corresponding time constant. Readers can send its hexadecimal code into R3 according to the constants provided in Table 1, and practice repeatedly to experience it. According to Table 1, notes can be played. This is not enough. To play a song accurately, the rhythm of the music must be accurately controlled, that is, the duration of a note.

    We can use timer T0 to control the beat of the note. By inputting different initial values, we can generate different timing times. For example, the rhythm of a song is 94 beats per minute, that is, one beat is 0.64 seconds. The corresponding relationship between other beats and time is shown in Table 2.

    However, since the maximum timing time of T0 can only be 131 milliseconds, it is impossible to directly change the initial value of T0 to achieve different beats. We can use T0 to generate a 10 millisecond time base, and then set an interrupt counter to control the length of the beat time by judging the value of the interrupt counter. Table 2 also gives the time constants corresponding to various beats. For example, for a 1/4 beat note, the timing time is 0.16 seconds, and the corresponding time constant is 16 (ie 10H); for a 3-beat note, the timing time is 1.92 seconds, and the corresponding time constant is 192 (ie C0H).

    We take the time constant of each note and its corresponding beat constant as a group, arrange all the constants in the music into a table in order, and then use the table lookup program to take them out in turn, generate notes and control the rhythm, so that the performance effect can be achieved. In addition, the end note and the body stop note can be represented by the codes 00H and FFH respectively. If the table lookup result is 00H, it means the end of the song; if the table lookup result is FFH, the corresponding pause effect is generated. In order to produce the rhythm of hand playing, some notes (such as two identical notes) are inserted with a slightly different frequency of one time unit.

    The following is a program list that can be played directly on the TD-III learning machine. For other different models of learning machines, just change the address accordingly. This program plays the folk song "Osmanthus flowers bloom everywhere in August", in the key of C, with a rhythm of 94 beats per minute. Readers can also find a song by themselves, translate the music into a code table according to the constants given in Tables 1 and 2, and input it into the machine without changing the program. This experimental method is simple. Even for people who don't understand music, it is easy to translate an unfamiliar song into code. Learning to sing a song along with the performance of the machine is endless fun.

    Program listing (omitted, please refer to the source program description).

    The program flowchart is shown in Figure 2.

   Click to browse the next page

    This course is provided by the MCU Tutorial Network. Please point out any problems.

    Hardware connection instructions:

    Just find a simulator or a microcontroller experiment board, as long as it works, input the program, run it, then find a speaker (there should be a pair next to your computer), unplug it, connect the front end of the plug to P1.0, and find a wire to connect the back part to the ground of the microcontroller, and there should be sound. Then it is up to you to improve the hardware connection...

 

    Music program assembly code Code 1 -------------VoICe.asm--------------------------

            ORG 0000H
        LJMP START
        ORG 000BH
        INC 20H ;Interrupt service, interrupt counter plus 1
        MOV TH0,#0D8H
        MOV TL0,#0EFH ;12M crystal, forming a 10 millisecond interrupt
        RETI
START:  
        MOV SP,#50H
        MOV TH0,#0D8H
        MOV TL0,#0EFH
        MOV TMOD,#01H
        MOV IE,#82H
MUSIC0:
        NOP
        MOV DPTR,#DAT ;Table header address sent to DPTR
        MOV 20H,#00H ;Interrupt counter cleared to 0
        MOV B,#00H ;Table number cleared to 0
MUSIC1:
        NOP
        CLR A
        MOVC A,@A+DPTR ;Look up the table to get the code
        JZ END0 ;If it is 00H, then end
        CJNE A,#0FFH,MUSIC5
        LJMP MUSIC3
MUSIC5:
        NOP
        MOV R6,A
        INC DPTR
        MOV A,B
        MOVC A,@A+DPTR ;Get the beat code and send it to R7
        MOV R7,A
        SETB TR0 ;Start counting
MUSIC2:
        NOP
        CPL P1.0
        MOV A,R6
        MOV R3,A
        LCALL DEL
        MOV A,R7
        CJNE A,20H,MUSIC2 ;Is the interrupt counter (20H) = R7?
                                ;If not equal, continue the loop
        MOV 20H,#00H ;If equal, get the next code
        INC DPTR
; INC B
        LJMP MUSIC1
MUSIC3:
        NOP
        CLR TR0 ;Pause for 100 milliseconds
        MOV R2,#0DH
MUSIC4:
        NOP
        MOV R3,#0FFH
        LCALL DEL
        DJNZ R2,MUSIC4
        INC DPTR
        LJMP MUSIC1
END0:
        NOP
        MOV R2,#64H ;Song ends, delay 1 second and continue
MUSIC6:
        MOV R3,#00H
        LCALL DEL
        DJNZ R2,MUSIC6
        LJMP MUSIC0
DEL:
        NOP
DEL3:                  
        MOV R4,#02H
DEL4:   
        NOP
        DJNZ R4,DEL4
        NOP
        DJNZ R3,DEL3
        RET
        NOP

DAT:
 db 26h,20h,20h,20h,20h,20h,26h,10h,20h,10h,20h,80h,26h,20h,30h,20h
 db 30h,20h,39h,10h,30h,10h,30h,80h ,26h,20h,20h,20h,20h,20h,1ch,20h
 db 20h,80h,2bh,20h,26h,20h,20h,20h,2bh,10h,26h,10h,2bh,80h,26h,20h
 db 30h,20h,30h,20h,39h,10h,26h,10h,26h,60h,40h,10h,39h,10h,26h,20h
 db 30h,20h,30h,20h,39h,10h,26h,10h,26h, 80h,26h,20h,2bh,10h,2bh,10h
 db 2bh,20h,30h,10h,39h,10h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,20h
 db 20h,10h,20h,10h,2bh,10h,26h,30h,30h,80h,18h,20h,18h,20h,26h,20h
 db 20h,20h,20h,40h,26h,20h,2bh,20h,30h, 20h,30h,20h,1ch,20h,20h,20h
 db 20h,80h,1ch,20h,1ch,20h,1ch,20h,30h,20h,30h,60h,39h,10h,30h,10h
 db 20h,20h,2bh,10h,26h,10h,2bh,10h,26h,10h,26h,10h,2bh,10h,2bh,80h
 db 18h,20h,18h,20h,26h,20h,20h,20h,20h, 60h,26h,10h,2bh,20h,30h,20h
 db 30h,20h,1ch,20h,20h,20h,20h,80h,26h,20h,30h,10h,30h,10h,30h,20h
 db 39h,20h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,10h
 db 20h,10h,2bh,10h,26h,30h,30h,80h,00H

    END

    Music program assembly code Code 2 -------------Vo IC e1.asm--------------------------

    ;Title 'August Osmanthus Fragrance' Voice Program        
;Abstract For details, see 'Radio' Issue 3, 1992
;Author Zhou ZhenanORG
  0000H
        LJMP START
        ORG 000BH
        INC 20H ;Interrupt service, interrupt counter plus 1
        MOV TH0,#0D8H
        MOV TL0,#0EFH ;12M crystal oscillator , forming a 10 millisecond interrupt
        RETI
START:  
        MOV SP,#50H
        MOV TH0 ,#0D8H MOV
        TL0,#0EFH
        MOV TMOD,#01H
        MOV IE,#82H
MUSIC0:
        NOP
        MOV DPTR,#DAT ;Table header address sent to DPTR
        MOV 20H,#00H ;Interrupt counter cleared to 0
        MOV B,#00H ;Table number cleared to 0
MUSIC1:
        NOP
        CLR A
        MOVC A,@A+DPTR ;Look up the table to get the code
        JZ END0 ;If it is 00H, then end
        CJNE A,#0FFH,MUSIC5
        LJMP MUSIC3
MUSIC5:
        NOP
        MOV R6,A
        INC DPTR
        MOV A,B
        MOVC A,@A+DPTR ;Get the beat code and send it to R7
        MOV R7,A
        SETB TR0 ;Start counting
MUSIC2:
        NOP
        CPL P1.0
        MOV A,R6
        MOV R3,A
        LCALL DEL
        MOV A,R7
        CJNE A,20H,MUSIC2 ;Is the interrupt counter (20H) = R7?
                                ;If not equal, continue the loop
        MOV 20H,#00H ;If equal, then get the next code
        INC DPTR
; INC B
        LJMP MUSIC1
MUSIC3:
        NOP
        CLR TR0 ;Pause for 100 milliseconds
        MOV R2,#0DH
MUSIC4:
        NOP
        MOV R3,#0FFH
        LCALL DEL
        DJNZ R2,MUSIC4
        INC DPTR
        LJMP MUSIC1
END0:
        NOP
        MOV R2,#64H ;Song ends, delay 1 second and continue
MUSIC6:
        MOV R3,#00H
        LCALL DEL
        DJNZ R2,MUSIC6
        LJMP MUSIC0
DEL:
        NOP
DEL3:                  
        MOV R4,#02H
DEL4:   
        NOP
        DJNZ R4,DEL4
        NOP
        DJNZ R3,DEL3
        RET
        NOP

DAT:   
        DB 18H, 30H, 1CH, 10H
        DB 20H, 40H, 1CH, 10H     
        DB 18H, 10H, 20H, 10H
        DB 1CH, 10H, 18H, 40H
        DB 1CH, 20H, 20H, 20H
        DB 1CH, 20H, 18H, 20 H     
        DB 20H, 80H, 0FFH, 20H
        DB 30H, 1CH, 10H, 18H
        DB 20H, 15H, 20H, 1CH
        DB 20H, 20H, 20H, 26H
        DB 40H, 20H, 20H, 2BH
        DB 20H, 26H, 20 H , 20H     
        DB 20H, 30H, 80H, 0FFH
        DB 20H, 20H, 1CH, 10H
        DB 18H, 10H, 20H, 20H
        DB 26H, 20H, 2BH, 20H
        DB 30H, 20H, 2BH, 40H
        DB 20H, 20H, 1CH, 10H
        DB 18H, 10H, 20H, 20H
        DB 26H, 20H, 2BH, 20H
        DB 30H, 20H, 2BH, 40H
        DB 20H, 30H, 1CH, 10H
        DB 18H, 20H, 15H, 20H
        DB 1CH, 20H, 20H , 20H
        DB 26H, 40H, 20H , 20H     
        DB 2BH, 20H, 26H , 20H     
        DB 20H, 20H, 30H , 80H
        DB 20H, 30H, 1CH , 10H
        DB 20H, 10H, 1CH , 10H
        DB 20H, 2 0H, 26H, 20H
        DB 2BH, 20H, 30H, 20H
        DB 2BH, 40H, 20H, 15H
        DB 1FH, 05H, 20H, 10H
        DB 1CH, 10H, 20H, 20H
        DB 26H, 20H, 2BH, 20H
        DB 30H, 20H, 2BH, 40H
        DB 20H, 30H, 1CH, 10H
        DB 18H, 20H, 15H, 20H
        DB 1CH, 20H, 20H, 20H
        DB 26H, 40H, 20H, 20H
        DB 2BH, 20H, 26H, 20H
        DB 20H, 20H, 30H, 30H      
        DB 20H, 30H, 1CH, 10H
        DB 18H, 40H, 1CH, 20H
        DB 20H, 20H, 26H, 40H
        DB 13H, 60H, 18H, 20H
        DB 15H, 40H, 13H, 40H
        DB 18H, 80H, 00H
end

Keywords:MCU Reference address:The principle of music programming based on single chip microcomputer

Previous article:UART register and UART5 initialization procedure
Next article:Working Principle and Software Design of External Expansion Memory Circuit

Recommended ReadingLatest update time:2024-11-16 13:59

MCU reads and writes SD card series (I)
SD card is a new generation memory device based on semiconductor flash memory. SD was successfully developed in August 1999 and weighs only 2 grams. However, it has high memory capacity, fast data transfer rate, great mobility and good security. SD card is also easy to reformat and has a wide range of applications. Fo
[Microcontroller]
Connection between microcontroller and incremental rotary encoder
The incremental rotary encoder converts the timing and phase relationship of its angle code disk through two internal photosensitive receiving tubes to obtain the increase (positive direction) or decrease (negative direction) of the angle displacement of its angle code disk. After combining with digital circuits, espec
[Microcontroller]
How does a computer control the relay in a single chip microcomputer?
The electronic world is really amazing. Here is my first computer-controlled relay in a microcontroller. Let you also feel the wonder of the electronic world. Microcontroller program:  Write this program into the microcontroller with the help of software:   Microcontroller   Software for writing pro
[Microcontroller]
How does a computer control the relay in a single chip microcomputer?
Design of Ultrasonic Generator Based on 51 Single Chip Microcomputer
The ultrasonic generator designed in this paper uses a single-chip microcomputer to generate an initial signal, and then generates ultrasonic waves for killing water fleas after a series of processing circuits. It has low cost and good effect, and can be used in agriculture. Here, three modules are designed: (1
[Microcontroller]
Design of Ultrasonic Generator Based on 51 Single Chip Microcomputer
N76E003 MCU IIC Soft Simulation
/* -----------------------------------------head File-----------------------------------------*/ #include "iic.h" /* -----------------------------------------Macro definition-----------------------------------------*/ #define IIC_SDA(N) IIC_SDA = N #define IIC_SCL(N) IIC_SCL = N /*Select the data direction of SDA*
[Microcontroller]
51 MCU Overview and Design of Minimum System
What is a microcontroller? Before learning, you must understand what this thing is, how to use it, and why it can be used in this way. If you understand these three questions, then you can learn the 51 single-chip microcomputer very well. Comparison of microcontrollers Here we only compare 8051 and 8052: Model Fla
[Microcontroller]
51 MCU Overview and Design of Minimum System
89C51 MCU Timer/Counter 0
The timing counting function of the 89C51 microcontroller is controlled by the special function registers TMOD and TCON. TMOD has no bit address and cannot perform bit operations. The names and functions of each bit are as follows: TMOD: GATE C/T1 M1 M0 GATE C/T1 M1 M0                          D7 D6 D5 D4 D3 D2 D1
[Microcontroller]
89C51 MCU Timer/Counter 0
Design of Switching Power Supply Control Using MSP430 Single Chip Microcomputer in Power System
The MSP430 series single-chip microcomputer is a 16-bit ultra-low power mixed signal processor launched by Texas Instruments (TI) in 1996. It is highly praised by users for its ultra-low power consumption, powerful processing capability, high-performance analog technology, rich on-chip peripheral mod
[Power Management]
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号