51单片机基本程序(共15页).doc

上传人:飞****2 文档编号:13833838 上传时间:2022-05-01 格式:DOC 页数:15 大小:40.50KB
返回 下载 相关 举报
51单片机基本程序(共15页).doc_第1页
第1页 / 共15页
51单片机基本程序(共15页).doc_第2页
第2页 / 共15页
点击查看更多>>
资源描述

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

1、精选优质文档-倾情为你奉上1第一位隔一秒闪烁一次#include#define uint unsigned intsbit led1=P10;uint i;uint j;void main() while(1) led1=0; for(i=1000;i0;i-) for(j=110;j0;j-); led1=1; for(i=1000;i0;i-) for(j=110;j0;j-); 2复杂广告灯#include#define uint unsigned int#define uchar unsigned charuchar discode= 0xFE,0xFD,0xFB,0xF7,0xEF,

2、0xDF,0xBF,0x7F,/正向流水灯 0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF,/反向流水灯 0xAA,0x55,0xAA,0x55,0xAA,0x55,0xFF,/隔灯闪烁3次 0xF0,0x0F,0xF0,0x0F,0xFF,/高四盏。低四盏闪烁2次 0x33,0xCC,0x33,0xCC,0x33,0xCC,0xFF/隔两盏闪烁3次 ;void delayms(uint ms) uint i; while(ms-) for(i=0;i120;i+); void main(void) uchar i; P0=0xFF; while(1) for(

3、i=0;i35;i+) P0=discodei; delayms(250); 3拉幕式与闭幕式广告灯#include#define uint unsigned int#define uchar unsigned charuchar discode14=0x18,0x24,0x42,0x81;uchar discode24=0x7E,0x3C,0x18,0x00;void delayms(uint ms) uint i; while(ms-) for(i=0;i120;i+); void main(void) uchar i,j; P0=0xFF; while(1) for(i=0;i4;i+)

4、 j=discode1i; P0=j; delayms(500); j=0xFF; P0=j; delayms(500); for(i=0;i4;i+) j=discode2i; P0=j; delayms(500); 4流水灯跑马灯左移右移#include#define uint unsigned int#define uchar unsigned charvoid delay(uint s) uint i; while(s-) for(i=0;i120;i+); void main(void) uchar i,j; P0=0xFF; while(1) j=0x01; for(i=0;i8;

5、i+) P0=j; delay(500); j=j1; P0=0xFF; delay(1000); j=0x80; for(i=0;i1; 5千位静态显示数字#includesbit SMG_q = P10;/定义数码管阳级控制脚(千位)sbit SMG_b = P11;/定义数码管阳级控制脚(百位)sbit SMG_s = P12;/定义数码管阳级控制脚(十位)sbit SMG_g = P13;/定义数码管阳级控制脚(个位)void main() SMG_q=0; P0=0xF8; while(1); 6,4位数码管同时静态显示#include#define unchar unsigned

6、char#define uint unsigned intsbit SMG_q = P10;/定义数码管阳级控制脚(千位)sbit SMG_b = P11;/定义数码管阳级控制脚(百位)sbit SMG_s = P12;/定义数码管阳级控制脚(十位)sbit SMG_g = P13;/定义数码管阳级控制脚(个位)unchar mun;uchar; code table=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;void delay(uint);void main(void) S

7、MG_q=0; SMG_b=0; SMG_s=0; SMG_g=0;/P1=0xf0;两种都可以实现。while(1) for(mun=0;mun0;i-); 7. 数码管动态显示#include#define uchar unsigned char#define uint unsigned intsbit q = P10;/定义数码管阳级控制脚(千位)sbit b = P11;/定义数码管阳级控制脚(百位)sbit s = P12;/定义数码管阳级控制脚(十位)sbit g = P13;/定义数码管阳级控制脚(个位)/unchar mun;uchar; code table=0xc0,0xf

8、9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;void delay(uint);void main(void) while(1) q=0; P0=table1; delay(500); q=1; b=0; P0=table2; delay(500); b=1; s=0; P0=table3; delay(500); s=1; g=0; P0=table4; delay(500); g=1; void delay(uint s) uint i; while(s-)for(i=120;i0;i-);

9、8.1中断定时器0工作1LED 1s闪烁#include#define uint unsigned int#define uchar unsigned charsbit led1=P10; uchar num;void main() TMOD=0x01; TH0=(65536-45872)/256; TL0=(65536-45872)%256; EA=1; ET0=1; TR0=1; while(1);void T0_time() interrupt 1 TH0=(65536-45872)/256; TL0=(65536-45872)%256; num+; if(num=50) num=0;

10、led1=led1;8.2中断定时器LED,前两位数码管59s循环计数#include#define uchar unsigned char#define uint unsigned intsbit led1=P00;uchar code table=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;void delay(uint);void display(uchar,uchar);uchar num,num1,num2,shi,ge;void main(void) TMOD=0x1

11、1; TH0=(65536-45872)/256; TL0=(65536-45872)%256; TH1=(65536-45872)/256; TL1=(65536-45872)%256; EA=1; ET0=1; ET1=1; TR0=1; TR1=1; while(1) display(shi,ge); void display(uchar shi,uchar ge) P1=0xff; P1=0xfd; P0=tableshi; delay(10); P1=0x00; P1=0xff; P1=0xfe; P0=tablege; delay(10); P1=0x00;void delay(u

12、int s) uint i,j; for(i=s;i0;i-) for(j=110;j0;j-);void T0_time()interrupt 1 TH0=(65536-45872)/256; TL0=(65536-45872)%256; num1+; if(num1=50) num1=0; led1=led1;void T1_time()interrupt 3 TH1=(65536-45872)/256; TL1=(65536-45872)%256; num2+; if(num2=20) num2=0; num+; if(num=60) num=0; shi=num/10; ge=num%

13、10; /灯的亮灭。9.1独立键盘使用#include#define uchar unsigned char#define uint unsigned intsbit k1=P34; /独立键盘,k1 k2 k3 k4分别接,p34.p35 p36 p37.sbit k2=P35;sbit k3=P36;sbit k4=P37;sbit d=P12;sbit c=P13;uchar code table=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;void delayms(uin

14、t);uchar numt0,num;void display(uchar numdis) /显示59s的常用方式 uchar shi,ge; shi=numdis/10; ge=numdis%10; P1=0xff; /控制十位数字 P1=0xfd; P0=tableshi; delayms(10); P1=0x00; P1=0xff; /控制个位数字 P1=0xfe; P0=tablege; delayms(10); P1=0x00;void delayms(uint xms) /简单的延时程序 uint i,j; for(i=xms;i0;i-) for(j=110;j0;j-);voi

