单片机C例程大全.docx

上传人:文*** 文档编号:83245019 上传时间:2023-03-29 格式:DOCX 页数:179 大小:1.16MB
返回 下载 相关 举报
单片机C例程大全.docx_第1页
第1页 / 共179页
单片机C例程大全.docx_第2页
第2页 / 共179页
点击查看更多>>
资源描述

《单片机C例程大全.docx》由会员分享,可在线阅读,更多相关《单片机C例程大全.docx(179页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、*LED闪烁的简单试验*连接方法:JP11(P2)和JP1用8PIN排线连接起来#include 此文件中定义了51的一些特殊功能寄存器void delay(unsigned int i);声明延时函数 main()(P2=0x00;置P0口为低电平delay(600);/调用延时程序P2= Oxff;置P0口为高电平delay(600);/调用延时程序I/*延时函数*/void delay(unsigned int i)(unsigned char j;for(i; i 0; i-)循环600*255次 for(j =255;j0;j-););二进制加法试验;p2口八个灯作二进制加法。理解二

2、进值的计算*;硬件连接:p2口8路指示灯描述:p2口八个灯作二进制加法。理解二进值的计算*连接方法:JP11(P2)和JP1(LED灯)用8PIN排线连接起来*#include void delay(unsigned int i);声明延时函数 main()(unsigned char Num = Oxff;while (1)P2= Num;delay(lOOO);延时函数Num;)unsigned char j;for(i; i0; i-)for(j =255;j0;j-);LED闪烁的简单试验延时实现p2口 LED流水灯效果(用循环移位指令)连接方法:JP1I(P2)和JP1(LED灯)用

3、8PIN排线连接起来#inckKie此文件中定义了51的一些特殊功能寄存器#include void delayms(unsigned char ms)/延时子程序unsigned char i; while(ms)for(i =0; i 120; i+);)main()unsigned char LED;LED = Oxfe;/Oxfe=11111110 while(l)P2= LED;delayms(250);LED = LED 1;循环右移1位,点亮下一个LED为左移位if(P2=0x00)LED = Oxfe;/Oxfe =11111110)描述:跑马灯程序连接方法:JP11(P2)和

4、JP1(LED灯)用8PIN排线连接起来#include#include *延时子程序void delayms(unsigned char ms)unsigned char i;while(ms-)(for(i =0;i120; i+);*主程序*main()(unsigned char LED;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灯

5、)用8PIN排线连接起来ORGOOOOhAJMPmainORGOOObhAJMPint_timerORG0030hmain: MOV tmod,#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);

6、重设10MS 定时。DJNZ30h,llMOV30h,#10DJNZMOVCPL11: RETIEND31h,ll31h,#6 p2.0;软件计数;10*6*10MS=600MS 时间到,Pl.l 取反;中断返回*跑马灯实验延时实现LED流水灯效果 p2口八个灯作跑马灯。采用了寄存器存中间数连接方法:JPH (P2)和JP1(LED灯)用8PIN排线连接起来#include void delay(unsigned int i);声明延时函数 char LED;main()unsigned char i; while (1)LED = Oxfe;for (i =0;i 8; i+)P2= LED

