本文介绍: 【51单片机演奏兰亭序】 https://www.bilibili.com/video/BV12G411D7uK/?使用开发板为普中51-实验板 普中-2。时钟频率:11.001081MHZ。数组较大,请在数组前加上。
使用开发板为普中51-实验板 普中-2
时钟频率:11.001081MHZ
演示视频:
【51单片机演奏兰亭序】 https://www.bilibili.com/video/BV12G411D7uK/?share_source=copy_web&vd_source=0f48f7cc0fef720b95e067122ac83437
delay.h
#ifndef __DELAY_H__
#define __DELAY_H__
void Delayxms(unsigned int xms); //@11.0592MHz
#endif
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);
}
}
Timer0.h
#ifndef __TIMER0_H__
#define __TIMER0_H__
void Timer0_Init();
#endif
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;
}
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口
}
}
原文地址:https://blog.csdn.net/jihuaTEL/article/details/134540208
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_32938.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。