电子音乐选播器源代码教材_第1页
电子音乐选播器源代码教材_第2页
电子音乐选播器源代码教材_第3页
免费预览已结束,剩余28页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include<reg52.h> #include "intrins.h"/#include "delay.h"sbitSPK二P2八2; /扬声器端口定义sbit KEY 1=P3A4;/ 按键端口定义sbit KEY 2=P3A5;sbit KEY3=P3A6;sbit KEY4=P3A7;sbit led1=P1A0; /发光二极管端口定义sbit led2=P1A1;sbit led3=P1A2;sbit led4=P1A3;sbit led5=P1A4;sbit RS = P2A4;/液晶屏端口定义sbit RW = P2A5;s

2、bit E = P2A6;/sbit RES = P2A3;sbit PSB = P2A1;#define DataPort P0/MCU P0<> LCM#define uchar unsigned charunsigned char Timer0_H,Timer0_L,Time;void choose(unsigned int i);void write_sfm(uchar miao,uchar fen);unsigned char f,miao,fen;unsigned char k,i,j;char aa3=0;/ 三个数值一组 : 第一个数值音调 (1234567) 第二

3、个数值 低中高音 第三个数字节拍/昆明湖code unsigned char MUSIC1=1,2,3,6,1,1,5,2,1,2,2,1,3,2,1, 1,2,1, 2,2,1,1,2,1, 6,1,1,1,2,1,2,2,1,3,2,1,5,2,1,3,2,1,3,2,3,3,2,1,2,2,1,1,2,1,1,2,2,1,2,3,6,1,1,5,2,1,2,2,1,3,2,1,1,2,1,2,2,1,1,2,1,6,1,1,5,1,1,5,1,4,6,1,1,1,2,1,2,2,1,3,2,1,2,2,1,1,2,1,6,1,1,5,1,1,1,2,2,6,1,1,5,1,1,6,1,4

4、,1,2,3,6,1,1,5,2,1,2,2,1,3,2,1,1,2,1,2,2,1,1,2,1,6,1,1,5,1,1,5,1,4,6,1,1,5,1,1,6,1,1,1,2,1,2,2,1,3,2,1,5,2,1,3,2,1,3,2,3,3,2,1,2,2,1,1,2,1,1,2,2,1,2,3,6,1,1,5,2,1,2,2,1,3,2,1,1,2,1,2,2,1,1,2,1,6,1,1,5,1,1,5,1,4,0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,3,1,2,3,1,4,1,2,4,6,1,3,1,2,1,6,1,1,5,1,1,3,1,1,5,1,1,6,1,1,1,2,1, 5,1,4,0,0,0;/送别code unsigned char MUSIC5=5,1,2,3,1,3,5,1,1,1,2,36,1,2,1,2,2,5,1,4,5,1,2,1,1,1,2,1,1,3,1,22,1,1,1,1,1,2,1,4,5,1,2,3,1,15,1,1,1,2,2,7,1,1,6,1,2,1,2,2,5,1,4,5,1,22,1,1,3,1,1,4,1,2,

9、7,1,1,1,1,4,6,1,2,1,2,21,2,4,7,1,2,6,1,1,7,1,1,1,2,4,6,1,1,7,1,1,1,2,1,6,1,1,6,1,1,5,1,1,3,1,1,1,1,1,2,1,4,5,1,2,3,1,1, 5,1,1, 1,2,2, 7,1,1,6,1,2,1,2,2,5,1,4,5,1,2,2,1,1,3,1,1,4,1,2,7,1,2,1,1,4,5,1,2,3,1,3,5,1,1,1,2,3,6,1,2,1,2,2,5,1,4,5,1,2,1,1,1,2,1,1,3,1,2,2,1,1,1,1,1,2,1,4,5,1,2,3,1,1,5,1,1,1,2,

10、2,7,1,1,6,1,2,1,2,2,5,1,4,5,1,2,2,1,1,3,1,1,4,1,2,7,1,1, 1,1,4,0,0,0;/ 音阶表高八位code unsigned char FREQH=0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, /1,2,3,4,5,6,7,8,i0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,/ 高音 2,3,4,5,6,70xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,/超高音 1,2,3,4,5,6,7 ;/音阶表低八位code unsigned char FREQL=0x42,0

11、xC1,0x17,0xB6,0xD0,0xD1,0xB6,0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, /1,2,3,4,5,6,7,8,i0xEE,0x44,0x6B,0xB4,0xF4,0x2D,0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,;/微秒延时void DelayUs2x(unsigned char t)while(-t);/毫秒延时void DelayMs(unsigned char t)while(t-)DelayUs2x(245);DelayUs2x(245);/写命令void Write_Cmd(unsigned

12、 char Cmd) RS=0;RW=0;E=1;DataPort=Cmd;DelayUs2x(5);E=0;DelayUs2x(5);/写数据 void Write_Data(unsigned char Data)RS=1;RW=0;E=1;DataPort=Data;DelayUs2x(5);E=0;DelayUs2x(5);/液晶初始化void Init_ST7920()DelayMs(40);PSB=1;DelayMs(1);Write_Cmd(0x30);DelayUs2x(50);Write_Cmd(0x30);DelayUs2x(20);Write_Cmd(0x0c);Delay

