51单片机----基础知识

基础:

1. 点亮二极管的压降为 1.6–1.7V,工作电流为3–10 mA;低电平点亮

2.线与指的是它们任意一开关只要对地导通,这根线就一定是低电平。

3.单片机I/O默认输入高电平;


LED 灯点亮

电路图:

51单片机----基础知识_第1张图片

# include
int main()
{
     P1=0xb3;   //10110011   总线点亮
    return 0;
}
# include
sbit led0=P1^0;  
sbit led1=P1^2;     //用来声明P1.1口为程序所要控制的端口,"sbit"KEIL专门用来声明某位IO口
int main()
{
    //P1=0xb3;  //10110011
    led0=0;
    led1=0;
    return 0;
}

独立按键扫描

电路图:

51单片机----基础知识_第2张图片

# include
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口进行段选

51单片机----基础知识_第3张图片

#include 
#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;
}

51单片机----基础知识_第4张图片

#include 
#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);
        } 
    } 
}

你可能感兴趣的:(硬件学习)