51单片机温度计+计数器(可调时间)+蜂鸣器(生日歌)+花样流水灯6种

合集下载

51单片机爱心流水灯原理及制作

51单片机爱心流水灯原理及制作

C 2—*XTAL1—P?-R5470△型30PSEN ALE FALED 32 1'LED31 2! _E「〔i 3 丄::艾_4!岸匚舷5L 竺总 :LEt® 了 :-已不芒P1 M2P- \T ;EX P\2 pfiLEMRiyELEr^BF 2 Li I■ 05 L£kfllR4D2p-F?RT2LtO@Ri>M —n <TE3.T>■LEr--BIH^ ■iflEJC" 尺2l --------- 1_CTM» ^TEJ.T=jv ---------1L£EQD a 6 LE 仙日問Tl TR31砂JL : r - ■二□ 1LHbtiROi<reAi*^C17 1 •PT1 --------- r 也LES-BF-3JOJl-■=>DiflLE>BIF3R77<L~-LEBHR-ft126UEP-jJ月:訥4KRFHLED*"耶R24R13 TF ―—■■——■ i'fOfc .E[I •曰耶口1 - P14"E- v L2b1 二盲 Q ?3耕LFBTW.LEMRAnpR151! ■R2DLEI :1,1L=PUI ID31 丐Ml 舲 ■=rE^7=- LECJpT-.D13 RIB i " °^DR "5FU2<ED !4R13LEE BF ILE : BR :-■* ----4?0RLE :』赋iEW1、51 单片机x1、40Pin 座x12、L ED x32 (建议用5mm七彩的)3、电阻470Q x334、晶振12MHz x15、10uf电解电容x1、谐振瓷片电容30pf x26•其他的可以看自己的爱好去加7、其实也可以不用那么多的电阻,用几个排阻就OKT 效果展示:作品程序:#in clude<>#defi ne uchar un sig ned charuchar flag=200;/////////////////////////////////////////////////////////////////////// uchar codeTab1[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xFF};〃下uchar codeTab2[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF};〃上uchar code Tab3[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};〃下uchar code Tab4[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00};〃上uchar code Tab11[]={0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0xff};〃下uchar code Tab22[]={0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00,0xff};//////////////////////////////////////////////////////////////////// 暗中左移向暗中右移向亮中左移向亮中右移向暗中左移向uchar code Tab33[]={0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF}; uchar code Tab44[]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};uchar code Tab55[]={0x08,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff};uchar code Tab5[]={0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xff}; uchar code Tab6[]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xff}; uchar code Tab7[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};uchar code Tab8[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};////////////////////////////////////////////////////////////////void shansuo();void xl(); /////////////////////////////////////////////////////////////// void delay(void){unsigned char m,n; for(m=0;m<flag;m++) for(n=0;n<250;n++)J} /////////////////////////////////// void hy1(void) // 点亮状态逆时针旋转90 度(一个一个{灭)unsigned char i; for(i=0;i<8;i++){ P0=Tab11[i];P1=Tab22[i];P2=Tab11[i];P3=Tab22[i]; delay();} for(i=0;i<8;i++){ P0=Tab44[i];P1=Tab55[i];P2=Tab44[i];P3=Tab55[i];delay();}}///////////////////////////////////////////void hy2(void) // 暗中逆时针转360 。

单片机 51 汇编 花样 流水灯—五种花样

单片机 51 汇编 花样 流水灯—五种花样

**==================花样流水灯=============================**;*********************花样五变流水灯******************************##;##:R7,R6延时;R5延时控制R4,R3 控制循环次数##;##:*HUAYANG* 花样模块开始*BIAOZHI* 花样切换标志##;##:单片机STC89C51 LED花样流水灯五种变化连续汇编基础##;##: 编写:毛毛虫##;*****毛毛虫*************毛毛虫***********毛毛虫****************** ##;--------------------------------------低电位灯亮---------------------------------------------## ORG 0000HSJMP MAINORG 0030H;*****************************************************************##;##——————————————主程序开始—————————————##;##—————————————花样1————————————————##;##***************************************************************##MAIN:LCALL DELAYLCALL HUAYANGYI //调用花样一LCALL BIAOZHI //花样切换标志LCALL DELAYLCALL HUAYANGER //调用花样二LCALL BIAOZHI //花样切换标志LCALL DELAYLCALL HUAYANGSAN //调用花样三LCALL BIAOZHI //花样切换标志LCALL DELAYLCALL HUAYANGSI //调用花样四LCALL BIAOZHI //花样切换标志LCALL DELAYLCALL HUAYANGWU //调用花样五LCALL BIAOZHI //花样切换标志LCALL DELAYAJMP MAIN //跳转到MAIN主函数运行//***************************花样一*********************************HUAYANGYI:MOV A,#0FEH ;给累加器A赋值MOV R3,#05 ;给R3寄存器赋值DEL0:MOV R4,#07 ;给R4寄存器赋值DEL1:MOV P0,A ;累加器A值送至P0口,LED灯亮LCALL DELAY ;延时RL A ;累加器A向左移一位DJNZ R4,DEL1 ;实现L1~L8逐个亮一次即判断循环DJNZ R3,DEL0 ;实现L1~L8灯逐个亮一次,执行7次即判断循环LCALL DELAY ;延时LCALL DELAYMOV A,#0FEH ;累加器A赋值MOV R3,#05 ;寄存器R3 赋值DEL2:MOV R4,#07 ;寄存器R4 赋值DEL3:MOV P0,A ;累加器A传送至p0 口LCALL DELAY ;延时RR A ;累加器A 右移DJNZ R4,DEL3 ;判断循环,实现花样DJNZ R3,DEL2 ;判断循环,实现花样次数LCALL DELAYMOV A,#0FEH ;累加器A 赋值MOV R3,#07 ;寄存器R3 赋值DEL4: MOV R4,#07 ;寄存器R4 赋值DEL5: MOV P0,A ;累加器A 送至P0口LCALL DELAYRL A ;累加器A 左移一位DJNZ R4,DEL5 ;判断循环LCALL DELAYMOV R4,#07 ;赋值DEL6: MOV P0,A ;传送LCALL DELAYRR A ;右移DJNZ R4,DEL6 ;判断循环DJNZ R3,DEL4 ;判断循环LCALL DELAYRET;-----------------------------------------------------------------------------------------------------------##;##****************************花样2××××××××××××××××××××××××××××××××## ;————————————————————————————————————## HUAYANGER:MOV A,#01HMOV R3,#05HYE0:MOV R4,#07HYE1:MOV P0,ALCALL DELAYLCALL DELAYRL ADJNZ R4,HYE1DJNZ R3,HYE0LCALL DELAYMOV R3,#05HYE2:MOV R4,#07HYE3:MOV P0,ALCALL DELAYLCALL DELAYRR ADJNZ R4,HYE3DJNZ R3,HYE2LCALL DELAYMOV R3,#05HYE4:MOV R4,#07HYE5:MOV P0,ALCALL DELAYRLC ADJNZ R4,HYE5MOV R4,#07HYE6: MOV P0,ALCALL DELAYRRC ADJNZ R4,HYE6DJNZ R3,HYE4LCALL DELAYRET;------------------------------------------------------------------------------------------------------------##;##***************************花样3×××××××××××××××××××××××××××××××××## ;————————————————————————————————————## HUAYANGSAN:MOV A,#0FEHMOV R3,#07HYS0:MOV R4,#14HYS1:MOV P0,ALCALL DELAYLCALL DELAYRL ADJNZ R4,HYS1MOV A,#0FCHHYS2:MOV R4,#12HYS3:MOV P0,ALCALL DELAYLCALL DELAYLCALL DELAYRL ADJNZ R4,HYS3MOV A,#0F8H HYS4:MOV R4,#10 HYS5:MOV P0,ALCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYRL ADJNZ R4,HYS5MOV A,#0F0H HYS6:MOV R4,#08 HYS7:MOV P0,ALCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYRL ADJNZ R4,HYS7MOV A,#0E0H HYS8:MOV R4,#06HYS9:MOV P0,ALCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYRL ADJNZ R4,HYS9MOV A,#0C0H HYSA:MOV R4,#04HYSB:MOV P0,ALCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYRL ADJNZ R4,HYSBMOV A,#80H HYSC:MOV R4,#02HYSD:MOV P0,ALCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYRL ADJNZ R4,HYSDLCALL DELAYLCALL DELAYMOV P0,#00HLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYRET;----------------------------------------------------------------------------------------------------------##;##**************************花样4×××××××××××××××××××××××××××××××××## ;———————————————————————————————————##;===========================查表程序============================## HUAYANGSI:MOV DPTR,#TABLEHYSI0:MOV A,#00HHYSI1:MOVC A,@A+DPTRMOV P0,AMOV R4,#08HYSI2:LCALL DELAYDJNZ R4,HYSI2INC DPTRCJNE A,#0FFH,HYSI1RETTABLE: DB 0FEH,0FEH,0FEH,0FEH,0FEHDB 0FBH,0FBH,0FBH,0FBH,0FBHDB 0EFH,0EFH,0EFH,0EFH,0EFHDB 7EH,7EH,7EH,7EH,7EH,7EHDB 3CH,3CH,3CH,3CH,3CH,3CHDB 0AAH,0AAH,0AAH,0AAH,0AAHDB 55H,55H,55H,55H,55H,55HDB 0E7H,0E7H,0E7H,0E7H,0E7HDB 0C3H,0C3H,0C3H,0C3H,0C3HDB 81H,81H,81H,81H,81H,81HDB 00H,00H,00H,00H,00HDB 0FFH;-----------------------------------------------------------------------------------------------------##;##*************************花样5***********************************## ;——————————————————————————————————## HUAYANGWU:MOV A,#0E7HMOV R3,#03HYW0:MOV R4,#07HYW1:MOV P0,ALCALL DELAYMOV P0,#0FFHLCALL DELAYLCALL DELAYDJNZ R4,HYW1MOV R4,#07HYW2:MOV P0,#0A5HLCALL DELAYLCALL DELAYMOV P0,#0FFHLCALL DELAYLCALL DELAYLCALL DELAYDJNZ R4,HYW2MOV R4,#07HYW3:MOV P0,#24HLCALL DELAYLCALL DELAYLCALL DELAYMOV P0,#0FFHLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYDJNZ R4,HYW3MOV R4,#07HYW4:MOV P0,#00HLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYMOV P0,#0FFHLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYDJNZ R4,HYW4DJNZ R3,HYW0RET//***********************延时******************************************## DELAY: MOV R7,#50 ;延时子程序,延时小于0.1S D1: MOV R6,#125D2: NOPNOPNOPNOPNOPNOPDJNZ R6,D2DJNZ R7,D1RET//***********************花样衔接标志***********************************## BIAOZHI: MOV R4,#20 ;循环次数B1: MOV P0,#00HLCALL DELAYMOV P0,#0FFHLCALL DELAYDJNZ R4,B1 ;判断循环结束RETEND。

51单片机经典流水灯程序

51单片机经典流水灯程序

单片机流水灯汇编程序设计开发板上的8只LED为共阳极连接,即单片机输出端为低电平时即可点亮LED。

程序A:;用最直接的方式实现流水灯ORG 0000HSTART:MOV P1,#01111111B ;最下面的LED点亮LCALL DELAY;延时1秒MOV P1,#10111111B ;最下面第二个的LED点亮LCALL DELAY;延时1秒MOV P1,#11011111B ;最下面第三个的LED点亮(以下省略)LCALL DELAYMOV P1,#11101111BLCALL DELAYMOV P1,#11110111BLCALL DELAYMOV P1,#11111011BLCALL DELAYMOV P1,#11111101BLCALL DELAYMOV P1,#11111110BLCALL DELAYMOV P1,#11111111B ;完成第一次循环点亮,延时约0.25秒AJMP START ;反复循环;延时子程序,12M晶振延时约250毫秒DELAY:MOV R4,#2L3: MOV R2 ,#250L1: MOV R3 ,#250L2: DJNZ R3 ,L2DJNZ R2 ,L1DJNZ R4 ,L3RETEND程序B:;用移位方式实现流水灯org 00h ;程序上电从00h开始ajmp main ;跳转到主程序org 0030h ;主程序起始地址main:mov a,#0feh ;给A赋值成11111110loop:mov p1,a ;将A送到P1口,发光二极管低电平点亮lcall delay ;调用延时子程序rl a ;累加器A循环左移一位ajmp loop ;重新送P1显示delay:mov r3,#20 ;最外层循环二十次d1:mov r4,#80 ;次外层循环八十次d2:mov r5,#250 ;最内层循环250次djnz r5,$ ;总共延时2us*250*80*20=0.8Sdjnz r4,d2djnz r3,d1retend51单片机经典流水灯程序,在51单片机的P2口接上8个发光二极管,产生流水灯的移动效果。

51单片机爱心流水灯原理及制作

51单片机爱心流水灯原理及制作

电路原理图:原件清单:1、51单片机x1、40Pin 座x12、LED x32(建议用5mm 七彩的)3、电阻470Ωx334、晶振12MHz x15、10uf 电解电容x1、谐振瓷片电容30pf x26·其他的可以看自己的爱好去加7、其实也可以不用那么多的电阻,用几个排阻就OK了。

~效果展示:作品程序:#include<>#define uchar unsigned char;uchar flag=200;///////////////////////////////////////////////////////////////////////uchar code Tab1[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xFF};//暗中左移向下uchar code Tab2[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF};//暗中右移向上uchar code Tab3[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};//亮中左移向下uchar code Tab4[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00};//亮中右移向上uchar code Tab11[]={0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0xff};//暗中左移向下uchar code Tab22[]={0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00,0xff};////////////////////////////////////////////////////////////////////uchar code Tab33[]={0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF};;uchar code Tab44[]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};uchar code Tab55[]={0x08,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff};uchar code Tab5[]={0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xff};uchar code Tab6[]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xff}; uchar code Tab7[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; uchar code Tab8[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};////////////////////////////////////////////////////////////////void shansuo();void xl();///////////////////////////////////////////////////////////////}void delay(void){unsigned char m,n;for(m=0;m<flag;m++)for(n=0;n<250;n++);}///////////////////////////////////void hy1(void) //点亮状态逆时针旋转90度(一个一个灭){.unsigned char i;for(i=0;i<8;i++){P0=Tab11[i];P1=Tab22[i];P2=Tab11[i];P3=Tab22[i];delay();}for(i=0;i<8;i++){P0=Tab44[i];P1=Tab55[i];!P2=Tab44[i];P3=Tab55[i];delay();}}///////////////////////////////////////////void hy2(void) //暗中逆时针转360。

基于51单片机的花样流水灯设计

基于51单片机的花样流水灯设计

摘要摘要计算机技术的飞速发展和提高,把我们带入了崭新的时代,现在,计算机的应用已经深入到千家万户。

单片微型计算机是制作在一块集成电路芯片上的计算机,简称单片机。

单片机在现在社会有着广泛的应用,小到人们的日常电子用品,大到航天飞机、宇宙飞船,上面都有单片机的广泛应用。

单片机具有体积小、功能强大、低功耗、应用广泛等特点。

以AT公司的芯片AT89C51 单片机来实现流水灯的设计。

本系统由单片机控制,I/O口接LED的负极,而LED的正极则直接与5V电源相连。

通过I/O口输出的低电平点亮LED灯。

因此可以通过控制单片机的I/O口的电平高低以达到控制LED,从而实现不同花样的流水灯的目的。

关键词:LED,单片机,高低电平变化ABSTRACTThe rapid development of computer technology and improving, bring us to the new era, now, the application of computer has been deep into the thousands. Single chip microcomputer is made on an integrated circuit chip computer, hereinafter referred to as single chip microcomputer. SCM has been widely used in the present society, small to People's Daily electronic products, big to aerospace aircraft, spacecraft, above has the wide application of single-chip microcomputer. SCM has small volume, powerful function, low power consumption, wide application, etc. AT the company's chip AT89C51 single-chip microcomputer to realize the water lamp design. This system controlled by single chip microcomputer, I/O ports connect the LED the cathode, and LED the anode is directly connected to 5V power supply. Through the I/O port output low level light leds. So you can single chip microcomputer control through the I/O ports to control LED, the level of high and low so as to realize the purpose of the different pattern of flowing water light.Key words:LED , MCU ,High and Low output leve目录第1章引言 (1)1.1 研究背景及意义 (1)1.2 国内外研究现状以及本系统的重点问题 (1)1.3 本文章节安排 (2)第2章系统设计方案论证 (3)2.1 控制器模块方案论证 (3)2.2 键盘模块方案 (4)2.3 电源模块方案 (4)第3章系统硬件设计方案 (5)3.1 系统设计框图 (5)3.2 硬件电路设计 (5)3.2.1 电源电路 (5)3.2.2 单片机89C52最小系统 (6)3.2.3 按键电路 (13)3.3.4 LED灯电路 (13)第4章程序设计及软件仿真 (15)4.1 软件设计框图 (15)4.2 软件开发平台选择 (15)4.3 软件系统功能模块 (16)4.3.1 初始化模块 (16)4.3.2 延时函数 (17)4.3.3 定时器控制模块 (17)4.3.4 键盘扫描模式 (17)4.3.5 LED灯输出控制模块 (18)4.4 程序调试仿真 (19)4.4.1 仿真平台介绍 (19)4.4.2 仿真测试 (19)第5章硬件调试 (21)5.1 硬件设计 (21)5.2 硬件调试 (21)5.2.1 下载功能调试 (21)5.2.2 LED电路测试 (21)5.2.3 模式选择功能调试 (22)5.2.4 速度加减功能调试 (22)5.2.5 复位电路调试 (22)5.2.6 稳定性测试 (22)总结 (23)致谢 (25)参考文献 (26)附录 (27)附录一:原理图 (27)附录二:硬件实物 (28)附录三:程序代码 (29)第1章引言第1章引言1.1 研究背景及意义现如今,随着集成化芯片的飞速发展,分立元件或数字逻辑电路正逐步被集成电路所取代,而单片机作为一种集成电路,其价格低廉,且可靠性强、控制简单但控制方法多样。

基于51单片机的流水灯设计

基于51单片机的流水灯设计

基于51单⽚机的流⽔灯设计基于51单⽚机的流⽔灯设计⼀.基本功能利⽤AT89c51作为主控器组成⼀个LED流⽔灯系统,实现8个LED 灯的左、右循环显⽰。

⼆.硬件设计图1.总设计图1.单⽚机最⼩系统1.1选⽤AT89C51的引脚功能图2. AT89C51XTAL1:单芯⽚系统时钟的反向放⼤器输⼊端。

XTAL2:系统时钟的反向放⼤器输出端,⼀般在设计上只要在XTAL1和XTAL2上接上⼀只⽯英震荡晶体系统就可以⼯作了,此外可以在两引脚与地之间加⼊20PF的⼩电容,可以使系统更稳定,避免噪⾳⼲扰⽽死机。

RESET:重置引脚,⾼电平动作,当要对晶体重置时,只要对此引脚电平提升⾄⾼电平并保持两个及其周期以上的时间便能完成系统重置的各项动作,使得内部特殊功能寄存器内容均被设成已知状态。

P3:端⼝3是具有内部提升电路的双向I/O端⼝,通过控制各个端⼝的⾼低电平了实现LED流⽔灯的控制。

1.2复位电路如图所⽰,当按下按键时,就能完成整个系统的复位,使得程序从新运⾏。

图3.复位电路1.3时钟电路时钟电路⽤于产⽣单⽚机⼯作所需要的时钟信号,单⽚机本⾝就是⼀个复杂的同步时序电路,为了保证同步⼯作⽅式的实现,电路应在唯⼀的时钟信号控制下严格地按时序进⾏⼯作。

在AT89C51芯⽚内部有⼀个⾼增益反相放⼤器,其输⼊端为芯⽚引脚X1,输出端为引脚X2,在芯⽚的外部跨接晶体振荡器和微调电容,形成反馈电路,就构成了⼀个稳定的⾃激振荡器。

此电路采⽤12MHz的⽯英晶体。

图4.时钟电路2.流⽔灯部分图5.流⽔灯电路三.软件设计3.1编程语⾔及编程软件的选择本设计选择C语⾔作为编程语⾔。

C语⾔虽然执⾏效率没有汇编语⾔⾼,但语⾔简洁,使⽤⽅便,灵活,运算丰富,表达化类型多样化,数据结构类型丰富,具有结构化的控制语句,程序设计⾃由度⼤,有很好的可重⽤性,可移植性等特点。

⽽汇编语⾔使⽤起来并没有这么⽅便。

本设计选⽤了Keil作为编程软件,.Keil C51⽣成的⽬标代码效率⾮常之⾼,多数语句⽣成的汇编代码很紧凑,容易理解。

51单片机程序(数字温度计)

51单片机程序(数字温度计)

数字温度计1、LCD.c#include <reg51.h>#include<LCD.h>unsigned char code number_X[]={ //宽x高=8x16,纵向字节倒序0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00, //00x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00, //10x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00, //20x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00, //30x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00, //40x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00, //50x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00, //60x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00, //70x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00, //80x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00, //90x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // .0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00, //-0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //nop 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00, //:0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00};void LCD_WriteCommandE1(unsigned char com) {while(CRADD1 & 0x80);CWADD1 = com;}void LCD_WriteDataE1(unsigned char dat)while(CRADD1 & 0x80);DWADD1 = dat;}void LCD_WriteCommandE2(unsigned char com) {while(CRADD2 & 0x80);CWADD2 = com;}void LCD_WriteDataE2(unsigned char dat){while(CRADD2 & 0x80);DWADD2 = dat;}void LCD_Init(){LCD_WriteCommandE1(0xe2);LCD_WriteCommandE2(0xe2);LCD_WriteCommandE1(0xa4);LCD_WriteCommandE2(0xa4);LCD_WriteCommandE1(0xa9);LCD_WriteCommandE2(0xa9);LCD_WriteCommandE1(0xa0);LCD_WriteCommandE2(0xa0);LCD_WriteCommandE1(0xc0);LCD_WriteCommandE2(0xc0);LCD_WriteCommandE1(0xaf);LCD_WriteCommandE2(0xaf);}void LCD_Clear(void){unsigned char i,j;for(i=0;i<4;i++){LCD_WriteCommandE1(i+0xb8);LCD_WriteCommandE2(i+0xb8);LCD_WriteCommandE1(0x00);LCD_WriteCommandE2(0x00);for(j=0;j<0x50;j++){LCD_WriteDataE1(0x00);LCD_WriteDataE2(0x00);}}void display_cn(unsigned char lin,unsigned int col,unsigned int len,unsigned char *p) {unsigned int seg,i,j;unsigned char a,L,n;switch(lin){case 0: n=0xba;break;case 1: n=0xb8;break;}for(i=0;i<len;i++){for(j=0;j<2;j++){L=col;LCD_WriteCommandE1(n+j);LCD_WriteCommandE2(n+j);for(seg=0;seg<16;seg++){if (L < 61){a = L;LCD_WriteCommandE1(a);LCD_WriteDataE1(*p++);}else{a = L-61;LCD_WriteCommandE2(a);LCD_WriteDataE2(*p++);}L++;}}col=col+16;}}void display_number(unsigned char lin,unsigned int col,unsigned char num){unsigned int seg,i,j;unsigned char a,L,n,k;switch(lin){case 0: n=0xba;break;case 1: n=0xb8;break;}k=num*16;for(j=0;j<2;j++){L=col;LCD_WriteCommandE1(n+j);LCD_WriteCommandE2(n+j);for(seg=0;seg<8;seg++){if (L < 61){a = L;LCD_WriteCommandE1(a);LCD_WriteDataE1(number_X[k++]);}else{a = L-61;LCD_WriteCommandE2(a);LCD_WriteDataE2(number_X[k++]);}L++;}}}void display_unsigned_int(unsigned char lin,unsigned int col,unsigned int dat) {unsigned int seg;unsigned char k[4];k[3]=dat%10;k[2]=((dat/10)%10);k[1]=((dat/100)%10);k[0]=((dat/1000)%10);if(k[0]==0) {k[0]=12;}if((k[0]==12)&&(k[1]==0)){ k[0]=12;k[1]=12;}if((k[0]==12)&&(k[1]==12)&&(k[2]==0)){k[0]=12;k[1]=12;k[2]=12;}for(seg=0;seg<4;seg++){display_number(lin,col,k[seg]);col=col+10;}}void display_signed_int(unsigned char lin,unsigned int col,signed int dat){unsigned int seg;unsigned char k[5],a;k[0]=12;if(dat<0){dat=(~dat)+1;k[0]=11;}k[4]=dat%10;k[3]=((dat/10)%10);k[2]=((dat/100)%10);k[1]=((dat/1000)%10);a=k[0];if(k[1]==0) {k[0]=12;k[1]=a;}if((k[1]==a)&&(k[2]==0)){ k[0]=12;k[1]=12;k[2]=a;}if((k[1]==12)&&(k[2]==a)&&(k[3]==0)){k[0]=12;k[1]=12;k[2]=12;k[3]=a;}for(seg=0;seg<5;seg++){display_number(lin,col,k[seg]);col=col+10;}}void display_unsigned_char(unsigned char lin,unsigned int col,unsigned char dat) {unsigned int seg;unsigned char k[3];k[1]=dat%10;k[0]=((dat/10)%10);for(seg=0;seg<2;seg++){display_number(lin,col,k[seg]);col=col+10;}}2、LCD.h#include <reg51.h>#include <absacc.h>#ifndef __LCD__#define __LCD__#define CWADD1 XBYTE[0x8000]#define DWADD1 XBYTE[0x8001]#define CRADD1 XBYTE[0x8002]#define DRADD1 XBYTE[0x8003]#define CWADD2 XBYTE[0x8004]#define DWADD2 XBYTE[0x8005]#define CRADD2 XBYTE[0x8006]#define DRADD2 XBYTE[0x8007]extern void LCD_Init();extern void display_cn(unsigned char lin,unsigned int col,unsigned int len,unsigned char *p);extern void display_signed_int(unsigned char lin,unsigned int col,signed int dat);extern void display_unsigned_int(unsigned char lin,unsigned int col,unsigned int dat);extern void display_unsigned_char(unsigned char lin,unsigned int col,unsigned char dat);extern void LCD_Clear(void);#endif3、DS18B20.c#include <reg51.h>#include "string.h"#include "intrins.h"#include "DS18B20.h"sbit DQ=P1^0;void delay(unsigned int uSeconds){for(;uSeconds>0;uSeconds--);}unsigned char ow_reset(void){unsigned char xdata presence;DQ = 0;delay(48);DQ = 1;delay(7);presence = DQ;delay(48);return(presence);}unsigned char read_byte(void){unsigned char i;unsigned char value = 0;for (i=8;i>0;i--){value>>=1;DQ = 0; // pull DQ low to start read timeslotDQ = 1; // then rlease DQ_nop_();_nop_();_nop_();_nop_();_nop_(); // read DQ data at 1 to 15us,here delay 6us;if(DQ)value|=0x80;delay(7); // wait for rest of timeslot,72us }return(value);}void write_byte(char val){unsigned char i;for (i=8; i>0; i--) // writes byte, one bit at a time{DQ = 0; // pull DQ low to start timeslotDQ = val&0x01;delay(7); // hold value for remainder of timeslot,here 72us DQ = 1;val=val/2;}delay(5);}float Read_Temperature(void){unsigned char Hdata,Ldata,b;int a;bit flag;float x,y,z;ow_reset();write_byte(0xCC); // Skip ROMwrite_byte(0xBE); // Read Scratch Paddelay(100);Ldata=read_byte(); // Low byte firstHdata=read_byte(); // High byte afterow_reset();write_byte(0xCC); //Skip ROMwrite_byte(0x44); // Start Conversiona=Hdata*256+Ldata;x=(float)(Ldata&0x0f);x=x/16;if(a<0)flag=1;else flag=0;b=a>>4;z=(float)(b);if(flag==1){b=~b+1;z=(float)(b);z=0-z;}y=z+x;return y;}4、DS18B20.h#ifndef __DS18B20__#define __DS18B20__extern float Read_Temperature(void); #endif5、main.c#include <reg51.h>#include<LCD.h>#include<main.h>#include "DS18B20.h"void wait(unsigned int x){unsigned int i;i=0;for(i=0;i<x;i++);}void main(void){float F;signed int a;LCD_Init();LCD_Clear();display_cn(0,20,5,szwdj);display_cn(1,0,3,wdz);while(1){F=Read_Temperature( );a=(signed int)F;display_signed_int(1,40,a);wait(5000);}}6、main.h#ifndef MAIN_H__#define MAIN_H__// 中文字模库16x16点阵code unsigned char szwdj[]={ //纵向字节倒序。

51单片机项目教程项目 4 流水灯实验

51单片机项目教程项目 4  流水灯实验
(4)查询溢出标志TFx的状态, 决定是否停止定时/计数器;
4.3 项目实施
4.3.1 流水灯开发实战环节
注意:J1接上才能开始做流水灯模块实验
图4- 6流水灯实物结果
实现延时通常有两种方法:一种是硬件延时,要用到定时器 /计数器,这种方法可以 提高CPU的工作效率,也能做到精确延时;另一种是软件延时,这种方法主要采用 循环体进行。
2、编写精确的延时函数 1)使用定时器/计数器实现精确延时
2)软件延时与时间计算
在很多情况下,定时器/计数器经常被用作其他用途,这时候就只能用软件方法延 时。下面介绍几种软件延时的方法。 2.1 短暂延时 2.2 在C51中嵌套汇编程序段实现延时 2.3 使用示波器确定延时时间 2.4 使用反汇编工具计算延时时间
(216-初值)×振荡周期×12
例如:若晶振频率为12MHz,则最长的定时时间为(2160)×(1/12)×12us=65.536ms
4.2 技术准备
3. 方式2 THx作为常数缓冲器,当TLx计数溢出时,在置“1”溢出标志TFx 的同时,还自动的将THx中的初值送至TLx,使TLx从初值开始重新计 数。 其定时时间为:
例如:机器时钟频率为12MHZ,机器周期为1μs 时,
若工作在模式0,则最大定时值为:213×1μs =8.192ms 若工作在模式1,则最大定时值为: 216×1μs =65.536ms (2)置定时/计数器初值,直接将初值写入寄存器的TH0、TL0或TH1、TL1;
(3)对TCON寄存器中的TR0或TR1置位,启动定时/计数器,置位以后,计数器 即按规定的工作模式和初值进行计数或开始定时。
4.2 技术准备
4.2.7定时器编程步骤 MCS-51单片机的定时器/计数器是可编程的,具体步骤如下: (1)对TMOD赋值,以确定定时器的工作模式; 初值计算: 设计数器的最大值为M,则置入的初值X为: 计数方式:X=M-计数值 定时方式:由(M-X)T=定时值,得X=M-定时值/T T为计数周期,是单片机的机器周期。 (模式0: M为213=8192 ,模式1: M为216=65536,模式2和3: M为28=256)

