實驗1 單片機IO口應用及數碼管顯示


1.   單片機驅動蜂鳴器的實驗;

a)         說明:Lab51單片機實驗板的蜂鳴器連接到單片機的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口LED流水燈效果(用循環移位指令)

#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.直接用IO口做位選信號,控制8位數碼管上顯示1,2,3,4F,循環顯示

說明: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顯示。從0000開始.

說明:JP15JP16 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顯示。從0000開始.

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%10000/1000);

       low=1; mid=0; high=1;  display(Count%1000/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==9999) 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.  數碼管前三位顯示一個跑表,從000999之間以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};//數字的編碼

//數碼管前三位顯示一個跑表,從000999之間以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%1000/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==999) 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};//數字的編碼

//數碼管前三位顯示一個跑表,從000999之間以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%1000/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==999) 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%1000/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);

    }

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM