首页资源分类嵌入式处理器51内核 > 单片机指令

单片机指令

已有 445109个资源

下载专区

上传者其他资源

    文档信息举报收藏

    标    签:单片机

    分    享:

    文档简介

    单片机指令单片机指令

    文档预览

    //点亮一个LED灯: #include sbit A=P1^2; void main(){A=0;} //延时程序: #include sbit D=P1^0; unsigned int a; void main() { a=1000000; while(a--); D=0; a=1000000; while(a--); D=1; } 点亮数码管: #include sbit wei=P2^7;//位锁存器 sbit duan=P2^6;//段寄存器 void main() { wei=1; P0=0xc0;//c0=1100 0000B,第1至6数码管有效 wei=0;//下降沿保持数据 duan=1; P0=0x7f;//7f=0111 1111B,反向abcdefg点亮 duan=0;//下降沿保持数据 } 循环显示“1348143”: 0x3f 0x06 0x5b 0x4f 0x66 0x6d 0x7d 0x07 0x7f 0x6f :0-----9 0x77 0x7c 0x39 0x5e 0x79 0x71 :A-----F #include sbit wei=P2^7; sbit duan=P2^6; unsigned char code b[16]={0x06,0x4f,0x66,0x7f,0x3f,0x06,0x66,0x4f};//编码 unsigned int num; void delay(unsigned int a);//延时程序 void main() { wei=1;//位锁存 P0=0xc0; wei=0; for(num=0;num<10;num++) { duan=1;//段锁存 P0=b[num]; delay(100); duan=0; } } void delay(unsigned int a) { unsigned int b,c; for(b=a;b>0;b--) for(c=750;c>0;c--); } 跑马灯: #include #include #define uint unsigned int #define uchar unsigned char uchar temp; void delay(uint a); void main() {temp=0xfe; P1=temp; while(1) {delay(25); temp=_crol_(temp,1); P1=temp;} } void delay(uint a) { uint b,c; for(b=a;b>0;b--) for(c=110;c>0;c--); } 跑马灯+数码管: #include #include #define uint unsigned int #define uchar unsigned char uchar temp; sbit wei=P2^7; sbit duan=P2^6; uchar code code1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d}; uchar code code2[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; uint num1,num2; void delay(uint a) { uint b,c; for(b=a;b>0;b--) for(c=100;c>0;c--); } void main() { num1=num2=0; temp=0xfe; P1=temp; wei=1; P0=0xfe; wei=0; duan=1; P0=0x3f; duan=0; while(1) { delay(500); temp=_crol_(temp,1); P1=temp; wei=1; P0=code2[num2]; num2++; wei=0; duan=1; P0=code1[num1]; num1++; duan=0; if(num1==6)num1=0; if(num2==6)num2=0; } } 计数器0-100: #include #define uint unsigned int #define uchar unsigned char uint a; uchar numdu,temp,bai,shi,ge,aa; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; sbit duan=P2^6; sbit wei=P2^7; void display(uchar bai,uchar shi,uchar ge); void delay(uint a); void init(); void main() { init(); temp=0; numdu=0; while(1) { if(aa==20) { aa=0; temp++; if(temp==100)temp=0; bai=temp/100; shi=temp%100/10; ge=temp%10; } display(bai,shi,ge); } } void init() { wei=0; duan=0; temp=0; TMOD=0x01; TH0=(65536-50000)/256; TH1=(65536-50000)%256; EA=1;ET0=1;TR0=1; } void timer0() interrupt 1 { TH0=(65536-50000)/256; TH1=(65536-50000)%256; aa++; } void display(uchar bai,uchar shi,uchar ge) { duan=1; P0=table[bai];// duan=0;//送百位数 P0=0xff;//消隐 wei=1; P0=0xfe;//位选通 wei=0; delay(1); duan=1; P0=table[shi]; duan=0; P0=0xff; wei=1; P0=0xfd; wei=0; delay(1); duan=1; P0=table[ge]; duan=0; P0=0xff; wei=1; P0=0xfb; wei=0; delay(1); } void delay(uint a) { uint b,c; for(b=a;b>0;b--) for(c=100;c>0;c--); } 用动态扫描方法和定时器1在数码管上显示出从765432开始以1/10秒的速度往下递减直至765398并保持显示此数,与此同时利用定时器0以500MS速度进行流水灯从上至下移动,当数码管上树减到停止时,实验板上流水灯也停止然后全部开始闪烁,3秒后(用T0计时)流水灯全部关闭,数码管上显示“hello”到此保持住。 #include #include #define uint unsigned int #define uchar unsigned char uchar temp,t0,t1,bai,shi,ge,flag,flag1; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x76,0x79,0x38,0x3f,0}; uint num,a; sbit duan=P2^6; sbit wei=P2^7; void delay(uint a) { uint b,c; for(b=a;b>0;b--) for(c=100;c>0;c--); } void init() { num=432; temp=0xfe; P1=temp; TMOD=0x11; TH0=(65536-50000)/256; TL0=(65536-50000)%256; TH1=(65536-50000)/256; TL1=(65536-50000)%256; EA=1; ET0=1; ET1=1; TR0=1; TR1=1; } void display(uchar aa,uchar bb,uchar cc,uchar bai,uchar shi,uchar ge) { duan=1; P0=table[aa]; duan=0; P0=0xff; wei=1; P0=0xfe; wei=0; delay(1); duan=1; P0=table[bb]; duan=0; P0=0xff; wei=1; P0=0xfd; wei=0; delay(1); duan=1; P0=table[cc]; duan=0; P0=0xff; wei=1; P0=0xfb; delay(1); duan=1; P0=table[bai]; duan=0; P0=0xff; wei=1; P0=0xf7; wei=0; delay(1); duan=1; P0=table[shi]; duan=0; P0=0xff; wei=1; P0=0xef; wei=0; delay(1); duan=1; P0=table[ge]; duan=0; P0=0xff; wei=1; P0=0xdf; delay(1); } void main() { init(); while(1) { if(flag1!=1)display(7,6,5,bai,shi,ge); else display(10,11,12,12,13,14); } } void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; t0++; if(flag!=1) { if(t0==10) { t0=0; temp=_crol_ (temp,1); P1=temp; } } else{ if(t0%4==0)P1=~P1; if(t0==60) { TR0=0; P1=0xff; } flag1=1; } } void timer1() interrupt 3 { TH1=(65536-50000)/256; TL1=(65536-50000)%256; t1++; if(t1==2) { t1=0; num--; bai=num/100; shi=num%100/10; ge=num%10; if(num==398) { TR0=0; TH1=(65536-50000)/256; TL1=(65536-50000)%256; TR0=1; flag=1; t0=0; P1=0xff; TR1=0; } } } 中断程序: void init() { wei=0; duan=0; temp=0; TMOD=0x01; TH0=(65536-50000)/256; TH1=(65536-50000)%256; EA=1;ET0=1;TR0=1; } void timer1() interrupt 1 { TH0=(65536-50000)/256; TH1=(65536-50000)%256; aa++; } 草稿: #include #define uint unsigned int #define uchar unsigned char uint a,e; uchar a0,a1,a2,a3,a4,a5; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xc0}; sbit duan=P2^6; sbit wei=P2^7; /*void init() { a=0; duan=1; wei=1; TMOD=0x01; TH0=(65536-50000)/256; TH1=(65536-50000)%256; EA=1;ET0=1;TR0=1; } */ void delay(uint e) { uint b,c; for(b=e;b>0;b--) for(c=10;c>0;c--); } void display(uchar a0,uchar a1,uchar a2,uchar a3,uchar a4,uchar a5) { duan=1; P0=table[a0]; duan=0; P0=0xff; wei=1; P0=0xfe; wei=0; delay(1); duan=1; P0=table[a1]; duan=0; P0=0xff; wei=1; P0=0xfd; wei=0; delay(1); duan=1; P0=table[a2]; duan=0; P0=0xff; wei=1; P0=0xfb; delay(1); duan=1; P0=table[a3]; duan=0; P0=0xff; wei=1; P0=0xf7; wei=0; delay(1); duan=1; P0=table[a4]; duan=0; P0=0xff; wei=1; P0=0xef; wei=0; delay(1); duan=1; P0=table[a5]; duan=0; P0=0xff; wei=1; P0=0xdf; delay(1); } void main() { //init(); while(1){ display(0,0,0,0,0,0);//delay(1000); display(1,0,0,0,0,0);}//delay(1000); //display(1,2,0,0,0,0);delay(1000); //display(1,2,3,0,0,0);delay(1000); //display(1,2,3,4,0,0);delay(1000); //display(1,2,3,4,5,0);delay(1000); //display(1,2,3,4,5,6);delay(1000); } /*void timer0() interrupt 1 { TH0=(65536-50000)/256; TH1=(65536-50000)%256; a++; } */ 4X4键盘: #include #define uint unsigned int #define uchar unsigned char sbit duan=P2^6; sbit wei=P2^7; sbit key=P3^5; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}; uchar num,num1,temp; void delay(uint a) { uint b,c; for(b=a;b>0;b--) { for(c=110;c>0;c--); } } uchar keyscanf() { P3=0xfe; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xee:num=1;break; case 0xde:num=2;break; case 0xbe:num=3;break; case 0x7e:num=4;break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } } } P3=0xfd; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xed:num=5;break; case 0xdd:num=6;break; case 0xbd:num=7;break; case 0x7d:num=8;break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } } } P3=0xfb; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xeb:num=9;break; case 0xdb:num=10;break; case 0xbb:num=11;break; case 0x7b:num=12;break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } } } P3=0xf7; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xe7:num=13;break; case 0xd7:num=14;break; case 0xb7:num=15;break; case 0x77:num=16;break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } } } return num; } void main() { num=17; duan=1; P0=0x00; duan=0; wei=1; P0=0xc0; wei=0; while(1) { num1=keyscanf(); duan=1; P0=table[num1-1]; duan=0; } }

    Top_arrow
    回到顶部
    EEWORLD下载中心所有资源均来自网友分享,如有侵权,请发送举报邮件到客服邮箱bbs_service@eeworld.com.cn 或通过站内短信息或QQ:273568022联系管理员 高进,我们会尽快处理。