简单的51单片机花样流水灯C语言源代码

简单的51单片机花样流水灯C语言源代码

简单的51单片机花样流水灯C语言源代码#include<reg51.h> //51系列单片机定义文件#define uchar unsigned char //定义无符号字符#define uint unsigned int //定义无符号整数void Delayms(uint x){ //定义延时函数uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}void main(){uint i;uchar temp;while(1){temp=0x01; //8个流水灯逐个闪动for(i=0;i<8;i++){P0=~temp;Delayms(50);temp<<=1;}temp=0x80; //8个流水灯反向逐个闪动for(i=0;i<8;i++){P0=~temp;Delayms(50);temp>>=1;}temp=0xfe; //8个流水灯依次全部点亮for(i=0;i<8;i++){P0=temp;Delayms(50);temp<<=1;}temp=0x7f; //8个流水灯依次反向全部点亮for(i=0;i<8;i++){P1=temp;Delayms(50);temp>>=1;}}}//此程序的作者向往未来#include<reg52.h>#include<intrins.h>#define uint unsigned int #define uchar unsigned char uchar z=50,e=0x00,f=0xff; uchar code table1[]={0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};uchar code table2[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00}; uchar code table3[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};uchar code table4[]={0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff}; uchar code table5[]={0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff};uchar code table6[]={0x7e,0x3c,0x18,0x00, 0x18,0x3c,0x7e,0xff}; void delay(uchar); void lsd1();void lsd2();void lsd3();void lsd4();void lsd5();void lsd6();void lsd7();void lsd8();void lsd9();void lsd10();void lsd11();void lsd12();main(){while(1){lsd1();lsd2();lsd3();lsd4();lsd5();lsd6();lsd7();lsd8();lsd9();lsd10();lsd11();lsd12();}}void delay(uchar x) //延时函数{uint i,j;for(i=x;i>0;i--)for(j=250;j>0;j--);}void lsd1() //lsd1 单个流水灯双程模式1 {uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);for(i=0;i<7;i++) //仅单个灯亮从上往下流{a=_crol_(a,1);P0=a;delay(z);}P0=0xff;a=0xfe;P2=a;delay(z);for(j=0;j<7;j++){a=_crol_(a,1);P2=a;delay(z);}P2=0xff;a=0xfe;P3=a;delay(z);for(k=0;k<7;k++){a=_crol_(a,1);P3=a;delay(z);}P3=0xff;a=0xfe;P1=a;delay(z);for(l=0;l<7;l++){a=_crol_(a,1);P1=a;delay(z);}a=0xbf;P1=a;delay(z);for(l1=0;l1<6;l1++) //l1==6 仅单个灯亮从下往上流{a=_cror_(a,1);P1=a;delay(z);}P1=0xff;a=0x7f;P3=a;delay(z);for(k1=0;k1<7;k1++) {a=_cror_(a,1);P3=a;delay(z);}P3=0xff;a=0x7f;P2=a;delay(z);for(j1=0;j1<7;j1++) {a=_cror_(a,1);P2=a;delay(z);}P2=0xff;a=0x7f;P0=a;delay(z);for(i1=0;i1<7;i1++) {a=_cror_(a,1);P0=a;delay(z);P0=0xff;}void lsd2() //lsd2 两个灯流水双程模式1 {uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);a=a<<1;P0=a;delay(z);for(i=0;i<6;i++){a=_crol_(a,1);P0=a;delay(z);}P0=0x7f;P2=0xfe;delay(z);P0=0xff;a=0xfc;P2=a;delay(z);for(j=0;j<6;j++){a=_crol_(a,1);P2=a;delay(z);P2=0x7f;P3=0xfe;delay(z);P2=0xff;a=0xfc;P3=a;delay(z);for(k=0;k<6;k++) {a=_crol_(a,1);P3=a;delay(z);}P3=0x7f;P1=0xfe;delay(z);P3=0xff;a=0xfc;P1=a;delay(z);for(l=0;l<6;l++) {a=_crol_(a,1);P1=a;delay(z);}P1=0x7f;delay(z);P1=0xff;delay(z);a=0x7f;P1=a;delay(z);a=a>>1;P1=a;delay(z);for(l1=0;l1<6;l1++) {a=_cror_(a,1);P1=a;delay(z);}P1=0xfe;P3=0x7f;delay(z);P1=0xff;a=0x3f;P3=a;delay(z);for(k1=0;k1<6;k1++) {a=_cror_(a,1);P3=a;delay(z);}P3=0xfe;P2=0x7f;delay(z);P3=0xff;P2=a;delay(z);for(j1=0;j1<6;j1++){a=_cror_(a,1);P2=a;delay(z);}P2=0xfe;P0=0x7f;delay(z);P2=0xff;a=0x3f;P0=a;delay(z);for(i1=0;i1<6;i1++){a=_cror_(a,1);P0=a;delay(z);}P0=0xfe;delay(z);P0=0xff;delay(z);}void lsd3() //lsd3 两个灯流水双程模式2 {uchar a,i,j,k,l,l1,k1,j1,i1;P0=a;delay(z);a=a<<1;P0=a;delay(z);for(i=0;i<6;i++) //_crol_与_cror_混合使用{a=_crol_(a,1);P0=a;delay(z);}P0=0x7f;a=0x7f;P2=a;delay(z);P0=f;a=a>>1;P2=a;delay(z);for(j=0;j<6;j++){a=_cror_(a,1);P2=a;delay(z);}P2=0xfe;a=0xfe;P3=a;delay(z);a=a<<1;P3=a;delay(z);for(k=0;k<6;k++) {a=_crol_(a,1);P3=a;delay(z);}P3=0x7f;a=0x7f;P1=a;delay(z);P3=f;a=a>>1;P1=a;delay(z);for(l=0;l<6;l++) {a=_cror_(a,1);P1=a;delay(z);}P1=0xfe;delay(z);P1=f;delay(z);a=0xfe;P1=a;a=a<<1;P1=a;delay(z);for(l1=0;l1<6;l1++) //l1==6 {a=_crol_(a,1);P1=a;delay(z);}P1=0x7f;a=0x7f;P3=a;delay(z);P1=f;a=a>>1;P3=a;for(k1=0;k1<6;k1++){a=_cror_(a,1);P3=a;delay(z);}P3=0xfe;a=0xfe;P2=a;delay(z);P3=f;a=a<<1;P2=a;for(j1=0;j1<6;j1++) {a=_crol_(a,1);P2=a;delay(z);}P2=0x7f;a=0x7f;P0=a;delay(z);P2=f;a=a>>1;P0=a;delay(z);for(i1=0;i1<6;i1++) {a=_cror_(a,1);P0=a;delay(z);}P0=0xfe;delay(z);P0=f;delay(z);}void lsd4() //lsd4{uchar a,i,j,k,l,l1,k1,j1,i1; a=0xfe;delay(z);for(i=0;i<7;i++){a=a<<1; //单个灯依次点亮所有灯,从上往下P0=a;delay(z);}a=0xfe;P2=a;delay(z);for(j=0;j<7;j++){a=a<<1;P2=a;delay(z);}a=0xfe;P3=a;delay(z);for(k=0;k<7;k++){a=a<<1;P3=a;delay(z);}a=0xfe;P1=a;delafor(l=0;l<7;l++){a=a<<1;P1=a;delay(z);}for(l1=0;l1<8;l1++) //l1==8{P1=table1[l1]; //单个灯依次熄灭所有灯,从下往上delay(z);}for(k1=0;k1<8;k1++){P3=table1[k1];delay(z);}for(j1=0;j1<8;j1++){P2=table1[j1];delay(z);}for(i1=0;i1<8;i1++){P0=table1[i1];delay(z);}}void lsd5() //lsd5{uchar a,i,j,k,l,l1,k1,j1,i1;a=0xfe;P0=a;delay(z);for(i=0;i<7;i++){a=a<<1;P0=a;delay(z);}for(j=0;j<8;j++) //单个灯依次点亮所有灯,从下往上{P2=table2[j];delay(z);}a=0xfe;P3=a;delay(z);for(k=0;k<7;k++){a=a<<1;P3=a;delay(z);}for(l=0;l<8;l++){P1=table2[l];delay(z);}for(l1=0;l1<8;l1++) //单个灯依次熄灭所有灯,从上往下{P1=table3[l1];delay(z);}for(k1=0;k1<8;k1++){P3=table1[k1];delay(z);}for(j1=0;j1<8;j1++) //单个灯依次熄灭所有灯,从上往下{P2=table3[j1];delay(z);}for(i1=0;i1<8;i1++){P0=table1[i1];delay(z);}}void lsd6() //每组为单位同亮同灭从左向右再向左{P0=0x00;delay(z);P0=0xff;P2=0x00;delay(z);P2=0xff;P3=0x00;delay(z);P3=0xff;P1=0x00;delay(z);P3=0x00;P1=0xff;delay(z);P2=0x00;P3=0xff;delay(z);P0=0x00;P2=0xff;delay(z);P0=0xff;delay(z);}void lsd7() //lsd7 全亮全灭{uchar i;for(i=0;i<2;i++){P0=0x00;P1=0x00;P2=0x00;P3=0x00;delay(z);P0=0xff;P1=0xff;P2=0xff;P3=0xff;delay(z);}}void lsd8() //每组仅两个灯,从中间往两边再往中间{uchar i;for(i=0;i<8;i++){P0=table4[i];P1=table4[i];P2=table4[i];P3=table4[i];delay(z);}}void lsd9() //每组两个灯引亮所有灯再引灭,从中间带两边再到中间{uchar i;for(i=0;i<8;i++){P0=table5[i];P1=table5[i];P2=table5[i];P3=table5[i];delay(z);}}void lsd10(){uchar a,i,j;a=0xfe;P0=a;P1=a;P2=a;P3=a;delay(z);for(i=0;i<7;i++) //仅单个灯亮从上往下流{a=_crol_(a,1);P0=a;P1=a;P2=a;P3=a;delay(z);}for(j=0;j<7;j++) //仅单个灯亮从上往下流{a=_cror_(a,1);P0=a;P1=a;P2=a;P3=a;delay(z);}P0=f;P1=f;P2=f;P3=f;delay(z);void lsd11() {uchar a,i,j;a=0xfe;P0=a;P1=a;P2=a;P3=a;delay(z);for(i=0;i<7;i++) {a=a<<1;P0=a;P1=a;P2=a;P3=a;delay(z);}for(j=0;j<8;j++) {P0=table1[j];P1=table1[j];P2=table1[j];P3=table1[j]; delay(z);}}void lsd12()uchar a,i,j,k,l;a=0xfe;P0=a;P1=a;delay(z);for(i=0;i<7;i++) {a=a<<1;P0=a;P1=a;delay(z);}a=0x7f;P2=a;P3=a;delay(z);for(j=0;j<7;j++) {a=a>>1;P2=a;P3=a;delay(z);}for(k=0;k&lt;8;k++){P2=table3[k]; P3=table3[k]; delay(z);for(l=0;l<8;l++) {P0=table1[l];P1=table1[l]; delay(z);}while(1);}。