13、Us2x(50);Write_Cmd(0x01);DelayMs(15);Write_Cmd(0x06);DelayUs2x(50);/*显示字符串x:范围08y: 范围 14 */void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s)switch(y)case 1: Write_Cmd(0x80+x);break;case 2: Write_Cmd(0x90+x);break;case 3: Write_Cmd(0x88+x);break;case 4: Write_Cmd(0x98+x);break

14、; default:break;while(*s>0)Write_Data(*s);s+;DelayUs2x(50);/清屏函数void ClrScreen()Write_Cmd(0x01);DelayMs(15); void xianshi1()ClrScreen();Init_ST7920();/3?e?-LCD_PutString(0,1," ?b?x矽 i 也");LCD_PutStri ng(0,2," a?-d ");LCD_PutString(0,3,"?o? eX&eue? S');LCD_PutStrin

15、g(0,4," ?Xp 11?od? e"); void xianshi2() ClrScreen();Init_ST7920();/3? e?-LCD_PutString(0,1," ?o?xd 蛋");LCD_PutString(0,2,"e ue? e);LCD_PutString(0,3,"? 0? ex;W? 1?');LCD_PutString(0,4," ?x 11?od? e"); void xianshi3() ClrScreen();Init_ST7920();/3?e?-LCD_Put

16、String(O,1," ?b?x;Qete? S);LCD_PutString(0,2," D? 1? ");LCD_PutString(0,3,"?o? eX&do x£);LCD_PutString(0,4," ?Xp 11?od? e"); void xianshi4() ClrScreen();Init_ST7920();/3? e?-LCD_PutString(0,1," ?dgeXD? 1?");LCD_PutString(0,2,"do x£');LCD

17、_PutString(0,3,"? 0? ex矽 i 也");LCD_PutString(0,4," ?xq 11?od? e"); void xianshi5() ClrScreen();Init_ST7920();/3?e?-LCD_PutString(0,1," ?d(?eXodo x£);LCD_PutString(0,2,"? i 主");LCD_PutString(0,3,"? 0? ex圮qi?P");LCD_PutString(0,4," ?xq 11?od? e&qu

18、ot;); /*? u ? oe ±o_ey?+a+|/4? u ?e±?址旷4/4125ms2/4 250ms怕/4187ms*/ void delay(unsigned int t)unsigned int g; for(g=0;g<t;g+) DelayMs(250);TR0=0;/*延时*/ void delay_us(int us) int g;for(g=0;g<50;g+)_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();void delay_ms(int ms)int

19、g;for(g=0;g<10000;g+)delay_us(1);/*中断*/void TIM0_ISR() interrupt 1TR0=0;SPK=!SPK;TH0=Timer0_H;TL0=Timer0_L;TR0=1;/*计时函数*/ void write_sfm(uchar miao,uchar fen) int i;int j=0;aa0=fen%10;aa1=miao/10;aa2=miao%10;LCD_PutString(5,2,":");for(i=0;i<3;i+)Write_Cmd(0x94+j);Write_Data(aai+0x30)

20、; j+;if(j=3)j=0;void Init_Timer1(void)TMOD |= 0x10;TH1=(65536-50000)/256;TL1=(65536-50000)%256;EA=1;ET1=1;TR1=1;void Timer1_isr(void) interrupt 3TH1=(65536-50000)/256;TL1=(65536-50000)%256;f+;if(f=20)f=0;miao+;if(miao=60)miao=0;fen+;write_sfm(miao,fen);/*歌曲演奏函数*/void Song()TH0=Timer0_H;TL0=Timer0_L;

21、TR0=1;delay(Time);/*按键扫描函数*/unsigned char KeyScan(void) if(KEY1=0) delay_us(10)if(KEY1=0) return 1;/* else if(KEY2=0) delay_us(10);return 2;/* else if(KEY3=0) delay_us(10); if(KEY3=0) return 3; /* else if(KEY4=0) delay_us(10);if(KEY4=0) return 4; /* elsereturn 0;/*歌曲播放函数*/ void Play(unsigned int cou

22、nt) switch(count) case 1: xianshi1(); led1=0; i=0;j=0;miao=fen=0;while(i<240) k=MUSIC1i+7*MUSIC1i+1-1; Timer0_H=FREQHk;Timer0_L=FREQLk;Time=MUSIC1i+2;i=i+3;Song();j=KeyScan();if(j=1)led1=1;choose(5);if(j=2)led1=1;choose(2);/暂停if(j=3)led1=1;while(1)j=KeyScan();if(j=4)break;led1=0;led1=1;delay_ms(5

23、);case 2: xianshi2();led2=0;i=0;j=0;miao=fen=0;while(i<75)k=MUSIC2i+7*MUSIC2i+1-1;Timer0_H=FREQHk;Timer0_L=FREQLk;Time=MUSIC2i+2;i=i+3;Song();j=KeyScan();if(j=1)led2=1;choose(1);if(j=2)led2=1;choose(3);if(j=3)led2=1;while(1)j=KeyScan();if(j=4)break;led2=1;delay_ms(5);case 3: xianshi3();led3=0;i=0;j=0;miao=fen=0;while(i<230) k=MUSIC3i+7*MUSIC3i+1-1; Timer0_H=FREQHk; Timer0_L=FREQLk; Time=MUSIC3i+2;i=i+3;Song();j=KeyScan();if(j=1)led3=1;choose(2);if(j=2)led

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论