单片机C例程大全

单片机C例程大全

ID:83046082

大小:1.16 MB

页数:179页

时间:2023-09-20

上传者:无敌小子
单片机C例程大全_第1页
单片机C例程大全_第2页
单片机C例程大全_第3页
单片机C例程大全_第4页
单片机C例程大全_第5页
单片机C例程大全_第6页
单片机C例程大全_第7页
单片机C例程大全_第8页
单片机C例程大全_第9页
单片机C例程大全_第10页
资源描述:

《单片机C例程大全》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

*LED闪烁的简单试验*连接方法:JP11(P2)和JP1用8PIN排线连接起来#include〃此文件中定义了51的一些特殊功能寄存器voiddelay(unsignedinti);〃声明延时函数main()(P2=0x00;〃置P0口为低电平delay(600);//调用延时程序P2=Oxff;〃置P0口为高电平delay(600);//调用延时程序I/*******延时函数*************/voiddelay(unsignedinti)(unsignedcharj;for(i;i>0;i-)〃循环600*255次for(j=255;j>0;j-););二进制加法试验;p2口八个灯作二进制加法。理解二进值的计算*;硬件连接:p2口8路指示灯描述:p2口八个灯作二进制加法。理解二进值的计算*连接方法:JP11(P2)和JP1(LED灯)用8PIN排线连接起来*#includevoiddelay(unsignedinti);〃声明延时函数main()(unsignedcharNum=Oxff;while(1){P2=Num;delay(lOOO);〃延时函数Num—;)

1unsignedcharj;for(i;i>0;i-)for(j=255;j>0;j-);LED闪烁的简单试验延时实现p2口LED流水灯效果(用循环移位指令)连接方法:JP1I(P2)和JP1(LED灯)用8PIN排线连接起来#inckKie〃此文件中定义了51的一些特殊功能寄存器#includevoiddelayms(unsignedcharms)//延时子程序unsignedchari;while(ms—)for(i=0;i<120;i++);)main()unsignedcharLED;LED=Oxfe;//Oxfe=11111110while(l)P2=LED;delayms(250);LED=LED«1;〃循环右移1位,点亮下一个LED为左移位if(P2==0x00){LED=Oxfe;}//Oxfe=11111110))描述:跑马灯程序连接方法:JP11(P2)和JP1(LED灯)用8PIN排线连接起来#include#include*延时子程序voiddelayms(unsignedcharms)unsignedchari;while(ms-)(for(i=0;i<120;i++);*主程序*main()(

