1. 驱动蜂鸣器的实验; a) 阐明:Lab 实验板的蜂鸣器衔接到单片机的P1.5 b) 基本请求:控制蜂鸣器每2秒响0.5秒。 #include <reg51.h> #define unit unsigned int void delay(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=x;j>0;--j); } } void main() { while(1){ P1=0x00; delay(250); //0.5秒 P1=0xff; delay(420);//2秒 } } 2. 单片机驱动继电器输出实验; a) 阐明:Lab51单片机实验板的蜂鸣器衔接到单片机的P1.4 b) 基本请求:控制继电器每5秒吸合0.5秒。 #include <reg51.h> #define unit unsigned int void delay(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=x;j>0;--j); } } sbit JiDian=P1^4; void main() { while(1){ JiDian=0x00; delay(250); //0.5秒 JiDian=0xff; delay(700);//2秒 } } 3.延时完成p2口 效果(用循环移位指令) #include <reg51.h> #define unit unsigned int unit table[]={~0x01,~0x02,~0x04,~0x08,~0x10,~0x20,~0x40,~0x80}; void delay(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=x;j>0;--j); } } void main() { int i; P2=0x00; while(1){ for(i=0;i<8;i++){ P2=table[i]; delay(250);//2秒 } } } 4.p2口八个灯作二进制加法。了解二进值的计算 #include<reg51.h> #define unit unsigned int void delay(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=x;j>0;--j); } } void main(){ while(1){ P2=0xff; while(P2!=0x00){ P2++; delay(250);//2秒 } } } 5.直接用 做位选信号,控制8位数码管上显现1,2,3,4…F,循环显现 阐明:P0是位选,P2是段选 #include <reg51.H> #define unit unsigned int unit code NumTable[]={0x06,~0x24,~0x30,~0x19,~0x12,~0x02,0x87,0xff,~0x10}; void delay(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=x;j>0;--j); } } void DisplayNumByOrder(unit DelayNum){ int i; for(i=0;i<=8;i++){ P0=NumTable[i]; delay(DelayNum);//2秒 } } void main(){ //P0是段选,P2是位选 P2=0x00; while(1){ DisplayNumByOrder(250); } } 6.用 138做片选信号,控制4位数码管上做加1显现。从0开端. 阐明:JP15与JP16 用8个短路冒衔接,JP10 (P0)与J12 用8PIN排线衔接 #include <reg51.h> #include <math.h> #define unit unsigned int unit code NumTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//数字的编码 // 用译码器138做位选信号,控制4位数码管上做加1显现。从0开端. void delay_1ms(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=110;j>0;--j); } } /** 在数码管上显现对应的值 **/ void display(unsignedchar k) { P0=NumTable[k]; delay_1ms(3); } sbit high=P2^4; sbit mid=P2^3; sbit low=P2^2; /** 控制 后4位,并合成计数值 **/ void DisplayNumByOrder(unit Count,unit delay_1msNum){ low=0; mid=0; high=0; display(0); low=1; mid=0; high=0; display(0); low=0; mid=1; high=0; display(0); low=1; mid=1; high=0; display(0); low=0; mid=0; high=1; display(Count%10/1); low=1; mid=0; high=1; display(Count%1/100); low=0; mid=1; high=1; display(Count%100/10); low=1; mid=1; high=1; display(Count%10); } //一个是位选,一个是段选 void main(){ int count=0; while(1){ count++; DisplayNumByOrder(count,300); if(count==9) count=0; } } 7.应用动态扫描措施在八位数码管上显现出稳定的87654321. #include <reg51.h> #define unit unsigned int unit table[]={~0x01,~0x02,~0x04,~0x08,~0x10,~0x20,~0x40,~0x80}; unit code NumTable[]={0x06,~0x24,~0x30,~0x19,~0x12,~0x02,0x87,0xff,~0x10}; void delay(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=x;j>0;--j); } } void DisplayNumByOrder(unit DelayNum){ int i; for(i=0;i<=7;i++){ P0=NumTable[i]; P2=table[i]; delay(DelayNum);//5ms } } //一个是位选,一个是段选 void main(){ //P0是位选,P2是段选 //P2=0x00; while(1){ DisplayNumByOrder(5); } } 8. 数码管前三位显现一个跑表,从到之间以1%秒速度运转,当按下一个独立键盘时跑表中止,松开手后跑表继续运转。 #include <reg51.h> #define unit unsigned int unit code NumTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//数字的编码 //数码管前三位显现一个跑表,从到之间以1%秒速度运转,当按下一个独立键盘时跑表中止,松开手后跑表继续运转。 sbit high=P2^4; sbit mid=P2^3; sbit low=P2^2; sbit IsCountKey=P3^0;//能否计数的按键 const bit PRESSED=0;//0的时分运转,1的时分暂停 //监测 void delay_1ms(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=110;j>0;--j); } } /** 在数码管上显现对应的值 **/ void display(unsignedchar Num) { P0=NumTable[Num]; delay_1ms(1); P0=0; //送完段选信号后,中止消影的处置 } /** 控制数码管显现后3位,并合成计数值 **/ void DisplayNumByOrder(unit Count){ low=0; mid=0; high=0; display(0); low=1; mid=0; high=0; display(0); low=0; mid=1; high=0; display(0); low=1; mid=1; high=0; display(0); low=0; mid=0; high=1; display(0); low=1; mid=0; high=1; display(Count%1/100); low=0; mid=1; high=1; display(Count%100/10); low=1; mid=1; high=1; display(Count%10); } //能否计数 void IsCount(unit count){ if(PRESSED==IsCountKey){ //当按键按下 delay_1ms(10); //去 if(PRESSED==IsCountKey){//当按键按下 while(PRESSED==IsCountKey){//当按键不时按下 DisplayNumByOrder(count);//显现原数值 } } } } //扫描键盘 void main(){ int count=0; while(1){ IsCount(count); count++; DisplayNumByOrder(count); if(count==) count=0; } } 9. 在上题的基础上,用另外三个独立键盘完成按下第一个时计时中止,按下第二个时计时开端,按下第三个是计数值清零从头开端。 #include <reg51.h> #define unit unsigned int unit code NumTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//数字的编码 //数码管前三位显现一个跑表,从到之间以1%秒速度运转,当按下一个独立键盘时跑表中止,松开手后跑表继续运转。 sbit high=P2^4; sbit mid=P2^3; sbit low=P2^2; sbit Key1=P3^0;//开端 sbit Key2=P3^1;//暂停 sbit Key3=P3^2;//清零 const bit PRESSED=0;//按下 const bit SUSPEND=1;//0的时分运转,1的时分暂停 //监测 void delay_1ms(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=110;j>0;--j); } } /** 在数码管上显现对应的值 **/ void display(unsignedchar Num) { P0=NumTable[Num]; delay_1ms(1); P0=0; //送完段选信号后,中止消影的处置 } /** 控制数码管显现后3位,并合成计数值 **/ void DisplayNumByOrder(unit Count){ low=0; mid=0; high=0; display(0); low=1; mid=0; high=0; display(0); low=0; mid=1; high=0; display(0); low=1; mid=1; high=0; display(0); low=0; mid=0; high=1; display(0); low=1; mid=0; high=1; display(Count%1/100); low=0; mid=1; high=1; display(Count%100/10); low=1; mid=1; high=1; display(Count%10); } //能否清零 void IsClear(unit * count){ if(PRESSED==Key3){ //当按键按下 delay_1ms(10); //去颤动 if(PRESSED==Key3){ //当按键按下 *count=0; } } } //能否暂停 void IsSuspend(unit * count){ if(PRESSED==Key2){ //当按键按下 delay_1ms(10); //去颤动 if(PRESSED==Key2){ //当按键按下 while(SUSPEND){ IsClear(count); //监测能否需求清零 if(PRESSED==Key1)return;//跳出暂停 DisplayNumByOrder(*count);//显现原数值 } } } } //扫描键盘 void main(){ int count=0; while(1){ IsSuspend(&count); IsClear(&count); count++; DisplayNumByOrder(count); if(count==) count=0; } } 10.按下16个 依次在数码管上显现1-16的平方。如按下第一个显现1,第二个显现4... #include <reg51.h> #include <stdio.h> #define unit unsigned int #define uchar unsigned char unit code NumTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//数字的编码 //10.按下16个矩阵键盘依次在数码管上显现1-16的平方。如按下第一个显现1,第二个显现4... sbit high=P2^4; sbit mid=P2^3; sbit low=P2^2; sbit Key1=P3^0;//开端 sbit Key2=P3^1;//暂停 sbit Key3=P3^2;//清零 const bit PRESSED=0;//按下 const bit SUSPEND=1;//0的时分运转,1的时分暂停 //延时 void delay_1ms(unit x){ unit i=x; unit j; for(;i>0;--i){ for(j=110;j>0;--j); } } /** 在数码管上显现对应的值 **/ void display(unsignedchar Num) { P0=NumTable[Num]; delay_1ms(1); P0=0; //送完段选信号后,中止消影的处置 } /** 控制数码管显现后3位,并合成计数值 **/ void DisplayNumByOrder(unit Count){ low=0; mid=0; high=0; display(0); low=1; mid=0; high=0; display(0); low=0; mid=1; high=0; display(0); low=1; mid=1; high=0; display(0); low=0; mid=0; high=1; display(0); low=1; mid=0; high=1; display(Count%1/100); low=0; mid=1; high=1; display(Count%100/10); low=1; mid=1; high=1; display(Count%10); } //能否清零 void IsClear(unit * count){ if(PRESSED==Key3){ //当按键按下 delay_1ms(10); //去颤动 if(PRESSED==Key3){ //当按键按下 *count=0; } } } //能否暂停 void IsSuspend(unit * count){ if(PRESSED==Key2){ //当按键按下 delay_1ms(10); //去颤动 if(PRESSED==Key2){ //当按键按下 while(SUSPEND){ IsClear(count); //监测能否需求清零 if(PRESSED==Key1)return;//跳出暂停 DisplayNumByOrder(*count);//显现原数值 } } } } uchar scanKey(){ uchar tmp, key; P3 =0xf0; tmp = P3; tmp = tmp &0xf0; if(tmp !=0xf0){//肯定列 switch(tmp){ case0xe0:key =1;break; case0xd0:key =2;break; case0xb0:key =3;break; case0x70:key =4;break; } } //肯定行 P3 =0x0f; tmp = P3; tmp = tmp &0x0f; if(tmp !=0x0f){ switch(tmp){ case0x0d:key = key;break; case0x0b:key =4+ key;break; case0x07:key =8+ key;break; } } return key; } //扫描键盘 void main(){ // int count=0; while(1){ unit key= scanKey(); DisplayNumByOrder(key*key); } } 原文链接: |