15、d init() /初始化函数 TMOD=0x01; TH0=(65536-45872)/256; TL0=(65536-45872)%256; EA=1; ET0=1;void keyscan() /控制键盘的程序 if(k1=0) delayms(10); /去抖动延时if(k1=0) num+; if(num=60) num=0;while(!k1); /等待按键释放 if(k2=0) delayms(10);if(k2=0) if(num=0) num=60;num-;while(!k2); if(k3=0) delayms(10);if(k3=0) num=0;while(!k3);

16、 if(k4=0) delayms(10);if(k4=0) while(!k4);TR0=TR0; void main() /主函数 init(); /1,先初始化数据init() while(1) keyscan(); /键盘选择? display(num); /显示两位数字函数 void T0_time()interrupt 1 /中断服务程序 TH0=(65536-45872)/256; TL0=(65536-45872)%256; numt0+; if(numt0=20) /1s的判断 numt0=0; num+; if(num=60) num=0; 9.2矩阵键盘使用#includ

17、e#define uchar unsigned char#define uint unsigned intuchar code table=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;void delayms(uint xms) uint i,j; for(i=xms;i0;i-) for(j=110;j0;j-);void display(uchar num) P0=tablenum; P1=0xf0;void jianpan() uchar temp,key; P3=0xfe

18、; temp=P3; temp=temp&0xf0; if(temp!=0xfe) /11111高位没有零,(没有键按下) delayms(10); temp=P3; /随时可能有键按下 temp=temp&0xf0; if(temp!=0xfe) temp=P3; switch(temp) case 0xee: key=0; break; case 0xde: key=1; break; case 0xbe: key=2; break; case 0x7e: key=3; break; while(temp!=0xf0) /等待键盘释放 temp=P3; temp=temp&0xf0; di

19、splay(key); P3=0xfd; temp=P3; temp=temp&0xf0; if(temp!=0xfe) /高位没有零,(没有键按下) delayms(10); temp=P3; temp=temp&0xf0; if(temp!=0xfe) temp=P3; switch(temp) case 0xed: key=4; break; case 0xdd: key=5; break; case 0xbd: key=6; break; case 0x7d: key=7; break; while(temp!=0xf0) temp=P3; temp=temp&0xf0; displa

20、y(key); P3=0xfb; temp=P3; temp=temp&0xf0; if(temp!=0xfe) /高位没有零,(没有键按下) delayms(10); temp=P3; temp=temp&0xf0; if(temp!=0xfe) temp=P3; switch(temp) case 0xeb: key=8; break; case 0xdb: key=9; break; case 0xbb: key=10; break; case 0x7b: key=11; break; while(temp!=0xf0) temp=P3; temp=temp&0xf0; display(

21、key); P3=0xf7; temp=P3; temp=temp&0xf0; if(temp!=0xfe) /44444高位没有零,(没有键按下) delayms(10); temp=P3; temp=temp&0xf0; if(temp!=0xfe) temp=P3; switch(temp) case 0xe7: key=12; break; case 0xd7: key=13; break; case 0xb7: key=14; break; case 0x77: key=15; break; while(temp!=0xf0) temp=P3; temp=temp&0xf0; display(key); void main(void) P1=0x0f; /打开数码管选通开关while(1)jianpan(); /扫描键盘程序 专心-专注-专业

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

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

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

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