2unsignedcharLED;LED=Oxfe;P2=LED;while(l)(delayms(250);LED=_crol_(LED,l);〃循环右移1位,点亮下一个LED此函数位库函数P2=LED;,字******-****************■**********************************;中断控制的P2.0小灯。采用11.0592/12MHZ晶振。;连接方法:JP11(P2)和JP1(LED灯)用8PIN排线连接起来ORGOOOOhAJMPmainORGOOObhAJMPint_timerORG0030hmain:MOVtmod,#01h;设置定时器工作方式寄存器MOVthO,#high(65536-10000);MOVtl0,#low(65536-10000);设10MS初始值。MOVie,#82h;设置中断允许寄存器MOV30h,#10MOV31h,#6;30H和31H是两个软件计数器。SETBtrO;启动定时AJMP$;原地踏步int_timer:;定时器。中断服务程序。MOVthO,#high(65536-10000);MOVH0,#low(65536-10000);重设10MS定时。DJNZ30h,llMOV30h,#10

3DJNZMOVCPL11:RETIEND31h,ll31h,#6p2.0;软件计数;10*6*10MS=600MS时间到,Pl.l取反;中断返回**跑马灯实验延时实现LED流水灯效果p2口八个灯作跑马灯。采用了寄存器存中间数连接方法:JPH(P2)和JP1(LED灯)用8PIN排线连接起来#includevoiddelay(unsignedinti);〃声明延时函数charLED;main()unsignedchari;while(1)LED=Oxfe;for(i=0;i<8;i++)P2=LED;delay(500);LED=LED«1;LED=LEDI0x01;if(LED==0x7f))//左移//移位后,后面的位为高电平break;〃提前退出FOR循环for(i=0;i<8;i++)P2=LED;delay(500);右移移位后,后面的位为高电平LED=LED»1;//LED=LEDI0x80;///*******.4正H寸*************/voiddelay(unsignedinti)unsignedcharj;for(i;i>0;i-)

4for(j=255;j>0;j-);按键状态显示试验/*连接方法:连接JP10(P0)与JP5(按钮接口)JP11(P2)与JP1(LED接口)/*按键则点亮LED灯,8路指示灯接p0口#include#includesbitBEEP=P1A5;//将蜂鸣器定义在Pl.5端口上sbitRELAY=P1A4;〃将继电器定义在Pl.4端口上sbitKI=P0A0;sbitK2=0x80Al;//P0和0X80表示的是同一个地址sbitsbitK3=P0A2;sbitK5=P0A4;sbitK4=Ox8OA3;K6=Ox8OA5;if(K2!=1)Iif(!K3)if(~K4)if(!Kl&&!K2)if(!K5)if(!K6)RELAY=0;else〃通过了反相器(HC6800试用)voidbeep();main()while(l)P2=Oxff;if(Kl==0)P2=Oxfe;P2=Oxfd;P2=Oxfb;P2=0xf7;।P2=0x00;//2个按钮同时按下P2□8个灯全部点亮beep。;〃喇叭发声RELAY=1;

5voidbeep()(unsignedchari,j;for(i=0;i<100;i++)//BEEP取反〃需要产生方波〃关闭蜂鸣器BEEP=!BEEP;for(j=0;j<250;j++)_nop_();)BEEP=1;按键控制程序*连接方法:JP10(P2)与JP1(LED灯)连接,*JP11(P0)与JP5(按键接口)连接*开始点亮PILED按P01LED向右移一位*按POOLED向左移一位*连续按动按钮LED会不停的左移或右移#include#includeunsignedcharscan_key();voidproc_key(unsignedcharkey_v);voiddelayms(unsignedcharms);sbitKI=P0A0;〃对应按钮KIsbitK2=P0Al;〃对应按钮K2main()unsignedcharkey_s,key_v;key_v=0x03;P2=Oxfe;while(l)key_s=scan_key();if(key_s!=key_v)delayms(lO);key_s=scan_key();if(key_s!=key_v)key_v=key_s;proc_key(key_v);}))unsignedcharscan_key()(unsignedcharkey_s;

6key_s=0x00;key_s1=K2;key_s«=1;key_s1=KI;returnkey_s;)voidproc_key(unsignedcharkey_v)(if((key_v&0x01)==0)(P2=_cror_(P2,l);elseif((key_v&0x02)=0){P2=_crol_(P2,1);)}voiddelayms(unsignedcharms)//延时子程序(unsignedchari;while(ms—)(for(i=0;i<120;i++);))*按键控制程序*连接方法:JP10(P2)与JP1(LED灯)连接,*JP11(P0)与JP5(按键接口)连接“*开始点亮PILED;键盘P2.5控制的小灯P0.0。P2.7对应K1"ORG0000HAJMPMAINORG0030H;检测按键MAIN:MOVC,p0.0MOVP2.O.C;根据按键决定输出。AJMPMAIN;循环END,*******************************;P0.0小键盘按下P2.0亮再按0.5秒灭。;连接方法:连接方法:JP10(P2)与JP1(LED灯)连接,;JP11(P0)与JP5(按键接口)连接*,******************************************************************************;

7FLAG20HEXPRESSTHE0.5STIMEARRIVED.;FLAG21HEXPRESSTHELEDLIGHTED.ORGOOOOhAJMPmainOORG0030hMAINO:CLR21H;清标志main:MOVC,P0.0;检测按键JCmainMOVR6,#200;设定延时MOVR7,#200TTO:JNB21H,TT2;检测灯状态tt:MOVC,P0.0;检测按键JNCttlJNB20H,MAIN;检测时间标志CLR20HAJMPTT3TT2:MOVc,P0.0;检测按键JNCtt2TT3:CPL21H;改变灯状态CPLp2.0AJMPmainTT1:NOP;延时但不是子程序NOPNOPNOPNOPNOPDJNZR6,TTMOVR6,#200DJNZR7,TTMOVR7,#200SETB20HAJMPTTEND,******************************************************************************,按键POO每按下次加一,通过的P2端口显示;连接方法:JP1O(P0)与JP1(LED灯)连接,;JP11(P2)与JP5(按键接口)连接*;显示出其的二进制计数值。orgOOOOhajmplooporg0030hmova,#Offhloop:jnbpO.O,ttjmplooptt:movp2,a;全不亮calldelay;延时。deca;每次减一。

8jmploopdelay:movr5,#50;延时。dl:movr6,#40d2:movr7,#20djnzr7,$djnzr6,d2djnzr5,dlretend,****************************************************************************;键盘控制的花样彩灯。*;连接方法:JP1O(P2)与JP1(LED灯)连接,;JP11(P0)与JP5(按键接口)连接;按p24,p27,p26,p27,p2口8个灯分别作移动右移动,停止右移左移*,******************************************************************************;亮灯控制程序;,****,ORG0000HUMPSTARTorg0030h

9START:MOVP3,#OFFHJNBP0.7,FUN0;检测按键JNBPO.4,FUN1JNBPO.5,FUN2JNBPO.6,FUN3;关闭按纽AJMPSTARTFUNO:LCALLDL10MS;消岸抖动JBPO.7,STARTWAITLO:JNBP07WAITL0;等待键释放FUNO1:LCALLFUNOOAJMPSTARTFUN1:LCALLDL10MS;消岸抖动JBP0.4,STARTWAITL1JNBP3.3,WAITL1;等待键释放FUN1O:LCALLFUN11AJMPSTARTFUN2:LCALLDL10MS;消岸抖动JBPO.5,STARTWAITL2JNBP0.5,WAITL2;等待键释放FUN2O:LCALLFUN22AJMPSTARTFUN3:LCALLDL10MS;消岸抖动JBP0.6,STARTWAITL3:JNBP0.6,WAITL3;等待键释放MOVP2,#OFFH;关显示LJMPSTARTFUNOO:MOVA,#OFEH;左移子程序FUNOOO:MOVP2,ALCALLDLO5SJNBACC.7,OUT;检测是否结束RLAAJMPFUNOOOOUT:RETFUN11:MOVA,#O7FH;右移子程序FUN111:MOVP2,ALCALLDLO5SJNBACC.O,OUT;检测是否结束RRA

10AJMPFUN1UFUN22:MOVA,#0AAH;间隔显示子程序MOVP2,ALCALLDL30SCPLAMOVP2,ALCALLDL30SRET,*******,;延时程序;,***,DL512:MOVR2,#0FFH;延时500USLOOP1:DJNZR2,LOOP1RETDL10MS:MOVR3,#14H;延时10MSLOOP2:LCALLDL512DJNZR3,LOOP2RETDL05S:MOVR4,#0AH;延时100MSLOOP3:LCALLDLIOMSDJNZR4,LOOP3RETDL30S:MOVR5,#O3H;延时300MS.LOOP4:LCALLDL05SDJNZR5,LOOP4RETend此为蚀立按键接口

11[文件名]C51音乐程序(八月桂花)[功能]通过单片机演奏音乐#include#include〃本例采用89C52,晶振为1I.0592MHZ〃关于如何编制音乐代码,其实十分简单,各位可以看以下代码.〃频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;〃所以拿出谱子,试探编吧!sbitBeep=P1A5;unsignedcharn=0;//n为节拍常数变量unsignedcharcodemusic_tab[]={0x18,0x30,OxlC,0x10,〃格式为:频率常数,节拍常数,频率常数,节拍常数,0x20,0x40,OxlC,0x10,0x18,0x10,0x20,0x10,OxlC,0x10,0x18,0x40,OxlC,0x20,0x20,0x20,OxlC,0x20,0x18,0x20,0x20,0x80,OxFF,0x20,0x30,OxlC,0x10,0x18,0x20,0x15,0x20,OxlC,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,OxFF,0x20,0x20,OxlC,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,Ox2B,0x40,0x20,0x20,OxlC,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,Ox2B,0x40,0x20,0x30,OxlC,0x10,0x18,0x20,0x15,0x20,OxlC,0x20,0x20,0x20,0x26,0x40,0x20,0x20,Ox2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,OxlC,0x10,0x20,0x10,OxIC,Ox10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,OxIF,0x05,0x20,0x10,OxIC,0x10,0x20,0x20,0x26,0x20,Ox2B,0x20,0x30,0x20,Ox2B,0x40,0x20,0x30,OxlC,OxlO,0x18,0x20,0x15,0x20,OxIC,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,OxIC,Ox10,0x18,0x40,OxlC

12,0x20,0x20,0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00voidintO()interrupt1〃采用中断0控制节拍{TH0=0xd8;TLO=Oxef;n-;}voiddelay(unsignedcharm)〃控制频率延时(unsignedi=3*m;while(-i);)voiddelayms(unsignedchara)〃豪秒延时子程序(while(-a);〃采用while(-a)不要采用while(a・・);各位可编译一下看看汇编结果就知道了!voidmain(){unsignedcharp,m;//m为频率常数变量unsignedchari=0;TMOD&=OxOf;TMODI=0x01;TH0=0xd8;TL0=0xef;IE=0x82;play:while(l)

13a:p=music_tab[i];if(p=OxOO){i=0,delayms(lOOO);gotoplay;}〃如果碰到结束符,延时1秒,回到开始再来一遍elseif(p==Oxff){i=i+l;delayms(100),TR0=0;gotoa;}〃若碰到休止符,延时100ms,继续取下一音符else{m=music_tab[i++],n=music_tab[i++];}〃取频率常数和节拍常数TRO=1;〃开定时器1while(n!=O)Beep=~Beep,delay(m);〃等待节拍完成,通过PI口输出音频(可多声道哦!)TR0=0;〃关定时器1ORGAJMP0000HMAIN;伪指令,指定程序从8000H开始存放;跳转指令,程序跳转到MAIN处ORG01OOH;伪指令,指定以下程序从8100H开始存放MAIN:MOVSP,#60;给堆栈指针赋初值LOOP:ACALLMUSICAJMPLOOP;蜂鸣器输出声响子程序,;控制音长;控制音调MUSIC:MOVR5,#60HMIC:CPLP1.5ACALLDELAY5MSDJNZR5,MICRET,***************;延时子程序DELAY5MS:

14MOVR7,#3HDELAYO:MOVDELAY1:DJNZR6,#40HR6,DELAY1DJNZR7,DELAY0RETENDorgOOOOhajmpmainorg0030hmain:MOVR2,#08HDLV1:MOVR3,#0FAHDLV2:CPLP1.5LCALLDELAY1;延时DJNZR3,DLV2DJNZR2,DLV1MOVR2,#10H;改变循环初值DLV3:MOVR3,#0FAHDLV4:CPLP1.5LCALLDELAY2DJNZR3,DLV4DJNZR2,DLV3ajmpmaindelay1:movr7,#OOhLLA:DJNZR7,LLAretdelay2:movr7,#128LLB:DJNZR7,LLBretend

15ORGJMPORGJMPORGMAIN:MOVMOVSETBSETBSETBSTARTO:;clrMOVNEXT:MOVMOVMOVCMOVJZANLMOVMOVSWAPANLJNZCLRJMPSING:DECMOVRLMOVMOVCMOVMOVMOVRLINCMOVCMOVMOVSETBDI:CALLINC0000HMAINOOOBHINTTO0100HSP,#60HTMOD,#01HETOEATROPl.530H,#00HA,30HDPTR,#TABLEA,@A+DPTRR2,AENDDA,#0FHR5,AA,R2AA,#0FHSINGTRODIA22H,AADPTR,#TABLE1A,@A+DPTRTH0,A21H,AA,22HAAA,@A+DPTRTL0,A20H,ATRODELAY30HJMPNEXTENDD:CLRTROJMPSTARTOINTT0:PUSHPSWPUSHACCMOVTL0,20HMOVTH0,21HCPLPl.5POPACCPOPPSWRETIDELAY:MOVR7,#02DELAY0:MOVR4,#187DELAY1:MOVR3,#248DJNZR3,$DJNZR4,DELAY1DJNZR7,DELAY0DJNZR5,DELAYRETTABLE:DB82H,01H,81H,94H,84H,0B4H,0A4H,04H

16DB82H,01H,81H,94H,84H,0C4H,0B4H,04HDB82H,01H,81H,0F4H,0D4H,0B4H,0A4H,94HDB0E2H,01H,OE1H,0D4H,0B4H,0C4H,0B4H,04HDB82H,01H,81H,94H,84H,0B4H,0A4H,04HDB82H,01H,81H,94H,84H,0C4H,0B4H,04HDB82H.01H,81H,0F4H,0D4H,0B4H,0A4H,94HDB0E2H,01H,0ElH,0D4H,0B4H,0C4H,0B4H,04H//TABLEI:DW64260,64400,64524,64580,64684,64777,64820,64898//DW64968,65030,65058,65110,65157,65178,65217ENDORG0000HUMPSTARTORG000BHINC20H;中断服务,中断计数器加1MOVTH0,#0D8HMOVTL0,#0EFH;12M晶振,形成10毫秒中断RETISTART:MOVSP,#50H

17MOVTH0,#0D8HMOVTL0,#0EFHMOVTMOD,#01HMOVIE,#82HMUSICO:NOPMOVDPTR,#DAT;表头地址送DPTRMOV20H,#00H;中断计数器清0MOVB,#00H;表序号清0MUSIC1:NOPCLRAMOVCA,@A+DPTR;查表取代码JZENDO;是OOH,则结束CJNEA,#0FFH,MUSIC5LJMPMUSIC3MUSIC5:NOPMOVR6,AINCDPTRMOVA,BMOVCA,@A+DPTR;取节拍代码送R7MOVR7,ASETBTRO;启动计数MUSIC2:NOPCPLPl.5MOVA,R6MOVR3,ALCALLDELMOVA,R7CJNEA,20H,MUSIC2;中断计数器(2OH)=R7否?;不等,则继续循环MOV20H,#00H;等于,则取下一代码INCDPTR;INCBLJMPMUSIC1MUSIC3:NOPCLRTRO淋止100毫秒MOVR2,#0DHMUSIC4:NOPMOVR3,#0FFH

18LCALLDELDJNZR2,MUSIC4INCDPTRLIMPMUSIC1ENDO:NOPMOVR2,#10H;歌曲结束,延时1秒后继续MUSIC6:MOVR3,#00HLCALLDELDJNZR2,MUSIC6LIMPMUSICODEL:NOPDEL3:MOVR4,#02HDEL4:NOPDJNZR4,DEL4NOPDJNZR3,DEL3RETNOPDAT:db26h,20h,20h,20h,20h,20h,26h,1Oh,2Oh,l0h,20h,80h,26h,20h,30h,20hdb3Oh,2Oh,39h,1Oh,3Oh,l0h,30h,80h,26h,20h,20h,20h,20h,20hJch,20hdb20h,80h,2bh,20h,26h,20h,20h,20h,2bh,l0h,26h,l0h,2bh,80h,26h,20hdb3Oh,2Oh,3Oh,2Oh,39h,l0h,26h,l0h,26h,60h,40h,10h,39h,l0h,26h,20hdb3Oh,2Oh,3Oh,2Oh,39h,l0h,26h,l0h,26h,80h,26h,20h,2bh,lOh,2bh,lOhdb2bh,2Oh,3Oh,lOh,39h,l0h,26h,lOh,2bh,l0h,2bh,20h,2bh,40h,40h,20hdb20h,l0h,20h,lOh,2bh,lOh,26h,3Oh,3Oh,8Oh,18h,2Oh,18h,20h,26h,20hdb20h,20h,20h,40h,26h,20h,2bh,20h,30h,20h,30h,20h,lch,20h,20h,20hdb2Oh,8Oh,lch,20h,lch,2Oh,lch,20h,30h,20h,30h,60h,39h,1Oh,3Oh,lOhdb20h,20h,2bh,l0h,26h,l0h,2bh,l0h,26h,l0h,26h,1Oh,2bh,lOh,2bh,8Ohdb18h,2Oh,l8h,20h,26h,20h,20h,20h,20h,60h,26h,lOh,2bh,2Oh,3Oh,2Ohdb3Oh,2Oh,lch,20h,20h,20h,20h,80h,26h,20h,30h,1Oh,3Oh,1Oh,3Oh,2Ohdb39h,20h,26h,lOh,2bh,l0h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,lOhdb20h,l0h,2bh,l0h,26h,30h,30h,80h,00HEND

19继电器实验/*连接方法:连接JP11(P2)与JP5(按钮接口)/*按下P2.7继电器P1.4动作#include必1的端口和各特殊寄存器定义在此文件中sbitRELAY=PlA4;sbitKI=P2A7;〃定义继电器时应单片机管脚//定义按钮对应单片机管脚主函数main()(if(!K1)RELAY=0;〃按钮按下开启继电器elseRELAY=1;//按钮松开关闭继用器

20*138译码器实验**实验目的:1.了解138译码器工作原理**2.学会用C语言对138译码器编程控制**实验现象:LED灯每隔一段时间循环点亮**接线方法:J15连JP1(参考图片连接)*#includesbitHC138A=P2A2;〃定义译码器输入端A在P2.2管脚上sbitHC138B=P2A3;〃定义译码器输入端B在P2.3管脚上sbitHC138C=P2A4;〃定义译码器输入端C在P2.4管脚h延时函数voiddelay(unsignedinti)(unsignedcharj;for(i;i>0;i-)〃循环1500*255次CPU在这里执行一段时间for(j=255;j>0;j-);main()(//点亮第一个LED灯HC138C=0;HC138B=0;HC138A=0;//输入000delay。500);//延时//点亮第二个LED灯HC138C=0;HC138B=0;HC138A=1;//输入001delay(1500);〃延时//点亮第三个LED灯HC138C=0;HC138B=l;HC138A=0;〃输入010delay(1500);//点亮第四个LED灯HC138C=0;HC138B=1;HC138A=1;〃输入Oildelay(2000);//点亮第五个LED灯HC138C=1;HC138B=0;HC138A=0;//输入100delay(1500);

21//点亮第六个LED灯HC138C=1;HC138B=O;HC138A=1;〃输入101delay(1500);HC138C=l;HC138B=l;HC138A=0;〃输入110delay(1500);HC138C=1;HC138B=1;HC138A=1;〃输入111delay(1500);H*N^itarCL*lavlawi.Xwdon'tc«e74LS138*标题:试验数码管上如何显示数字(共阳极)**连接方法:P0与JP3用8PIN排线连接*请学员认真消化本例程,用单片机脚直接控制数码管#includevoiddelay(unsignedinti);//此表为LED的字模89ab〃函数声名01defunsignedcharcode234567LED7Code[]={〜0x3F,〜0x06,〜0x5B,〜0x4F,〜0x66,〜0x6D,〜0x7D,〜0x07,〜0x7F,〜0x6F,〜0x77,〜0x7C,〜0x39,〜0x5E,〜0x79,〜0x71};main()(unsignedintLedNumVal;〃定义变量while(l)//将字模送到P0口显示LedNumVal+4-;InputsOutputsEnableSelectG1G2,CBAY0Y1Y2Y3Y4Y5Y6Y7XHXXXHHHHHHHHLXXXXHHHHHHHHHLLLLLHHHHHHHHLLLHHLHHHHHHHLLHLHHLHHHHHHLLHHHHHLHHHHHLHLLHHHHLHHHHLHLHHHHHHLHHHLHHLHHHHHHLHHLHHHHHHHHHHL•S2-G2A*G2BP0=LED7Code[LedNumVal%16]&0x7f;//LED70x7f为小数点共阴和共阳此处也是不一样;delay(6000);〃调用延时程序

22延时程序voiddelay(unsignedinti)(charj;for(i;i>0;i-)〃循环6000*200次for(j=200;j>0;j-);i«<1i1I极去攵。4彳子*标题:试验数码管上显示数字(单片机直接实现位选共阴极)*连接方法:P0与J12用8PIN排线连接P1与JP16用排线连接*请学员认真消化本例程,用573锁存器控制和单片机脚直接位选控制(非译码器控制)数码管*#includevoiddelay(unsignedinti);〃函数声名charDelayCNT;〃定义变量〃此表为LED的字模,共阴数码管0-9-unsignedcharcodeDisp_Tab[J={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};〃段码控制〃此表为8个数码管位选控制,共阴数码管1-8个-unsignedcharcodedispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F};〃位选控制查表的方法控制/************主函数**********************/main()unsignedinti,LedNumVal=l;〃变量定义〃变量定义unsignedintLedOut[10];DelayCNT=0;while(l)if(++DelayCNT>=20)〃控制数字变化速度(DelayCNT=0;〃20个扫描周期清零一次

23++LedNumVal;〃每隔20个扫描周期加一次LedOut[0]=Disp_Tab[LedNumVal%10000/1000];LedOut[1]=Disp_Tab[LedNumVal%1000/100]l0x80;LedOut[2]=Disp_Tab[LedNumVal%100/10];LedOut[3]=Disp_Tab[LedNumVal%10];LedOut[4]=Disp_Tab[LedNumVal%10000/1000];〃千位LedOut[5]=Disp_Tab[LedNumVal%1000/100]l0x80;〃百位带小数点LedOut[6]=Disp_Tab[LedNumVal%100/10];〃十位LedOut[7]=Disp_Tab[LedNumVal%10];//个位for(i=0;i<9;i++)(PO=LedOutli];Pl=dispbit[i];〃使用查表法进行位选/*switch(i)〃使用switch语句控制位选(caseO:P1=0x7F;break;case1:P1=OxbF;break;case2:P1=OxdF;break;case3:P1=OxeF;break;case4:P1=0xf7;break;case5:P1=Oxfb;break;case6:P1=Oxfd;break;case7:P1=Oxfe;break;}*/delay(l50);〃扫描间隔时间太长会数码管会有闪烁感voiddelay(unsignedinti)charj;for(i;i>0;i-)for(j=200;j>0;j-);)*标题:试验数码管上显示数字(译码器位选共阴极)**连接方法:P0与J12用8PIN排线连接*请学员认真消化本例程,用573锁存器和译码器控制和数码管#include

24sbitLS138A=P2A2;〃定义138译码器的输入A脚由P2.2控制sbitLS138B=P2A3;〃定义138译码器的输入脚B由P2.3控制sbitLS138C=P2A4;〃定义138译码器的输入脚C由P2.4控制voiddelay(unsignedinti);〃函数声名charDelayCNT;〃此表为LED的字模,共阴数码管0・9-unsignedcharcodeDisp_Tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40);main(){unsignedinti,LedNumVal;unsignedintLedOut[101;DelayCNT=0;while(l)〃进入循环状态(if(++DelayCNT>=50){DelayCNT=0;〃延时计数每扫描一次加一次++LedNumVal;〃每隔50个扫描周期加一次LedOut[0]=Disp_Tab[LedNumVal%10000/1000];LedOut[1J=Disp_Tab[LedNumVal%1000/100JI0x80;LedOut[2]=Disp_Tab[LedNumVal%100/10];LedOut[3J=Disp_Tab[LedNumVal%10];LedOut[4]=Disp_Tab[LedNumVal%10000/1000];〃千位LedOut[5]=Disp_Tab[LedNumVal%1000/100]l0x80;〃百位带小数点LedOut[6]=Disp_Tab[LedNumVal%100/10];〃十位LedOutl7]=Disp_Tab[LedNumVal%10J;〃个位for(i=0;i<8;i++)〃实现8位动态扫描循环(PO=LedOut[i];〃将字模送到P0口显示switch(i)〃使用switch语句控制位选也可以是用查表的方式学员可以试着自己修改{caseO:LS138A=0;LS138B=O;LS138C=O;break;case1:LS138A=1;LS138B=O;LS138C=O;break;case2:LS138A=0;LS138B=1;LS138C=O;break;case3:LS138A=1;LS138B=1;LS138C=O;break;case4:LS138A=0;LS138B=O;LS138C=1;break;case5:LS138A=1;LS138B=O;LS138C=1;break;

25case6:LS138A=0;LS138B=1;LS138C=1;break;case7:LS138A=1;LS138B=1;LS138C=1;break;delay(15O);延时程序voiddelay(unsignedinti)charj;for(i;i>0;i-)for(j=200;j>0;j-);*****标题:74HC573输出使能试验(单片机直接实现位选共阴极)*连接方法:P0与J12用8PIN排线连接P16与JP11(P2)用排线连接

26#include#includevoiddelay(unsignedinti);〃函数声名sbitLE=P1AO;〃定义输出使能控制脚在P1.0上charDelayCNT;〃定义变量〃此表为LED的字模,共阴数码管0-9-unsignedcharcodeDisp_Tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};〃段码控制〃此表为8个数码管位选控制,共阴数码管1-8个-unsignedcharcodedispbit[8J={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF\0x7F};〃位选控制查表的方法控制/************^^函^^**********************/main()|unsignedinti,LedNumVal=l;〃变量定义unsigned血LedOut[10];〃变量定义DelayCNT=0;while(l)(if(++DelayCNT>=20){DelayCNT=0;++LedNumVal;〃控制数字变化速度//20个扫描周期清零一次〃每隔20个扫描周期加一次LedOut[0]=Disp_Tab[LedNumVal%10000/1000];LedOutllJ=Disp_Tab[LedNumVal%1000/100JI0x80;LedOut[2]=Disp_Tab[LedNumVal%100/10];LedOut[3J=Disp_TablLedNumVal%10];LedOut[4]=Disp_Tab[LedNumVal%10000/1000];〃千位LedOut[5]=Disp_Tab[LedNumVal%1000/100]l0x80;〃百位带小数点LedOut[6]=Disp_Tab[LedNumVai%100/10];〃卜位LedOut[7]=Disp_Tab[LedNumVal%10];〃个位for(i=0;i<9;i++)(LE=0;〃输出使能,锁存器不输出数据PO=LedOut[i];LE=1;〃输出使能,锁存器将8位数据一口吐出P2=dispbit[i];〃使用查表法进行位选

27delay(150);〃扫描间隔时间太长会数码管会有闪烁感LE=O;〃锁存器不输出数据延时处理voiddelay(unsignedinti)(charj;for(i;i>0;i-)forO=200;j>0;j-);J1674LS1381234678COM'1ccccccccY6FFY3一Y4FZY7QNDABABCG1G2G2y2U13COMqqqqqqqqDOD1D2D3D4D5D6D7aMCOM*标题:试验动静LED灯——实现动态控制*连接方法:P0与J12用8PIN排线连接J14短路帽短接*请学员认真消化本例程,用单片机脚直接控制和138译码器控制数码管#include#include

28sbitLED8_1=P1AI;勿14接在Pl.1上voiddelay(unsignedinti);〃函数声名charDelayCNT;charLED;main()LED=0x01;DelayCNT=0;while(l)if(++DelayCNT>=10)〃点亮动静LED灯速度控制DelayCNT=0;//左移LED=LED«1;if(LED==0)LED=1;〃移位后,后面的位为高电平}P0=LED;LED8_l=0;delay(lOOO);〃调用延时程序voiddelay(unsignedinti)charj;for(i;i>0;i-)for(j=200;j>0;j-);I此处可以用珏用线连单片机的任何I脚住.根据自己的需要连接、0-0§=0-§=GNLE01234567sDQQQQQQQQ<。VG101234567-EEDCDDDdDEXD_0L19TS立161514亘TT^2^3■^5^720VCC10GNDJ21D111D12:D13二D】4:D157D16二D17二D184彳路HJ时,生提Pl.1帆

29*LED点阵实验(左右上下流动显示)*说明通过P0和P2作为点阵接口*要求学员掌握LED点阵的工作原理和各点阵脚的定义及接法,具体接线请参考接线说明*我们采用的LED点阵式是低功耗,在做实验时可以用单片机脚直接驱动#includeunsignedcharcodetaba[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f);〃纵条表unsignedcharcodetabb[]={0x01,0x02,0x04,0x08,Ox10,0x20,0x40,0x80};〃衡条表voiddelay1(void)(unsignedchari,j,k;for(k=10;k>0;k-)〃循环时间是10*20*248次for(i=20;i>0;i—)for(j=248;j>0;j-);/*************主函数*****************************/voidmain(void)unsignedcharij;while(l)(for(j=0;j<3;j++)〃从左到右3次(for(i=0;i<8;i++)〃循环8次逐条点亮8个LED点阵灯{P2=taba[i];PO=Oxff;delay1();)for(j=0;j<3;j++)〃从右到左3次(for(i=0;i<8;i++)〃循环8次逐条点亮8个LED点阵灯(P2=taba[7-i];PO=Oxff;delayl();))for(j=0;j<3;j++)〃从上到下3次[for(i=0;i<8;i++)//循环8次逐条点亮8个LED点阵灯[

30P2=0x00;PO=tabb[7-i];delay1();))for(j=0;j<3;j++)〃从下到上3次(for(i=0;i<8;i++)〃循环8次逐条点亮8个LED点阵灯(P2=0x00;PO=tabb[i];delay1();))))*LED点阵实验(流动显示123456789)*说明通过P0和P2作为点阵接口*要求学员掌握LED点阵的工作原理和各点阵脚的定义及接法,具体接线请参考接线说明*我们采用的LED点阵式是低功耗,在做实验时可以用单片机脚直接驱动#includeunsignedcharcodetab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,);〃列选通控制unsignedcharcodedigittab[l8J[8]={{0x00,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00},//0{0x00,0x00,0x00,0x00,0x21,0x7f,0x01,0x00},〃1{0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00},〃2{0x00,0x00,0x22,0x49,0x49,0x49,0x36,0x00},〃3{0x00,0x00,OxOc,Ox14,0x24,Ox7f,0x04,0x00},〃4{0x00,0x00,0x72,0x51,0x51,0x51,0x4e,0x00},//5{0x00,0x(X),0x3e,0x49,0x49,0x49,0x26,0x00),//6{0x00,0x00,0x40,0x40,0x40,0x4f,0x70,0x00),//7{0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00},〃8

31{0x00,0x00,0x32,0x49,0x49,0x49,Ox3e,0x00},//9{0x00,OxOO,Ox7F,0x48,0x48,0x30,0x00,0x00},//P{0x00,0x00,Ox7F,0x48,0x4C,0x73,0x00,0x00},//R{0x00,0x00,Ox7F,0x49,0x49,0x49,0x00,0x00},//E{0x00,0x00,0x3E,0x41,0x41,0x62,0x00,0x00},〃C{0x00,0x00,0x7F,0x08,0x08,0x7F,0x00,0x00},//H{0x00,0x00,0x00,OxFF,OxFF,0x00,0x00,0x00},//I{0x00,0x7F,0x10,0x08,0x04,0x7F,0x00,0x00),//N{0x7C,0x48,0x48,OxFF,0x48,0x48,0x7C,0x00}〃中};unsignedinttimecount1,timecount2;〃定义的变量unsignedcharcntx,cnty;//cntx列控制数的变量cnty用来控制字符与字符之间的切换voidmain(void)(cnty=O;while(l)(if(cntyvl8)〃红色Pl=0xFF;〃列选通都关闭掉

32P2=tab[cntx];//列线PO=digittab[cnty][cntx];//行线)else〃绿色(P2=0xFF;〃红色的列选通都关闭掉Pl=tab[cntx];//列线PO=digittab[cnty-18][cntx];//行线)〃用于控制动态扫描的速度if(++timecountl>=50)(timecount1=0;if(++cntx>=8)cntx=0;)〃用于控制动字符间的切换速度if(++timecount2>=20000)(timecount2=0;if(++cnty>=36)cnty=0;53'4?'iOEVCCLEGND?^DPc20xrc10GNDGR1GR2GR5REIREIRE3RE4REFRE6J18贮RE8*标题:步进电机试验二*通过本例程了解步进马达使用及驱动程序编写单双八拍工作方式:*;A-AB-B-BC-C-CD-D-DA(即一个脉冲,转3.75度)*J14短路冒需断开*请学员一定要消化掉本例程#includeHreg52.hMunsignedcharcodeRUN[8]={Oxf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};〃步进电机相序表voiddelay(unsignedintt);

33〃步进电机驱动voidmotor_ffw()(unsignedchari;for(i=0;i<8;i++)〃一个周期转3.75*8=30度(Pl=RUN[i]&0xlf;〃取数据delay(50);〃调节转速)voiddelay(unsignedintt)(unsignedintk;while(t—)(for(k=0;k<60;k++)〃用for的空循环延长程序的执行时间())}main()(while(l)(motor_ffw();〃调用旋转处理函数*标题:步进电机试验一*通过本例程了解步进马达使用及驱动程序编写*;单双八拍工作方式:*;A-AB-B-BC-C-CD-D-DA(即一个脉冲,转3.75度)*J14短路冒需断开*请学员一定要消化掉本例程#include”reg52.h”//MotorsbitA=PIA0;〃定义管脚sbitb=PlAl;sbitC=PlA2;sbitD=PlA3;〃步进电机驱动unsignedcharMotorStep=0;〃步进马达步序

34unsignedintMotorDelay,Speed=1,TIM,CT;*definespeed28//调整速度数值不要设的太低低了会引起震动。*初始化马达*voidInitMotor()(A=1;b=1;C=l;D=1;}voidSetMotor()(//if(Speed==0)return;MotorDelay=Speed;〃调整速度switch(MotorStep)(case0:if(TIM)//A(A=0;//Oxflb=1;C=1;D=1;MotorStep=1;TIM=0;)break;case1://ABif(TIM)(A=0;//Oxf3b=0;C=1;D=1;MotorStep=2;TIM=0;}break;case2://Bif(TIM)

35A=1;b=0;//0xf2C=l;D=1;MotorStep=3;TIM=0;)break;case1://BCif(TIM)(A=1;b=0;//0xf6C=0;D=1;MotorStep=4;TIM=O;)break;case2://Cif(TIM)A=1;b=1;//0xf4C=0;D=1;MotorStep=5;TIM=0;)break;case3://CDif(TIM)(A=1;b=1;//OxfcC=0;D=0;MotorStep=6;TIM=0;)break;case4://Dif(TIM)(A=1;

36b=1;//0xf8C=1;D=0;MotorStep=7;TIM=0;)break;case1://DAif(TIM)(A=0;b=1;//0xf9C=l;D=0;MotorStep=0;TIM=0;)break;voidsystem」ni()(TMODI=Oxll;TH0=0xDC;//11.0592MTL0=0x00;IE=0x8A;TRO=1;main(){system_Ini();InitMotor();while(l)(SetMotor();定时中断延时voidTzd(void)interrupt1TH0=0xfe;//11.0592TLO=0x33;if(CT++==speed){TIM=1;CT=0;}}

37*标题:步进电机试验三(加减速运行)*通过本例程了解步进马达使用及驱动程序编写*;单双八拍工作方式:*;A-AB-B-BC-CCD-D-DA(即一个脉冲,转3.75度)*请学员一定要消化掉本例程#includeureg52.h*,voiddelay();//MotorsbitFl=P1AO;sbitF2=PlAl;sbitF3=PlA2;sbitF4=PlA3;unsignedcharcodeFFW[8J={Oxfl,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9);unsignedcharcodeFFZ[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xfl};〃正转unsignedintK,rate;步进电机驱动voidmotor_ffw()unsignedchari;for(i=0;i<8;i++)〃一个周期转30度(Pl=FFW[i]&0xlf;//取数据〃调节转速delay();))延时程序voiddelay()(unsignedintk,t;t=rate;while(t—)(for(k=0;k<100;k++){)*步进电机运行

38voidmotor_tum()(unsignedcharx;rate=0x2a;x=0x40;do(motor_ffw();〃加速rate-;}while(rate!=0x08);do(motor_ffw();〃匀速x-;}while(x!=0x01);do(motor_ffw();〃减速rate++;}while(rate!=0x2a);)main()(while(l)(motor_tum();标题:步进电机试验四通过本例程了解步进马达使用及驱动程序编写双四拍工作方式:AB-BC-CD-DA(即一个脉冲,转7.5度)*请学员一定要消化掉本例程,include"reg52.hn//MotorsbitFl=P1AO;sbitF2=PlAl;sbitF3=PlA2;sbitF4=Pl八3;〃步进电机驱动unsignedcharMotorStep=0;unsignedintMotorTimer=0;unsignedintTIM.CT;voidInitMotor()(

39Fl=1;F2=1;F3=l;F4=l;voidSetMotor()(//if(Speed==0)return;switch(MotorStep)(case0:if(TIM){Fl=0;F2=0;F3=1;F4=1;MotorStep=1;TIM=0;)break;case1:if(TIM)(Fl=1;F2=0;F3=0;F4=1;MotorStep=2;TIM=0;)break;case2:if(TIM){Fl=1;F2=1;F3=0;F4=0;MotorStep=3;TIM=0;)break;

40case3:if(TIM){Fl=0;F2=I;F3=1;F4=0;MotorStep=0;TIM=0;}break;voidsystem」ni()(TM0DI=0x11;TH0=0xDC;//11.0592MTL0=0x00;IE=0x8A;TRO=1;main(){system_Ini();InitMotor();while(l)SetMotor();[tl(0.5ms)中断]中断中做PWM输出1000/(0.02ms*250)=200HzvoidTzd(void)interrupt1THO=Oxfe;//11.0592TLO=0x33;if(CT++==35){TIM=1;CT=0;})

41*****标题:步进电机试验五(正转一圈反转一圈)通过本例程了解步进马达使用及驱动程序编写单双八拍工作方式:A・AB-B-BC・C・CD・D・DA(即一个脉冲,转3.75度)请学员一定要消化掉本例程#includenreg52.hHvoiddelay(unsignedintt);//MotorsbitFl=P1AO;sbitF2=PlAl;sbitF3=PlA2;sbitF4=PlA3;unsignedcharcodeFFW[8]={Oxfl,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};unsignedcharcodeFFZ[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xn,0xf3,0xfl};〃正转unsignedintK;*步进电机驱动*voidmotor_ffw()(unsignedchari;unsignedintj;for(j=0;j<12;j++)〃转1*n圈(for(i=0;i<8;i++)〃一个周期转30度(if(K==l)Pl=FFW[i]&0xlf;〃取数据if(K==2)Pl=FFZ[i]&0xlf;delay(5);〃调节转速延时程序voiddelay(unsignedintt)unsignedintk;while(t—)(for(k=0;k<80;k++){)

42)main()(while(l)(K=l;motor_ffw();K=2;motor_ffw();

43**矩阵键盘1602显示键值连接方法:JP8(P1)与JP4(矩阵键盘接口)连接矩阵键盘定义:P1.1-P1.4为列线,P1.4-PL7为行线矩阵键盘P1喇叭接P1.5口请将JP165短路冒断开

44#include#include#defineucharunsignedchar#defineuintunsignedintuchardis_buf;〃显示缓存uchartemp;ucharkey;〃键顺序吗voiddelayO(ucharx);//x*0.14MSsbitLCD_RW=P2A5;sbitLCD_RS=P2A6;sbitLCD_EN=P2A7;ucharcdisl[16]={MKEYNUMBER"};ucharcdis2[16]={"KEY-CODE:H"};definedelayNOP();{_nop_();_nop_();_nop_();_nop_(););/**//*延时子程序*//**/voiddelay(ucharx){ucharj;while((x—)!=0){for(j=0;j<125;j++){;}/**//*检查LCD忙状态*//*lcd_busy为1时,忙,等待。Icd-busy为0时,闲,可写指令与数据*/

45*/

46bitlcd_busy()(bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOPO;result=(bit)(P0&0x80);LCD_EN=0;retum(result);*/*/可/*/*写指令数据到LCD/*RS=L,RW=L,E=ift脉冲,口04)7=指令码。产voidlcd_wcmd(ucharcmd)while(lcd_busy());LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_();P0=cmd;delayNOP();LCD_EN=1;delayNOP();LCD_EN=0;*/列/*产写显示数据到LCD/*RS=H,RW=L,6=高脉冲,D0-D7=数据。/*

47while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;delayNOP();LCD_EN=1;delayNOPO;LCD_EN=0;/*LCD初始化设定voidlcdjnit()(delay(15);〃16*2显示,5*7点阵,8位数据lcd_wcmd(Ox38);delay(5);lcd_wcmd(0x38);delay(5);lcd_wcmd(0x38);delay(5);lcd_wcmd(0x0c);delay⑸;lcd_wcmd(0x06);delay(5);lcd_wcmd(0x01);delay(5);〃显示开,关光标〃移动光标〃清除LCD的显示内容/*/*设定显示位置/*

48Icd_wcmd(posI0x80);〃数据指针=80+地址变量/*键扫描子程序(4*3的矩阵)PL4PL5Pl.6PL7为行voidkeyscan(void){temp=0;Pl=0xF0;delay(l);temp=P1;temp=temp&0xF0;Pl.lP1.2P1.3为歹ij〃高四位输入行为高电平列为低电平〃读P1口〃屏蔽低四位temp=~((temp»4)IOxFO);if(temp==l)//p1.4被拉低key=0;elseif(temp==2)//pl.5被拉低key=l;elseif(temp==4)//pl.6被拉低key=2;elseif(temp==8)//pl.7被拉低key=3;elsekey=16;Pl=0x0F;〃低四位输入列为高电平行为低电平delay(l);temp=Pl;〃读Pl口temp=temp&0x0F;temp=-(templOxF0);if(temp==2)//pl.l被拉低key=key+0;elseif(temp==4)//pl.2被拉低key=key+4;elseif(temp==8)//pl.3被拉低key=key+8;elsekey=16;〃键值入显示缓存dis_buf=key;dis_buf=dis_buf&OxOf;if(dis_buf>9)〃转换为ASCH码dis_buf=dis_buf+0x37;

49else*/dis_buf=dis_buf+0x30;/*判断键是否按下voidkeydown(void)Pl=OxFO;if(Pl!=OxFO)〃判断按键是否按下如果按钮按下会拉低P1其中的一个端口keyscan();/*主程序main()ucharm;P0=0xFF;〃置P0口Pl=OxFF;//置Pl口delay(lO);〃延时lcdjnit();〃初始化LCDlcd_pos(0);〃设置显示位置为第一行的第1个字符m=0;while(cdisl[mj!=W')(〃显示字符lcd_wdat(cdis1[m]);m++;}lcd_pos(0x40);〃设置显示位置为第二行第1个字符m=0;while(cdis2[m]!=W)(lcd_wdat(cdis2[m]);〃显示字符m++;)dis_buf=0x2d;〃显示字符while(l)keydown();lcd_pos(0x4c);lcd_wdat(dis_buf);〃第一位数显示

5087654321描述:*矩阵键盘数码管显示键值**排线连接方法:JP8(P1)与JP4(矩阵键盘接口)连接P0与JP3(静态数码管)连接**矩阵键盘定义:**P1.1-P1.3为行线,P1.4-PL7为列线**喇叭接P1.5口矩阵键盘P1口,*注意:请将JP165短路冒断开*include*defineucharunsignedchar〃宏的定义变量类型uchar代替unsignedchar*defineuintunsignedint〃宏的定义变量类型uint代替unsignedintuchardis_buf;〃显示缓存uchartemp;ucharkey;〃键顺序码voiddelay(ucharx);//x*0.14MS//此表为LED的字模0123456789abcdefunsignedcharcodeLED7Code[]={〜0x3F,〜0x06,〜0x5B,〜0x4F,〜0x66,〜0x6D,〜0x7D,〜0x07,〜0x7F,〜0x6F,〜0x77,〜0x7C,〜0x39,〜0x5E,-0x79,-0x71);

51*延时子程序voiddelay(ucharx){ucharj;while((x-)!=0)//CPU执行x*12次{forG=0;j<125;j++){;}键扫描子程序(3*4的矩阵)PL4PL5Pl.6PL7为歹!Jvoidkeyscan(void){temp=0;Pl=0xF0;delay(l);temp=P1;temp=temp&0xF0;Pl.lP1.2P1.3为行〃高四位输入列为高电平行为低电平〃延时〃读P1口〃屏蔽低四位temp=-((temp»4)IOxFO);if(temp==l)//pl.4被拉低key=l;〃第1个按键键值elseif(temp==2)//pl.5被拉低key=2;〃第2个按键键值elseif(temp==4)//pl.6被拉低key=3;〃第3个按键键值elseif(temp==8)//pl.7被拉低key=4;〃第4个按键键值elsekey=16;Pl=0x0F;〃低四位输入行为高电平列为低电平delay(l);〃延时temp=Pl;〃读Pl口temp=temp&0x0F;temp=~(templ0xF0);if(temp==2)〃第一行pl.l被拉低(直接temp==2是因为我们P1.0空的,是3*4的矩阵键盘)key=key+0;elseif(temp==4)〃第二行pl.2被拉低key=key+4;

52elseif(temp==8)〃第三行pl.3被拉低key=key+8;elsekey=16;dis_buf=key;〃键值入显示缓存dis_buf=dis_buf&OxOf;*判断键是否按下voidkeydown(void)(Pl=0xF0;〃将高4位全部置1低四位全部置0if(Pl!=0xF0)〃判断按键是否按下如果按钮按下会拉低P1其中的一个端口(keyscan();〃调用按键扫描程序*主程序main()P0=0xFF;Pl=0xFF;delay(lO);〃置P0口〃置Pl口〃延时while(l)(keydown();〃调用按键判断检测程序P0=LED7Code[dis_buf%16]&0x7f;//LED70x7f为小数点共阴和共阳此处也是不一样;%16表示输出16进制

53*标题:试验74HC595驱动8位LED灯(C语言)*连接方法:JP12用条线冒短接JP1(LED灯接口)和JP2(595接口)用8PIN排线连接*通过本例程了解74HC595(串入并出)基本原理和使用*3个I/O扩展8个输出,通过片选可以串接更多74HC595芯片,得到更多的输出数*请学员认真消化本例程,懂74c595在C语言中的操作*此汇编程序留给大家做为课后作业自己完成。关于HC595汇编驱动参考“静态显示(74HC595驱动)#include#include#defineNOP()_nop_()/*定义空指令*///SPIIOsbitMOSIO=P3八4;sbitR_CLK=P3A5;sbitS_CLK=P3A6;voiddelay(unsignedinti);〃函数声名voidHC595SendData(unsignedcharSendVal);main(){unsignedcharLed=0xfe;//11111110HC595SendData(0xff);〃初始化595使他为高电平让LED处于熄灭状态while(l)HC595SendData(Led);〃调用595驱动程序把LED的数据送到595Led«=l;Led=Ledl0x01;〃移位后,后面的位为高电平;

54if(Led==Oxff)Led=0xfe;//llll1110delay(200);*延时子程序voiddelay(unsignedinti)unsignedintj;for(i;i>0;i-)for(j=300;j>0;j-);**函数名称:HC595SendData**功能描述:向SPI总线发送数据voidHC595SendData(unsignedcharSendVal)(unsignedchari;for(i=0;i<8;i++)if((SendVal«i)&0x80)MOSIO=1;//setdatalinehigh0X80最高位与SendVal左移的最高位进行逻辑运算elseMOSIO=0;//如果为真MOSIO=1S_CLK=0;NOP();〃产生方形波NOP();S_CLK=1;R_CLK=0;//setdatalinelowNOP();〃产生方形波NOP();R_CLK=1;〃片选

55*标题:试验74HC595驱动数码管上显示数字(C语言)*连接方法:JP12用条线冒短接JP3和JP2用8PIN排线连接*通过本例程了解74HC595(串入并出)基本原理和使用*请学员认真消化本例程,懂74c595在C语言中的操作#include#include#defineNOP()_nop_()/*定义空指令*///SPIIOsbitMOSIO=P3A4;〃串行数据线sbitR_CLK=P37;〃数据并行输出控制sbitS_CLK=P3A6;〃串行时钟线voiddelay(unsignedinti);〃函数声名voidHC595SendData(unsignedcharSendVal);〃函数声名//此表为LED的字模〃012345678gAbcdE-LPUHidden_(20)unsignedcharcodeLED7Code[]={〜0x3F,〜0x06,〜0x5B,〜0x4F,〜0x66,〜0x6D,〜0x7D,〜0x07,〜0x7F,〜0x6F,〜0x77,〜0x7C,〜0x39,〜0x5E,-0x79,-0x71};main(){unsignedcharHC595SendVal;unsignedintLedNumVal;while(l)(//LED7;显示0-FLedNumVal++;HC595SendVal=LED7Code[LedNumVal%16];LedNumVal%10显示0-9HC595SendData(HC595SendVal);〃调用595驱动函数delay(200);})/*******************至正************/voiddelay(unsignedinti)(unsignedintj;for(i;i>0;i-)//CPU循环执行i*300次for(j=300;j>0;j-);

56**函数名称:HC595SendData**功能描述:向SPI总线发送数据voidHC595SendData(unsignedcharSendVal)(unsignedchari;for(i=0;i<8;i++)(if((SendVal«i)&0x80)MOSIO=1;//setdatalinehigh0X80最高位与SendVal左移的最高位进行逻辑运算elseMOSIO=0;//如果为真MOSIO=1S_CLK=0;NOP();〃短暂延时产生一定宽度的脉冲信号NOP();〃短暂延时S_CLK=1;R_CLK=0;//setdatalinelowNOP();〃短暂延时NOP();〃短暂延时R_CLK=1;//*双595驱动LED点阵实验**说明:点阵的红绿接口分别接在595的输出端口JP2和P595*现象:(流动显示123456789)*实验目的:掌握LED点阵的动作原理和74HC595的级联控制*要求学员掌握LED点阵的工作原理和各点阵脚的定义及接法,具体接线请参考接线说明#include#include#defineNOP()_nop_()/*定义空指令*///SPIIOsbitMOSIO=P3八4;〃定义管脚sbitR_CLK=P3A5;〃定义管脚

57sbitS_CLK=P3A6;〃定义管脚voidHC595SendData(unsignedintSendVal);//595驱动函数声明unsignedintVai;unsignedcharcodetab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsignedcharcodedigittab[18]l8]={〃字符显示字模{0x00,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00},//0{0x00,0x00,0x00,0x00,0x2l,0x7f,0x01,0x00},//I{0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00),//2{0x00,0x00,0x22,0x49,0x49,0x49,0x36,0x00),//3{0x00,0x00,0x0c,Ox14,0x24,Ox7f,0x04,0x00},//4{0x00,0x00,0x72,0x51,0x51,0x51,0x4e,0x00},//5{0x00,0x00,0x3e,0x49,0x49,0x49,0x26,0x00},116{0x00,0x00,0x40,0x40,0x40,0x4f,0x70,0x00},//7{0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00},//8{0x00,0x00,0x32,0x49,0x49,0x49,0x3e,0x00},//9{0x00,0x00,0x7F,0x48,0x48,0x30,0x00,0x00},//P{0x00,0x00,0x7F,0x48,0x4C,0x73,0x00,0x00},//R{0x00,0x00,0x7F,0x49,0x49,0x49,0x00,0x00),//E{0x00,0x00,0x3E,0x41,0x41,0x62,0x00,0x00},//C{0x00,0x00,0x7F,0x08,0x08,Ox7F,0x00,0x00},//H{0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00},//I{0x00,0x7F,0x10,0x08,0x04,0x7F,0x00,0x00},//N{0x7C,0x48,0x48,OxFF,0x48,0x48,0x7C,0x00)〃中

58);unsignedinttimecount;unsignedcharcnta;unsignedcharcntb;voidmain(void)(TMOD=0x01;TH0=(65536-3000)/256;TL0=(65536-3000)%256;TR0=l;〃开启定时0ET0=l;EA=1;〃开启中断cntb=O;while(1)())东*定时中断voidtO(void)interrupt1using0TH0=(65536-3000)/256;〃定时器高位装载数据TLO=(65536-3OOO)%256;〃定时器低位装载数据if(cnlbvl8)〃红色(//Pl=OxFF;//P2=tab[cnta];

59PO=digittab[cntb][cnta];Vai=tab[cnta]&OxOOff;Vai«=8;Vai=Vai+OxOOff;HC595SendData(Val);)else〃绿色(//P2=0xFF;//Pl=tab[cnta];PO=digittab[cnlb-l8][cnta];Vai=tab[cnta];Vai=Vai+OxFFOO;HC595SendData(Val);)if(++cnta>=8)cnta=O;if(++timecount>=333)(timecount=0;if(++cntb>=36)cntb=O;})**函数名称:HC595SendData**功能描述:向SP1总线发送数据voidHC595SendData(unsignedintSendVai)(unsignedchari;for(i=0;i<16;i+4-)

60if((SendVal«i)&0x8000)MOSIO=1;//setdatalinehigh0X8000最高位与SendVal左移的最高位进行逻辑运算elseMOSIO=0;〃如果为真MOSIO=1S_CLK=0;NOP();NOP();S_CLK=1;R_CLK=0;//setdatalinelowNOP();NOP();R_CLK=1;〃片选00000000标题:学习板的按钮输入控制对应的继电器输出和LED灯连接方法:用8PIN排线将JP5与JP6连接和JP11(P2)与JP1(LED灯)连接1.通过本例程了解74HC165(并入串出)的基本原理和使用2.了解掌握SPI总线接口的工作原理及一般编程方法。3.懂的74HC165在C语言中是如何操作#include#include#defineNOP()_nop_()/*定义空指令*/

61//SPI接口sbitCLK=P3A6;〃串行时钟sbitIN_PL=P1A6;〃把数据加载到锁存器中sbitIN_Dat=PlA7;〃数据通过P1.7脚移进单片机内处理sbitRELAY=P1A4;sbitBEEP=P1A5;unsignedcharbdataKey;sbitK0=KeyA0;〃位定义sbitKl=KeyAl;〃位定义sbitK2=KeyA2;〃位定义sbitK3=KeyA3;〃位定义sbitK4=KeyA4;sbitK5=KeyA5;sbitK6=KeyA6;sbitK7=KeyA7;bitMO,KOJ;〃位定义unsignedlongReHC74165(void);〃函数声名voidbeep();*主函数main()while(l)|unsignedlongInput=ReHC74165();〃调用165驱动程序Key=Input&Oxff;〃将数据传给位变量RELAY=1;P2=Oxff;〃清除〃实现脉冲输入大家仔细体会if(K0&K0J)M0=-M0;K0J=~K0;}〃实现脉冲输入}//Kl为1时开启蜂鸣器和2个灯if(M0){RELAY=0;P2=Ox7f;if(Kl){beepO;P2=Oxbf;if(K2){beep();P2=Oxdf;if(K3){beep();P2=Oxef;if(K4){beepO;P2=Oxf7;

62if(K5){beep();if(K6){beepO;if(K7){beep();P2=Oxfb;}P2=Oxfd;}P2=Oxfe;}*蜂鸣器(让蜂鸣器发出动听声音)voidbeep()(unsignedchari,j;for(i=0;i<2;i++)forQ=0;j<255;j++){_nop_();}BEEP=!BEEP;//BEEP取反)BEEP=1;〃关闭蜂鸣器/*************************此部分为74HC165的驱动程序使用SPI总线连接**函数名称:74HC165**功能描述:**管脚描述:请参考相关的芯片资料(学习光盘中以配)unsignedlongReHC74165(void)(unsignedchari;unsignedintindata;IN_PL=0;NOP();〃短暂延时产生一定宽度的脉冲IN_PI^1;〃将外部信号全部读入锁存器中NOP();indata=0;〃保存数据的变量清0for(i=0;i<8;i++)(indata=indata«1;〃左移一位if(IN_Dat=l)indata=indata+l;〃如果IN_Dat检测到高电平保存数据的变量加1CLK=0;〃时钟置0NOP();CLK=1;〃时钟置1

63return(〜indata);〃将保存数据的变量取反后返回)*****)|yj***********************************1.通过本例程了解并掌握AD-DA转换芯片的基本原理和使用*2.了解掌握12c总线接口的工作原理及一般编程方法。*连接方法:JPIO(P0)与J12用跳线连接*通过改变学习板上的2个电位器对应的2段模拟输入,实现模拟输入,学员观察数码管的数字变化情况*通过改D[4]的值,实现模拟输出,学员观察学习板上DA处LED的亮度变化*注意:I2C驱动程序只能在12T模式下工作6T模式自己修改相应时序*请学员认真消化本例程,懂得AD-DA在C语言中的操作应用#includeinclude#definePCF85910x90//PCF8591地址//elseIOsbitLS138A=P2A2;sbitLS138B=P2A3;sbitLS138C=P2A4;〃此表为LED的字模,共阴数码管0・9-unsignedcharcodeDisp_Tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};unsignedcharAD_CHANNEL;unsignedlongxdataLedOut[8];unsignedintD[32];DAC变换,转化函数bitDACconversion(unsignedcharsla,unsignedcharc,unsignedcharVai)(StartJ2c();〃启动总线SendByte(sla);〃发送器件地址if(ack==0)retum(0);SendByte(c);〃发送控制字节if(ack==0)retum(0);SendByte(Val);〃发送DAC的数值if(ack==0)retum(0);

64〃结束总线Stop_I2c();retum(l);ADC发送字节[命令]数据函数bitISendByte(unsignedcharsla,unsignedcharc)Starl_I2c();SendByte(sla);if(ack==O)retum(O);SendByte(c);if(ack==O)retum(O);Stop_I2c();retum(l);〃启动总线//发送器件地址〃发送数据〃结束总线ADC读字节数据函数unsignedcharIRcvByte(unsignedcharsla){unsignedcharc;Start_I2c();SendByte(sla+l);if(ack==O)return(O);c=RcvByte();Ack_12c⑴;StopJ2c();〃启动总线〃发送器件地址〃读取数据0〃发送非就答位〃结束总线retum(c);main(){chari,j;while(l)(/********|^^卜^^^^*************/switch(AD_CHANNEL)(case0:ISendByte(PCF8591,0x41);D[O]=IRcvByte(PCF8591)*2;//ADCO模数转换1放大2倍显示break;

65case1:ISendByte(PCF8591,0x42);D[1]=IRcvByte(PCF8591)*2;//ADC1模数转换2break;case2:ISendByte(PCF8591,0x43);D[2]=IRcvByte(PCF8591)*2;//ADC2模数转换3break;case3:ISendByte(PCF8591,0x40);D[31=IRcvByte(PCF8591)*2;//ADC3模数转换4break;case4:DACconversion(PCF8591,0x40,D[4]/4);//DAC数模转换break;//D[4]=400;〃数字一》模拟输出D[4]=D[0];//把模拟输入采样的信号通过数模转换输出if(++AD_CHANNEL>4)AD_CHANNEL=0;/********以下将AD的值送到LED数码管显示*************/LedOut[0]=Disp_Tab[D[l]%10000/1000];LedOut[1]=Disp_Tab[D[1]%i000/100];LedOut[2]=Disp_Tab[D[l]%100/10]l0x80;LedOut[3J=Disp_Tab[D[l]%10];LedOut[4J=Disp_Tab[D[OJ%10000/1000J;LedOut[51=Disp_Tab[D[0]%1000/100];LedOutl6]=Disp_Tab[D[0J%100/10JI0x80;LedOut[7]=Disp_Tab[D[0]%10];for(i=0;i<8;i++){P0=LedOutfi];switch(i)〃使用switch语句控制138译码器也可以是用查表的方式学员可以试着自己修改(case0:LS138A=0;LS138B=0;LS138C=0;break;case1:LS138A=1;LS138B=O;LS138C=0;break;case2:LS138A=0;LS138B=1;LS138C=0;break;

66case3:LS138A=1;LS138B=1;LS138C=0;break;case4:LS138A=0;LS138B=O;LS138C=1;break;case5:LS138A=1;LS138B=O;LS138C=1;break;case6:LS138A=0;LS138B=1;LS138C=1;break;〃扫描间隔时间case7:LS138A=1;LS138B=1;LS138C=1;break;for(j=0;j<90;j++){;}}P0=0;/*************************此部分为pc总线的驱动程序#include#include#include#defineNOP()_nop_()/*定义空指令*/#define_Nop()_nop_()/*定义空指令*/sbitSCL=P2A1;//I2C时钟sbitSDA=P2A0;//I2C数据bitack;/*应答标志位*/起动总线函数函数原型:voidStartJ2c();功能:启动12c总线,即发送12C起始条件.voidStart_I2c(){SDA=1;/*发送起始条件的数据信号*/_Nop();SCL=1;_Nop();/*起始条件建立时间大于4.7us,延时*/_Nop();

67_Nop();,Nop();_Nop();SDA=0;_Nop();_Nop();_Nop();_Nop();_Nop();SCL=O;_Nop();,Nop();/*发送起始信号*//*起始条件锁定时间大于41*s*//*钳住12c总线,准备发送或接收数据*/结束总线函数函数原型:voidStopJ2c();功能:结束12c总线,即发送12c结束条件.voidStop_I2c()SDA=O;_Nop();SCL=1;_Nop();_Nop();_Nop();_Nop();_Nop();SDA=1;_Nop();_Nop();_Nop();_Nop();/*发送结束条件的数据信号*//*发送结束条件的时钟信号*//*结束条件建立时间大于4us*//*发送12c总线结束信号*/字节数据发送函数函数原型:voidSendByte(UCHARc);功能:将数据c发送出去,可以是地址,也可以是数据,发完后等待应答,并对此状态位进行操作.(不应答或非应答都使ack=O)发送数据正常,ack=l;ack=O表示被控器无应答或损坏。voidSendByte(unsignedcharc)

68unsignedcharBitCnt;for(BitCnt=0;BitCnt<8;BitCnt++)/*要传送的数据长度为8位*/(if((c«BitCnt)&0x80)SDA=l;/*判断发送位*/elseSDA=O;_Nop();SCL=1;/*置时钟线为高,通知被控器开始接收数据位*/_Nop();_Nop();/*保证时钟高电平周期大于4us*/_Nop();_Nop();_Nop();SCL=0;_Nop();_Nop();SDA=1;/*8位发送完后释放数据线,准备接收应答位*/_Nop();_Nop();SCL=1;_Nop();_Nop();_Nop();if(SDA==l)ack=O;elseack=l;/*判断是否接收到应答信号*/SCL=0;_Nop();_Nop();字节数据接收函数函数原型:UCHARRcvByteO;功能:用来接收从器件传来的数据,并判断总线错误(不发应答信号),发完后请用应答函数应答从机。unsignedcharRcvByte()(unsignedcharretc;unsignedcharBitCnt;

69retc=O;SDA=1;/*置数据线为输入方式*/for(BitCnt=O;BitCnt<8;BitCnt++)(_Nop();SCL=O;/*置时钟线为低,准备接收数据位*/_Nop();_Nop();/*时钟低电平周期大于4.7us*/_Nop();_Nop();_Nop();SCL=1;/*置时钟线为高使数据线上数据有效*/_Nop();,Nop();retc=retc«1;if(SDA==1)retc=retc+1;/*读数据位,接收的数据位放入retc中*/_Nop();_Nop();)SCL=O;_Nop();_Nop();retum(retc);应答子函数函数原型:voidAck」2c(bita);功能:主控器进行应答信号(可以是应答或非应答信号,山位参数a决定)voidAck_I2c(bita)if(a==0)SDA=0;elseSDA=1;_Nop();_Nop();_Nop();SCL=1;_Nop();_Nop();_Nop();_Nop();_Nop();/*在此发出应答或非应答信号*//*时钟低电平周期大于4us*/

70SCL=O;_Nop();_Nop();/*清时钟线,钳住12c总线以便继续接收*/AINOA()iir•ALErtOXjAIN0,AIN1,:AIN2(AIN3〃包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#includesbitSCK=P3A6;〃时钟sbitSDA=P3A4;〃数据sbitRST=P3A5;//DS1302复位sbitLS138A=P2A2;sbitLS138B=P2A3;sbitLS138C=P2A4;bitReadRTC_Flag;〃定义读DS1302标志unsignedcharl」mpdate[7]={0,0,12,15,5,3,8};〃秒分时11月周年08-05-1512:00:00unsignedcharl_tmpdisplay[8J;codeunsignedcharwrite_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};〃秒分时日月周年最低位读写位codeunsignedcharread_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};

71codeunsignedchartable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};〃共阴数码管0-9',熄灭'表函数声明列voidWrite_Ds1302_byte(unsignedchartemp);voidWrite_Ds1302(unsignedcharaddress,unsignedchardat);unsignedcharRead_Ds1302(unsignedcharaddress);voidRead_RTC(void)y/readRTCvoidSet_RTC(void);//setRTCvoidInitTIMER0(void)y/initaltimerOvoidmain(void)(InitTIMEROO;Set_RTC();入,此程序应该屏蔽主函数〃初始化定时器0〃写入时钟值,如果使用备用电池时候,不需要没每次上电写while(l)(if(ReadRTC_Flag)(ReadRTC_Flag=O;Read_RTC();〃数据的转换,因我们采用数码管〃加入l_tmpdisplay[0]=l_tmpdate[2]/16;0~9的显示,将数据分开l_tmpdisplay[1]=l_tmpdate[2]&0x0f;l_tmpdisplay[2]=10;l_tmpdisplay[3]=l_tmpdate[l]/16;l_tmpdisplay[4]=l_tmpdate[1]&OxOf;l_tmpdisplay[5]=10;l_tmpdisplay[6]=Ltmpdate[0]/16;l_tmpdisplay[7]=l_tmpdate[0]&0x0f;

72定时器0初始化voidInitTIMERO(void)(TMODI=0x01;〃定时器设置16位TH0=0xef;〃初始化值TL0=0xf0;ETO=1;TRO=1;EA=1;写一个字节voidWrite_Ds1302_Byte(unsignedchartemp)unsignedchari;for(i=0;i<8;i++)〃循环8次写入数据SCK=0;SDA=temp&OxO1;〃每次传输低字节temp»=1;SCK=1;)}〃右移一位写入DS1302*/voidWrite_Ds1302(unsignedcharaddress,unsignedchardat)RST=0;_nop_();SCK=0;_nop_();RST=1;_nopj);〃启动Write_Ds1302_Byte(address);〃发送地址Write_Ds1302_Byte(dat);〃发送数据RST=0;〃恢复

73*/读出DS1302数据unsignedcharRead_Dsl302(unsignedcharaddress)(unsignedchari,temp=OxOO;RST=O;_nop_();_nop_();SCK=O;_nop_();_nop_();RST=1;_nop_();_nop_();Write_Dsl302_Byte(address);for(i=0;i<8;i++)//循环8次读取数据(if(SDA)templ=0x80;〃每次传输低字节SCK=O;temp»=l;〃右移一位_nop_();_nop_();_nop_();SCK=1;)RST=0;_nop_();〃以下为DS13O2复位的稳定时间_nop_();RST=O;SCK=0;_nop_();_nop_();_nop_();_nop_();SCK=1;_nop_();_nop_();SDA=0;_nop_();_nop_();SDA=1;_nop_();

74_nop_();return(temp);〃返回*/voidRead_RTC(void)〃读取日历unsignedchari,*p;p=read_rtc_address;for(i=0;i<7;i++)〃地址传递〃分7次读取秒分时日月周年l_tmpdate[i]=Read_Ds1302(*p);P++;设定时钟数据voidSet_RTC(void)〃设定日历读时钟数据unsignedchari,*p,tmp;for(i=0;i<7;i++){//BCD处理tmp=l_tmpdate[i]/10;l_tmpdate[i]=l_tmpdate[i]%10;l_tmpdateli]=l_tmpdate[i]+tmp*16;)Write_Ds1302(0x8E,0X00);p=write_rtc_address;〃传地址for(i=0;i<7;i++)//7次写入秒分时日月周年{Write_Ds1302(*p,l_tmpdate[i]);P++;*/Write_Ds1302(0x8E,0x80);定时器中断函数voidtim(void)interrupt1using1//中断,用于数码管扫描staticunsignedchari,num;TH0=0xf5;TLO=OxeO;

75PO=tablell_tmpdisplayliJ];〃查表法得到要显示数字的数码段switch(i){case0:LS138A=0;LS138B=O;LS138C=O;break;case1:LS138A=1;LS138B=0;LS138C=0;break;case2:LS138A=0;LS138B=1;LS138C=0;break;case3:LS138A=1;LS138B=1;LS138C=0;break;case4:LS138A=0;LS138B=O;LS138C=1;break;case5:LS138A=1;LS138B=O;LS138C=1;break;case6:LS138A=0;LS138B=1;LSI38C=1;break;case7:LS138A=1;LS138B=1;LS138C=1;break;i++;if(i==8)(i=0;num++;if(10==num)〃隔段时间读取1302的数据。时间间隔可以调整(ReadRTC_Flag=l;//使用标志位判断num=0;))短路帽

76/*************************此部分为74HC595的驱动程序使用SPI总线连接#include#include#defineNOP()_nop_()/*定义空指令*/#define_Nop()_nop_()/*定义空指令*/voidHC595SendData(unsignedintSendVal);//SPIIOsbitMOSIO=P1A5;sbitR_CLK=P1A6;把数据加载到锁存器中数据移出sbitS_CLK=P1A7;sbitIN_PL=P3A4;//74HC165shiftloadsbitIN_Dat=P3A5;//74HC165outputsbitOE=P3A6;**函数名称:HC595SendData功能描述:向SPI总线发送数据voidHC595SendData(unsignedintSendVal)unsignedchari;for(i=0;i<16;i++)if((SendVal«i)&0x8000)MOSIO=1;//setdata!inehighelseMOSIO=0;S_CLK=O;NOP();NOP();S_CLK=1;R_CLK=0;//setdatalinelow

77NOP();NOP();R_CLK=1;〃片选OE=0;*标题:试验数码管上显示温度***1.通过本例程了解DLASS18b20的基本原理和使用,理解并掌握18B20驱动程序的编写**2.了解掌握12c总线接口的工作原理及一般编程方法。**插上18B2O观察数码管的实际温度显示*用排线将JP10(P0口)与J12连接在数码管上可以看温度显示*注意:18B20程序只能在12T模式下工作6T模式自己修改相应时序*请学员认真消化本例程,懂DLASS18b20在C语言中的操作*#includeexternGetTempO;externunsignedintidataTemperature;voiddelay(unsignedinti);〃声明引用外部函数//声明引用外部变量//else10sbitLS138A=P2A2;sbitLS138B=P2八3;sbitLS138C=P2A4;〃管脚定义〃此表为LED的字模,共阴数码管0-9-unsignedcharcodeDisp_Tab[]={Ox3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};unsignedlongLedOut[5],LedNumVal;

78voidsystem_Ini()(TMODI=Oxll;TH1=0xD8;//10TL1=0xF0;IE=0x8A;TRI=1;main(){unsignedchari;system_Ini();while(l)(GetTempO;/********以下将读18b20的数据送到LED数码管显示*************/LedNumVal=Temperature;〃把实际温度送到LedNumVal变量中LedOut[0]=Disp_Tab[LedNumVal%10000/1000];LedOut[l]=Disp_Tab[LedNumVal%1000/100];LedOut[2]=Disp_Tab[LedNumVal%100/10];〃十位LedOut[3]=Disp_Tab[LedNumVal%10];〃个位for(i=0;i<4;i++)(PO=LedOut[i];switch(i){//138译码case0:LS138A=0;LS138B=O;LS138C=0;break;case1:LS138A=1;LS138B=0;LS138C=0;break;case2:LS138A=0;LS138B=1;LS138C=0;break;case3:LS138A=1;LS138B=1;LS138C=0;break;delay(lOO);)P0=0;〃延时程序voiddelay(unsignedinti)(

79charj;for(i;i>0;i-)for(j=200;j>0;j-);}/*************************此部分为]8B20的驱动程序#include#includesbitD18B20=P3A7;#defineNOP()_nop_()/*定义空指令*/#define_Nop()_nop_()/*定义空指令*/voidTempDelay(unsignedcharidataus);voidInitl8b20(void);voidWriteByte(unsignedcharidatawr);〃单字节写入voidread_bytes(unsignedcharidataj);unsignedcharCRC(unsignedcharj);voidGemTemp(void);voidConfig18b20(void);voidReadID(void);voidTemperatuerResult(void);bitflag;unsignedintidataTemperature;unsignedcharidatalemp_buff[9];〃存储读取的字节,readscratchpad为9字节,readromID为8字节unsignedcharidataid_buff[8];unsignedcharidata*p,TIM;unsignedcharidatacrc_data;unsignedcharcodeCrcTable[256]={0,94,188,226,97,63,221,131,194,156,126,32,163,253,31,65,157,195,33,127,252,162,64,30,95,1,227,189,62,96,130,220,35,125,159,193,66,28,254,160,225,191,93,3,128,222,60,98,190,224,2,92,223,129,99,61,124,34,192,158,29,67,161,255,70,24,250,164,39,121,155,197,132,218,56,102,229,187,89,7,219,133,103,57,186,228,6,88,25,71,165,251,120,38,196,154,101,59,217,135,4,90,184,230,167,249,27,69,198,152,122,36,248,166,68,26,153,199,37,123,58,100,134,216,91,5,231,185,140,210,48,110,237,179,81,15,78,16,242,172,47,113,147,205,

8017,79,173,243,112,46,204,146,211,141,111,49,178,236,14,80,175,241,19,77,206,144,114,44,109,51,209,143,12,82,176,238,50,108,142,208,83,13,239,177,240,174,76,18,145,207,45,115,202,148,118,40,171,245,23,73,8,86,180,234,105,55,213,139,87,9,235,181,54,104,138,212,149,203,41,119,244,170,72,22,233,183,85,11,136,214,52,106,43,117,151,201,74,20,246,168,116,42,200,150,21,75,169,247,182,232,10,84,215,137,107,53);//*Function:延时处理*parameter:*Retum:★Modify:voidTempDelay(unsignedcharidataus)while(us—);*Function:18B20初始化*parameter:"Return:"Modify:voidInitl8b20(void)D18B20=l;_nop_();D18B20=0;TempDelay(80);//delay530uS//80_nop_();D18B20=l;TempDelay。4);//delay100uS//14_nop_();_nop_();_nop_();if(D18B2O==O)flag=1;//detect1820success!elseflag=0;//detect1820fail!TempDelay(20);//20_nop_();_nop_();

81D18B20=1;"Function:向18B20写入•个字节*parameter:*Retum:*Modify:voidWriteByte(unsignedcharidatawr)〃单字节写入unsignedcharidatai;for(i=0;i<8;i++)D18B20=0;_nop_();D18B20=wr&0x01;TempDelay⑶;//delay45uS//5_nop_();_nop_();D18B20=I;wr»=1;}}*Funclion:读18B20的一个字节*parameter:*Retum:*Modify:unsignedcharReadByte(void)〃读取单字节unsignedcharidatai,u=O;for(i=0;i<8;i++)D18B20=0;u»=1;D18B20=1;if(D18B20==l)u1=0x80;TempDelay(2);_nop_();)retum(u);"Function:读18B20*parameter:*Retum:*Modify:

82voidread_bytes(unsignedcharidataj)(unsignedcharidatai;for(i=0;i

83"Function:读取温度*parameter:*Retum:"Modify:voidGemTemp(void)read_bytes(9);if(CRC(9)=0)〃校验正确Temperature=temp_buff[1]*0x100+temp_buff[O];//Temperature*=0.625;Temperature/=16;TempDelay(l);"Function:内部配置parameter:Return:Modify:voidConfig18b20(void)〃币:新配置报警限定值和分辨率Initl8b20();WriteByte(Oxcc);WriteByte(0x4e);WriteByte(0xl9);WriteByte(Oxla);WriteByte(0x7f);Initl8b20();WriteByte(Oxcc);WriteByte(0x48);Initl8b20();WriteByte(Oxcc);WriteByte(0xb8);//skiprom//writescratchpad〃上限〃下限//set12bit(0.125)//skiprom〃保存设定值//skiprom〃回调设定值♦Function:^18B20ID*parameter:"Return:

84♦Modify:voidReadID(void)〃读取器件idInitl8b2O();WriteByte(0x33);//readromread_bytes(8);*Function:18B20ID全处理*parameter:*Retum:*Modify:voidTemperatuerResult(void)p=id_buff;ReadIDO;Config18b20();Initl8b2O();WriteByte(Oxcc);WriteByte(0x44);//skiprom//TemperatureconvertInitl8b2O();WriteByte(Oxcc);WriteByte(Oxbe);p=temp_buff;GemTempO;//skiprom//readTemperaturevoidGetTempOif(TIM==100)〃每隔1000ms读取温度{TIM=0;TemperatuerResult();}[tl(10ms)中断]中断

85voidTlzd(void)interrupt3(TH1=0xD8;//10TL1=OxFO;TIM++;PR3一^上拉也迎A103JVCCDS18B20/*************************!|•匕部分为AT2402的驱动程序使用I2C总线连接#include//AT2402的功能函数向有子地址器件发送多字节数据函数函数原型:bitISendStr(UCHARsla,UCHARsuba,ucahr*s,UCHARno);功能:从启动总线到发送地址,子地址,数据,结束总线的全过程,从器件地址sla,子地址suba,发送内容是s指向的内容,发送no个字节。如果返回1表示操作成功,否则操作有误。注意:使用前必须已结束总线。bitISendStr(unsignedcharsla,unsignedcharsuba,unsignedchar*s,unsignedcharno)(unsignedchari;Start_I2c();SendByte(sla);if(ack==0)retum(0);SendByte(suba);if(ack==0)retum(0);/*启动总线*//*发送器件地址*//*发送器件子地址*/for(i=0;i

86SendByte(*s);if(ack==O)return(O);s++;/*发送数据*/Slop_I2c();/*结束总线*/retum(l);向有子地址器件读取多字节数据函数函数原型:bitRecndStr(UCHARsla.UCHARsuba,ucahr*s,UCHARno);功能:从启动总线到发送地址,子地址,读数据,结束总线的全过程,从器件地址sla,子地址suba,读出的内容放入s指向的存储区,读no个字节。如果返回1表示操作成功,否则操作有误。注意:使用前必须已结束总线。bitIRcvStr(unsignedcharsla,unsignedcharsuba,unsignedchar*s,unsignedcharno)unsignedchari;Start」2c();SendByte(sla);if(ack==O)return(O);SendByte(suba);if(ack==O)return(O);六启动总线*//*发送器件地址*//*发送器件子地址*/StartJ2c();SendByte(sla+I);if(ack==O)return(O);fdr(i=0;i

87标题:EEPROM读写试验(按钮输入)率*此例程学员更进一步地熟悉并掌握(存储芯片AT2402)的操作,有保存和读取操作了解掌握12c总线接口的工作原理及一般编程方法。跳线操作:PO(JP10)连数码管(JP12)?■Pl(JP8)连独立按键(JP5)率具体操作:第1个按钮保存第2个按钮读取*第3个按钮减减第4个按钮加数**希望学员认真体会消化此例程,试着用别的资源来操作EEPROM!*/#include#include#defineAT24C02OxaO//AT24C02地址//elseIOsbitLS138A=P2A2;〃译码器端sbitLS138B=P2A3;sbitLS138C=P2A4;sbitKI=P1AO;〃保存sbitK2=P1A1;//读取sbitK3=P1A2;//+数据sbitK4=P1A3;〃-数据

88voiddelay(unsignedinti);〃此表为LED的字模,共阴数码管0-9-unsignedcharcodeDisp_Tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};unsignedcharCount1;unsignedlongD[17],LedOut[5],LedNumVal;unsignedintidataUSEC;voidsystem_ini()(TMODI=0x11;TH1=0xfe;//11.0592TL1=0x33;TRI=1;IE=0x8A;main(){unsignedchari;unsignedcharpDat网;system_ini();while(l)(//=============IIC读取if(K2==0)〃第二个按钮读取数据(IRcvStr(AT24C02,0,&pDat[OJ,8);for(i=0;i<4;i++)(D[14+i]=pDat[i*24-0]+pDat[i*2+l]*0xl00;Count1=D[14];))//:HC保存if(Kl==0)〃第一个按钮保存数据D[14]=Count1;for(i=0;i<4;i++)(pDat[i*2+0]=D[14+i];pDat[i*2+l]=D[14+i]»8;}ISendStr(AT24C02,0,&pDat[0],8);D[14]=0;

89)/********以下将2402中保存的数据送到LED数码管显示*************/LedNumVal=Count1;LedOut[0]=Disp_Tab[LedNumVal%10000/1000];LedOutf1]=Disp_Tab[LedNumVal%1000/100];LedOut[2]=Disp_Tab[LedNumVal%100/10]l0x80;LedOut[3]=Disp_Tab[LedNumVal%10];for(i=0;i<4;i++)P0=LedOut[i];switch(i)(case0:LS138A=0;LS138B=0;LS138C=0;break;case1:LS138A=1;LS138B=O;LS138C=0;break;case2:LS138A=0;LS138B=1;LS138C=O;break;case3:LS138A=1;LS138B=1;LS138C=O;break;case4:LS138A=0;LS138B=O;LS138C=I;break;)delay(lOO);//LS138A=1;LS138B=1;LS138C=1;voiddelay(unsignedinti)charj;for(i;i>0;i-)for(j=200;j>0;j..);[tl(0.5ms)中断]中断中做PWM输出1000/(0.02ms*250)=200Hz

90voidTlzd(void)interrupts//3为定时器1的中断号1定时器0的中断号0外部中断12外部中断24串口中断TH1=0xfe;//12MTL1=0x33;if(USEC++=200){USEC=0;if(!K3)Count1++;〃改变数据if(!K4&Countl!=0)Count1-;/*************************此部分为[2C总线的驱动程序#include#include#include//#defineNOP()_nop_()/*定义空指令*/#define_Nop()_nop_()/*定义空指令*/sbitSCL=P2A1;//I2C时钟sbitSDA=P2A0;//I2C数据bitack;/*应答标志位*/起动总线函数

91函数原型:voidStartJ2c();功能:启动12c总线,即发送12c起始条件.voidStart_I2c()(SDA=1;/*发送起始条件的数据信号*/_Nop();SCL=1;/*起始条件建立时间大于4.7us,延时*//*发送起始信号*/_Nop();_Nop();_Nop();,Nop();_Nop();SDA=0;Nop();/*起始条件锁定时间大于4us*/_Nop();_Nop();_Nop();_Nop();SCL=0;/*钳住12c总线,准备发送或接收数据*/_Nop();_Nop();结束总线函数函数原型:voidStop_I2c();功能:结束12c总线,即发送12C结束条件.voidStop_I2c()(SDA=0;/*发送结束条件的数据信号*/_Nop();/*发送结束条件的时钟信号*/SCL=1;/*结束条件建立时间大于4口s*/_Nop();_Nop();_Nop();_Nop();_Nop();SDA=1;/*发送12c总线结束信号*/_Nop();_Nop();_Nop();_Nop();

92字节数据发送函数函数原型:voidSendByte(UCHARc);功能:将数据c发送出去,可以是地址,也可以是数据,发完后等待应答,并对此状态位进行操作.(不应答或非应答都使ack=0)发送数据正常,ack=l;ack=0表示被控器无应答或损坏。voidSendByte(unsignedcharc)(unsignedcharBitCnt;for(BitCnt=0;BitCnt<8;BitCnt++)/*要传送的数据长度为8位*/Iif((c«BitCnt)&0x80)SDA=1;/*判断发送位*/elseSDA=O;_Nop();SCL=1;/*置时钟线为高,通知被控器开始接收数据位*/_Nop();_Nop();/*保证时钟高电平周期大于4us*/_Nop();_Nop();_Nop();SCL=0;)_Nop();_Nop();SDA=1;/*8位发送完后释放数据线,准备接收应答位*/_Nop();_Nop();SCL=1;_Nop();_Nop();_Nop();if(SDA==l)ack=O;elseack=l;/*判断是否接收到应答信号*/SCL=0;_Nop();_Nop();

93字节数据接收函数函数原型:UCHARRcvByteO;功能:用来接收从器件传来的数据,并判断总线错误(不发应答信号),发完后请用应答函数应答从机。unsignedcharRcvByte()(unsignedcharretc;unsignedcharBitCnt;retc=O;SDA=1;/*置数据线为输入方式*/for(BitCnt=0;BitCnt<8;BitCnt++)[_Nop();SCL=0;/*置时钟线为低,准备接收数据位*/_Nop();_Nop();/*时钟低电平周期大于4.7us*/_Nop();_Nop();_Nop();SCL=1;/*置时钟线为高使数据线上数据有效*/_Nop();,Nop();retc=retc«l;if(SDA==1)retc=retc+1;/*读数据位,接收的数据位放入retc中*/_Nop();,NopO;)SCL=O;_Nop();_Nop();retum(retc);应答子函数函数原型:voidAck_I2c(bita);功能:主控器进行应答信号(可以是应答或非应答信号,山位参数a决定)voidAck_I2c(bita)if(a==O)SDA=O;/*在此发出应答或非应答信号*/

94elseSDA=1;_Nop();_Nop();_Nop();SCL=1;_Nop();_Nop();_Nop();_Nop();_Nop();SCL=0;,Nop();_Nop();/*时钟低电平周期大于4us*//*清时钟线,钳住12c总线以便继续接收*/>EAXpp»ALEPROG0PSEN1P2.7302S-LCDE27RD26WR25~24P2323~P22-22SPA21~SCL-JP1112345678ccccccca*描述:LCD1602滚动显示显示方式:1>从左到右逐字显示,闪动二次,清屏。2、再从右到左逐字显示,闪动二次,清屏。3、周期性地重复上述显示方式。#include//51寄存器文件#includetypedefunsignedcharBYTE;〃用BYTE代替unsignedchartypedefbitBOOL;//用BOOL代替bitsbitLCD_RS=P2A6;〃复位端

95sbitLCD_RW=P2八5;〃写数据端sbitLCD_EP=P2A7;〃使能端BYTEcodedisl[]={*'WELCOMETO”};BYTEcodedis2[]={”WWW.PRECHIN.COM"};BYTEcodedis3[]={”OTEMOCLEW”};BYTEcodedis4[]={“MOC.NIHCERP.WWW”};delay(intms){〃延时子程序inti;while(ms-)[for(i=0;i<250;i++)(_nop_();〃空执行_nop_();_nop_();_nop_();)))/****************侧,忙函数************************/BOOLlcd_bz()BOOLresult;LCD_RS=0;LCD_RW=1;LCD_EP=1;_nop_();_nop_();_nop_();_nop_();result=(BOOL)(PO&0x80);〃检测P0最高位是否为1LCD_EP=0;returnresult;〃返[可侧忙结果/****************写命令函数************************/lcd_wcmd(BYTEcmd)

96//写入指令数据到LCDwhile(lcd_bz());LCD_RS=0;LCD_RW=0;LCD_EP=0;_nop_();_nop_();P0=cmd;//将8位指令通过PO口传给1602_nop_();_nop_();〃用于产生一个脉冲宽度_nop_();_nop_();LCD_EP=1;_nop_();_nop_();_nop_();_nop_();LCD_EP=0;〃设定显示位置lcd_pos(BYTEpos)(lcd_wcmd(posI0x80);)/****************写数据函数************************/lcd_wdat(BYTEdat){〃写入字符显示数据到LCDwhile(lcd_bz());LCD_RS=1;LCD_RW=0;LCD_EP=0;PO=dat;〃将8位数据通过PO口传给1602_nop_();_nop_();〃用于产生一个脉冲宽度_nop_();_nop_();LCD_EP=1;_nop_();_nop_();_nop_();_nop_();LCD_EP=0;)

97IcdJnitOlcd_wcmd(0x38);delay(l);lcd_wcmd(0x0c);delay(l);lcd_wcmd(0x06);delay(l);lcd_wcmd(0x01);delay(l);)//LCD初始化设定〃16*2显示,5*7点阵,8位数据〃显示开,关光标"移动光标〃清除LCD的显示内容/****************主函数************************/main()BYTEi;lcd_init();//初始化LCDdelay(lO);while(l)lcd_wcmd(0x06);〃向右移动光标lcd_pos(0);//设置显示位置为第一行的第1个字符i=0;while(disl[i]!=W){〃显示字符"WLCOMETO”lcd_wdat(dis1[iJ);i++;delay(30);〃控制两字之间显示速度)lcd_pos(0x40);〃设置显示位置为第二行第1个字符i=0;while(dis2[iJ!=MT)(lcd_wdat(dis2[i]);〃显示字符”WWW.PRECHIN.COMi++;delay(30);〃控制两字之间显示速度)delay(800);〃控制停留时间lcd_wcmd(0x01);〃清除LCD的显示内容delay(l);lcd_wcmd(0x04);〃向左移动光标lcd_pos(15);〃设置显示位置为第行的第16个字符i=0;while(dis3[i]!=W){〃显示字符"WLCOMETO”

98lcd_wdat(dis3[i]);i++;delay(3O);〃控制两字之间显示速度)lcd_pos(0x4F);〃设置显示位置为第二行的第16个字符i=0;while(dis4[i]!=W)(lcd_wdat(dis4[i]);〃显小字符”WWW.PRECHIN.COM"i++;delay(30);〃控制两字之间显示速度)delay(800);//控制停留时间lcd_wcmd(OxO1);〃清除LCD的显示内容delay(200);//控制两屏转换时间))*描述:*1602字符型LCD显示演示程序*在第一行显示welcome*在第二行显示www.PRECHIN.com*******************************************************************************#include//51寄存器文件#includetypedefunsignedcharBYTE;〃用BYTE代替unsignedchartypedefunsignedintWORD;//WORD代替unsignedinttypedefbitBOOL;〃用BOOL代替bitsbitrs=P2八6;〃复位端sbitrw=P2A5;〃写数据端sbit叩=P2A7;〃使能端BYTEcodedisl[]={"welcome!"};BYTEcodedis2[]={“www.PRECHlN.com”};delay(BYTEms)BYTEi;while(ms-)(for(i=0;i<250;i++){_nop_();_nop_();//空执行消耗CPU时间达到等待的效果_nop_();_nop_();

99/*******************lcd侧,"函数********************/BOOLlcd_bz()(BOOLresult;rs=O;rw=1;ep=1;_nop_();_nop_();_nop_();_nop_();result=(BOOL)(PO&0x80);〃检测P0最高位是否为1ep=0;returnresult;〃返回侧忙结果lcd_wcmd(BYTEcmd){//写入指令数据到LCDwhile(lcd_bz());rs=0;rw=0;ep=0;_nop_();_nop_();P0=cmd;〃将8位指令通过P0口传给1602_nop_();_nop_();〃用于产生一个脉冲宽度

100_nop_();_nop_();ep=1;_nop_0;_nop_();_nop_();_nop_();ep=0;//设定显示位置lcd_pos(BYTEpos)(lcd_wcmd(posI0x80);lcd_wdat(BYTEdat){〃写入字符显示数据到LCDwhile(lcd_bz());rs=1;rw=0;ep=0;P0=dat;//将8位数据通过P0口传给1602_nop_();_nop_();_nop_();〃用于产生一个脉冲宽度一nop_();ep=1;_nop_();_nop_();_nop_();_nop_();lcd_init()(lcd_wcmd(0x38);delay(l);lcd_wcmd(0x0c);delay(l);lcd_wcmd(0x06);delay(l);lcd_wcmd(0x01);delay(l);ep=0;//LCD初始化设定〃//16*2显示,5*7点阵,8位数据〃显示开,关光标〃移动光标〃清除LCD的显示内容

101main()(BYTEi;lcd_init();delay(lO);lcd_pos(4);i=0;while(disl[i]!=W)(lcd_wdat(disl[i]);i++;)〃初始化LCD〃设置显示位置为第一行的第5个字符〃显示字符"welcome!"Icd_pos(0x41);〃设置显示位置为第二行第二个字符i=0;while(dis2[i]!=W)lcd_wdat(dis2[i]);i++;)〃显示字符"www,PRECHIN.com"while(l);//1602液晶6位计数器:本程序调试通过硬件:插上1602液晶*〃液晶屏读操作定义〃液晶屏写操作定义RSEQUP2.6RWEQUP2.5EEQUP2.7org00ha_bitequ30hb_bitequ31hc_bilequ32hd_bitequ33he_bitequ34hf_b\tequ35hmova,#30h**;个位数存放处;十位数存放处;百位数存放处;千位数存放处;万位数存放处;十万位数存放处;30H的ASCIC码为0mova_bit,amovb_bit,amovc_bit,amovd_bit,a

102move_bit,amov匚bit,aorg0030hMOVPO,#0000000IB;清屏ACALLENABLEMOVP0,#00111000B;显示功能ACALLENABLEMOVP0,#00001100B;显示开关控制ACALLENABLEMOVP0,#06H输入模式ACALLENABLEMOVpO,#8OH;第一行的开始位置CALLENABLEMOVP0,#0000000IB;清屏ACALLENABLEMOVDPTR,#TABLE1;显示PASSWORDCALLWRITE1;至1」TABLEI取码?stlop:acalldisplay;显示inca_bit;力口1mova,a_bitcjnea,#3AH,stlop;如果A的值到3AH那么继续否则市新mova_bit,#30h;显示数送30H(就是显示0)incb_bitmova,b_bitcjnea,#3AH,stlopmovb_bit,#30hincc_bitmova,c_bitcjnea,#3AH,stlopmovc_bit,#30hincd_bitmova,d_bitcjnea,#3AH,stlopmovd_bit,#30hince_bitmova,e_bitcjnea,#3AH,stlopmove_bit,#30hincf_bitmova,Cbit

103cjnea,#3AH,stlopmovf_bit,#30hajmpstlopdisplay:;显示子程序MOVP0,#0C9HACALLENABLEMOVA,A_BITACALLWRILTE2MOVP0,#0C8HACALLENABLEMOVA,B_BITACALLWRILTE2MOVP0,#0C7HACALLENABLEMOVA,C_BITACALLWRILTE2MOVP0,#0C6HACALLENABLEMOVA,D_BITACALLWRILTE2MOVP0,#0C5HACALLENABLEMOVA,E_BITACALLWRILTE2MOVP0,#0C4HACALLENABLEMOVA,F_BIT;显示的位置;显示10位数;显示;数据存贮器地址;数据存贮器地址;数据存贮器地址;数据存贮器地址ACALLWRILTE2;1MS延时(按12MHZ算)DIMS:MOVR7,#2DJNZR7,$RETWRILTE2:MOVpO,A;显示SETBRSCLRRWCLRECALLDELAYCALLDELAY3SETBERETWRITE1:MOVRl,#00H;显示table中的值A1:MOVA,R1;到table取码MOVCA,@A+DPTRcallWRILTE2;显示到led

104INCRICJNEA,#OOH,A1;是否到OOhRETENABLE:CLRRS;送命令CLRRWCLREACALLDELAYSETBERETDELAY:MOVP0,#0FFHCLRRSSETBRWCLRENOPSETBEJBP0.7,DELAY;判断忙标志RETDELAY3:;延时程序MOVR7,#255D1:MOVR6,#25D2:DJNZR6,D2DJNZR7,D1RETtablel:db"Counter",00Hend,***1602液晶显不p2.6读操作p2.5写操作p2.7使能控制端,.****率****.************字***.*****索****,***********字************,*****RSEQUp2.6;液晶接口的定义RWEQUp2.5EEQUp2.7orgOOOOhMOVSP,#70H;设置堆栈

105MOVp0,#01H;清屏CALLENABLEMOVpO,#38H;显示功能CALLENABLEMOVpO,#OFH;显示开关控制CALLENABLEMOVpO,#O6H;+lCALLENABLEMOVp0,#80H;第一行的开始位置CALLENABLEMOVDPTR,#TABLE1;显示CALLWRITE1;至ljTABLEI取码?MOVpO,#OCOH;第二行的位置CALLENABLEMOVDPTR,#TABLE2;显示CALLWRITE14l]TABLE2取码;MOVpO,#OC2H;光标定位OCOHCALLENABLEJMP$ENABLE:CLRRS;送命令CLRRWCLRECALLDELAYSETBERETWRITE1:MOVRl,#00H;显示table中的值Al:MOVA,Rl在lJtable取码MOVCA,@A+DPTRcallwRITE2;显示到ledINCRICJNEA,#OOH,A1;是否到OOhRETWRITE2:MOVpO,A;显示SETBRSCLRRWCLRECALLDELAYSETBERETDELAY:MOVR4,#05D1:MOVR5,#0FFHDJNZR5,$DJNZR4,D1RETtablel:dbuSHENZHENPUZHONGH,00Htable2:dbuGTECHNOLOGY”,00Hend

106*描述:LCD1602移动显示*显示方式:**1、LCD第一行显示:BIGBIGWORLD*LCD第二行显示:ILOVEPUZHONG*2、先将两行字符写入DDRAM中,然后从右至左逐字移出显示。*3、在指定位置停留1.6秒后,闪烁2次,清屏。**4、反复循环上述显示方式。*#include#include#defineucharunsignedchar#defineuintunsignedintsbitLCD_RS=P2A6;sbitLCD_RW=P2A5;sbitLCD_EN=P2A7;ucharcodedisl[]={"BIGBIGWORLD"};ucharcodedis2[]={nILOVEPUZHONG'1);/*延时子程序voiddelay(intms)inti;while(ms—)for(i=0;i<250;i++)_nop.();_nop_();_nop_();_nop__();俨/*检查LCD忙状态/*lcd_busy为1时,忙,等待。Icd-busy为0时,闲,可写指令与数据。/*bitlcd_busy()(bitresult;

107LCD_RS=0;LCD_RW=1;LCD_EN=1;_nop_();_nop_();_nopj);_nop_();result=(bit)(P0&0x80);LCD_EN=0;returnresult;/*写指令数据到LCD/*RS=L,RW=L,氏高脉冲,口04)7=指令码。/*voidlcd_wcmd(ucharcmd)(while(lcd_busy());LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_();P0=cmd;_nop.();一nop_();_nop_();_nop_();LCD_EN=1;_nop_();_nop_();_nop_();_nop_();LCD_EN=0;/*/*写显示数据到LCD/*RS=H,RW=L,£=高脉冲,D0-D7=数据。/*voidlcd_wdat(uchardat)

108(while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;_nop_();_nop_();_nop_();_nop_();LCD_EN=1;_nop_();_nop_();_nop_();_nop_();LCD_EN=0;/*设定显示位置/*

109Icd_wcmd(posl0x80);〃数据指针=80+地址变量/*/*LCD初始化设定俨voidlcd_init()(delay(15);lcd_wcmd(0x38);delay(5);lcd_wcmd(0x38);delay(5);lcd_wcmd(0x38);delay⑸;lcd_wcmd(0x0c);delay(5);lcd_wcmd(0x06);delay(5);lcd_wcmd(0x01);delay(5);〃等待LCD电源稳定〃16*2显示,5*7点阵,8位数据〃显示开,关光标〃移动光标〃清除LCD的显示内容/*/*清屏子程序/*voidlcd_clr()(lcd_wcmd(0x01);delay(5);〃清除LCD的显示内容/*/*闪动子程序/*

110voidflash()delay(600);lcd_wcmd(0x08);delay(200);lcd_wcmd(0x0c);delay(200);lcd_wcmd(0x08);delay(200);lcd_wcmd(0x0c);delay(200);〃控制停留时间〃关闭显示〃延时〃开显示〃关闭显示〃延时〃开显示/*主程序main()(uchari,j;delay(lO);lcd_init();〃初始化LCDwhile(l){lcd_clr();lcd_pos(0xl0);〃设置显示位置为第一行第17列i=0;while(disl[ij!='0'){〃显示字符”lcd_wdat(disl[i]);i++;)lcd_pos(0x50);〃设置显示位置为第二行第17列i=0;while(dis2[i]!=W)

111Icd_wdat(dis2[i]);i++;〃显示字符for(j=0;j<16;j++)(〃向左移动16格lcd_wcmd(Ox18);〃字符同时左移一格delay(lOO);)〃控制移动时间flash();〃闪动二次VCCLCD1286V240128RJ-502»LCD16O2#include',reg51.hHvoiddelay(void)(unsignedchara,b,c;for(c=1;c>0;c-);for(b=142;b>0;b—);for(a=2;a>0;a—);voidmain()

112P2=0xff;delay();P2=OxOO;delay();描述:12864标准字库液晶演示数据pO,控制p2刊#include#include#defineucharunsignedchar#defineuintunsignedint/*************]2864LCD弓I脚定义*************/#defineLCD_dataP0〃数据口sbitLCD_RS=P2八6;〃寄存器选择输入sbitLCD_RW=P2A5;〃液晶读/写控制sbitLCD_EN=P2A7;〃液晶使能控制sbitLCD_PSB=P3A2;〃串/并方式控制sbitLCD_RST=P3A7;〃液晶复位端口#definedelayNOP();{_nop_();_nop_();_nop_();_nop_();};ucharcodeucharcodeucharcodeucharcodeDIS1[]={H普中科技DIS2[]={”进入单片机世界”};DIS3[]={"www.prechin.com"};DIS4[J={nTEL:755-29325187M};ucharcodePhoto1[J={0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x3F,0xC0,0x80,0x42,0x04,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x8A,0x1F,OxFC,0x42,0x04,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,0x01,0xFF,0xDE,0x88,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x2A,0x87,0xF0,0xA2,0x0A,0xF8,0x00,0x00,0x00,0x00,

1130x00,0x00,0x00,0x00,0x00,OxOO,OxBF,0x84,Ox10,0xA2,0x0A,0x88,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,Ox1F,0xFC,0xA2,0x0A,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x9F,0x90,0x05,0x2F,0xCA,0xA4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x03,0xEl,0xA8,0x4A,OxA8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBF,0xC2,0x24,0x48,0x44,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x92,0x84,0x24,0xA8,0x4A,0xA8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA6,0x58,0x3D,0x0E0xD0,0xC4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,OxOO,Ox1C,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x23,0x08,0x37,0x80,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,Ox10,0x08,0x40,0x00,OxFF,0x90,Ox10,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81,0x86,Ox10,0x40,0x00,0x08,0x08,OxIO,OxFF,0x86,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x49,0x40,0x40,0x00,0x7F,0x0B,OxFC,0x08,OxOF,0x00,0x00,0x00,0x00,0x00,0x01,0x08,0x00,0x40,0x20,0x00,0x10,0x00,Ox10,0x49,OxOF,0x00,0x00,0x00,0x00,0x00,0x02,0x08,0x00,0x20,0x20,0x01,0xFF,0xDD,Ox10,0x2A,0x0F,0x00,0x00,0x00,0x00,0x00,0x04,0x31,0x83,0x20,0x20,0x00,0x20,0x04,0x91,0xFF,0xC6,0x00,0x00,0x00,0x00,0x00,0x04,0x52,0x44,0x90,0x20,0x00,0x7F,0x04,0x90,Ox1C,0x06,0x00,0x00,0x00,0x00,0x00,0x09,0xA4,0x28,0x4C,Ox10,0x00,OxA1,0x04,0x10,0x2A,0x00,0x00,0x00,0x00,0x00,0x00,OxOE,0x44,0xE9,0xC3,0x90,0x0l,0x3F,0x04,0x70,0x49,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x82,0xC5,0x81,0x70,0x00,0x21,0x0A,0x01,0x88,0xC6,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x83,0x00,0x80,0x00,Ox3F,Ox11,OxFC,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x0LOxOC,0x70,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,Ox13,0x87,Ox10,0x40,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x02,0x20,0x78,OxAO,0x40,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x20,0x00,0x60,0x40,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x21,0x80,0x60,0x40,0x00,0x00,0x71,0xEE,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x11,0x98,0x90,0x80,0x00,0x00,Ox8E,0x3l,0xC6,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x08,Ox18,0x80,0x80,0x00,0x01,0x24,0x24,0x83,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x96,0x01,0x01,0x00,0x00,0x01,0x40,0x28,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x49,0x86,0x03,0x00,0x00,0x01,0x40,0x28,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x78,Ox8C,0x00,0x00,0x01,0x00,0x20,0x03,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,OxOE,0x01,0x30,0x00,0x00,0x00,0x80,0x30,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x71,0x87,OxCE,0x00,0x00,0x00,0x40,0x68,OxOC,0x00,0x00,0x00,0x00,0x00,0x00,OxOO,OxEO,0x78,0x07,0x00,0x00,0x00,0x20,0xC4,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xAF,0xFF,0xC5,0x80,0x00,0x00,Ox11,0x82,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xA9,0xB6,0x45,OxCO,0x00,0x00,OxOB,0x01,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x2F,0x03,0xC4,0xE0,0x00,0x00,0x06,0x00,OxCO,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x

11416,0x01,0x88,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,OxOF,0x08,0x00,Ox10,OxFO,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,OxOF,0x84,0x00,0x21,OxFO,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};ucharcodePhoto2[J={/*-调入了一幅图像:G:\HC-6800\12864\C语言\pz.bmp-*//*-宽度x高度=128x64-*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0LOxFE,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x63,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x63,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x63,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x7E,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x6C,0x08,Ox1F,0xF8,0x3F,0xF8,0x7F,0xF0,0x0F,0xF9,0xF1,0xF0,0xFF,0x87,0xC3,0xC4,0x6C,0x08,0x0E,0x7C,0x1E,0xFC,0x38,0xF8,0x3E,0xF8,0xE0,0xE0,0x1C,0x03,0xC1,0x84,0x66,0x08,0x0E,0x1E,0x1C,0x3C,0x38,0x38,0x38,0x38,OxEO,OxEO,Ox1C,0x03,0xE1,0x84,0x66,0x08,OxOE,OxlE,Ox1C,0x1C,0x38,0x18,0x78,0x38,0xE0,0xE0,0x1C,0x03,0xE1,0x82,0x63,Ox10,0x0E,0x0E,0x1C,0x1C,0x38,0x00,OxFO,Ox18,0xE0,0xE0,0x1C,0x03,0xFl,0x81,0xF3,0xA0,OxOE,OxOE,Ox1C,Ox3C,Ox38,OxCO,OxFO,OxOO,OxEO,OxEO,Ox1C,0x03,OxFl,0x81,0x80,0x60,0x0E,0x1E,0x1C,0x3C,0x38,OxCO,OxFO,0x00,0xE0,0xE0,Ox1C,0x03,0x79,0x80,0x40,0x80,OxOE,OxIE,Ox1C,0xF8,0x39,0xC0,0xE0,0x00,0xE0,0xE0,0x1C,0x03,0x79,Ox8O,Ox3F,0x00,0x0E,0x7C,0x1F,0xF0,0x3F,0xC0,0xE0,0x00,0xFF,0xE0,0x1C,0x03,0x3D,0x80,0x00,0x00,0x0F,0xF8,0x1D,0xE0,0x39,OxCO,OxEO,0x00,OxEO,OxEO,Ox1C,0x03,Ox3D,Ox80,0x00,0x00,OxOE,OxOO,Ox1D,0xE0,0x38,0xC0,0xE0,0x00,0xE0,0xE0,0x1C,0x03,Ox1F,0x80,0x00,0x00,OxOE,0x00,OxlC,OxFO,0x38,OxCO,OxFO,0x00,0xE0,0xE0,0xlC,0x03,OxlF,0x80,0x00,0x00,0

115x0E,0x00,0xlC,0xF0,0x38,0x00,0xF0,0x00,0xE0,0xE0,0x1C,0x03,OxOF,0x80,0x00,0x00,OxOE,0x00,OxlC,0x78,0x38,0x00,OxFO,Ox18,0xE0,0xE0,0x1C,0x03,OxOF,Ox80,0x00,0x00,0x0E,0x00,0xlC,0x78,0x38,0xl8,0x70,0x38,OxEO,OxEO,OxIC,0x03,0x07,0x80,0x00,0x00,OxOE,0x00,OxlC,0x78,0x38,0x38,0x78,0x30,0xE0,0xE0,0xlC,0x03,0x07,0x80,0x00,0x00,OxOE,OxOO,OxIC,0x3C,0x38,0xF8,0x3E,0xF0,0xE0,0xE0,OxIC,0x03,0x03,0x80,0x00,0x00,Ox1F,0x00,0x3E,0x3E,0x7F,0xR),0x1F,OxE1,OxFl,0xF0,0xFF,0x87,0x83,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x(X),0x00,0x00,0x00,0x00,OxFO,0x38,0x00,0x00,0x78,0x00,0x00,Ox18,0x07,0x80,Ox1C,0x03,0x80,0x00,0x00,0x00,0x7C,0x7C,0x00,0x00,0x78,0x00,0x00,0x7C,0x07,0x80,OxIC,0x03,OxCO,0x00,0x00,0x00,Ox3C,0x70,0x00,0x00,0x70,0x00,0x07,OxFC,0x07,0x00,OxlC,0x03,0x80,0x00,0x00,0x00,Ox1C,0xE3,0xC0,0x00,0x70,0x00,Ox1F,OxE1,0xC7,0x(X),0x1C,0x03,0x80,0x00,0x00,OxOF,OxFF,OxFF,OxEO,0x00,0x70,0x00,0x00,0xEl,0xE7,0x00,OxlC,0x03,0x80,0x00,0x00,0x06,0x1C,0xE6,0x00,OxFF,OxFF,OxFC,0x00,0xE0,0xF7,0x00,Ox1D,0x83,0x9E,0x00,0x00,0x07,OxIC,0xE7,0x80,OxFF,OxFF,OxFC,0x00,0xE0,0xF7,0x03,OxFF,OxFF,OxFE,0x00,0x00,0x03,0x9C,0xEF,0x80,0xE0,0x70,0x38,0x00,OxEE,0x67,0x00,0xIC,0x03,0x80,0x00,0x00,0x03,OxDC,OxEE,0x00,OxEO,0x70,0x38,0x3F,0xFE,0x07,0x00,OxlC,0x03,0x80,0x00,0x00,0x01,0xFC,0xFC,0x00,OxEO,0x70,0x38,0x01,0xE0,0x07,0x00,0x1C,0x03,0x80,0x00,0x00,0x01,0xDC,0xF8,0xE0,0xE0,0x70,0x38,0x01,0xE1,0xC7,0x00,0x1D,0xC3,0xB8,0x00,0x00,0x3F,0xFF,0xFF,0xF0,0xE0,0x70,0x38,0x03,OxFl,0xE7,0x00,0x1F,OxFF,OxFC,OxOO,0x00,0x3F,0xFF,0xFF,0xF0,0xE0,0x70,0x38,0x03,0xFC,0xE7,0x00,0x7E,0x38,0x38,0x00,0x00,0x00,0x80,0x0C,0x00,0xFF,0xFF,0xF8,0x07,0xFE,0xE7,0xE3,0xFC,Ox18,0x78,0x00,0x00,0x00,OxFF,OxFE,0x00,OxEO,0x70,0x38,0x07,OxFE,0x07,0xF3,0xFC,0xlC,0x70,0x00,0x00,0x00,OxFF,OxFE,OxOO,OxEO,0x70,0x38,0x0F,0xEC,0x3F,0xC1,0x9C,0x1C,0xF0,0x00,0x00,0x00,OxEO,Ox1E,0x00,0x00,0x70,0x00,0x0E,0xE7,0xFF,0x00,Ox1C,OxOE,OxEO,OxOO,0x00,0x00,OxEO,Ox1E,0x00,0x00,0x70,0x00,0x1C,0xEF,0x07,0x00,0x1C,OxOF,OxEO,OxOO,0x00,0x00,OxFF,OxFE,0x00,0x00,0x70,0x00,0x38,OxEO,0x07,0x00,OxlC,0x07,OxCO,0x00,

1160x00,0x00,OxEO,OxIE,0x00,0x00,0x70,0x00,0x30,0xE0,0x07,0x00,Ox1C,0x07,0x80,0x00,0x00,0x00,OxEO,Ox1E,0x00,0x00,0x70,0x00,0x00,OxEO,0x07,0x00,0x1C,OxOEOxEO,OxOO,0x00,0x00,OxEO,Ox1E,0x00,0x00,0x70,0x00,0x00,0xE0,0x07,0x01,0xDC,0x3F,0xF8,0x00,0x00,0x00,OxFF,OxFE,0x00,0x00,0x70,0x00,0x00,OxEO,0x07,0x01,OxFC,0x78,OxFF,OxOO,0x00,0x00,0xE0,OxIE,0x00,0x00,0x70,0x00,0x00,OxEO,0x07,0x00,0x7F,0xE0,0x3F,0x00,0x00,0x00,OxCO,OxlC,0x00,0x00,0x70,0x00,0x00,OxCO,0x07,0x00,0x37,0x80,OxOC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x(X),0x00,0x00,0x00,0x0F,0xFE0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00*/*/*//*/*延时函数/*voiddelay(intms)while(ms-)uchari;for(i=0;i<150;i++)_nop_();_nop_();_nop_();_nop_();)))/**/*//*延时函数/*voiddelayl(intms)while(ms—)uchary;for(y=0;y<100;y++);

117产/*检查LCD忙状态/*lcd_busy为1时,忙,等待。Icd-busy为0时,闲,可写指令与数据。/*可*/bitlcd_busy()bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOPO;result=(bit)(P0&0x80);LCD_EN=0;retum(result);/*写指令数据到LCD/*RS=L,RW=L,£=高脉冲,D037=指令码。*/voidlcd_wcmd(ucharcmd)while(lcd_busy());LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_();P0=cmd;delayNOP();LCD_EN=1;delayNOPO;LCD_EN=0;/*写显示数据到LCD/*RS=H,RW=L,氏高脉冲,D0-D7=数据。/*到*/voidlcd_wdat(uchardat)

118while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;delayNOP();LCD_EN=1;delayNOPO;*/*/LCD_EN=0;/*/*LCD初始化设定/*LCD_PSB=1;〃并口方式〃液晶复位LCD_RST=0;delay(3);LCD_RST=1;delay(3);〃显示开,关光标〃清除LCD的显示内容lcd_wcmd(0x34);delay(5);lcd_wcmd(0x30);delay(5);lcd_wcmd(0x0C);delay(5);lcd_wcmd(0x01);delay(5);〃扩充指令操作〃基本指令操作切*//*/*设定显示位置/*voidlcd_pos(ucharX,ucharY)ucharpos;if(X==l){X=0x80;}

119elseif(X==2){X=0x90;}elseif(X==3){X=Ox88;)elseif(X==4){X=0x98;)pos=X+Y;lcd_wcmd(pos);〃显示地址*闪烁函数voidIcdflagO(lcd_wcmd(0x08);delay(400);lcd_wcmd(0x0c);delay(400);lcd_wcmd(0x08);delay(400);lcd_wcmd(0x0c);delay(400);lcd_wcmd(0x08);delay(200);lcd_wcmd(0x0c);delay(5);lcd_wcmd(0x01);delay(5);)***图形显示***voidphotodisplay(uchar*bmp){uchari,j;lcd_wcmd(0x34);〃写数据时,关闭图形显示for(i=0;i<32;i+4-)

120lcd_wcmd(0x80+i);//先写入水平坐标值lcd_wcmd(0x80);〃写入垂直坐标值for(j=0;j<16;j++)〃再写入两个8位元的数据lcd_wdat(*bmp++);delay1(1);for(i=0;i<32;i++)(lcd_wcmd(0x80+i);lcd_wcmd(0x88);for(j=0;j<16;j++)lcd_wdat(*bmp++);delayl(l);}lcd_wcmd(0x36);〃写完数据,开图形显示)***清屏函数***voidclr_screen()(〃扩充指令操作//基本指令操作〃清屏lcd_wcmd(0x34);delay(5);lcd_wcmd(0x30);delay(5);lcd_wcmd(0x01);delay(5);;显示字符表代码voidbytecode()(uchars;〃清屏〃设置显示位置为第一行clr_screen();lcd_wcmd(0x80);for(s=0;s<16;s++)(lcd_wdat(0x30+s);

121icd_wcmd(0x90);for(s=0;s<16;s++)〃设置显示位置为第二行lcd_wdat(0x40+s);)lcd_wcmd(0x88);for(s=0;s<16;s++)(lcd_wdat(0x50+s);)lcd_wcmd(0x98);for(s=0;s<16;s++)(lcd_wdat(0x60+s);〃设置显示位置为第三行〃设置显示位置为第四行*主函数voidmain()(uchari;delay(lOO);〃上电,等待稳定lcd_init();〃初始化LCDwhile(l){lcd_pos(l,0);〃设置显示位置为第一行fdr(i=O;i<16;i++)(lcd_wdat(DISl[iJ);delay(3O);}lcd_pos(2,0);for(i=0;i<16;i++)(lcd_wdat(DIS2[i]);delay(3O);}lcd_pos(3,0);for(i=0;i<16;i++)〃设置显示位置为第二行〃设置显示位置为第三行

122lcd_wdat(DIS3[i]);delay(30);lcd_pos(4,0);〃设置显示位置为第四行for(i=0;i<16;i++)(lcd_wdat(DIS4[i]);delay(30);}delay(lOOO);IcdflagO;clr_screen();〃清屏photodisplay(Photo1);〃显小图片1delay(2000);clr_screen();〃清屏photodisplay(Photo2);〃显示图片2delay(2000);bytecode();〃显示字符表代码delay(2000);clr_screen();}*描述:*12864标准字库液晶演示数据pO,控制p2*/#include#include#defineucharunsignedchar#defineuintunsignedint/*************j2864LCD弓I脚定义*************/#defineLCD_dataPO〃数据口sbitLCD_RS=P2A6;〃寄存器选择输入sbitLCD_RW=P2八5;〃液晶读/写控制sbitLCD_EN=P2A7;//液晶使能控制sbitLCD.PSB=P3八2;〃串/并方式控制sbitLCD_RST=P3A7;〃液晶复位端口#defineNOP();{_nop_();_nop_();_nop_();_nop_();};ucharcodeDIS1[]={"普中科技"};

123ucharcodeDIS21J={"进入单片机世界"};ucharcodeDIS3[]=["www.prechin.com”};ucharcodeDIS4[]={"TEL:755-29325187H};/*延时函数voiddelay(intms){while(ms—)(uchari;for(i=0;i<150;i++)(_nop_();_nop_();_nop_();_nop_();)*/*/)/*/*检查LCD忙状态/*lcd_busy为1时,忙,等待。Icd-busy为0时,闲,可写指令与数据。/*bitlcd_busy()bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;NOP();result=(bit)(P0&0x80);LCD_EN=0;retum(result);

124/*写指令数据到LCD/*RS=L,RW=L,£=高脉冲,D0-D7二指令码。*/*/voidlcd_wcmd(ucharcmd)(while(lcd_busy());LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_();P0=cmd;NOP();LCD_EN=1;NOP();LCD_EN=0;/**//*写显示数据到LCD*/Z*RS=H,RW=L,艮=高脉冲,D0-D7=数据。*//**/voidlcd_wdat(uchardat)while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;NOP();LCD_EN=1;NOP();LCD_EN=0;/*LCD初始化设定voidlcd_init()

125LCD_PSB=1;〃并口方式LCD_RST=0;〃液晶复位delay⑶;LCD_RST=1;delay⑶;lcd_wcmd(0x34);delay(5);lcd_wcmd(0x30);delay(5);lcd_wcmd(0x0C);delay(5);lcd_wcmd(0x01);delay(5);//扩充指令操作〃基本指令操作〃显示开,关光标〃清除LCD的显示内容*//清屏函数*voidclr_screen()(lcd_wcmd(0x34);〃扩充指令操作delay(5);lcd_wcmd(0x30);〃基本指令操作设定显示位置voidlcd_pos(ucharX,ucharY)(ucharpos;if(X==l){X=0x80;}elseif(X==2){X=0x90;}elseif(X==3){X=0x88;}elseif(X==4){X=0x98;}pos=X+Y;lcd_wcmd(pos);〃显示地址

126〃清屏delay⑸;lcd_wcmd(OxO1);delay(5);*主函数voidmain()uchari;delay(lOO);lcd_init();whiled)〃上电,等待稳定〃初始化LCDlcd_pos(1,0);for(i=0;i<16;i++)〃设置显示位置为第一行lcd_wdat(DISl[i]);delay(30);lcd_pos(2,0);fdr(i=0;i<16;i++)〃设置显示位置为第二行lcd_wdat(DIS2[iJ);delay(30);)lcd_pos(3,0);for(i=0;i<16;i++)〃设置显示位置为第三行lcd_wdat(DIS3[i]);delay(30);}lcd_pos(4,0);fbr(i=0;i<16;i++)〃设置显示位置为第四行lcd_wdat(DIS4[i]);delay(30);)clr_screen();〃清屏函数,为下次刷新准备)

127连接方法描led1602显示led1602显示喇叭接pl.5使用红外功fsbitIRIN=P3A2;sbitRELAY=P1A4;sbitBEEP=PlA5;〃红外接收器数据线〃继电器驱动线〃蜂鸣器驱动线述:遥控键值读取器遥控器接p3.2继电器接pl.4时J1跳线短接#include#include#defineNOP()_nop_()/*定义空指令*/#defineucharunsignedchar#defineuintunsignedint#definedelayNOP();{_nop_();_nop_();_nop_();_nop_();};voiddelay(ucharx);//x*0.14MSvoiddelay1(intms);voidbeep(void);ucharIRCOM17J;ucharedis1[]={nREMOTECONTROLH;ucharcdis2[]={"IR-CODE:--H”};//LCDIOsbitLCD_RW=P2A5;sbitLCD.RS=P2A6;sbitLCD_EN=P2A7;unsignedcharY0;

128产/*检查LCD忙状态/*lcd_busy为1时,忙,等待。Icd-busy为0时,闲,可写指令与数据。/*可*/*/bitlcd_busy()(bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOPO;result=(bit)(P0&0x80);LCD_EN=0;retum(result);/*写指令数据到LCD/*RS=L,RW=L,氏高脉冲,D037=指令码。/**/*/*/voidlcd_wcmd(ucharcmd)while(lcd_busy());LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_();P0=cmd;delayNOPO;LCD_EN=1;delayNOPO;LCD_EN=0;

129/*写显示数据到LCD/*RS=H,RW=L,氏高脉冲,D0-D7=数据。/**/刊*/voidlcd_wdat(uchardat)(while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;delayNOPO;LCD_EN=1;delayNOPO;LCD_EN=0;/*/*LCD初始化设定voidlcd_init()(delayl(15);lcd_wcmd(0x38);delayl(5);lcd_wcmd(0x38);delayl(5);lcd_wcmd(0x38);delayl(5);lcd_wcmd(0x0c);delay1(5);lcd_wcmd(0x06);delay1(5);lcd_wcmd(0x01);delay1(5);〃16*2显示,5*7点阵,8位数据〃显示开,关光标〃移动光标〃清除LCD的显示内容

130/*设定显示位置*//**/voidlcd_pos(ucharpos)lcd_wcmd(posI0x80);〃数据指针=80+地址变量main()(ucharm;IE=0x81;TCON=0x01;〃允许总中断中断,使能INTO外部中断〃触发方式为脉冲负边沿触发IRIN=1;BEEP=1;RELAY=1;//I/O□初始化delayl(lO);lcd_init();〃延时〃初始化LCDlcd_pos(0);〃设置显示位置为第一行的第1个字符m=0;while(cdisl[m]!=W){lcd_wdat(cdisl[m]);m++;)〃显示字符lcd_pos(0x40);m=0;while(cdis2[m]!=AO')(〃设置显示位置为第二行第1个字符lcd_wdat(cdis2[m]);m++;〃显示字符while(l);}//endmain

131voidIRJN(void)interrupt0〃外部中断服务程序unsignedcharj,k,N=O;EXO=0;delay(15);if(IRIN==l){EX0=l;return;)//确认IR信号出现while(!IRIN){delay(l);)〃等IR变为高电平,跳过9ms的前导低电平信号。for(j=0;j<4;j-H-)f〃收集四组数据ifor(k=0;k<8;k++)(while(IRIN){delay(l);)while(!IRIN){delay(l);)while(IRIN)〃每组数据有8位〃等IR变为低电平,跳过4.5ms的前导高电平信号。〃等IR变为高电平〃计算IR高电平时长delay(l);N++;if(N>=30){EX0=l;return;})//0.14ms计数过长自动离开。〃高电平计数完毕IRCOM[j]=IRCOM[j]»1;〃数据最高位补“0”if(N>=8){IRCOMfj]=IRCOMLj]I0x80;)〃数据最高位补“1”N=0;}//endfork}//endforjif(IRCOM[2]!=-IRCOM[3J){EX0=l;return;}IRCOM[5]=IRCOM[2]&OxOF;〃取键码的低四位IRCOM[6]=IRCOM[2]»4;〃右移4次,高四位变为低四位if(IRCOM[5]>9){IRCOM[5]=IRCOM[5J+Ox37;}elseIRCOM[5]=IRCOM[5]+Ox30;

132if(IRCOM[6]>9){IRCOM[6]=IRCOM[6]+Ox37;}elseIRCOM[6]=IRCOM[6]+Ox30;lcd_pos(0x4b);lcd_wdat(IRCOM[6]);〃第一位数显示lcd_pos(0x4c);lcd_wdat(IRCOM[5]);〃第二位数显示Y0=0;switch(IRCOM[2])(case0x09:Y0=0x01;break;caseOxID:Y0=0x02;break;caseOxIF:Y0=0x03;break;caseOxOD:Y0=0x04;break;case0x19:Y0=0x05;break;caseOx1B:Y0=0x06;break;caseOxll:Y0=0x07;break;case0x15:Y0=0x08;break;case0x17:Y0=0x09;break;case0x13:RELAY=1;break;case0x14:RELAY=1;break;case0x51:RELAY=0;break;)if(Y0&0x01)RELAY=0;〃打开继电器elseRELAY=1;〃关闭继电器beep();EXO=1;voidbeep(void)(unsignedchari;for(i=0;i<100;i++)

133delay(4);BEEP=!BEEP;}BEEP=1;)//BEEP取反〃关闭蜂鸣器voiddelay(unsignedcharx)//x*0.14MSunsignedchari;while(x—)for(i=0;i<13;i++){}voiddelayl(intms)unsignedchary;while(ms-)for(y=0;y<250;y++)_nop_();_nop_();_nop_();_nop_();}}**率*led1602显示遥捽键值读取器**led1602显示遥控器接p3.2**喇叭接pl.5继电器接pl.4*连接方法:使用红夕卜功能时J1跳线短接描述

134*/#include#include#defineucharunsignedchar#defineuintunsignedint#definedelayNOPO;(_nop_();_nop_();_nop_();_nop_();};voiddelay(ucharx);//x*0.14MSvoiddelay1(intms);voidbeep();sbitIRIN=P3A2;sbitBEEP=PlA5;sbitRELAY=P1A4;〃红外接收器数据线〃蜂鸣器驱动线〃继电器驱动线ucharIRCOM[7];sbitLCD_RS=P2八6;sbitLCD_RW=P2A5;sbitLCD_EN=P2A7;ucharcodecdisl[]={Hucharcodecdis2[]={MRedControln;IR-CODE:-H”};/*/*检查LCD忙状态/*lcd_busy为1时,忙,等待。Icd-busy为0时,闲,可写指令与数据。/**/*/bitlcd_busy()(bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOPO;result=(bit)(P0&0x80);LCD_EN=0;retum(result);

135/*/*写指令数据到LCD/*RS=L,RW=L,E=i$j脉冲,D0-D7二指令码。俨*/*/voidlcd_wcmd(ucharcmd)while(lcd_busy());LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_();P0=cmd;delayNOPO;LCD_EN=1;delayNOPO;LCD_EN=0;/*写显示数据到LCD*//*RS=H,RW=L,4=高脉冲,D0-D7=数据。*//**/voidlcd_wdat(uchardat)(while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;delayNOPO;LCD_EN=1;delayNOPO;LCD_EN=0;

136/**//*LCD初始化设定*//**/voidlcd_init(){delayl(15);lcd_wcmd(Ox38);delay1(5);lcd_wcmd(Ox38);delay1(5);lcd_wcmd(Ox38);delay1(5);lcd_wcmd(0x0c);delay1(5);lcd_wcmd(0x06);delayl(5);lcd_wcmd(0x01);〃16*2显示,5*7点阵,8位数据〃显示开,关光标〃移动光标〃清除LCD的显示内容delay1(5);*/*//*/*设定显示位置voidlcd_pos(ucharpos)(lcd_wcmd(posI0x80);〃数据指针=80+地址变量main()(ucharm;//I/O□初始化IRIN=1;BEEP=1;RELAY=1;

137delay1(10);lcd_init();〃延时〃初始化LCDlcd_pos(0);〃设置显示位置为第一行的第1个字符m=0;while(cdisl[m]!=AO')(lcd_wdat(cdism++;)〃显示字符lcd_pos(0x40);〃设置显示位置为第二行第1个字符m=0;while(cdis2[m]!=AO')lcd_wdat(cdis2[m]);m++;)〃显示字符IE=0x81;TCON=0x01;〃允许总中断中断,使能INTO外部中断〃触发方式为脉冲负边沿触发while(l);}//endmainvoidIR_IN()interrupt0using0(unsignedcharj,k,N=O;EXO=0;delay(15);if(IRIN==1){EX0=l;return;for(k=O;k<8;k++)〃每组数据有8位while(!IRIN){delay(l);)//确认IR信号出现〃等IR变为高电平,跳过9ms的前导低电平信号。for(j=0;j<4;j++)〃收集四组数据while(IRIN)〃等IR变为低电平,跳过4.5ms的前导高电平信号。{delay(l);)

138while(!IRIN)〃等IR变为高电平{delay(l);)while(IRIN)〃计算IR高电平时长(delay(l);N++;if(N>=30){EXO=1;return;}//0.14ms计数过长自动离开。)〃高电平计数完毕IRCOM[j]=IRCOM[j]»1;//数据最高位补“0”if(N>=8){IRCOM[jJ=IRCOM[jJ10x80;}〃数据最高位补“1”N=0;}//endfork}//endforjif(IRC0M[2]!=-IRCOM[3]){EX0=l;return;}IRCOM[5]=IRCOM⑵&OxOF;〃取键码的低四位IRCOM[6]=IRCOM[2]»4;〃右移4次,高四位变为低四位if(IRCOM[5]>9){IRCOM[5]=IRCOM[5J+Ox37;}elseIRCOM[5J=IRCOM15]+Ox30;if(IRCOM[6J>9){IRCOM[6]=IRCOM[6]+Ox37;}elseIRCOM[6]=IRCOM[6]+Ox30;lcd_pos(0x4b);lcd_wdat(IRCOM[6]);〃第一位数显示lcd_pos(0x4c);lcd_wdat(IRCOM[5]);〃第二位数显示beep();EX0=1;

139voidbeep()(unsignedchari;for(i=0;i<100;i++)(delay(4);//BEEP取反〃关闭蜂鸣器BEEP=!BEEP;)//x*0.14MSBEEP=1;voiddelay(unsignedcharx)(unsignedchari;while(x—)(for(i=0;i<13;i++){}voiddelayl(intms)unsignedchary;while(ms—)for(y=0;y<250;y++)_nop_();_nop_();_nop_();_nop_();数码管显示遥控键值读取器遥控器接p3.2

140现象:当按遥控器时,按下事数码管会有闪烁,为什么会闪烁呢连接方法:使用红外功能时J1跳线短接#includesbitIRIN=P3A2;〃红外接收器数据线voidIRdelay(charx);//x*0.14MSvoidbeep();unsignedcharIRCOM[7];externunsignedcharY0;externvoiddisplay(unsignedcharKey_Val);voidIRInit()IE1=0x81;TCON1=0x01;IRIN=1;〃允许总中断中断,使能INTO外部中断〃触发方式为脉冲负边沿触发//I/O口初始化voidIRJN(void)interrupt0(unsignedcharj,k,N=O;EXO=0;IRdelay(5);if(IRIN==l){EX0=l;return;}〃确认IR信号出现while(!IRIN)〃等IR变为高电平,跳过9ms的前导低电平信号。{IRdelay(l);)

141for(j=0;j<4;j++)〃收集四组数据for(k=0;k<8;k++)〃每组数据有8位while(IRIN){IRdelay(l);)while(!IRIN){IRdelay(l);)while(IRIN)〃等IR变为低电平,跳过4.5ms的前导高电平信号。〃等IR变为高电平〃计算IR高电平时长IRdelay(l);N++;if(N>=30){EX0=l;return;}lRCOM[j]=IRCOM[j]»1;//0.14ms计数过长自动离开。〃高电平计数完毕〃数据最高位补“0”if(N>=8){IRCOM[j]=IRCOMUJI0x80;}〃数据最高位补“1”N=0;}//endfork}//endforjif(IRCOM[2]!=-IRCOM[3J){EX0=l;return;EX0=1;voidIRdelay(unsignedcharx)//x*0.14MSunsignedchari;while(x—)for(i=0;i<13;i++){}

142描述:数码管显示遥控键值读取器遥控器接p3.2/#include#include#defineNOP()_nop_()/*定义空指令*/〃译码器管脚定义sbitLS138A=P2A2;〃管脚定义sbitLS138B=P2A3;sbitLS138C=P2A4;〃此表为LED的字模,共阴数码管0・9-unsignedcharcodeDisp_Tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};unsignedlongLedOut[5],LedNumVal;externunsignedcharIRCOM[7J;voiddelay(unsignedinti);〃函数声名voiddisplay(unsignedcharKey_Val);externvoidIRInitQ;//extern是引用IR.C中的函数main()(IRInitO;while(l)〃显示红外键码display(IRCOM[2]);

143voiddelay(unsignedinti)(charj;for(i;i>0;i-)ford=200;j>O;j-);voiddisplay(unsignedcharKey_Val)(unsignedchari;/********以下将键值送到led数码管显示*************/LedNumVal=Key_Val;〃把键值送到LedNumVal变量中LedOut[0]=Disp_Tab[LedNumVal%10000/1000];LedOut[1]=Disp_Tab[LedNumVal%1000/100];LedOut[2]=Disp_Tab[LedNumVal%100/10];〃十位LedOut[3]=Disp_Tab[LedNumVal%10];〃个位for(i=0;i<4;i++)(P0=LedOut[i];switch(i){〃138译码case0:LS138A=0;LS138B=O;LS138C=O;break;case1:LS138A=1;LS138B=O;LS138C=O;break;case2:LS138A=0;LS138B=1;LS138C=0;break;case3:LS138A=1;LS138B=1;LS138C=O;break;)delay(lOO);P0=0;

144PR3is/h;连强为中蚌方式若制红土ax一7)2此处可以用杜邦线连接单片机的任何脚位.可以实现舌通的单片机脚控制红外红外遥控器波形:forPanasonicTV(tc—80902)byyangzhenshang2003.7.21♦**555定时器试验-——频率计******LCD1602显示**改变VR1电位器的电阻值,观察液晶屏频率发生变化***#include#includeunsignedcharcodecdislf]=("CYMOMETER"//频率计);unsignedcharcodecdis2[]=Hz"FREQ:sbitLCD_RS=P2A6;sbitLCD_RW=P2A5;sbitLCD_EN=P2A7;bitsec=0;unsignedcharmsec=0,Hdata=0,Ldata=0,Count=0;

145unsignedlongtemp=0;unsignedchardatadisplay1]=0x00,0x00,0x00,0x00,0x00,0x00charcodereserve[3]_at_0x3b;〃保留0x3b开始的3个字节us延时函数(4.34us)voiddelayNOP()_nop_();_nop_();_nop_();_nop_();)ms延时函数voiddeiayms(unsignedintms)unsignedcharn;while(ms—)for(n=0;n<114;n++)检查LCD忙状态lcd_busy为1时,忙,等待。Icd-busy为0时,闲,可写指令与数据。bitlcd_busy()bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOPO;result=(bit)(PO&0x80);LCD_EN=0;return(result);*写指令数据到LCD*RS=L,RW=L,E=rftj脉冲,D0-D7/令码。*voidlcd_wcmd(unsignedcharcmd)while(lcd_busy())LCD_RS=0;LCD_RW=0;LCD_EN=1;P0=cmd;delayNOPO;LCD_EN=0;**写显示数据到LCDRS=H,RW=L,£=高脉冲,D0-D7=数据。

146voidlcd_wdat(unsignedchardat)(while(lcd_busy())LCD_RS=1;LCD_RW=0;LCD_EN=1;P0=dat;delayNOP();LCD_EN=0;***LCD初始化设定***voidlcd_init()(delayms(15);lcd_wcmd(0x38);〃16*2显示,5*7点阵,8位数据delayms(5);lcd_wcmd(0x38);delayms(5);lcd_wcmd(Ox38);delayms(5);lcd_wcmd(0x0c);〃开显示,不显示光标delayms(5);lcd_wcmd(0x06);//delayms(5);lcd_wcmd(0x01);〃清除LCD的显示内容delayms(5);*设定显示位置voidlcd_pos(unsignedcharpos)Icd_wcmd(posI0x80);〃数据指针=80+地址变量*显示函数voidplay()(

147unsignedcharn;for(n=0;n<=4;n++)〃数据转换(display[n]=temp%10+0x30;temp=temp/10;)display[5]=temp+0x30;for(n=5;n>0;n-)〃高位为"0”不显不(if(display[n]==0x30)display[n]=0x20;elsebreak;}[cd_pos(0x46);〃显示实际频率值for(n=5;n!=Oxff;n—)lcd_wdat(display[nj);**主函数*voidmain()(unsignedcharm;unsignedlongfrq_num;P3=Oxff;lcd_init();lcd_pos(0x00);〃设置显示位置为第一行for(m=0;m<16;m++)lcd_wdat(cdisl[m]);〃显示字符lcd_pos(0x40);〃设置显示位置为第二行for(m=0;m<16;m++)lcd_wdat(cdis2[m]);〃显示字符

148TMOD=0x51;〃定时器0工作在定时方式〃定时器1工作在计数方式TH0=0x4c;//50ms定时TL0=0x00;TH1=0x00;〃计数初值TL1=0x00;ETO=1;〃使能TIMER0中断ET1=1;〃使能TIMER1中断EA=1;〃允许中断PT1=1;〃定义TIMER1中断优先TR0=1;TRI=1;while(1)(if(sec)(Hdata=THl;〃取计数值Ldata=TLl;frq_num=((Count*65535+Hdata*256+Ldata)*108/100);TH1=0;TL1=0;sec=0;Count=0;TRI=1;TR0=1;)temp=frq_num;playO;***TimeO中断函数***voidTimeO()interrupl1{TH0=0x4c;〃50ms定时TLO=0x00;msec++;if(msec==20)//5O*2O=1S(TR0=0;〃关闭TIMERO

149TRI=0;〃关闭TIMERImsec=0;sec=1;〃置秒标记位***Timel中断函数***voidTimel()interrupt3(Count++;RS232串口通信试验(发送)*1.通过本例程了解串口的基本原理及使用,理解并掌握对串口进行初始化2.请使用串口调试助手(Baud4800、数据位8、停止位1、效验位无)做为上位机来接收单片机串口所发的信息。观察串口调试助手接收窗口请学员认真消化本例程,学会用C语言操作串口

150#include#includevoiddelay(unsignedinti);〃函数声明charcodeMESSAG印二”普中科技助您学习RS232,请使用串口调试助手:unsignedchara;voidmain(void){SCON=0x50;TMODI=0x20;PCONI=0x80;//TH1=0xFD;无(11.0592)TH1=0xF3;效验位无(12M)TL1=0xF3;TRI=1;ES=1;EA=1;//IE=0x0;//REN=1允许串行接受状态,串口工作模式2〃定时器工作方式2〃波特率提高一倍//baud*2/*reloadvalue19200s数据位8、停止位1。效验位////baud*2/*波特率4800、数据位8、停止位1.〃开启定时器1〃开串口中断//开总中断while(l){a=0;while(MESSAGE[a]!=\0)(SBUF=MESSAGE[a];//SUBF接受/发送缓冲器(又叫串行通信特殊功能寄存器)while(!TI);〃等特数据传送(TI发送中断标志)TI=0;〃清除数据传送标志a++;//下一个字符)delay(lOOO);)延时处理程序voiddelay(unsignedinti)unsignedcharj;for(i;i>0;i-)for(j=200;j>0;j-);

151RS232串口通信试验(接收)*1.通过本例程了解串口的基本原理及使用,理解并掌握对串口进行初始化****2.请使用串口调试助手(Baud4800、数据位8、停止位1、效验位无)做为上位机发给单片机,*如果在字符数字发送框发0X00蜂铃器就会叫,如果发Oxff蜂铃器就停叫。*请学员认真消化本例程,学会用C语言操作串口*****/include#includesbitBEEP=P1A4;unsignedcharb;voidmain(void){SCON=0x50;//REN=1允许串行接受状态,串口工作模式1TMODI=0x20;〃定时器工作方式2PCONI=0x80;//TH1=0xFD;//baud2.请使用串口调试助手(Baud4800、数据位8、停止位1、效验位无)做为上位机来做收发试验。*在调试助手字符串输入框中输入数字和字符,按发送按钮,然后观察接受窗口的变化。也可以用*调试助手自动循环发送。2/*reloadvalue19200、数据位8、停止位1。效验位无(11.0592)TH1=0xF3;〃//baud*2/*波特率4800、数据位8、停止位1。效验位无(12M)TL1=0xF3;TRI=1;ES=1;〃开串口中断EA=1;//开总中断BEEP=1;//IE=0x0;

152while(l)if(RI)//RI接受中断标志{RI=0;〃清除RI接受中断标志b=SBUF;〃SUBF接受/发送缓冲器)BEEP=b;*****RS232串口通信试验(接受与发送)*1.通过本例程了解串口的基本原理及使用,理解并掌握对串口进行初始化*请学员认真消化本例程,学会用C语言操作串口*******/#includesbitBEEP=P1A4;bitFlag;unsignedintReData,SenData;voidmain(void){SCON=0x50;//REN=1允许串行接受状态,串口工作模式1TMODI=0x20;〃定时器工作方式2PCONI=0x80;//TH1=0xFD;//baud*2/*reloadvalue19200、数据位8、停止位1。效验位无(11.0592)TH1=0xF3;////baud*2/*波特率4800、数据位8、停止位1。效验位无(12M)TL1=0xF3;TRI=1;

153ES=1;〃开串口中断EA=1;〃开总中断BEEP=1;//IE=0x0;while(l)if(Flag==l)(SBUF=SenData;//SUBF接受/发送缓冲器while(TI==0);TI=0;Flag=0;

154串口中断程序voidser_int(void)interrupt4using1if(RI==1)//RI接受中断标志(RI=0;〃清除RI接受中断标志ReData=SBUF;〃SUBF接受/发送缓冲器SenData=ReDala;Flag=l;RXD-U短路帽10KPL23O3DSRNDCDNCTSNSHID.NEE.CLKEEDATADTR.NRTS.N\T)D232PLL.ItSTGND_PLLVDD_PLLLDMODEIWMODERESETGNDA3VDD_3\3J-Raa/rxd-urr~RXD:RXDR:

1551.通过本例程了解串口的基本原理及使用,理解并掌握对串口进行初始化*2.请使用串口调试助手(Baud4800、数据位8、停止位1、效验位无)做为上位机(发送)数据和(接受)数据,*请在字符串输入框中输入字符和数字(HEX),按发送按钮。观看接受窗显示情况.也可以用自动循环发送。*3.试验RS485需要用RS232转485的转换头。开板A对485转换头AB对应485转换头B.接法大家可以参考原理图。*请学员认真消化本例程,学会用C语言操作串口和485的实现*/include#includesbitRS485E=P3A7;〃定义485的使能脚bitSendFlag;unsignedintReData,SenData;延时程序voiddelay(unsignedchari)(unsignedcharj;for(i;i>0;i-)for(j=200;j>0;j-);voidmain(void){SCON=0x50;//REN=1允许串行接受状态,串口工作模式1TMODI=0x20;〃定时器工作方式2PCONI=0x80;TH1=0xFD;//baud*2/*reloadvalue19200^数据位8、停止位1。效验位无(11.0592)//TH1=0xF3;〃//baud*2/*波特率4800、数据位8、停止位!<,效验位无(12M)TL1=0xF3;TRI=1;

156ES=I;〃开串口中断EA=1;〃开总中断//IE=0x0;while(l)if(SendFlag==l)〃max485(半双工通信)RE/DE定义RE=0为接受状态DE=1为发送状态(参考MAX485芯片管脚){RS485E=1;//RS5485E=0为接收状态RS5485E=1为发送状态SBUF=SenData;〃发送数据delay(50);)elseRS485E=0;〃接收状态)串口中断程序voidser_int(void)interrupt4using1(if(TI==1)(TI=0;SendFlag=0;if(RI==1)//RI接受中断标志(RI=0;〃清除RI接受中断标志ReData=SBUF;//SUBF接受/发送缓冲器SenData=ReData;SendFlag=1;

157RS485通信试验(接收与发送)1.通过本例程了解串口的基本原理及使用,理解并掌握对串口进行初始化2.Baud19200、数据位8、停止位1、效验位无两台485设备a机发b机收*说明:东使用两个带有485功能硬件设备(例如:两个带485的开发箱等)每个设备都有信号端A和B,通过导线**分别连接两台设备A连AB连B排线连接参考图片**现象:按A机的独立按钮在B机的LED等显示其状态对应位的LED灯亮*注意:18B20请卸F,否则会影响RS485*请学员认真消化本例程,学会用C语言操作串口和485的实现简单收发功能*include#includesbitRS485E=P3A7;〃定义485的使能脚延时程序voiddelay(unsignedchari)

158unsignedcharj;for(i;i>0;i-)for(j=200;j>0;j-);voidmain(void){SCON=0x50;〃REN=1允许串行接受状态,串口工作模式1TMODI=0x20;〃定时器工作方式2PCONI=0x80;TH1=OxFD;//baud*2/*reloadvalue19200>数据位8、停止位1。效验位TRI=1;ES=1;〃开串口中断EA=1;〃开总中断while(l)if(P0!=0XFF)//max485(半双工通信)RE/DE定义RE=0为接受状态DE=1为发送状态(参考MAX485芯片管脚)RS485E=1;为发送状态SBUF=P0;delay(40);//〃发送数据RS5485E=0为接收状态RS5485E=1*标题:RS485通信试验(接收与发送)1.通过本例程了解串口的基本原理及使用,理解并掌握对串口进行初始化

159*2.Baud19200、数据位8、停止位1、效验位无两台485设备a机发b机收*说明:**使用两个带有485功能硬件设备(例如:两个带485的开发箱等)每个设备都有信号端A和B,通过导线**分别连接两台设备A连AB连B排线连接参考图片**现象:按A机的独立按钮在B机的LED等显示其状态对应位的LED灯亮**注意:18B20请卸下,否则会影响RS485❖*请学员认真消化本例程,学会用c语言操作串口和485的实现简单收发功能*include#includesbitRS485E=P3A7;〃定义485的使能脚unsignedintReData;延时程序voiddelay(unsignedchari)(unsignedcharj;for(i;i>0;i-)for(j=200;j>0;j-);voidmain(void){SCON=0x50;//REN=1允许串行接受状态,串口工作模式1TMODI=0x20;〃定时器工作方式2PCONI=0x80;

160TH1=0xFD;//baud连接方法:JP10(P0)与J12JP9(P3)与JP5用8PIN排线连接P3.2和P3.3外部中断输入由两个四位数码管分别显示计数2/*reloadvalue19200、数据位8、停止位1。效验位无TRI=1;ES=1;〃开串口中断EA=1;〃开总中断while(l){P0=Oxff;P0=ReData;//max485(半双工通信)RE/DE定义RE=0为接受状态DE=1为发送状态(参考MAX485芯片管脚)RS485E=0;〃RS5485E=0为接收状态RS5485E=1为发送状态delay(20);串口中断程序voidser_int(void)interrupt4using1if(RI==1)//RI接受中断标志(RI=0;〃清除RI接受中断标志ReDaia=SBUF;//SUBF接受/发送缓冲器*标题:试验数码管上显示外部中断计数注意:P3.2接红外接收头,请把J1断开,以免影响P3.2中断效果*请学员认真消化本例程,用单片机脚直接控制数码管和外部中断使用*#include#includesbitLS138A=P2A2;sbitLS138B=P2A3;

161sbitLS138C=P2A4;unsignedintLedNumVal_l,LedNumVal_2,LedOut[8];〃此表为LED的字模,共阴数码管0・9-unsignedcharcodeDisp_Tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};voiddelay(unsignedinti)(charj;for(i;i>0;i-)for(j=200;j>0;j-);voidmain(void){unsignedchari;PO=Oxff;Pl=0xff;P2=0xff;//IT0=0;〃低电平触发IT0=l;〃下降沿触发EX0=l;//IT1=0;〃低电平触发IT1=1;〃下降沿触发EX1=1;EA=1;while(l){LedOut[0]=Disp_Tab[LedNumVal_l%10000/1000];LedOutf1]=Disp_Tab[LedNumVal_1%1000/100]10x80;//0x7f小数点LedOut[2]=Disp^Tab[LedNumVal_1%100/10];LedOut[3]=Disp_Tab[LedNumVal_l%10];LedOut[4]=Disp_Tab[LedNumVal_2%10000/1000];LedOut[5]=Disp_Tab[LedNumVal_2%1000/100];LedOut[6]=Disp_Tab[LedNumVal_2%100/10];LedOut[7]=Disp_Tab[LedNumVal_2%10];

162for(i=0;i<8;i++){PO=LedOut[i];switch(i)(case0:LS138A=0;LS138B=O;LS138C=0;break;case1:LS138A=1;LS138B=O;LS138C=0;break;case2:LS138A=0;LS138B=1;LS138C=0;break;case3:LS138A=1;LS138B=1;LS138C=0;break;case4:LS138A=0;LS138B=O;LS138C=1;break;case5:LS138A=1;LS138B=O;LS138C=1;break;case6:LS138A=0;LS138B=1;LS138C=1;break;case7:LS138A=1;LS138B=1;LS138C=1;break;)delay(150);*INTO中断函数*voidcounterO(void)interrupt0using1EXO=O;LedNumVal_1++;EXO=1;*INTI中断函数*voidcounter1(void)interrupt2using2EX1=0;LedNumVal_2++;EX1=1;*标题:试验数码管上显示定时器中断计数

163*连接方法:JP10(P0)与J12用8PIN排线连接❖*请学员认真消化本例程,用单片机脚直接控制数码管和定时器中断使用*号#include#includesbitLS138A=P2A2;〃定义138管脚sbitLS138B=P2A3;sbitLS138C=P2A4;unsignedintLedNumVal,LedOut[8];//此表为LED的字模〃0123456789AbcdE-LPUHidden_(20)unsignedcharcodeDisp_Tab[]={~0xC0,〜0xF9,~0xA4,〜OxBO,〜0x99,~0x92,~0x82,〜0xF8,~0x80,〜0x90,~0x88,~0x83,〜0xC6产OxA1,~0x86广0xbf,~0xc7广0x8c,~0xc1,~Oxff,~0xf7);purpose:系统初始化voidsystem_Ini()(TMOD=0x00;〃选择模式013位计数最大计数8192//TH0=0xlC;//12.000M预装初值//TL0=0xl8;TH0=(8192-1000)/32;TL0=(8192-1000)%32;IE=0x8A;〃中断TRO=1;〃开启TO定时器voiddelay(unsignedinti)(charj;

164for(i;i>0;i-)for(j=200;j>0;j-);)/********丰函数****/main()system_Ini()while(l)unsignedchari;LedOut[0]=Disp_Tab[LedNumVal%10000/1000];LedOut[1]=Disp_Tab[LedNumVal%1000/100];LedOut[2]=Disp_Tab[LedNumVal%100/10];LedOut[3]=Disp_Tab[LedNumVal%10];for(i=0;i<8;i++){PO=LedOut[i];switch(i)(caseO:LS138A=O;LS138B=O;LS138C=O;break;case1:LS138A=1;LS138B=O;LS138C=0;break;case2:LS138A=0;LS138B=1;LS138C=0;break;case3:LS138A=1;LS138B=1;LS138C=0;break;case4:LS138A=0;LS138B=O;LS138C=1;break;case5:LS138A=1;LS138B=O;LS138C=1;break;case6:LS138A=0;LS138B=1;LS138C=1;break;case7:LS138A=1;LS138B=1;LS138C=1;break;)delay(150);)

165[TO(1ms)中断]中断voidTOzd(void)interrupt1〃3定时器1的中断号1定时器0的中断号0外部中断12外部中断24串口中断

166//THO=OxFc;//12.000//TL0=0xl8;THO=(8192-1000)/32;TL0=(8192-1000)%32;LedNumVal++;)*标题:试验定时中断1使蜂铃器发声(C语言)*连接方法:P1.5蜂铃器**请学员认真消化本例程,熟练定时中断1的编程*东/#include#includesbitBeep=Pl八5;unsignedintUSE_C;主函数voidmain()TMOD=OxlO;TH0=0x4C;TLO=OxOO;EA=1;ET1=1;TR1=1;〃定时/计数器1工作于方式1〃定时器初始值//50ms定时常数〃开总中断〃允许定时/计数器1中断〃启动定时/计数器1中断while(l);

167中断服务函数voidTime1(void)interrupt3//using0{THl=0x4c;〃50ms定时TLl=0x00;if(++USE_C==20)〃每隔1秒发声{Beep=-Beep;USE_C=0;标题计数器中断实验*实验说明:用单片机产生的脉冲波,作为T0外部事件的脉冲输入,对于HC68OOEM3的型**号,可以自带的脉冲发生器作为TO外部事件的脉冲输入**连接方法:参开图片或视频**请学员认真消化本例程,掌握定时器/计数器中断使用*#include#includesbitLED=P0A0;sbitPulse=PlA0;voidTimerO()interrupt1(TH0=0Xa0;〃给计数器装初值TLO=0X00;LED=-LED;//LED灯取反

168voidmain()(PO=OXFF;TMOD=0X05;〃选用计数模式和选用模式1TH0=OXaO;〃给计数器装初值TL0=0x00;EA=1;〃开发所有中断ETO=1;〃开启定时器TO中断TR0=1;〃启动TO定时器while(l)(Pulse-0;_nop_();〃空指令函数用于产生方波_nop_();_nop_();_nop_();Pulse=1;_nop_0;_nop_();_nop_();_nop_();*标题:试验1位数码管上显示外部中断计数*连接方法:P0与JP3P3与JP5用8PIN排线连接P3.2外部中断输入**请学员认真消化本例程,用单片机脚直接控制数码管和外部中断使用**/#include#includeunsignedintLedNumVal;//此表为LED的字模〃0123456789AbcdE-LPUHidden_(20)unsignedcharcodeDisp_Tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xAl,0x86,0xbf,0xc7,0x8c,0xc1,Oxff,0xf7};

169*主程序*voidmain(void)//IT0=0;〃低电平触发IT0=l;〃下降沿触发EA=1;EX0=l;while(l)PO=Disp_Tab[LedNumVal%10];

170*INTO中断函数*voidcounter(void)interrupt0(EX0=0;LedNumVal++;〃中断计数EXO=1;)*标题:试验4位数码管上显示外部中断计数*连接方法:P0与J12P3与JP5用8PIN排线连接P3.2外部中断输入JP15和JP16用8个短路冒连上率*请学员认真消化本例程,用单片机通过译码器控制数码管和外部中断使用**/#include#includesbitLS138A=P2A2;sbitLS138B=P2A3;sbitLS138C=P2A4;unsignedintLedNumVal=0,LedOut[8];//此表为LED的字模〃0123456789AbcdE-LPUHidden_(20)unsignedcharcodeDisp_Tab[]={~0xC0,~0xF9,〜0xA4,~0xB0,~0x99,~0x92,~0x82,~0xF8,~0x80,~0x90产0x88产0x83,~0xC6,~0xA1,~0x86,~0xbf,〜0xc7,~0x8c,~0xc1,~Oxff,~0xf7};voiddelay(unsignedinti)charj;for(i;i>0;i-)for(j=200;j>0;j-);

171displayO{unsignedchari;LedOut[0]=Disp_Tab[LedNumVal%10000/1000];LedOut[l]=Disp_Tab[LedNumVal%1000/100]&0x7f;LedOut[2]=Disp_Tab[LedNumVal%100/10];LedOut[3]=Disp_Tab[LedNumVal%10];for(i=0;i<8;i++){PO=LedOut[i];switch(i)(case0:LS138A=0;LS138B=O;LS138C=O;break;casel:LS138A=l;LS138B=0;LS138C=0;break;case2:LS138A=0;LS138B=1;LS138C=0;break;case3:LS138A=1;LS138B=1;LS138C=0;break;case4:LS138A=0;LS138B=O;LS138C=1;break;case5:LS138A=l;LS138B=0;LS138C=I;break;case6:LS138A=0;LS138B=1;LS138C=1;break;case7:LS138A=1;LS138B=1;LS138C=1;break;)delay(150);*主程序

172voidmain(void)PO=Oxff;Pl=Oxff;P2=Oxff;//IT0=0;〃低电平触发ITO=1;〃下降沿触发EA=1;EXO=1;while(l)displayO;*INTO中断函数*voidcounter(void)interrupt0//unsignedcharx;EX0=0;LedNumVal++;〃中断计数EXO=1;JP9187654321JP8rPIO1WPH2P1.0VCCPl.lPOOPl.2P0.1P1.3P0.2Pl.4P0.3Pl.5P0.4Pl.6P0.5Pl.7P0.6RSTVPDP0.7P30/RxDEA/V^pP3.1/TxDALEPROGP32.INT0PSENP3.3/INT1P2.7P3.4/T0P2.6P3.5/T1P2.5P36WRP2.4P37/RDP23XTAL2P2.2XTAL1P2.1rxMTfcdon40VCC39POO-JP10JPUWP12338P01jWP13437P02;士P14536P03:父P15635P04;MP16734P05上二P17833P06:RSI932P07;rRXD10:TXD1131VCC口30WP32/CS112WP33/CS2132928LCDE.;P341427RD二WP351526WR1士P361625P242WP371724P23:XT21823P22:XT11922SDA士GND2021SCL、CPU

173PS2读电脑键盘实验*连接方法:P0口与J12连接拔开跳线帽用杜邦线连接左针与P3.2右针与P1.6连接*使用方法:用外部中断读取PS2键盘的数据并在数码管上显示,此程序只处理数字键盘。*其余按键功能用户可以自行扩展,为了程序简单便于阅读和学习,程序中没有使用奇偶校验,*校验程序请自行添加。**?■#includesbitLS138A=P2A2;sbitLS138B=P2A3;sbitLS138C=P2A4;sbitBEEP=PlA5;sbitKey_Data=Pl八6;〃定义犍盘引脚sbitKey_CLK=P3A2;〃使用外部中断函数声明*/voidDecode(unsignedcharScanCode);〃解码子程序voiddelay(unsignedinti);〃函数声名voiddisplayO;//此表为LED的字模AbcdEunsignedchar//0123456789voidbeep();LPUHidden_(20)codeDisp_Tab[]{~0xC0,〜0xF9,~0xA4,〜OxBO,〜0x99,~0x92,~0x82,~0xF8,~0x80,~0x90,~0x88,~0x83,~0xC6产OxA1,~0x86,~0xbf,~0xc7,~0x8c,~0xc1,-Oxff,~0xf7};

174bitBF;unsignedcharKeyV;*/unsignedcharLedNumValintKEY_val,IntNum;主函数voidmain()IT1=0;EA=1;EX0=1;whiled)(if(BF)(if(KeyV!=240)beep();Decode(KeyV);//解码)elseEA=1;〃开中断displayO;〃设外部中断1为低电平触发〃外部中断开〃开中断

175*/〃因键盘数据是低>>高,结合上一句所以右移一位〃当键盘数据线为1时到最高位〃等待PS/2CLK拉高外部中断读入信息voidKeyboard_out(void)interrupt0(if((IntNum>0)&&(IntNum<9))(KeyV=KeyV»1;if(Key_Data)KeyV=KeyVI0x80;)IntNum++;while(!Key_CLK);

176if(InlNum>10)(IntNum=0;一次接收BF=1;EA=0;〃当中断11次后表示一帧数据收完,清变量准备下〃标识有字符输入完了〃关中断等显示完后再开中断解码信息*/voidDecode(unsignedcharScanCode)〃注意:如SH1FT+G为I2H34HFOH34HFOH12H,也就是说shift的通码+G的通码+shift的断码+G的断码switch(ScanCode)(case0x70://当收到OxFO,Key_UP置1表示断码开始KEY_val=0;break;case0x69:KEY_val=1;break;case0x72:KEY_val=2;break;case0x7A:KEY_val=3;break;case0x6B:KEY_val=4;break;case0x73:KEY_val=5;break;case0x74:KEY_val=6;break;case0x6c:KEY_val=7;break;case0x75:KEY_val=8;break;

177case0x7d:KEY_val=9;break;case0x71://dot=0X7F;break;BF=0;〃标识字符处理完了)*********/voiddisplay()(unsignedchari;unsignedintLedOut[8J;LedNumVal=KEY_val;〃显示键值LedOut[0]=Disp_Tab[KeyV%10000/1000];LedOut[1J=Disp_Tab[KeyV%1000/100];LedOut[2]=Disp_Tab[KeyV%100/10];LedOut[3]=Disp_Tab[KeyV%10];〃显示对应的字符LedOut[4]=Disp_Tab[LedNumVal%10000/1000];〃千位LedOut[5]=Disp_Tab[LedNumVal%1000/100];〃百位带小数点LedOutl6J=Disp_Tab[LedNumVal%100/10];〃十位LedOut[7]=Disp_Tab[LedNumVal%10];〃个位for(i=0;i<8;i++){P0=LedOut[i];switch(i)(case0:LS138A=0;LS138B=O;LS138C=O;break;case1:LS138A=1;LS138B=O;LS138C=0;break;case2:LS138A=0;LS138B=1;LS138C=0;break;case3:LS138A=1;LS138B=1;LS138C=0;break;case4:LS138A=0;LS138B=O;LS138C=1;break;case

1785:LS138A=1;LS138B=0;LS138C=1;break;case6:LS138A=0;LS138B=1;LS138C=1;break;case7:LS138A=1;LS138B=1;LSI38C=1;break;delay(50);)延时程序voiddelay(unsignedinti)charj;for(i;i>0;i-)for(j=200;j>0;j-);蜂铃器发声*********/voidbeep()(unsignedchari;for(i=0;i<255;i++)(delay(5);BEEP=!BEEP;//BEEP取反IBEEP=1;〃关闭蜂鸣器)此接口为USB形的PS2口,目前USB键盘比较普遍。请用USB口电脑键盘JP7*目的:熟悉74HC573的LE输出使能控制*请学员认真消化本例程,用573锁存器控制和单片机脚直接位选控制(非译码器控制)数码管*

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
关闭