Experiment 5. Controlling the sound of a single-chip microcomputer
[Copy link]
The hardware connection of this experiment is the same as the previous one, and the program is not very complicated and easy to understand. What we mainly learn and refer to is the calling of his program. In the main program, it calls the "sound subroutine" and "0.5s delay subroutine", and the sound subroutine calls the "1ms delay subroutine" and "0.5ms delay subroutine". ;Main program bb bit p2.0 ;Define bb to represent p2.0 in the following program star: acall sund acall d05s ljmp star
;Sound subroutine sund: mov r2,#250 ;Sound 250 cycles slop1: clr bb ;Turn on v1 9015 acall d1ms ;Delay setb bb ;Turn off v1 9015 acall d1ms djnz r2,slop1 ret
;1ms delay subroutine d1ms: mov r0,#2 dloop: mov r1,#250 ;Changing the value of r0 will change the sound frequency djnz r1,$ djnz r0,dloop ret
;0.5s delay subroutine d05s: mov r0,#5 d05l1: mov r1,#200 d05l2: mov r2,#250 djnz r2,$ djnz r1,d05l2 djnz r0,d05l1 ret
end
In the previous sound experiment program, the sound was continuous. Here we make the sound into a subroutine, and fix the sound duration in the subroutine. Let the main program call it when needed, and return to the main program to do other work after the sound is finished. This program makes a delay of about 0.5S when it is not making a sound, so the whole program makes a sound of "bb---bb---bb......".
|