微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 用C51写的单片机音乐彩灯程序

用C51写的单片机音乐彩灯程序

时间:11-22 来源:互联网 点击:
#include

#define TLEDS 20//每个花样状态持续时间,以50mS为单位
#define NUM_OF_LED_S 4//每种模式LED花样数?
typedef unsigned char uchar;
typedef unsigned int uint;

data uchar led_array_num;//当前模式下,LED花样状态序号
data uchar music_array_num;//音符的序号
data uchar i;//模式号
data uchar t_count_led;//用于LED状态持续时间计时
data uint t_count_music;//
data uint t_yinfu;//音符持续时间

uchar code led_array_p0[4][NUM_OF_LED_S]={{0xc0,0xc0,0xc0,0xc0},{0x38,0x38,0x38,0x38},{0x05,0x07,0x02,0x07},{0xff,0x81,0xff,0xff}};
uchar code led_array_p2[4][NUM_OF_LED_S]={{0x07,0x0b,0x0d,0x0e},{0x07,0x09,0x0e,0x00},{0x06,0x09,0x06,0x00},{0x07,0x09,0x0e,0x00}};
uchar code music_array_h[4][128]={{0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfe,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,
0xfc,0xfc,0xfc,0xfc,0xfc,0xfd,0xfd,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfe,0xfd,0xfd,
0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfc,0xfc,0xfc,0xfc,0xfd,0xfc},
{0xfc,0xfc,0xfd,0xfc,0xfc,0xfc,0xfd,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfc,0xfd,
0xfd,0xfd,0xfd,0xfd,0xfc,0xfc,0xfd,0xfc,0xfc,0xfd,0xfc},
{0xfb,0xfb,0xfb,0xfb,0xfc,0xfc,0xfb,0xfb,0xfb,0xfb,0xfc,0xfc,0xfb,0xfb,0xfd,0xfd,0xfc,0xfc,0xfb,0xfd,0xfd,
0xfd,0xfc,0xfc,0xfc},
{0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfc,
0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfc,0xfc,0xfc}};
uchar code music_array_l[4][128]={{0x43,0x43,0x43,0x08,0x81,0x81,0x81,0x81,0xc7,0xc7,0xc7,0x21,0x81,0x33,0x33,0x33,0xc7,0x08,0x08,0x08,0x81,0xb5,0xb5,0xb5,0xb5,0xb5,0x81,0x81,0x43,0x43,0x43,0x08,0x81,0x81,0x81,0x81,0xc7,0xc7,0xc7,0x21,0x81,0x33,0x33,0x33,0xc7,0x08,0x08,0x08,0x08,0x08,0x81,0xb5,0xb5,0xb5,0xb5,0x08,0x43},{0x43,0xb5,0x08,0x43,0x43,0xb5,0x08,0x43,0x08,0x33,0x81,0x08,0x33,0x81,0x81,0xc7,0x81,0x33,0x08,0x43,0x81,0xc7,0x81,0x33,0x08,0x43,0x43,0x81,0x43,0x43,0x81,0x43},{0x03,0x03,0x8f,0x03,0x43,0x0b,0x03,0x03,0x8f,0x03,0xb5,0x43,0x03,0x03,0x81,0x08,0x43,0x0b,0x8f,0x33,0x33,0x08,0x43,0xb5,0x43},{0x43,0x43,0x81,0x81,0xc7,0xc7,0x81,0x33,0x33,0x08,0x08,0xb5,0xb5,0x43,0x81,0x81,0x33,0x33,0x08,0x08,0xb5,0x81,0x81,0x33,0x33,0x08,0x08,0xb5,0x43,0x43,0x81,0x81,0xc7,0xc7,0x81,0x33,0x33,0x08,0x08,0xb5,0xb5,0x43}};
uint code music_array_t[4][128]={{523,523,523,658,784,784,784,784,880,880,880,1046,784,698,698,698,880,658,658,658,784,593,593,593,593,593,784,784,523,523
,523,658,784,784,784,784,880,880,880,1046,784,698,698,698,880,658,658,658,658,658,784,593,593,593,593,658,523,0},{523,593,658,523,523,593,658,523,658,698,3134,658,698,3134,391,440,391,698,658,523,391,568,391,698,658,523,523,784,1046,523,784,1046,0},{391,391,880,391,523,1486,391,391,880,391,593,1569,391,391,3134,658,523,988,440,698,698,1317,523,593,1569,0},{523,523,784,784,880,880,784,698,698,658,658,593,593,523,784,784,698,698,658,658,593,784,784,698,698,658,658,593,523,523,784,784,880,880,784,698,698,658,658,593,593,523,0}};

void delay(void)
{
uchar c_1;
uchar c_2;
uchar c_3;
for (c_1=0; c_1< 10;c_1++)
for(c_2 = 0;c_2< 10;c_2++)
for(c_3 = 0; c_3<55;c_3++)
;
}

void main(void)
{

led_array_num = 0;
music_array_num = 0;
t_yinfu = music_array_t[0][0]; //初始化

//delay();

i = 0;
TMOD = 0x11; //定时器工作方式设置
TR0 = 0; //关定时器
TR1 = 0;

TH1 = 0x3c;
TL1 = 0xb0;
P0 = led_array_p0[0][0];//彩灯
P2 = led_array_p2[0][0];
t_count_led = 0;

TH0 = music_array_h[0][0];//音乐
TL0 = music_array_l[0][0];
t_yinfu = music_array_t[0][0];
t_count_music = 0;

IE = 0x8a;
TR0 = 1;
TR1 = 1;

//以下开始扫描按键
while(1)
{
P1_0 = 1;
if(P1_0 == 0)
{
delay();
if(P1_0 == 0)
{
TR0 = 0;
TR1 = 0;
i = 0;//去

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top