51单片机演奏兰亭序

使用开发板为普中51-实验板 普中-2

时钟频率:11.001081MHZ

演示视频:

【51单片机演奏兰亭序】 https://www.bilibili.com/video/BV12G411D7uK/?share_source=copy_web\&vd_source=0f48f7cc0fef720b95e067122ac83437

源码如下:

数组较大,请在数组前加上code关键字

c 复制代码
delay.h
#ifndef __DELAY_H__
#define __DELAY_H__

void Delayxms(unsigned int xms); //@11.0592MHz

#endif
c 复制代码
delay.c
#include <INTRINS.H>

void Delayxms(unsigned int xms) //@11.0592MHz
{
	while (xms--)
	{
		unsigned char i, j;

		_nop_();
		i = 2;
		j = 199;
		do
		{
			while (--j)
				;
		} while (--i);
	}
}
c 复制代码
Timer0.h
#ifndef __TIMER0_H__
#define __TIMER0_H__
 
void Timer0_Init();
 
#endif
c 复制代码
Timer0.c
#include <REGX52.H>
 
/**
 * @brief 计时器0初始化
 */
void Timer0_Init() // 1毫秒@11.0592MHz
{
	TMOD &= 0xF0; // 设置定时器模式
	TMOD |= 0x01; // 设置定时器模式
	TL0 = 0x66;	  // 设置定时初值
	TH0 = 0xFC;	  // 设置定时初值
	TF0 = 0;	  // 清除TF0标志
	TR0 = 1;	  // 定时器0开始计时
	// 配置中断
	ET0 = 1;
	EA = 1;
	PT0 = 0;
}
c 复制代码
main.c
#include <REGX52.H>
#include "delay.h"
#include "Timer0.h"

// 蜂鸣器端口
sbit Buzzer = P2 ^ 5;

#define SPEED 500 // 速度

// 音符与索引对应表,P:休止符,L:低音,M:中音,H:高音,下划线:升半音#
#define P 0
#define L1 1
#define L1_ 2
#define L2 3
#define L2_ 4
#define L3 5
#define L4 6
#define L4_ 7
#define L5 8
#define L5_ 9
#define L6 10
#define L6_ 11
#define L7 12
#define M1 13
#define M1_ 14
#define M2 15
#define M2_ 16
#define M3 17
#define M4 18
#define M4_ 19
#define M5 20
#define M5_ 21
#define M6 22
#define M6_ 23
#define M7 24
#define H1 25
#define H1_ 26
#define H2 27
#define H2_ 28
#define H3 29
#define H4 30
#define H4_ 31
#define H5 32
#define H5_ 33
#define H6 34
#define H6_ 35
#define H7 36

unsigned int FreqTable[] = {
    0,
    63628, 63731, 63835, 63928, 64021, 64103, 64185, 64260, 64331,
    64400, 64463, 64524, 64580, 64633, 64684, 64732, 64777, 64820,
    64860, 64898, 64934, 64968, 65000, 65030, 65058, 65085, 65110,
    65134, 65157, 65178, 65191, 65217, 65235, 65252, 65268, 65283};
// 音,结束标志:0xFF
code unsigned char Music[] = {0, 0, 0, M5, M6, H1, H2, H1, H2, H3, H2, H1, H2, H3, H5, H3, H2, H1, M6, M5, H3, H5, H6, H3, H2, H2, H1, H3, H2, H2, H1, H1, H2, H3, H2, H1, M6, M5, H2, H3, H1, 0, M5, M3, M5, M6, M3, M5, M6, M5, M3, M2, M3, 0, M3, M2, M3, H1, M6, H1, M6, M5, M3, M1, M3, M2, M2, 0, M5, M5, M5, M6, M6, H1, H2, H1, M6, M5, M6, M5, M3, M2, M5, M3, M3, M2, M1, M2, M3, M2, M1, M3, M1, M1, M1, M1, 0xFF};
// 音长,标准音为4
code unsigned char MusicTime[] = {4, 4, 2, 2, 2, 2, 8, 2, 4, 2, 2, 12, 2, 2, 8, 2, 2, 2, 2, 2, 12, 2, 2, 8, 2, 2, 2, 2, 2, 2, 2, 8, 2, 2, 8, 2, 2, 2, 2, 2, 8, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4};
unsigned char FreqSelect, MusicSelect;
void main()
{
    Timer0_Init();
    while (1)
    {
        if (Music[MusicSelect] != 0xFF)
        {
            FreqSelect = Music[MusicSelect];
            Delayxms(500 / 4 * MusicTime[MusicSelect]);
            MusicSelect++;
            TR0 = 0;     // 计时器停
            Delayxms(5); // 每个音间隔5ms
            TR0 = 1;
        }
        else
        {
            TR0 = 0;
            MusicSelect = 0;
        }
    }
}

void Timer0_Routine() interrupt 1
{
    if (FreqTable[FreqSelect])
    {
        TL0 = FreqTable[FreqSelect] % 256; // 设置定时初值
        TH0 = FreqTable[FreqSelect] / 256; // 设置定时初值
        Buzzer = !Buzzer;                  // 翻转蜂鸣器IO口
    }
}
相关推荐
嵌R式小Z12 分钟前
STM32短按,长按,按键双击实现
stm32·单片机·嵌入式硬件
佩佩(@ 。 @)1 小时前
嵌入式:走馬燈-stm32GPIOF_LED9、10; GPIOE_D10、D12 流水綫蜂鸣器
stm32·嵌入式硬件·算法
华普微HOPERF2 小时前
Sub-GHz无线收发单片机,低功耗物联网通信的硬件“基石”
单片机·嵌入式硬件·物联网
大阳1232 小时前
51单片机4(温度传感器DS18B20)
开发语言·单片机·嵌入式硬件·算法·51单片机
朱自清的诗.2 小时前
使用python脚本储存mosquito服务器数据到sqlite
python·单片机·sqlite·esp32
应用市场4 小时前
深入理解STM32运行原理:从上电到主程序执行的完整过程
stm32·单片机·嵌入式硬件
空管电小二4 小时前
【开关电源篇】整流及其滤波电路的工作原理和设计指南-超简单解读
经验分享·单片机·嵌入式硬件·社交电子·学习方法
逼子格5 小时前
Altium Designer(AD)原理图更新PCB后所有器件变绿解决方案
单片机·嵌入式硬件·硬件工程·问题解决·ad·pcb·altium designer
wei-dong-183797540086 小时前
嵌入式硬件工程师的每日提问
嵌入式硬件
文火冰糖的硅基工坊7 小时前
[硬件电路-180]:集成运放,在同向放大和反向放大电路中,失调电压与信号一起被等比例放大;但在跨阻运放中,失调电压不会与电流信号等比例放大。
嵌入式硬件·系统架构·电路·跨学科融合