標(biāo)題:
多模式帶音樂(lè)跑馬燈單片機(jī)程序+Proteus仿真
[打印本頁(yè)]
作者:
xin16
時(shí)間:
2020-11-13 12:36
標(biāo)題:
多模式帶音樂(lè)跑馬燈單片機(jī)程序+Proteus仿真
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
捕獲.PNG
(91.71 KB, 下載次數(shù): 75)
下載附件
2020-11-13 12:33 上傳
共有6個(gè)模式,可以顯示速率和模式。
單片機(jī)源程序如下:
#include <reg51.h>
#define uchar unsigned cha
#define uint unsigned int
//定義特殊功能寄存器的位變量
sbit key1=P3^2;//按key1可自動(dòng)//外部中斷
sbit key2=P3^3;//按key2可切換//外部中斷
sbit fm=P3^7;//蜂鳴器連續(xù)的IO口
sbit key3=P3^4;
sbit key4=P3^5;
sbit wei=P3^0;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共陰極數(shù)碼管顯示碼制0~F
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //共陰極數(shù)碼管顯示碼制0~F
//速度模式共有5種
unsigned char code SpeedCode[]={100,60,30,14,5};
uchar count2=1,RunMode=1;//音樂(lè)、流水燈標(biāo)志
uchar zd=1; //自動(dòng)播放標(biāo)志
uchar timeh,timel,i;
uint LEDIndex = 0;//LED位置,聲明8位變量LEDIndex,用來(lái)指示8個(gè)LED哪個(gè)燈被點(diǎn)亮
bit LEDDirection = 1,LEDFlag = 1;//LED方向,LED亮滅
uint Timer0Count,SystemSpeed;//跑馬燈速度,系統(tǒng)速度
char SystemSpeedIndex=1;//速度模式參數(shù)
//---------------------------簡(jiǎn)譜---------------------------------------
//編程規(guī)則:字節(jié)高位是簡(jiǎn)譜,低位是持續(xù)時(shí)間,
//代表多少個(gè)十六分音符
//1-7代表中央C調(diào),8-E代表高八度,0代表停頓
//最后的0是結(jié)束標(biāo)志
uchar code qnzl[]={ //千年之戀
0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,
//前奏
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
//竹林的燈火 到過(guò)的沙漠
0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,
//七色的國(guó)度 不斷飄逸風(fēng)中
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
//有一種神秘 灰色的旋渦
0x32,0x31,0x21,0x32,0x82,0x83,0x71,0x72,0x02,
//將我卷入了迷霧中
0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,
//看不清的雙手
0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,
//一朵花傳來(lái)誰(shuí)經(jīng)過(guò)的溫柔
0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,
//穿越千年的傷痛
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
//只為求一個(gè)結(jié)果
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
//你留下的輪廓 指引我
0x42,0x61,0x62,0x83,0x83,0x91,0x91,
//黑夜中不寂寞
0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,
//穿越千年的哀愁
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
//是你在盡頭等我
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
//最美麗的感動(dòng) 會(huì)值得
0x42,0x82,0x88,0x02,0x74,0x94,0x89,0xff//結(jié)束標(biāo)志
//用一生守候
} ;
uchar code jmszl[]={ //寂寞沙洲冷
0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,
//自你走后心憔悴
0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,
//白色油桐風(fēng)中紛飛
0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,
//落花似人有情 這個(gè)季節(jié)
0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,
//河畔的風(fēng)放肆拼命地吹
0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,
//不斷撥弄離人的眼淚
0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,
//那樣濃烈的愛(ài)再也無(wú)法給
0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,
//傷感一夜一夜
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,
//當(dāng)記憶的線纏繞過(guò)往支離破碎
0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,
//是慌亂占據(jù)了心扉
0x31,0x81,0x81,0x81,0x61,0x91,0x82,
//有花兒伴著蝴蝶
0x51,0x51,0x51,0x51,0x31,0x61,0x53,
//孤雁可以雙飛
0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,
//夜深人靜獨(dú)徘徊
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,
//當(dāng)幸福戀人寄來(lái)紅色分享喜悅
0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,
//閉上雙眼難過(guò)頭也不敢回
0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,
//仍然撿盡寒枝不肯安歇微帶著后悔
0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,
//寂寞沙洲我該思念誰(shuí)
0xff};
uchar code srkl[]={0x52,0x52,0x64,0x54,0x84,0x74,//生日快樂(lè)
0x52,0x52,0x64,0x54,0x94,0x84,
0x52,0x52,0xc4,0xA4,0x84,0x74,0x64,
0xB2,0xB2,0xA4,0x84,0x94,0x84,
0x52,0x52,0x64,0x54,0x84,0x74,
0x52,0x52,0x64,0x54,0x94,0x84,
0x52,0x52,0xc4,0xA4,0x84,0x74,0x64,
0xB2,0xB2,0xA4,0x84,0x94,0x84,0xff};
uchar code goon[]={0x64,0x84,0x92,0x91,0x81,0x93,0x61, //康定情歌
0x54,0x62,0x82,0x92,0x91,0x81,0x92,
0x64,0x62,0x82,0x92,0x91,0x81,0x93,
0x61,0x54,0x82,0x62,0x51,0x61,0x51,
0x41,0x52,0x24,0x92,0x54,0x82,0x64,
0x52,0x42,0x94,0x82,0x62,0x51,0x61,
0x51,0x41,0x52,0x24,0xff};
//----------------------------簡(jiǎn)譜音調(diào)對(duì)應(yīng)的定時(shí)器初值---------------------------
//適合11.0592M的晶振
uchar code chuzhi[]={
0xff,0xff,//占位
0xFC,0x8E,//中央C調(diào)1-7
0xFC,0xED,
0xFD,0x43,
0xFD,0x6A,
0xFD,0xB3,
0xFD,0xF3,
0xFE,0x2D,
0xFE,0x47, //高八度1-7
0xFE,0x76,
0xFE,0xA1,
0xFE,0xC7,
0xFE,0xD9,
0xFE,0xF9,
0xFF,0x16
};
void LEDShow(unsigned int LEDStatus)
{
P1 = ~(LEDStatus&0x00FF); //P1輸出低八位
}
//速度選擇函數(shù)
void SetSpeed(unsigned char Speed)
{
SystemSpeed =SpeedCode[Speed];//速度模式
}
void delay(uint z) //延時(shí)165MS,即十六分音符
{ uint x,y;
for(x=z;x>0;x--)
for(y=19000;y>0;y--);
}
void delay1(uint z) //延時(shí)1MS
{ uint x,y;
for(x=z;x>0;x--)
for(y=112;y>0;y--);
}
//模式1(一個(gè)燈右移)
void Mode_1(void)
{
LEDShow(0x80>>LEDIndex);
LEDIndex = (LEDIndex+1)%8;//LEDIndex+1對(duì)8取余,保證LEDIndex在0~7之間
}
//Mode 2
void Mode_2(void)
{
if(LEDDirection) //選擇LED方向
LEDShow(~(0x01<<LEDIndex)); //LED一個(gè)滅的燈左移
else
LEDShow(~(0x80>>LEDIndex));//LED一個(gè)滅的燈右移
if(LEDIndex==7)
LEDDirection = !LEDDirection;//改變方向
LEDIndex = (LEDIndex+1)%8;//循環(huán)
}
//Mode 3
void Mode_3(void)
{
if(LEDDirection)
{
if(LEDFlag) //LED滅
LEDShow(0xAA<<LEDIndex); //四滅的燈左移
else
LEDShow(~(0x55>>LEDIndex));//四個(gè)亮的燈右移
}
else
{
if(LEDFlag)
LEDShow(0x7F>>LEDIndex); //一個(gè)滅的燈右移
else
LEDShow(~(0xFE<<LEDIndex));//一個(gè)亮的燈左移
}
if(LEDIndex==7)
{
LEDDirection = !LEDDirection;
if(LEDDirection) LEDFlag = !LEDFlag;
}
LEDIndex = (LEDIndex+1)%8;
}
//Mode 4
void Mode_4(void)
{
if(LEDDirection)
LEDShow(0x0F<<LEDIndex); //四個(gè)右燈左移
else
LEDShow(0xF0>>LEDIndex); //四個(gè)左燈右移
if(LEDIndex==7)
LEDDirection = !LEDDirection;
LEDIndex = (LEDIndex+1)%8;
}
//Mode 5
void Mode_5(void)
{
if(LEDDirection)
LEDShow(~(0x0F<<LEDIndex));//左四個(gè)燈左移
else
LEDShow(~(0xF0>>LEDIndex));//四個(gè)燈右移
if(LEDIndex==7)
LEDDirection = !LEDDirection;
LEDIndex = (LEDIndex+1)%8;
}
//Mode 6
void Mode_6(void)
{
if(LEDDirection)
LEDShow(~(0x1F<<LEDIndex));//左五個(gè)燈左移
else
LEDShow(~(0x30>>LEDIndex));//兩個(gè)燈右移
if(LEDIndex==7)
LEDDirection = !LEDDirection;
LEDIndex = (LEDIndex+1)%8;
}
void Timer0EventRun(void)
{
switch(RunMode)
{
case 1:Mode_1();break;
case 2:Mode_2();break;
case 3:Mode_3();break;
case 4:Mode_4();break;
case 5:Mode_5();break;
case 6:Mode_6();break;
default:break;
}
}
void song()
{
uint temp;
uchar jp;//jp是簡(jiǎn)譜
i=0;
while(1)
{
if(key3==0)
{
delay1(7);
if(key3==0)
{
P2=tab[SystemSpeedIndex];
SystemSpeedIndex++; //選擇速度
if(SystemSpeedIndex>5)
SystemSpeedIndex=1;
SetSpeed(SystemSpeedIndex);
}
while(!key3);
}
if(key4==0)
{
delay1(7);
if(key4==0)
{
P2=tab[SystemSpeedIndex];
SystemSpeedIndex--;
if(SystemSpeedIndex<1)
SystemSpeedIndex=5;
SetSpeed(SystemSpeedIndex);
}
while(!key4);
}
P0=table[RunMode]; //共陰極數(shù)碼管顯示碼制
P2=tab[SystemSpeedIndex];//速度顯示
if(count2==0)
{
break;
}
if(count2==1) //千年之戀
temp=qnzl[i];
if(count2==2) //寂寞沙洲冷
temp=jmszl[i];
if(count2==3) //生日快樂(lè)
temp=srkl[i];
if(count2==4) //康定情歌
temp=goon[i];
if(temp==0xff)
break;
jp=temp/16; //取數(shù)的高4位
if(jp!=0)
{
timeh = chuzhi[jp*2];
timel = chuzhi[jp*2+1];
TR0=1;//定時(shí)器T0的開(kāi)定時(shí)器位,TR0=1表示開(kāi)定時(shí)器
}
else
{
TR0=0;//定時(shí)器T0的開(kāi)定時(shí)器位,TR0=0表示關(guān)定時(shí)器
fm=0;//關(guān)蜂鳴器
}
delay(temp%16); //取數(shù)的低4位
TR0=0; //唱完一個(gè)音停15MS,//定時(shí)器T0的開(kāi)定時(shí)器位,TR0=0表示關(guān)定時(shí)器
fm=0;
i++;
}
TR0=0;//定時(shí)器T0的開(kāi)定時(shí)器位,TR0=0表示關(guān)定時(shí)器
fm=0;
}
main()
{
EX1=1;//開(kāi)外部中斷1
IT1=1;//外部中斷1下降沿觸發(fā)方式
EX0=1;//開(kāi)外部中斷0
IT0=1;//外部中斷0下降沿觸發(fā)方式
TMOD=0x11;//定時(shí)器0,1工作在方式1
TH0=0;TL0=0;
TH1=0xFC;//定時(shí)器1 的高8位里寫(xiě)入十六進(jìn)制值 0xFC
TL1=0xFB;
ET0=1;//定時(shí)器0的中斷允許開(kāi)關(guān)
ET1=1;
TR0=0;//定時(shí)器T0的開(kāi)定時(shí)器位,TR0=0表示關(guān)定時(shí)器
wei=1;
P0=table[1];//共陰(陽(yáng))極數(shù)碼管顯示碼制0~F
P2=tab[1];
SetSpeed(0);
EA=1;//開(kāi)總中斷
TR1=1;//啟動(dòng)定時(shí)器T
while(1)
{
song();
delay1(50);
if(zd)//自動(dòng)播放標(biāo)志zd=1
{
count2++;
if(count2>4)
count2=1;
RunMode++;
if(RunMode>6)
RunMode=1;
SystemSpeedIndex++;
if(SystemSpeedIndex>5)
SystemSpeedIndex=1;
if(SystemSpeedIndex<1)
SystemSpeedIndex=5;
}
}
}
void int1() interrupt 2 //自動(dòng)播放歌曲和跑馬燈,外部中斷1
{
EA=0;//關(guān)總中斷
if(key2==0)
{
delay1(7);
if(key2==0)
{
zd = 0;
TR0=1; //定時(shí)器T0的開(kāi)定時(shí)器位,TR0=1表示開(kāi)定時(shí)器
i=0;//從頭開(kāi)始唱
delay1(5);
count2++;
if(count2>4)//音樂(lè)標(biāo)志
count2=1;
RunMode++;
if(RunMode>6)//流水燈標(biāo)志
RunMode=1;
}
while(!key2);
}
EA=1;//開(kāi)總中斷
}
void int0() interrupt 0
{
EA=0;
delay1(7);
if(key1==0)
{
delay1(5);
if(key1==0)
{
zd=1;
}
while(!key1);
}
EA=1;
}
void timer0() interrupt 1
{
EA=0;
TR0=0;
TH0=timeh;
TL0=timel;
fm=~fm;
TR0=1;
EA=1;
}
void Timer1() interrupt 3
{
EA=0;
TR1=0;
TH1=0XFC;
TL1=0xfB;
Timer0Count++;
if(Timer0Count>=SystemSpeed)
{
Timer0Count=0;
Timer0EventRun();
}
TR1=1;
EA=1;
}
復(fù)制代碼
全部資料51hei下載地址:
多模式帶音樂(lè)跑馬燈.rar
(61.6 KB, 下載次數(shù): 82)
2020-11-13 12:38 上傳
點(diǎn)擊文件名下載附件
程序和仿真圖
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1