51单片机爱心流水灯原理及制作

51单片机爱心流水灯原理及制作

电路原理图:原件清单:1、51单片机x1、40Pin 座x12、LED x32(建议用5mm 七彩的)3、电阻470Ωx334、晶振12MHz x15、10uf 电解电容x1、谐振瓷片电容30pf x26·其他的可以看自己的爱好去加7、其实也可以不用那么多的电阻,用几个排阻就OK了。

效果展示:作品程序:#include<reg52.h>#define uchar unsigned charuchar flag=200;///////////////////////////////////////////////////////////////////////uchar code Tab1[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xFF};//暗中左移向下uchar code Tab2[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF};//暗中右移向上uchar code Tab3[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};//亮中左移向下uchar code Tab4[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00};//亮中右移向上uchar code Tab11[]={0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0xff};//暗中左移向下uchar code Tab22[]={0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00,0xff};////////////////////////////////////////////////////////////////////uchar code Tab33[]={0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF};uchar code Tab44[]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};uchar code Tab55[]={0x08,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff};uchar code Tab5[]={0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xff};uchar code Tab6[]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xff};uchar code Tab7[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};uchar code Tab8[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; ////////////////////////////////////////////////////////////////void shansuo();void xl();///////////////////////////////////////////////////////////////void delay(void){unsigned char m,n;for(m=0;m<flag;m++)for(n=0;n<250;n++);}///////////////////////////////////void hy1(void) //点亮状态逆时针旋转90度(一个一个灭){unsigned char i;for(i=0;i<8;i++){P0=Tab11[i];P1=Tab22[i];P2=Tab11[i];P3=Tab22[i];delay();}for(i=0;i<8;i++){P0=Tab44[i];P1=Tab55[i];P2=Tab44[i];P3=Tab55[i];delay();}}///////////////////////////////////////////void hy2(void) //暗中逆时针转360。

