#include <stdio.h> #include <intrins.h>

#define uchar unsigned char #define uint unsigned int

sbit beep = P3^5; sbit key1 = P1^5; sbit key2 = P1^6;

void DelayMs(uint ms) { uint i, j; for(i=0; i<ms; i++) for(j=0; j<110; j++); }

void PlayMusic1() { uint i; uchar music[] = {262, 294, 330, 349, 392, 440, 494, 523, 0}; for(i=0; music[i]!=0; i++) { beep = 1; DelayMs(5); beep = 0; DelayMs(500/music[i]-5); } }

void PlayMusic2() { uint i; uchar music[] = {523, 494, 440, 392, 349, 330, 294, 262, 0}; for(i=0; music[i]!=0; i++) { beep = 1; DelayMs(5); beep = 0; DelayMs(500/music[i]-5); } }

void main() { key1 = 1; key2 = 1; while(1) { if(!key1) { PlayMusic1(); while(!key1); } if(!key2) { PlayMusic2(); while(!key2); } } }

STC8单片机Keil5编程:蜂鸣器演奏多首音乐,按键切换

原文地址: https://www.cveoy.top/t/topic/owN0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录