1. 程式人生 > >51微控制器----基礎知識

51微控制器----基礎知識

基礎:

1. 點亮二極體的壓降為 1.6–1.7V,工作電流為3–10 mA;低電平點亮

2.線與指的是它們任意一開關只要對地導通,這根線就一定是低電平。

3.微控制器I/O預設輸入高電平;


LED 燈點亮

電路圖:

這裡寫圖片描述

# include<reg52.h>
int main()
{
     P1=0xb3;   //10110011   匯流排點亮
    return 0;
}
# include<reg52.h>
sbit led0=P1^0;  
sbit led1=P1^2;     //用來宣告P1.1口為程式所要控制的埠,"sbit"
KEIL專門用來宣告某位IO口 int main() { //P1=0xb3; //10110011 led0=0; led1=0; return 0; }

獨立按鍵掃描

電路圖:

這裡寫圖片描述

# include<reg52.h>
sbit led0=P1^0;
sbit key3=P3^5;
int main()
{
    P3=0xff;         // 將P3 I/O口設定為輸入狀態。但按下鍵盤時,對應的I/O口為低電平
    while(1){
       if(key3==0)      // 當key3按下時,點亮led0 。
           led0=0;
        else
led0=1; } return 0; }

數碼管動態顯示

電路圖: P2口的低四位進行片選,P0口進行段選

#include <reg52.h>
#define uchar unsigned char
#define uint  unsigned int
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};        //共陰極的段選
uchar table1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80
,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //共陰極的段選 uchar DuanXuan[]={0xfe,0xfc,0xfb,0xf7}; uchar st; void delay(uint x){ while(x--); } int main() { while(1){ for(st=0;st<4;st++){ P2=DuanXuan[st]; P0=table1[st]; delay(100); } } return 0; }

兩個鎖存器的段選和位選

#include <reg52.h>
#define uchar unsigned char
#define uint  unsigned int

uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};        //共陰極的段選
uchar table1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};        //共陰極的段選
uchar We[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar st;
sbit WE=P2^7;
sbit DU=P2^6;
void delay(uint xms){
    uint i,j;
    for(i=xms; i>0; i--)
       for(j=110; j>0; j--);
}
void main()
{
     while(1){
        for(st=0;st<6;st++){
           WE=1;
           P0=We[st];
           WE=0;
           DU=1;
           P0=table[st];
           DU=0;
           delay(500);
        } 
    } 
}