找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4001|回复: 7
收起左侧

求助。怎么用STC12C5A60S2的蜂鸣器放音乐

[复制链接]
ID:278182 发表于 2018-1-21 22:08 | 显示全部楼层 |阅读模式
求助各位大神,怎么用STC12C5A60S2的蜂鸣器放音乐啊,虽然有学过C语言,但是看到好多类似的单片机程序还是一脸蒙蔽。。。头一次接触单片机。谢谢各位大神了
回复

使用道具 举报

ID:210244 发表于 2018-1-21 23:29 | 显示全部楼层
通过控制蜂鸣器的开关时间来生成调子,网上有很多蜂鸣器的曲谱,改改用就好了
回复

使用道具 举报

ID:278453 发表于 2018-1-22 01:05 | 显示全部楼层
从网上直接搜索例程,现在这种做蜂鸣器的很简单
回复

使用道具 举报

ID:164602 发表于 2018-1-22 08:32 | 显示全部楼层
给你一个例程看看。
/*
本实例利用无源蜂鸣器发出音乐声音。
还用到了定时计数器0
*/

#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char

sbit voice=P1^5;//蜂鸣器接口

uchar code sound[]={0xff,

0x30,0x10,0x2b,0x10,0x26,0x80,0xff,0x40,0x30,0x20,0x30,0x10,0x39,0x10,
0x40,0x80,0xff,0x40,0xff,0x20,0x40,0x20,0x26,0x20,0x26,0x10,0x2b,0x10,
0x2b,0x40,0xff,0x20,0x40,0x20,0x26,0x20,0x2b,0x20,0x2b,0x80,0xff,0x40,
0xff,0x10,0x40,0x10,0x40,0x10,0x40,0x10,0x1c,0x20,0x20,0x20,0x1c,0x20,
0x26,0x20,0x26,0x20,0xff,0x20,0x2b,0x30,0x30,0x10,0x26,0x10,0x2b,0x60,
0xff,0x20,0x39,0x20,0x30,0x20,0x39,0x20,0x30,0x40,0xff,0x20,0xff,0x20,
0xff,0x20,0x39,0x10,0x39,0x10,0x2b,0x20,0x2b,0x20,0x30,0x40,0xff,0x40,
0x26,0x80,0xff,0x20,0x2b,0x20,0x30,0x20,0x30,0x10,0x39,0x10,0x40,0x80,
0xff,0x40,0xff,0x20,0x40,0x10,0x40,0x10,0x1c,0x20,0x20,0x60,0xff,0x20,
0x30,0x20,0x2b,0x20,0x26,0x10,0x2b,0x10,0x2b,0x80,0xff,0x40,0xff,0x20,
0x40,0x10,0x40,0x10,0x39,0x20,0x40,0x20,0x1c,0x30,0x20,0x10,0xff,0x20,
0x1c,0x20,0x20,0x20,0x1c,0x20,0x26,0x80,0xff,0x30,0x2b,0x10,0x2b,0x20,
0x30,0x10,0x26,0x20,0x2b,0x60,0xff,0x40,0x39,0x20,0x30,0x20,0x30,0x80,
0xff,0x40,0xff,0x40,0x40,0x20,0x39,0x20,0x30,0x20,0x2b,0x20,0x26,0x20,
0x1c,0x20,0x20,0x40,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x1c,0x10,0x20,0x10,
0x20,0x20,0x20,0x10,0x26,0x10,0x26,0x40,0x26,0x20,0x26,0x20,0x26,0x20,
0x26,0x10,0x2b,0x10,0x2b,0x20,0x30,0x10,0x33,0x10,0x39,0x40,0x30,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x40,0x10,0x40,0x80,0x40,0x20,0x39,0x20,
0x30,0x20,0x2b,0x20,0x26,0x20,0x1c,0x20,0x20,0x40,0x1c,0x20,0x1c,0x20,
0x1c,0x20,0x1c,0x10,0x20,0x10,0x20,0x20,0x20,0x10,0x26,0x10,0x26,0x40,
0x26,0x20,0x26,0x20,0x26,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x10,
0x30,0x10,0x39,0x40,0x2b,0x20,0x2b,0x20,0x2b,0x20,0x2b,0x10,0x30,0x10,
0x30,0x80,0x00};

uchar zdjs=0, jp;

void del(yj);

void main(void)
{
        uint dpjs=0;
        uchar yj;
        TMOD=0x01;
        IE=0x82;
        TL0=0xf0;
        TR0=1;
        while(1)
        {  
                zdjs=0;
                dpjs++; yj=sound[dpjs];
                dpjs++; jp=sound[dpjs];
                while(zdjs!=jp)
                {
                        if(yj!=0xff)
                        {
                                if(yj!=0)
                                {
                                        voice=!voice;
                                        del(yj);
                                }
                                else
                                {
                                        dpjs=0;
                                        break;
                                }
                        }
                        else
                        {
                                voice=1;
                                del(jp);
                        }
                }
        }
}

void time0() interrupt 1
{
        TH0=0xd8;
        TL0=0xf0;
        zdjs++;
}

void del(yj)
{
        uchar yj2=2;
        while(yj!=0)
        {      
                while(yj2!=0)
                {
                        yj2--;
                }
                        yj2=2;
                        yj--;  
        }  
}
回复

使用道具 举报

ID:123289 发表于 2018-1-22 13:06 | 显示全部楼层
先读通单片机原理,C语言再好只是语言好,与懂不懂单片机无关!
回复

使用道具 举报

ID:267935 发表于 2018-1-22 15:01 | 显示全部楼层
利用IO口输出PWM波来驱动三极管,从而驱动蜂鸣器,曲子的话,网上都能百度到的
回复

使用道具 举报

ID:278182 发表于 2018-1-22 21:53 | 显示全部楼层
谢谢各位,太感谢了
回复

使用道具 举报

ID:247467 发表于 2018-1-23 08:50 | 显示全部楼层
找到你需要的曲谱在稍微修改,在单片机中运行看看看看效果咋样。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表