7、;delay(500);LED = LED 1;LED = LED I 0x01; if (LED =0x7f)/左移/移位后,后面的位为高电平break;提前退出FOR循环for (i =0;i 0; i-)for(j =255;j0;j-);按键状态显示试验/*连接方法:连接JP10(P0)与JP5(按钮接口)JP11(P2)与 JP1(LED 接口)/*按键则点亮LED灯,8路指示灯接p0口#include #include sbit BEEP = P1A5;/将蜂鸣器定义在Pl.5端口上sbit RELAY = P1A4;将继电器定义在Pl.4端口上sbit KI = P0A0; sb

8、it K2=0x80Al;/P0和0X80表示的是同一个地址sbit sbitK3= P0A2; sbitK5= P0A4; sbitK4= Ox8OA3;K6= Ox8OA5;if(K2 != 1) I if(!K3) if(K4) if(!Kl&!K2) if(!K5 ) if(!K6)RELAY = 0; else通过了反相器(HC6800试用)void beep();main()while(l)P2= Oxff;if(Kl =0) P2= Oxfe;P2= Oxfd;P2= Oxfb;P2=0xf7; P2=0x00;/2个按钮同时按下 P28个灯全部点亮beep。;喇叭发声RELAY

9、 =1;void beep()(unsigned char i, j;for (i=0;i100;i+)/BEEP取反需要产生方波关闭蜂鸣器BEEP=!BEEP;for (j =0; j250; j+)_nop_();)BEEP=1;按键控制程序* 连接方法:JP10(P2)与JP1(LED灯)连接,* JP11(P0)与JP5(按键接口)连接* 开始点亮PILED按P01 LED向右移一位* 按POOLED向左移一位* 连续按动按钮LED会不停的左移或右移#include #include unsigned char scan_key();void proc_key(unsigned cha

10、r key_v);void delayms(unsigned char ms);sbit KI = P0A0;对应按钮 KI sbit K2= P0Al;对应按钮 K2 main()unsigned char key_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);)unsigned char scan_key()(unsigned c

11、har key_s;key_s =0x00;key_s 1= K2;key_s =1;key_s 1= KI;return key_s;)void proc_key(unsigned char key_v)(if(key_v &0x01)=0)(P2=_cror_(P2,l);else if(key_v &0x02)=0)P2=_crol_(P2,1);)void delayms(unsigned char ms)/延时子程序(unsigned char i;while(ms)(for(i =0; i 120; i+);)* 按键控制程序* 连接方法:JP10(P2)与JP1(LED灯)连接,

12、* JP11(P0)与JP5(按键接口)连接“* 开始点亮PILED;键盘P2.5控制的小灯P0.0。P2.7对应K1ORG0000HAJMP MAINORG0030H;检测按键MAIN: MOV C,p0.0MOV P2.O.C;根据按键决定输出。AJMP MAIN;循环END,*;P0.0小键盘按下P2.0亮再按0.5秒灭。;连接方法:连接方法:JP10(P2)与JP1(LED灯)连接,;JP11(P0)与JP5(按键接口)连接*,*;FLAG 20H EXPRESS THE 0.5S TIME ARRIVED.;FLAG 21H EXPRESS THE LED LIGHTED.ORG O

13、OOOh AJMP mainO ORG 0030hMAINO:CLR21H;清标志main:MOVC,P0.0;检测按键JC mainMOVR6,#200;设定延时MOV R7,#200TTO:JNB21H,TT2;检测灯状态tt:MOV C,P0.0;检测按键JNCttlJNB 20H,MAIN ;检测时间标志CLR20HAJMP TT3TT2:MOV c,P0.0;检测按键JNC tt2TT3:CPL21H ;改变灯状态CPLp2.0AJMP mainTT1:NOP;延时但不是子程序NOPNOPNOPNOPNOPDJNZ R6,TTMOV R6,#200DJNZ R7,TTMOV R7,#

14、200SETB 20H AJMP TT END ,*,按键POO每按下次加一,通过的P2端口显示;连接方法:JP1O (P0)与JP1(LED灯)连接,;JP11(P2)与JP5(按键接口)连接*;显示出其的二进制计数值。org OOOOh ajmploop org 0030h mov a,#Offh loop:jnb pO.O,tt jmp loop tt:mov p2,a;全不亮call delay;延时。deca;每次减一。jmploopdelay: movr5,#50;延时。dl:movr6,#40d2:movr7,#20djnzr7,$djnzr6,d2djnzr5,dlret en

15、d ,*;键盘控制的花样彩灯。*;连接方法:JP1O (P2)与JP1(LED灯)连接,;JP11(P0)与JP5(按键接口)连接;按p24,p27,p26,p27,p2口8个灯分别作移动右移动,停止右移左移* ,*;亮灯控制程序; ,*,ORG0000HUMP START org 0030hSTART:MOV P3,#OFFHJNBP0.7,FUN0;检测按键JNBPO.4,FUN1JNBPO.5,FUN2JNBPO.6,FUN3;关闭按纽AJMPSTARTFUNO: LCALLDL10MS;消岸抖动JBPO.7,STARTWAITLO:JNBP07WAITL0;等待键释放FUNO1: LC

16、ALLFUNOOAJMPSTARTFUN1: 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;左移子程序FUN

17、OOO: MOVP2,ALCALLDLO5SJNBACC.7,OUT;检测是否结束RLAAJMPFUNOOOOUT: RETFUN11: MOVA,#O7FH;右移子程序FUN111: MOVP2,ALCALLDLO5SJNBACC.O,OUT;检测是否结束RRAAJMPFUN1UFUN22:MOVA,#0AAH ;间隔显示子程序MOVP2,ALCALLDL30SCPLAMOVP2,ALCALLDL30SRET,*,;延时程序;,* ,DL512:MOVR2,#0FFH;延时500USLOOP1: DJNZR2,LOOP1RETDL10MS:MOVR3,#14H;延时10MSLOOP2: LC

18、ALLDL512DJNZR3,LOOP2RETDL05S:MOVR4,#0AH;延时100MSLOOP3: LCALLDLIOMSDJNZR4,LOOP3RETDL30S:MOVR5,#O3H;延时300MS.LOOP4:LCALLDL05SDJNZR5,LOOP4RETend此为蚀立按键接口文件名C51音乐程序(八月桂花)功能通过单片机演奏音乐#include #include 本例采用89C52,晶振为1I.0592MHZ关于如何编制音乐代码,其实十分简单,各位可以看以下代码.频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;所以拿出谱子,试探编吧!sbit Beep = P1A

19、5;unsigned char n=0;/n为节拍常数变量unsigned char code music_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,0x

20、20,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,O

21、x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30, OxlC ,0x10,0x20,0x10, Ox IC, Ox 10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15, Ox IF,0x05,0x20,0x10, Ox IC,0x10,0x20,0x20,0x26,0x20, Ox2B ,0x20,0x30,0x20, Ox2B ,0x40,0x20,0x30, OxlC,OxlO,0x18,0x20,0x15,0x20, Ox IC,0x20,0x20,0x20,0x26

22、,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30, Ox IC, Ox 10,0x18,0x40, OxlC ,0x20,0x20,0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00 void intO() interrupt 1采用中断0控制节拍 TH0=0xd8;TLO=Oxef;n-;void delay (unsigned char m)控制频率延时(unsigned i=3*m;while(-i);)void del

23、ayms(unsigned char a)豪秒延时子程序(while(-a);采用while(-a)不要采用while(a);各位可编译一下看看汇编结果就知道了!void main() unsigned char p,m;/m为频率常数变量 unsigned char i=0;TMOD&=OxOf;TMODI=0x01;TH0=0xd8;TL0=0xef;IE=0x82;play:while(l)a: p=music_tabi;if(p=OxOO) i=0, delayms(lOOO); goto play;如果碰到结束符,延时1秒,回到开始再来一遍else if(p=Oxff) i=i+l;

24、delayms(100),TR0=0; goto a;若碰到休止符,延时100ms,继续取下一音符elsem=music_tabi+, n=music_tabi+;取频率常数和节拍常数TRO=1;开定时器1while(n!=O) Beep=Beep,delay(m);等待节拍完成,通过PI 口输出音频(可多声道哦!) TR0=0;关定时器1ORGAJMP0000HMAIN;伪指令,指定程序从8000H开始存放;跳转指令,程序跳转到MAIN处ORG01 OOH;伪指令,指定以下程序从8100H开始存放MAIN:MOVSP,#60;给堆栈指针赋初值LOOP:ACALL MUSICAJMPLOOP;

25、蜂鸣器输出声响子程序,;控制音长;控制音调MUSIC:MOV R5,#60HMIC:CPLP1.5ACALL DELAY5MSDJNZ R5,MIC RET,*;延时子程序DELAY5MS:MOV R7,#3HDELAYO:MOVDELAY 1:DJNZR6,#40HR6,DELAY1DJNZ R7,DELAY0 RETENDorg OOOOh ajmpmain org 0030hmain:MOV R2,#08HDLV1: MOV R3,#0FAHDLV2: CPLP1.5LCALL DELAY 1;延时DJNZ R3,DLV2DJNZ R2,DLV1MOV R2,#10H;改变循环初值DLV

26、3: MOV R3,#0FAHDLV4: CPLP1.5LCALL DELAY2DJNZ R3,DLV4DJNZ R2,DLV3ajmpmaindelay 1:mov r7,#OOhLLA: DJNZ R7,LLA retdelay2:mov r7,#128LLB:DJNZ R7,LLBretendORG JMPORG JMPORGMAIN: MOVMOV SETBSETBSETBSTARTO: ;clrMOVNEXT: MOVMOVMOVCMOV JZ ANL MOVMOV SWAPANL JNZ CLRJMPSING: DECMOV RL MOV MOVCMOV MOVMOV RL INC

27、 MOVCMOV MOV SETBDI:CALLINC0000H MAINOOOBH INTTO0100HSP,#60H TMOD,#01HETOEATROPl.530H,#00H A,30HDPTR,#TABLE A,A+DPTR R2,AENDDA,#0FHR5,A A,R2 A A,#0FH SING TRO DIA 22H,A ADPTR,#TABLE1 A,A+DPTR TH0,A 21H,A A,22HAAA,A+DPTR TL0,A 20H,ATRO DELAY30HJMPNEXTENDD: CLRTROJMPSTARTOINTT0:PUSHPSWPUSHACCMOVTL0,20

28、HMOVTH0,21HCPLPl.5POPACCPOPPSWRETIDELAY: MOVR7,#02DELAY0: MOVR4,#187DELAY 1: MOVR3,#248DJNZR3,$DJNZR4,DELAY 1DJNZR7,DELAY0DJNZR5,DELAYRETTABLE: DB 82H,01 H,81 H,94H,84H,0B4H,0A4H,04HDB 82H,01H,81 H,94H,84H,0C4H,0B4H,04HDB 82H,01 H,81 H,0F4H,0D4H,0B4H,0A4H,94HDB 0E2H,01H,OE1 H,0D4H,0B4H,0C4H,0B4H,04H

29、DB 82H,01 H,81 H,94H,84H,0B4H,0A4H,04HDB 82H,01 H,81 H,94H,84H,0C4H,0B4H,04HDB 82H.01 H,81 H,0F4H,0D4H,0B4H,0A4H,94HDB 0E2H,01H,0El H,0D4H,0B4H,0C4H,0B4H,04H/TABLEI: DW64260,64400,64524,64580,64684,64777,64820,64898/DW 64968,65030,65058,65110,65157,65178,65217ENDORG 0000HUMP STARTORG000BHINC 20H;中断服

30、务,中断计数器加1MOVTH0,#0D8HMOVTL0,#0EFH;12M晶振,形成10毫秒中断RETISTART:MOVSP,#50HMOVTH0,#0D8HMOVTL0,#0EFHMOVTMOD,#01HMOVIE,#82HMUSICO:NOPMOVDPTR,#DAT;表头地址送DPTRMOV20H,#00H;中断计数器清0MOVB,#00H;表序号清0MUSIC 1:NOPCLRAMOVCA,A+DPTR;查表取代码JZENDO;是OOH,则结束CJNEA,#0FFH,MUSIC5LJMPMUSIC3MUSIC5:NOPMOVR6,AINCDPTRMOVA,BMOVCA,A+DPTR;取

31、节拍代码送R7MOVR7,ASETBTRO;启动计数MUSIC2:NOPCPLPl.5MOVA,R6MOVR3,ALCALLDELMOVA,R7CJNEA,20H,MUSIC2;中断计数器(2OH)=R7否?;不等,则继续循环MOV20H,#00H;等于,则取下一代码INCDPTR;INCBLJMPMUSIC 1MUSIC3:NOPCLRTRO淋止100毫秒MOVR2,#0DHMUSIC4:NOPMOVR3,#0FFHLCALL DELDJNZR2,MUSIC4INCDPTRLIMPMUSIC 1ENDO:NOPMOVR2,#10H ;歌曲结束,延时1秒后继续MUSIC6:MOVR3,#00H

32、LCALL DELDJNZR2,MUSIC6LIMPMUSICODEL:NOPDEL3:MOV R4,#02HDEL4:NOPDJNZR4,DEL4NOPDJNZR3,DEL3RET NOPDAT:db 26h,20h,20h,20h,20h,20h,26h,1 Oh,2Oh,l 0h,20h,80h,26h,20h,30h,20h db 3Oh,2Oh,39h,1 Oh,3Oh,l 0h,30h,80h,26h,20h,20h,20h,20h,20hJ ch,20h db 20h,80h,2bh,20h,26h,20h,20h,20h,2bh,l 0h,26h,l 0h,2bh,80h,26

33、h,20h db 3Oh,2Oh,3Oh,2Oh,39h,l0h,26h,l0h,26h,60h,40h,10h,39h,l0h,26h,20h db 3Oh,2Oh,3Oh,2Oh,39h,l 0h,26h,l 0h,26h,80h,26h,20h,2bh,l Oh,2bh,l Oh db 2bh,2Oh,3Oh,lOh,39h,l0h,26h,lOh,2bh,l0h,2bh,20h,2bh,40h,40h,20h db 20h,l 0h,20h,l Oh,2bh,l Oh,26h,3Oh,3Oh,8Oh,18h,2Oh,18h,20h,26h,20h db 20h,20h,20h,40h,

34、26h,20h,2bh,20h,30h,20h,30h,20h,lch,20h,20h,20h db 2Oh,8Oh,l ch,20h,l ch,2Oh,l ch,20h,30h,20h,30h,60h,39h,1 Oh,3Oh,l Oh db 20h,20h,2bh,l0h,26h,l0h,2bh,l0h,26h,l0h,26h,1 Oh,2bh,lOh,2bh,8Oh db 18h,2Oh,l 8h,20h,26h,20h,20h,20h,20h,60h,26h,l Oh,2bh,2Oh,3Oh,2Oh db 3Oh,2Oh,l ch,20h,20h,20h,20h,80h,26h,20h

35、,30h,1 Oh,3Oh,1 Oh,3Oh,2Oh db 39h,20h,26h,l Oh,2bh,l 0h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,l Oh db 20h,l0h,2bh,l0h,26h,30h,30h,80h,00HEND继电器实验/*连接方法:连接JP11(P2)与JP5(按钮接口)/*按下P2.7继电器P1.4动作#include 必1的端口和各特殊寄存器定义在此文件中sbit RELAY = Pl A4;sbit KI = P2A7;定义继电器时应单片机管脚 /定义按钮对应单片机管脚主函数main()(if (!K1) RELAY =

36、0;按钮按下开启继电器else RELAY =1;/按钮松开关闭继用器*138译码器实验*实验目的:1.了解138译码器工作原理* 2.学会用C语言对138译码器编程控制* 实验现象:LED灯每隔一段时间循环点亮* 接线方法:J15连JP1(参考图片连接)*#include sbit HC138A = P2A2;定义译码器输入端A在P2.2管脚上sbit HC138B = P2A3;定义译码器输入端B在P2.3管脚上sbit HC138C = P2A4;定义译码器输入端C在P2.4管脚h延时函数void delay(unsigned int i)(unsigned char j;for(i;

37、i 0; i-)循环1500*255次CPU在这里执行一段时间for(j =255;j0;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;输入010 delay(1500);/点亮第四个LED灯HC138C =0; HC138B =1;HC138A=1;输入 Oil delay(2000);/

38、点亮第五个LED灯HC138C=1;HC138B =0; HC138A =0;/输入100delay(1500);/点亮第六个LED灯HC138C=1;HC138B = O; HC138A=1;输入101 delay(1500);HC138C= l;HC138B = l;HC138A =0;输入110 delay(1500);HC138C=1; HC138B =1;HC138A=1;输入111 delay(1500);H*Ni tarC L*lav lawi. Xwdont ce74LS138* 标题:试验数码管上如何显示数字(共阳极)* 连接方法:P0与JP3用8PIN排线连接* 请学员认真

39、消化本例程,用单片机脚直接控制数码管#include void delay(unsigned int i); /此表为LED的字模 89 a b函数声名01defunsignedcharcode234567LED7Code=0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71;main()(unsigned int LedNumVal; 定义变量 while(l)/将字模送到P0 口显示LedNumVal+4-;InputsOutputsEnableSelectG1G2,CBAY0Y1Y2Y3Y4Y5Y6Y7XHXXXHHHHHHHHLXXXXHHHHHHHHHLLLL

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