51单片机常见的五种流水灯编写方式

51单片机常见的五种流水灯编写方式

For personal use only in study and research; not for commercial use51单片机常见的五种流水灯编写方式通过改变赋值实现流水灯void main(){while(1){P2=0xfe;delay1ms(500);P2=0xfd;delay1ms(500);P2=0xfb;delay1ms(500);P2=0xf7;delay1ms(500);P2=0xef;delay1ms(500);P2=0xdf;delay1ms(500);P2=0xbf;delay1ms(500);P2=0x7f;delay1ms(500);}}通过公式运算实现流水灯void main(){while(1){uint a,b=1;P2=0xfe;delayms(500);for(a=0;a<7;a++){P2-=b; // P2=P2-bdelayms(500);b=b*2; //都化为同一类型进制运算}}}通过操作符<<与“|”实现流水灯(通过移位实现流水灯)void main(){uchar a,i;while(1){a=0xfe; //点亮第一位LED灯for(i=0;i<8;i++){P2=a;a=a<<1; //左移一位a=a|0x01; //左移一位后与0x01相或,保证左移后最低位为1 delay_ms(500);}}}通过库函数_crol_(字符左移)实现流水灯void main(){uint a;a=0xfe;while(1){P2=a;a=_crol_(a,1);delay_ms(500);}}采用数组实现流水灯uchar code table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};void main(void){uchar i;while (1){for(i=0;i<8;i++) //循环点亮8只LED灯{P2=table[i];delay_ms(500); //延时500毫秒}}}For personal use only in study and research; not for commercial use.Nur für den persönlichen für Studien, Forschung, zu kommerziellen Zwecken verwendet werden.Pour l 'étude et la recherche uniquement à des fins personnelles; pas à des fins commerciales.толькодля людей, которые используются для обучения, исследований и не должны использоваться в коммерческих целях.以下无正文For personal use only in study and research; not for commercial use.Nur für den persönlichen für Studien, Forschung, zu kommerziellen Zwecken verwendet werden.Pour l 'étude et la recherche uniquement à des fins personnelles; pas à des fins commerciales.толькодля людей, которые используются для обучения, исследований и не должны использоваться в коммерческих целях.以下无正文。

基于51单片机的花样流水灯设计

基于51单片机的花样流水灯设计

156本文以AT89C51单片机芯片为核心,通过硬件设计工作和软件设计工作,实现控制8个发光LED二极管的闪烁,即使LED二极管按照某种位置放置,按照时间先后和间隔顺序进行发光与熄灭的实现,也称流水灯控制系统。

该系统的设计主要分为三部分,即系统框架设计、硬件设计以及系统软件设计。

1 系统框架设计本设计的目的主要是为了能够控制8个LED发光二极管的闪烁工作,对于一般的基于单片机技术的控制系统开发而言,需要实现系统供电、系统驱动、系统复位以及功能扩展。

为了简化系统设计,减少系统开发工作量,因此本设计的系统框架搭建主要分为单片机最小工作系统模块、灯光控制模块、以及LED发光二极管电路模块3部分,而单片机最小工作系统模块中又分为时钟电路模块和复位电路模块,以及单片机芯片。

2 AT89C51单片机单片机全称为单片微型计算机,又被称为单片微控制器,经过不断的技术迭代和改进,如今的单片机已将一个基本完整的、能够实现计算机基本功能的器件集成于一块微型芯片当中。

开发人员在开发各种控制系统时,常常将单片机作为系统的核心,通过其接口来扩展和实现其他功能,通过向单片机内烧录已经制定好的程序来实现系统自动控制。

AT89C51型单片机是51系列单片机众多款型中最为的经典的单片机之一。

该单片机以其低廉的价格、可靠的性能、良好的扩展和兼容性占据了广大的单片机市场,获得了许多电子工程师的应用。

2.1 单片机最小工作系统要想驱动一块单片芯片正常工作,需要为其提供合适的工作电压,需要能够提供时钟脉冲信号来控制单片机内部各个指令的有序执行和操作的时钟电路,以及能够控制系统复位操作的复位电路三部分。

它们是实现单片机正常工作的最小条件,在单片机芯片的接口上也具有专门的接口来实现这些条件。

2.1.1 工作电压单片机在封装过程中提供了正负极两个独立的引脚,以便外部供电电源的接入。

AT89C51型单片机的标准电压为+5V,因此只需将电源+5V接口接入单片机正极,而单片机的负极则接地便可实现单片机的正常供电条件。

51单片机音乐流水灯程序

51单片机音乐流水灯程序

#include <reg52."h>sbitspeaker = P1^6;sbitkey1=P1^5;sbitkey2=P1^7;unsigned char kk=0;unsigned long t3;unsigned char timer0h, timer0l, time,n;codecharled[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x00,0x55,0xaa,0x00,0x5 5,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x 7f,0x00,0x55,0xaa,0x00,0x55,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x 7f,0x00,0x55,0xaa,0x00,0x55,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x 7f,0x00,0x55,0xaa,0x00,0x55,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x 7f,0x00,0x55,0xaa,0x00,0x55,0xaa,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0x3c,0x18,0x00,0x18,0x3c,0x7e,0x00}; //灯亮数组//--------------------------------------//单片机晶振采用11."0592MHz//频率-半周期数据表高八位本软件共保存了四个八度的28个频率数据code unsigned char FREQH[] = {0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8,//低音0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE,//高音2345670xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF};//超高音//频率-半周期数据表低八位code unsigned char FREQL[] = {0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6,//低音0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D,//高音2345670x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16};//超高音//--------------------------------------//世上只有妈妈好数据表要想演奏不同的乐曲,只需要修改这个数据表codeint shengrikuaile[] = {5,1,1, 5,1,1, 6,1,2, 5,1,2, 1,2,2, 7,1,4,//生日快乐5,1,1, 5,1,1, 6,1,2, 5,1,2, 2,2,2, 1,2,4,5,1,1, 5,1,1, 5,2,2, 3,2,2, 1,2,2, 7,1,2, 6,1,2,4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4,0,0,0};code int tonghua[]={5,1,2, 5,2,2, 4,2,2,3,2,4, 3,2,2, 4,2,1, 3,2,1, 3,2,4, 3,2,2, 4,2,2,3,2,2, 4,2,2, 3,2,2, 2,2,1, 1,2,1, 1,2,2, 1,2,2, 3,2,2, 5,2,2,6,2,4, 6,2,2, 6,2,1, 5,2,1, 5,2,2, 2,2,2, 2,2,2, 4,2,2,3,2,8, 1,2,2, 3,2,2, 5,2,2,6,2,4, 6,2,2, 6,2,1, 5,2,1, 2,2,2, 2,2,2, 4,2,2,3,2,2, 4,2,2, 3,2,2, 2,2,1, 1,2,1, 1,2,4, 2,2,2, 3,2,2,6,1,4, 6,1,2, 1,2,2, 1,2,2, 7,1,2, 7,1,2, 1,2,1, 1,2,16,0,0,0};//童话//一个音符有三个数字。

基于51单片机实现流水灯的若干种编程方法

基于51单片机实现流水灯的若干种编程方法

基于51单片机实现流水灯的若干种编程方法易礼智【期刊名称】《铜仁学院学报》【年(卷),期】2012(014)006【摘要】51单片机对流水灯的控制是进行单片机课程教学和单片机产品开发的一个重要案例。

本文以C语言的形式给出了6种不同编程方法,并分析了每种方法的实现过程,针对部分方法进行了拓展。

旨在为相关的理论教学和单片机产品的开发提供选择。

%The control of 51 Single-chip on the water light is an important case of single-chip teaching and its product development. Six different programming methods were given in the form of a C language, the realizing process of each method was explained and some of the methods were expanded to provide more choices for theory of teaching and the development of the51 Single-chip products.【总页数】3页(P125-127)【作者】易礼智【作者单位】湖南工程职业技术学院信息工程系,湖南长沙410151【正文语种】中文【中图分类】TP311.1【相关文献】1.51单片机上超酷流水灯的设计和实现 [J], 张嘉勇2.一种51单片机任务调度编程方法与实现 [J], 万关茗3.基于51单片机的流水灯的设计与实现 [J], 吴文兵;脱建智;4.基于AT89C51单片机花式流水灯的设计与实现 [J], 范昭君5.基于Keil软件实现8位流水灯的编程方法 [J], 仝敏因版权原因,仅展示原文概要,查看原文内容请购买。

51单片机流水灯实验报告

51单片机流水灯实验报告

51单片机流水灯试验一、实验目的1.了解51单片机的引脚结构。

2.根据所学汇编语言编写代码实现LED灯的流水功能。

3.利用开发板下载hex文件后验证功能。

二、实验器材个人电脑,80c51单片机,开发板三、实验原理单片机流水的实质是单片机各引脚在规定的时间逐个上电,使LED灯能逐个亮起来但过了该引脚通电的时间后便灭灯的过程,实验中使用了单片机的P2端口,对8个LED灯进行控制,要实现逐个亮灯即将P2的各端口逐一置零,中间使用时间间隔隔开各灯的亮灭。

使用rl或rr a实现位的转换。

A寄存器的位经过rr a之后转换如下所示:然后将A寄存器转换一次便送给P2即MOV P2,A便将转换后的数送到了P2口,不断循环下去,便实现了逐位置一操作。

四、实验电路图五、通过仿真实验正确性代码如下:ORG 0MOV A,#00000001BLOOP:MOV P2,ARL AACALL DELAYSJMP LOOPDELAY:MOV R1,#255DEL2:MOV R2,#250DEL1:DJNZ R2,DEL1DJNZ R1,DEL2RETEnd实验结果:六、实验参考程序#include<reg52.h>#define uchar unsigned char#define uint unsigned intuchar table[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};sbit P00=P0^0;sbit P01=P0^1;void delay(uchar t){uchar i,j;for(i=0;i<t;i++)for(j=0;j<110;j++); }void main(){uchar i;while(1){P00=1;delay(2000);P00=0;for(i=0;i<8;i++){P1=table[i];P2=table[i];delay(2000);}P01=1;delay(2000);P01=0;}}实物展示:(1)单片机最小系统板(2)自己焊制的集成最小系统板(3)自己制作的心形流水灯实验板(4)系统板与实验板的连接展示七.实验总结:这次试验我通过Proteus仿真实现对流水灯功能的实现。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

51单片机课程设计蜂鸣器(生日歌)+温度计+计数器(可调时间)+花样流水灯(6种)#include<reg52.h>#include<intrins.h>#include<stdlib.h>#define uchar unsigned char#define uint unsigned int#define led P1 //将P1口定义位8个leduchar a, count, S1num, xqnum, temp, j, tm, key, coun, L, qdq, mb; //定义变量char yue, ri, shi, fen, miao; //定义月日时分秒int nian; //定义年// ==================定义初始化日历uchar code table0[] = "2020-09-4--FRI--";uchar code table1[] = "08:48:00";uchar code xingqi[][3] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };uint led1[]={0x7e,0xbd,0xdb,0xe7,0xff};// ==================定义初始化日历sbit lcdrs=P2^6; //将lcd1602的RS端口(数据/命令选择端)定义到P2.0 sbit lcdrw=P2^7; //将lcd1602的R/W端口(读/写选择端)定义到P2.1 sbit lcden=P2^5; //将lcd1602的E端口(使能端口)定义到P2.2sbit DQ=P3^7;//ds18b20sbit S1=P3^1; //进入设置退出设置sbit S2=P3^2; //+1 或秒表模式sbit S3=P3^3; //-1 或开始秒表计时sbit S0=P3^0; //进入设置退出设置sbit S4=P3^4; //灯模式切换sbit S5=P3^5;sbit beep = P2^2; //定义蜂鸣器uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126,159,142,159,0};uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时unsigned char code str2[16]={" "};uchar data disdata[5];uint value;//温度值温度值uchar flag;//正负标志正负标志void delay1ms(unsigned int ms)//延时1毫秒{unsigned int i,j;for(i=0;i<ms;i++)for(j=0;j<100;j++);}void write_com1(unsigned char com1)//写指令//{ delay1ms(1);lcdrs=0;lcdrw=0;lcden=0;P0=com1;delay1ms(1);lcden=1;delay1ms(1);lcden=0;}void write_dat1(unsigned char dat1)//写数据// { delay1ms(1);lcdrs=1;lcdrw=0;lcden=0;P0=dat1;delay1ms(1);lcden=1;delay1ms(1);lcden=0;}void lcd_init()//初始化设置//{write_com1(0x38);delay1ms(5);write_com1(0x08);delay1ms(5);write_com1(0x01);delay1ms(5);write_com1(0x06);delay1ms(5);write_com1(0x0c);delay1ms(5);}void display(unsigned char *p)//显示{while(*p!='\0'){write_dat1(*p);p++;delay1ms(1);}}init_play()//初始化显示{lcd_init();write_com1(0xc0);display(str2);}void delay_18B20(unsigned int i)//延时1微秒{while(i--);}void ds1820rst()/*ds1820复位*/{unsigned char x=0;DQ = 1;//DQ复位delay_18B20(4);//延时DQ=0;//DQ拉低delay_18B20(100);//精确延时大于480usDQ=1;//拉高delay_18B20(40);}uchar ds1820rd()/*读数据*/{unsigned char i=0;unsigned char dat1 = 0;for (i=8;i>0;i--){DQ = 0; //给脉冲信号dat1>>=1;DQ=1;//给脉冲信号if(DQ)dat1|=0x80;delay_18B20(10);}return(dat1);}void ds1820wr(uchar wdata)/*写数据*/ {unsigned char i=0;for(i=8;i>0;i--){DQ=0;DQ=wdata&0x01;delay_18B20(10);DQ=1;wdata>>=1;}}read_temp()/*读取温度值并转换*/ {uchar a,b;ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0x44);//*启动温度转换*/?ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0xbe);//*读取温度*/a=ds1820rd();b=ds1820rd();value=b;value<<=8;value=value|a;if(value<0x0fff)flag=0;else{value=~value+1;flag=1;}value=value*(0.625);//温度值扩大10倍,精确到1位小数return(value);}/*******************************************************************/ void ds1820disp()//温度值显示{uchar flagdat;disdata[0]=value/1000+0x30;//百位数disdata[1]=value%1000/100+0x30;//十位数disdata[2]=value%100/10+0x30;//个位数disdata[3]=value%10+0x30;//小数位if(flag==0)flagdat=0x20;//正温度不显示符号elseflagdat=0x2d;//负温度显示负号:-if(disdata[0]==0x30){disdata[0]=0x20;//如果百位为0,不显示if(disdata[1]==0x30){disdata[1]=0x20;//如果百位为0,十位为0也不显示}}write_com1(0xc8);write_dat1(flagdat);//显示符号位write_com1(0xc9);write_dat1(disdata[0]);//显示百位write_com1(0xca);write_dat1(disdata[1]);//显示十位write_com1(0xcb);write_dat1(disdata[2]);//显示个位write_com1(0xcc);write_dat1(0x2e);//显示小数点write_com1(0xcd);write_dat1(disdata[3]);//显示小数位}void delay(uint t) //@12.000MHz延时函数{uint x, y;for (x = t; x > 0; x--)for (y = 110; y > 0; y--);}void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0) { //播放各个音符,SONG_LONG 为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}//======================液晶void write_com(uchar com) //液晶写指令{lcdrw = 0;lcdrs = 0;P0 = com;delay(2);lcden = 1;delay(2);lcden = 0;}void write_data(uchar dat) //液晶写数据{lcdrw = 0;lcdrs = 1;P0 = dat;delay(2);lcden = 1;delay(2);lcden = 0;}void write_sfm(uchar add, uchar dat) //写时分秒函数{uchar shi, ge;shi = dat / 10;ge = dat % 10;write_com(0x80 + 0x40 + add);write_data(0x30 + shi);write_data(0x30 + ge);}void write_yr(uchar add, uchar dat) //写月日函数{uchar shi, ge;shi = dat / 10;ge = dat % 10;write_com(0x80 + add);write_data(0x30 + shi);write_data(0x30 + ge);}void write_nian(uchar add, uint dat) //写年函数{uint qian, bai, shi, ge;qian = dat / 1000;bai = dat % 1000 / 100;shi = dat % 100 / 10;ge = dat % 10;write_com(0x80 + add);write_data(0x30 + qian);write_data(0x30 + bai);write_data(0x30 + shi);write_data(0x30 + ge);}void init_lcd() //液晶初始化{lcden = 0;nian = 2020;yue = 1;ri = 3;shi = 0; //初始shi、fen、miaofen = 0;miao = 0;write_com(0x38); //设置16x2显示,5x7点阵,8位数据口write_com(0x0c); //设置开显示,不显示光标write_com(0x06); //写一个字符后地址指针加1write_com(0x01); //显示清0,数据指针清0}//==================液晶void init() //总初始化函数{init_lcd(); //液晶初始化write_com(0x80); //设置显示初始坐标for (a = 0; a < 14; a++) //显示年月日初始值{write_data(table0[a]);delay(3);}write_com(0x80 + 0x40); //设置显示初始坐标for (a = 0; a < 8; a++) //显示时分秒初始值{write_data(table1[a]);delay(3);}write_nian(0, nian);write_sfm(6, miao); //分别将shi、fen、miao送去液晶显示write_sfm(3, fen);write_sfm(0, shi);qdq=0;mb=0;j=0;L=0;coun=0;count = 0;xqnum = 0;S1num = 0;PT0=1;PT1=0;TH1=(65536-50000)/256;TL1=(65536-50000)%256;TR1=1;ET1=1;led=0xfe;key=0;TMOD = 0x011; //设置定时器0工作模式1TH0 = (65536-50000) / 256; //定时器装初始值TL0 = (65536-50000) % 256;EA = 1; //开总中断ET0 = 1; //开定时器0中断TR0 = 1; //启动定时器0}//=======================流水灯void t0isr() interrupt 3 //定时器2,用于流水灯{TH1=(65536-50000)/256;TL1=(65536-50000)%256;tm++;if(tm>=9){tm=0;switch(key) //六种样式{case 0:led=_crol_(led,1);break;case 1:led=_cror_(led,1);break;case 2:led=_cror_(led,1);break;case 3:led=_crol_(led,1);break;case 4:if(led==0x00)led=0xff;else led=led*2;break;case 5:led=~led;break;}}}//=======================流水灯//=========================== 独立键盘扫描函数void keyscan(){//————————————————————————————————————————————进入日历时钟设置if(S5 == 0){ read_temp();//读取温度ds1820disp();//显示}if (S0 == 0){ beep=0;PlayMusic(); //播放生日快乐beep=1;}if (S1 == 0){delay(2); //确认定义键被按下if (S1 == 0){while(!S1); //确认按键松开S1num++; //定义键S1按下次数记录if (S1num == 1) //S1按下一次时{TR0 = 0; //关闭定时器write_com(0xc0 + 7); //光标定位到秒位置write_com(0x0f); //光标闪烁}if (S1num == 2) //S1按下两次时{write_com(0xc0 + 4); //光标定位到分位置}if (S1num == 3) //S1按下三次时{write_com(0xc0 + 1); //光标定位到时位置}if (S1num == 4) //S1按下四次时{write_com(0x80 + 13); //光标定位到星期位置}if (S1num == 5) //S1按下五次时{write_com(0x80 + 9); //光标定位到日位置}if (S1num == 6) //S1按下六次时{write_com(0x80 + 6); //光标定位到月位置}if (S1num == 7) //S1按下七次时{write_com(0x80 + 3); //光标定位到年位置}if (S1num == 8) //退出设置,开启中断{S1num = 0;delay(3);TR0 = 1;write_com(0x0c);}}}if (S1num != 0) //只有定义键按下后S2、S3、S4才有效{if (S2 == 0){delay(2); //防抖if (S2 == 0) //确认按键被按下{while (!S2); //释放按键确认if (S1num == 1) //S1按下一次时{miao++; //调整秒加1if (miao == 60) //满60清零miao = 0;write_sfm(6, miao); //每调节一次送液晶显示一次write_com(0x80 + 0x40 + 6); //显示位置重新回到调节处}if (S1num == 2) //S1按下两次时{fen++; //调整分加1if (fen == 60)fen = 0;write_sfm(3, fen);write_com(0x80 + 0x40 + 3);}if (S1num == 3) //S1按下三次时{shi++; //调整时加1if (shi == 24)shi = 0;write_com(0x80 + 0x40);write_sfm(0, shi);}if (S1num == 4) //星期加调整{xqnum++;if (xqnum == 7)xqnum = 0;write_com(0x80 + 0x0b);for (a = 0; a < 3; a++){write_data(xingqi[xqnum][a]);delay(5);}}if (S1num == 5) //日加调整{ri++;if (yue == 2){if (nian % 400 == 0){if (ri == 30)ri = 1;}if (nian % 400 != 0){if (ri == 29)ri = 1;}}else if (yue <= 7){if (yue % 2 == 0 & yue != 2){if (ri == 31){ ri = 1; }}else if (yue % 2 != 0 & yue != 2){if (ri == 32){ ri = 1; }}}else if (yue >= 8){if (yue % 2 == 0){if (ri == 32){ ri = 1; }}else if (yue % 2 != 0){if (ri == 31){ ri = 1; }}}write_yr(8, ri);}if (S1num == 6) //月加调整{yue++;if (yue == 13)yue = 1;write_yr(5, yue);}if (S1num == 7) //年加调整{nian++;if (nian == 2030)nian = 2019;write_nian(0, nian);}}}if (S3 == 0){delay(2);if (S3 == 0) //确认按键被按下{while (!S3); //确认按键松开if (S1num == 1){miao--; //调整秒减1if (miao == -1) //减到00后再减重新设置为59miao = 59;write_sfm(6, miao);write_com(0x80 + 0x40 + 6);}if (S1num == 2){fen--; //调整分减1if (fen == -1)fen = 59;write_sfm(3, fen);write_com(0x80 + 0x40 + 3);}if (S1num == 3){shi--; //调整时减1if (shi == -1)shi = 23;write_sfm(0, shi);write_com(0x80 + 0x40);}if (S1num == 4){xqnum--; //调整星期减一if (xqnum == -1)xqnum = 6;write_com(0x80 + 0x0b);for (a = 0; a < 3; a++){write_data(xingqi[xqnum][a]);delay(5);}}if (S1num == 5) //调整日{ri--;if (yue == 2){if (nian % 400 == 0){if (ri == 0){ ri = 29; }}if (nian % 400 != 0){if (ri == 0){ ri = 28; }}}else if (yue <= 7){if (yue % 2 == 0 & yue != 2){if (ri == 0){ ri = 30; }}else if (yue % 2 != 0 & yue != 2){if (ri == 0){ ri = 31; }}}else if (yue >= 8){if (yue % 2 == 0){if (ri == 0){ ri = 31; }}else if (yue % 2 != 0){if (ri == 0){ ri = 30; }}}write_yr(8, ri);}if (S1num == 6) //调整月{yue--;if (yue == 0)yue = 12;write_yr(5, yue);}if (S1num == 7) //调整年{nian--;if (nian == 2030)nian = 2019;write_nian(0, nian);}}}}//————————————————————————————————————-推出日历时钟设置//——————————————————————————————————流水灯设置初值if(S4==0){delay(2);if(S4==0) //确认按键按下{while(!S4);qdq=0; //抢答器重置TR1=1;delay(2);while(S4==0);delay(2);key++;key%=6;if(key==0)led=0xfe;else if(key==1)led=0x7f;else if(key==2)led=0x80;else if(key==3)led=0x01;else if(key==4)led=0xfe;else led=0x55;}}//——————————————————————————————————结束流水灯设置初值}//=========================== 独立键盘扫描函数//=======================定时器0中断服务程序,用于日历时钟、秒表void timer0() interrupt 1{TH0 = (65536-50000) / 256; //重装定时器初始值TL0 = (65536-50000) % 256;count++; //中断次数累加//————————————————————————————————————————————日历时钟if (count == 20) //20次50毫秒即一秒{count = 0;miao++;if (miao == 60) //秒加到60时分进位{miao = 0;fen++;if (fen == 60) //分加到60时时进位{fen = 0;shi++;if (shi == 24) //时加到24时清0{shi = 0;xqnum++;ri++;if (yue == 2) //如果是二月{if (nian % 400 == 0) //闰年判断{if (ri == 30){ri = 1;yue++;write_yr(5, yue);}}if (nian % 400 != 0) //平年判断{if (ri == 29){ri = 1;yue++;write_yr(5, yue);}}}else if (yue <= 7 & yue != 2) //一月到七月{if (yue % 2 == 0) //偶数月(除二月){if (ri == 31){ri = 1;yue++;}}else if (yue % 2 != 0) //奇数月{if (ri == 32){ri = 1;yue++;}}write_yr(5, yue);}else if (yue >= 8) //八月到12月{if (yue % 2 == 0) //偶数月(除二月){if (ri == 32){ri = 1;yue++;if (yue == 13) //如果判断月份为12月,则加一后重新定义月份为1{yue = 1;nian++;write_nian(0, nian);}write_yr(5, yue);}}else if (yue % 2 != 0) //奇数月{if (ri == 31){ri = 1;yue++;write_yr(5, yue);}}}write_yr(8, ri);if (xqnum == 7) //星期写入xqnum = 0;write_com(0x80 + 0x0b);for (a = 0; a < 3; a++){write_data(xingqi[xqnum][a]);delay(5);}}write_sfm(0, shi); //重新写入数据}write_sfm(3, fen); //重新写入数据}write_sfm(6, miao); //重新写入数据}//——————————————————————————————————————————日历时钟结束}//=======================定时器0中断服务程序,用于日历时钟void main() //主函数{init(); //初始化while (1){keyscan(); //检测按键是否被按下}}相关视频在哔哩哔哩:BV1ef4y1q7xY。

相关文档
最新文档