2022年多功能时钟程序 .pdf
《2022年多功能时钟程序 .pdf》由会员分享,可在线阅读,更多相关《2022年多功能时钟程序 .pdf(35页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、多功能时钟程序:#include #include #define SoundSpace 3/4 unsigned char sec,min,hour,hour1,day,month,year,cen,week,shishi,fenfen; /秒,分,小时,公历日期,月份,年份,世纪,星期unsigned char LunarMonth,LunarDay,LunarYear; /阴历日期,月份,年份bit c_moon; /大小月标志位bit cenbit=0; /世纪标志,为1,则表示 20 世纪,为 0,则表示 21 世纪bit w,liu,xiao,wei; /调时状态标志位unsign
2、ed char next,s2num,bp,ha; /*定义 LCD12864 的数据线和相关函数*/ sbit LCD_RS=P10; /模式位,低电平输入指令,高电平输入数据sbit LCD_RW=P11; /读写位,低电平读,高电平写sbit LCD_E=P12; /LCD12864 使能位,低电平无效,高电平使能sbit PSB=P21; #define Busy 0 x80 #define LCD_Data P0 /LCD12864 的 8 位数据总线,单片机的P0 端口void WriteDataLCD(unsigned char WDLCD); /给 LCD12864 写数据vo
3、id WriteCommandLCD(unsigned char WCLCD); /给 LCD12864 写指令void LCDInit(void); /LCD12864 初始化void LCDClear(void); /LCD12864 清屏void CheckBusy(void); /忙检测void LCDSendWord(unsigned char *p); /向 LCD 发送一个字符串void LCDTestWord(bit i,unsigned char word); void DisplaySec(void); /在 LCD 上显示秒钟void DisplayMin(void);
4、/在 LCD 上显示分钟void DisplayHour(void); /在 LCD 上显示小时void DisplayDay(void); /在 LCD 上显示日期void DisplayYear(void); /在 LCD 上显示年份void DisplayMonth(void); /在 LCD 上显示月份void DisplayWeek(void); /在 LCD 上显示星期void Holidays(void); /在 LCD 上显示节日/*定义时间芯片DS1302 的数据线及相关函数*/ sbit DS1302_CLK=P22; /DS1302 的时钟信号线sbit DS1302_I
5、O=P23; /DS1302 的数据端口线sbit DS1302_RST=P24; /DS1302 的片选信号线void WriteDs1302byte(unsigned char temp); /给 DS1302 写一个字节void WriteDs1302(unsigned char address,unsigned char dat); /给 DS1302 写入时间, 先确定地址,再确定要写入的数据unsigned char ReadDs1302(unsigned char address); /读取 DS1302 的数据void InitDS1302(); 名师资料总结 - - -精品资
6、料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 35 页 - - - - - - - - - /*定义温度传感器DS18B20 的数据线和相关函数*/ sbit DS18B20=P25; /DS18B20 的数据线int temp; void DS18B20Init(void); /DS18B20 初始化函数int GetTemp(void); void TempWriteByte(unsigned char dat); unsigned char TempRead(void); bit TempRe
7、adBit(void); void delayb(unsigned int count); void TempChange(void); void Conversion(bit cen_m,unsigned char year,unsigned char month,unsigned char day); bit GetMoonDay(unsigned char month_p,unsigned int TableAddr); void DisplayShengXiao(void); void DelayMs(unsigned int a); void Delay(unsigned char
8、num); /*定义开关按键 ,及蜂鸣器的数据线*/ sbit SetKey=P32; /按键功能:设置sbit SureKey=P35; /按键功能:确认sbit PlusKey=P36; /按键功能:加sbit ReduceKey=P37; /按键功能:减sbit qiehuan=P30; /1224 sbit naozhong=P31; /naozhong void Key(void); code unsigned char tablemin=0 x00,0 x01,0 x02,0 x03,0 x04,0 x05,0 x06,0 x07,0 x08,0 x09,0 x10,0 x11,0
9、 x12,0 x13,0 x14,0 x15,0 x16,0 x17,0 x18,0 x19,0 x20,0 x21,0 x22,0 x23,0 x24,0 x25,0 x26,0 x27,0 x28,0 x29,0 x30,0 x31, 0 x32,0 x33,0 x34,0 x35,0 x36,0 x37,0 x38,0 x39,0 x40,0 x41,0 x42,0 x43,0 x44,0 x45,0 x46,0 x47,0 x48,0 x49,0 x50,0 x51,0 x52,0 x53,0 x54,0 x55,0 x56,0 x57,0 x58,0 x59; #define Fos
10、c 12000000 /定义晶振频率12000000HZ #define jiepai 3/4 /定义普通音符演奏的长度分率,/每 4 分音符间隔sbit Speaker=P33; /定义输出管脚unsigned int code FreTab12=262,277,294,311,330,349,369,392,415,440,466,494; /C 调歌曲 12 个半音的频率unsigned char code PositTab7=0,2,4,5,7,9,11; /17在频率表中的位置unsigned char code LengthTab7=1,2,4,8,16,32,64; unsign
11、ed char SoundTempTH0,SoundTempTL0; /音符定时器初值暂存unsigned char SoundTempTH1,SoundTempTL1; /音长定时器初值暂存void SpeakerDiDi(void); void SpeakerDiDi(void) unsigned char i; for(i=0;i220;i+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 35 页 - - - - - - - - - Speaker=0; Del
12、ayMs(10); Speaker=1; DelayMs(10); void InitialSound(void) Speaker=1; SoundTempTH1=(65535-(1/1200)*Fosc)/256; /计算 TL1 应装入的初值(10ms 的初装值 ) SoundTempTL1=(65535-(1/1200)*Fosc)%256; /计算 TH1 应装入的初值TH1=SoundTempTH1; /装入初值TL1=SoundTempTL1; TMOD|=0 x11; /定时器 1、2 都工作在定时器模式,工作方式1 ET0=1; /定时器 0 开中断ET1=0; /定时器 1
13、关中断TR0=0; /不启动定时器TR1=0; /不启动定时器EA=1; /开启 CPU 中断 void Speakertimer(void) interrupt 1 /音符发生中断 Speaker=!Speaker; /取反TH0=SoundTempTH0; /定时器 0 重新装初值TL0=SoundTempTL0; void PlayMusic(unsigned char *SoundName,unsigned char diaohao,unsigned shengjiang,unsigned int Speed) /SoundName 为演奏歌曲歌名diaohao 是指乐曲升多少个半音演
14、奏/shengjiang 1:降八度 , 2:不升不降 , 3:升八度 ; Speed歌曲演奏速度 unsigned int NewFreTab12; /新的频率表unsigned char i,j; unsigned int Point,yinfuchang,yipai,LDiv1,LDiv2,sifenyifu,CurrentFre,TempCounter,SoundLength; /Point 指针索引值LDiv 音符演奏的长度(多少个 10ms) yipai 1 分音符的长度 (几个 10ms)即一个节拍/sifenyifu 4分 音 符 的 长 度CurrentFre 查 出 对 应
15、 音 符 的 频 率SoundLength 歌曲长度TempCounter 计算计数器初值名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 35 页 - - - - - - - - - unsigned char yindiao,length,yinfu,shengban,gaodi,SLen,leixing,fudian; /yindiao 音调length 音长yinfu 音符gaodi 高低音shengban 是否升半leixing 音符类型 (0 普通 1 连音 2
16、 顿音 ) for(i=0;i11) j=j-12; NewFreTabi=FreTabj*2; /频率翻倍速 else NewFreTabi=FreTabj; if(shengjiang=1) NewFreTabi=2; /如果是降调则频率除以2 else if(shengjiang=3) NewFreTabi=2; /如果是升调则频率翻倍速 SoundLength=0; while(SoundNameSoundLength!=0 x00) /计算歌曲长度以 0X00 结尾 SoundLength+=2; Point=0; yindiao=SoundNamePoint; /第一个字节为音符音
17、调length=SoundNamePoint+1; /第二个字节为音符时值yipai=12000/Speed; /算出一个节拍的长度(单位是 10ms) sifenyifu=yipai/4; /算出 4 分音符的长度sifenyifu=sifenyifu-sifenyifu*jiepai; /普通音最长间隔标准TR0=0; TR1=1; /启动定时器1 while(Point=2; /低音,频率除以2 if (gaodi=3) CurrentFre=2; /高音 , 频率翻倍速TempCounter=65536-(50000/CurrentFre)*10/(12000000/Fosc);/计算
18、计数器初值SoundTempTH0=TempCounter/256; SoundTempTL0=TempCounter%256; TH0=SoundTempTH0; TL0=SoundTempTL0+12; /加 12 是对中断延时的补偿名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 35 页 - - - - - - - - - SLen=LengthTablength%10; /算出是几分音符leixing=length/10%10; /算出音符类型 (0 普通 1 连
19、音 2 顿音) fudian=length/100; yinfuchang=yipai/SLen; /算出连音音符演奏的长度(多少个10ms) if (fudian=1) yinfuchang=yinfuchang+yinfuchang/2; if(leixing!=1) if(leixing=0) /算出普通音符的演奏长度if (SLen0;i-) /发规定长度的音 while(TF1=0); TH1=SoundTempTH1; TL1=SoundTempTL1; TF1=0; if(LDiv2!=0) TR0=0; Speaker=0; for(i=LDiv2;i0;i-) /音符间的间隔
20、 while(TF1=0); TH1=SoundTempTH1; TL1=SoundTempTL1; TF1=0; Point+=2; yindiao=SoundNamePoint; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 35 页 - - - - - - - - - length=SoundNamePoint+1; Speaker=1; unsigned char code HappyBirthday= / 生日歌0 x19,0 x03, 0 x19,0 x03
21、, 0 x1A,0 x02, 0 x19,0 x02, 0 x1F,0 x02, 0 x1B,0 x01, 0 x19,0 x03, 0 x19,0 x03, 0 x1A,0 x02, 0 x19,0 x02, 0 x20,0 x02, 0 x1F,0 x01, 0 x19,0 x03, 0 x19,0 x03, 0 x23,0 x02, 0 x21,0 x02, 0 x1F,0 x02, 0 x1B,0 x0C, 0 x1A,0 x15, 0 x22,0 x03, 0 x22,0 x03, 0 x21,0 x02, 0 x1F,0 x02, 0 x20,0 x02, 0 x1F,0 x15,
22、 0 x00,0 x00 ; unsigned char code Butterfly= 0 x17,0 x03, 0 x16,0 x03, 0 x17,0 x01, 0 x16,0 x03, 0 x17,0 x03, 0 x16,0 x03, 0 x15,0 x01, 0 x10,0 x03, 0 x15,0 x03, 0 x16,0 x02, 0 x16,0 x0D, 0 x17,0 x03, 0 x16,0 x03, 0 x15,0 x03, 0 x10,0 x03, 0 x10,0 x0E, 0 x15,0 x04, 0 x0F,0 x01, 0 x17,0 x03, 0 x16,0
23、x03, 0 x17,0 x01, 0 x16,0 x03, 0 x17,0 x03, 0 x16,0 x03, 0 x15,0 x01, 0 x10,0 x03, 0 x15,0 x03, 0 x16,0 x02, 0 x16,0 x0D, 0 x17,0 x03, 0 x16,0 x03, 0 x15,0 x03, 0 x10,0 x03, 0 x15,0 x03, 0 x16,0 x01, 0 x17,0 x03, 0 x16,0 x03, 0 x17,0 x01, 0 x16,0 x03, 0 x17,0 x03, 0 x16,0 x03, 0 x15,0 x01, 0 x10,0 x
24、03, 0 x15,0 x03, 0 x16,0 x02, 0 x16,0 x0D, 0 x17,0 x03, 0 x16,0 x03, 0 x15,0 x03, 0 x10,0 x03, 0 x10,0 x0E, 0 x15,0 x04, 0 x0F,0 x01, 0 x17,0 x03, 0 x19,0 x03, 0 x19,0 x01, 0 x19,0 x03, 0 x1A,0 x03, 0 x19,0 x03, 0 x17,0 x01, 0 x16,0 x03, 0 x16,0 x03, 0 x16,0 x02, 0 x16,0 x0D, 0 x17,0 x03, 0 x16,0 x0
25、3, 0 x15,0 x03, 0 x10,0 x03, 0 x10,0 x0D, 0 x15,0 x00, 0 x19,0 x03, 0 x19,0 x03, 0 x1A,0 x03, 0 x1F,0 x03, 0 x1B,0 x03, 0 x1B,0 x03, 0 x1A,0 x03, 0 x17,0 x0D, 0 x16,0 x03, 0 x16,0 x03, 0 x16,0 x0D, 0 x17,0 x01, 0 x17,0 x03, 0 x17,0 x03, 0 x19,0 x03, 0 x1A,0 x02, 0 x1A,0 x02, 0 x10,0 x03, 0 x17,0 x0D
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年多功能时钟程序 2022 多功能 时钟 程序
限制150内