51基础入门知识

    • LED

51基础入门知识_第1张图片
#include "reg52.h"
//引入51头文件

typedef unsigned int uint;
typedef unsigned char uchar;
//重命名

sbit HC138_A = P2^5;
sbit HC138_B = P2^6;
sbit HC138_C = P2^7;
//sbit定义特殊功能寄存器的位变量
//定义好P25\P26\P27三个地址


//定义延时函数
void Delays(uint t)
{
    while(t--);
    while(t--);
}


void LEDRuning()
{
    uint i;
    
    HC138_A = 0;
    HC138_B = 0;
    HC138_C = 1;
    for(i = 0; i < 3; i++)
    {
        P0 = 0x00; //全部打开
        Delays(60000);
        Delays(60000);
        P0 = 0xff; //全部关闭
        Delays(60000);
        Delays(60000);
    }
    
    for(i = 1; i <= 8; i++)
    {
        P0 = 0XFF << i;
        Delays(60000);
        Delays(60000);
    }
    for(i = 1; i <= 8; i++)
    {
        P0 = ~(0XFF << i);
        Delays(60000);
        Delays(60000);
    }
}

void main()
{
        while(1)
        {
            LEDRuning();
        }
}
    • 蜂鸣器与继电器

51基础入门知识_第2张图片
#include "reg52.h"
//引入51头文件

typedef unsigned int uint;
typedef unsigned char uchar;
//重命名

sbit HC138_A = P2^5;
sbit HC138_B = P2^6;
sbit HC138_C = P2^7;
//sbit定义特殊功能寄存器的位变量
//定义好P25\P26\P27三个地址


//定义延时函数
void Delays(uint t)
{
    while(t--);
    while(t--);
}

void InitHC138(uint n)
{
    switch(n)
    {
        case 4:
            HC138_C = 1;
            HC138_B = 0;
            HC138_A = 0;
        break;
        case 5:
            HC138_C = 1;
            HC138_B = 0;
            HC138_A = 1;
        break;
        case 6:
            HC138_C = 1;
            HC138_B = 1;
            HC138_A = 0;
        break;
        case 7:
            HC138_C = 1;
            HC138_B = 1;
            HC138_A = 1;
        break;
            
    }
}

void OutPutP0(uint n, uchar dat)
{
    InitHC138(n);
    P0 = dat;
}

void LEDRuning()
{
    uint i;
    

    for(i = 0; i < 3; i++)
    {
        OutPutP0(4,0x00);
        Delays(60000);
        Delays(60000);
        OutPutP0(4,0xff);
        Delays(60000);
        Delays(60000);
    }
    
    for(i = 1; i <= 8; i++)
    {
        P0 = 0XFF << i;
        Delays(60000);
        Delays(60000);
    }
    
    OutPutP0(5,0x10);
    Delays(60000);
    Delays(60000);
    OutPutP0(5,0x00);
    
    
    for(i = 1; i <= 8; i++)
    {
        OutPutP0(4,~(0XFF << i));
        Delays(60000);
        Delays(60000);
    }
    OutPutP0(5,0x40);
    Delays(60000);
    Delays(60000);
    OutPutP0(5,0x00);
}

void Initsys()
{
    HC138_A = 1;
    HC138_B = 0;
    HC138_C = 1;
    
    P0 = 0X00;
}

void main()
{
        while(1)
        {
            Initsys();
            LEDRuning();
        }
}

3共阳数码管静态

51基础入门知识_第3张图片
#include 

typedef unsigned int uint;
typedef unsigned char uchar;

uchar code SMG_duanma[18] = 
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
     0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

     
//配置HC138
void InitHC138(uchar n)
{
    switch(n)
    {
        case 4:
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

//延时
void Delay(unsigned int n)
{
    while(n--);
    while(n--);
}

void ShowSMG_Bit(unsigned pos, uchar dat)
{
    
    InitHC138(6);  //数码管的位置
    P0 = 0x01 << pos;
    
    InitHC138(7);  //数码管的内容
    //Delay(100000000);
    //Delay(80000);
    P0 = SMG_duanma[dat];
    Delay(6000);
    Delay(6000);
    //P0 = 0xff;// 消影
    
}

void SMG_Static()
{
    uchar i,j;
    for(i = 0; i < 8; i++)
    {
        for(j = 0; j < 10; j++)
        {
            ShowSMG_Bit(i,j);
        }
        //Delay(600000);
        //Delay(600000);
    }
}

void Initsys()
{
    InitHC138(5);
    P0 = 0X00;
    InitHC138(4);
    P0 = 0XFF;
}

void main()
{
    while(1)
    {
        Initsys();
      SMG_Static();
    }
}

4,动态数码管

51基础入门知识_第4张图片
51基础入门知识_第5张图片
#include 

typedef unsigned int uint;
typedef unsigned char uchar;
//定义月份
uint yue = 1;
uchar code SMG_duanma[18] = 
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
     0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”


void Delay_SMG(uint n)
{
    while(n--);
}



//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

//选择位码并显示
void DisplaySMG_Bit(uchar pos, uchar dat)
{
    SelectHC138(6);
    P0 = 0X01 << pos;
    SelectHC138(7);
    P0 = SMG_duanma[dat];
}

void Display_Dymaic()
{
    //uint i;
    DisplaySMG_Bit(0,2);
    Delay_SMG(500);
    DisplaySMG_Bit(1,0);
    Delay_SMG(500);
    DisplaySMG_Bit(2,2);
    Delay_SMG(500);
    DisplaySMG_Bit(3,2);
  Delay_SMG(500);
    DisplaySMG_Bit(4,16);
    Delay_SMG(500);
    DisplaySMG_Bit(5,16);
  Delay_SMG(500);
    
    DisplaySMG_Bit(6,yue / 10);
  Delay_SMG(500);
    DisplaySMG_Bit(7,yue % 10);
  Delay_SMG(500);
}

//初始化系统
void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

void Delay_yue(uint n)
{
    while(n--)
    {
        Display_Dymaic();
    }
}
void main()
{
    while(1)
    {
        Initsys();
        //DisplaySMG_Bit(2,6);
        Display_Dymaic();
        yue++;
        if(yue > 12)
        {
            yue = 1;
        }
        Delay_yue(200);
    }
}

5.独立按键

51基础入门知识_第6张图片
//任务1
#include 

typedef unsigned int uint;
typedef unsigned char uchar;

sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;

//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

//初始化系统
void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}


void DelayK(uint n)
{
    while(n--);
}

//配置独立按键
void ScanKeys_Alone()
{
    if(S7 == 0)
    {
        DelayK(100); //去抖
        if(S7 == 0) 
        {
            
            L1 = 0;
            while(S7 == 0);
            L1 = 1;
        }
    }
    if(S6 == 0)
    {
        DelayK(100); //去抖
        if(S6 == 0) 
        {
            
            L2 = 0;
            while(S6 == 0);
            L2 = 1;
        }
    }
    if(S5 == 0)
    {
        DelayK(100); //去抖
        if(S5 == 0) 
        {
            
            L3 = 0;
            while(S5 == 0);
            L3 = 1;
        }
    }
    if(S4 == 0)
    {
        DelayK(100); //去抖
        if(S4 == 0) 
        {
            
            L4 = 0;
            while(S4 == 0);
            L4 = 1;
        }
    }
}

void main()
{
    Initsys();
    while(1)
    {
        SelectHC138(4);
        ScanKeys_Alone();
    }
}
//任务2
#include 

typedef unsigned int uint;
typedef unsigned char uchar;

sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
sbit L6 = P0^5;

//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

//初始化系统
void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}


void DelayK(uint n)
{
    while(n--);
}

//状态变量
uchar stat_K = 0;
//配置独立按键
void ScanKeys_Alone()
{
    if(S7 == 0)
    {
        DelayK(100); //去抖
        if(S7 == 0) 
        {
            if(stat_K == 0)
            {
                L1 = 0;
                stat_K = 1;  //第一次按下,形成状态一
            }
            else if(stat_K == 1)
            {
                L1 = 1;
                stat_K = 0;  //第二次按下,初始化状态
            }
            while(S7 == 0); //等待按键松开
        }
    }
    if(S6 == 0)
    {
        DelayK(100); //去抖
        if(S6 == 0) 
        {
            if(stat_K == 0)
            {
                L2 = 0;
                stat_K = 2;   //第一次按下,形成状态二
            }
            else if(stat_K == 2)
            {
                L2 = 1;
                stat_K = 0;//第二次按下,初始化状态
            }
            while(S6 == 0); //等待按键松开
        }
    }
    if(S5 == 0)
    {
        DelayK(100); //去抖
        if(S5 == 0) 
        {
            if(stat_K == 1)
            {
                L3 = 0;
                while(L3 == 0);
                L3 = 1;
            }
            else if(stat_K == 2)
            {
                L4 = 0;
                while(L4 == 0);
                L4 = 1;
            }
        }
    }
    if(S4 == 0)
    {
        DelayK(100); //去抖
        if(S4 == 0) 
        {
            if(stat_K == 1)
            {
                L5 = 0;
                while(L5 == 0);
                L5 = 1;
            }
            else if(stat_K == 2)
            {
                L6 = 0;
                while(L6 == 0);
                L6 = 1;
            }
        }
    }
}

void main()
{
    Initsys();
    while(1)
    {
        SelectHC138(4);
        ScanKeys_Alone();
    }
}

6矩阵键盘

51基础入门知识_第7张图片
51基础入门知识_第8张图片
#include 

typedef unsigned int uint;
typedef unsigned char uchar;

sfr P4 = 0XC0;

sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;

sbit C4 = P3^4;
sbit C3 = P3^5;
sbit C2 = P4^2; //P36 -> P42
sbit C1 = P4^4; //P37 -> P44

void DelayK(uint n)
{
    while(n--);
}

uchar code SMG_duanma[18] = 
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
     0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

//初始化系统
void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

void DisplayKeyNum(uchar dat)
{
    SelectHC138(6);
    P0 = 0X01;
    SelectHC138(7);
    P0 = SMG_duanma[dat];
}

uchar key_num;
void ScanKeyMulti()
{
    R1 = 0;
    R2 = R3 = R4 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0)
    {
        key_num = 0;
        DisplayKeyNum(key_num);
        while(C1 == 0);
    }
    else if(C2 == 0)
    {
        key_num = 1;
        DisplayKeyNum(key_num);
        while(C2 == 0);
    }
    else if(C3 == 0)
    {
        key_num = 2;
        DisplayKeyNum(key_num);
        while(C3 == 0);
    }
    else if(C4 == 0)
    {
        key_num = 3;
        DisplayKeyNum(key_num);
        while(C4 == 0);
    }
    R2 = 0;
    R1 = R3 = R4 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0)
    {
        key_num = 4;
        DisplayKeyNum(key_num);
        while(C1 == 0);
    }
    else if(C2 == 0)
    {
        key_num = 5;
        DisplayKeyNum(key_num);
        while(C2 == 0);
    }
    else if(C3 == 0)
    {
        key_num = 6;
        DisplayKeyNum(key_num);
        while(C3 == 0);
    }
    else if(C4 == 0)
    {
        key_num = 7;
        DisplayKeyNum(key_num);
        while(C4 == 0);
    }
    R3 = 0;
    R2 = R1 = R4 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0)
    {
        key_num = 8;
        DisplayKeyNum(key_num);
        while(C1 == 0);
    }
    else if(C2 == 0)
    {
        key_num = 9;
        DisplayKeyNum(key_num);
        while(C2 == 0);
    }
    else if(C3 == 0)
    {
        key_num = 10;
        DisplayKeyNum(key_num);
        while(C3 == 0);
    }
    else if(C4 == 0)
    {
        key_num = 11;
        DisplayKeyNum(key_num);
        while(C4 == 0);
    }

    R4 = 0;
    R2 = R3 = R1 = 1;
    C1 = C2 = C3 = C4 = 1;
    if(C1 == 0)
    {
        key_num = 12;
        DisplayKeyNum(key_num);
        while(C1 == 0);
    }
    else if(C2 == 0)
    {
        key_num = 13;
        DisplayKeyNum(key_num);
        while(C2 == 0);
    }
    else if(C3 == 0)
    {
        key_num = 14;
        DisplayKeyNum(key_num);
        while(C3 == 0);
    }
    else if(C4 == 0)
    {
        key_num = 15;
        DisplayKeyNum(key_num);
        while(C4 == 0);
    }
    
}

void main()
{
    Initsys();
    while(1)
    {
        ScanKeyMulti();
    }
}

7.中断

51基础入门知识_第9张图片
#include 

typedef unsigned int uint;
typedef unsigned char uchar;

sbit L1 = P0^0;
sbit L8 = P0^7;

void Delay(uint t)
{
    while(t--);
    while(t--);
    while(t--);
}


//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

//初始化系统
void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

void Working()
{
    SelectHC138(4);
    L1 = 0;
    Delay(60000);
    L1 = 1;
    Delay(60000);
}
//*******************************************
void Init_INT0()
{
    IT0 = 1;//下降沿触发
    EX0 = 1;
    EA = 1;
}

uchar stat_int = 0;
void ServiceINT0() interrupt 0
{
    stat_int = 1; //中断服务函数,尽量少做事情
}

void LEDINT()
{
    if(stat_int == 1)
    {
        L8 = 0;
        Delay(60000);
        Delay(60000);
        Delay(60000);
        Delay(60000);
        Delay(60000);
        Delay(60000);
        L8 = 1;
    }
}
//*******************************************

void main()
{
    Init_INT0();
    Initsys();
    while(1)
    {
        Working();
        LEDINT();
    }
}

8.定时器

51基础入门知识_第10张图片
#include 

typedef unsigned int uint;
typedef unsigned char uchar;

sbit L1 = P0^0;
sbit L8 = P0^7;

//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

//初始化系统
void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}
//********************************
void InitTime0()
{
    TMOD = 0X01;
    TH0 = (65535 - 50000) / 256;
    TL0 = (65535 - 50000) % 256;
    
    ET0 = 1;
    EA = 1;
    TR0 = 1;
}

uchar count = 0;
void ServiceTime0() interrupt 1
{
    TH0 = (65535 - 50000) / 256;
    TL0 = (65535 - 50000) % 256;
    count++;
    if(count % 10 == 0)
    {
        L1 = ~L1;
    }
    if(count == 100)
    {
        L8 = ~L8;
        count = 0;
    }
}
//********************************

void main()
{
    SelectHC138(4);
    InitTime0();
    Initsys();
    while(1)
    {
        
    }
}

9.秒表

51基础入门知识_第11张图片
#include 

sbit S4 = P3^3;
sbit S5 = P3^2;

typedef unsigned int uint;
typedef unsigned char uchar;

uchar code SMG_duanma[18] = 
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
     0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

uchar t_m = 0;
uchar t_s = 0;
uchar t_005s = 0;
//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value)
{
    SelectHC138(6);
    P0 = 0X01 << pos;
    SelectHC138(7);
    P0 = value;
}

void DelaySMG(uint t)
{
    while(t--);
}

//数码管显示
void DisplayTime()
{
    DisplaySMG_Bit(7,SMG_duanma[t_005s%10]);
    DelaySMG(500);
    DisplaySMG_Bit(6,SMG_duanma[t_005s/10]);
    DelaySMG(500);
    DisplaySMG_Bit(5,SMG_duanma[16]);
    DelaySMG(500);
    
    DisplaySMG_Bit(4,SMG_duanma[t_s%10]);
    DelaySMG(500);
    DisplaySMG_Bit(3,SMG_duanma[t_s/10]);
    DelaySMG(500);
    DisplaySMG_Bit(2,SMG_duanma[16]);
    DelaySMG(500);
    
    DisplaySMG_Bit(1,SMG_duanma[t_m%10]);
    DelaySMG(500);
    DisplaySMG_Bit(0,SMG_duanma[t_m/10]);
    DelaySMG(500);
}

//********定时器相关函数*********
void InitTimer0()
{
    TMOD = 0x01;
    TH0 = (65535 - 50000) / 256;
    TL0 = (65535 - 50000) % 256;
    
    ET0 = 1;
    EA = 1;
    TR0 = 1;
}

void ServiceTimer0() interrupt 1
{
    TH0 = (65535 - 50000) / 256;
    TL0 = (65535 - 50000) % 256;
    t_005s++;
    if(t_005s == 20)
    {
        t_s++;
        t_005s = 0;
        if(t_s == 60)
        {
            t_m++;
            t_s = 0;
        }
        if(t_m == 99)
        {
            t_m = 0;
        }
    }
}
//*******************************

//按键去抖延时(uchar就够用)
void DelayK(uchar t)
{
    while(t--);
}

//按键扫描函数
void ScanKeys()
{
    if(S4 == 0)  //秒表启动/暂停
    {
        DelayK(100);
        if(S4 == 0)
        {
            TR0 = ~TR0;
            while(S4 == 0)
            {
                DisplayTime();
            }
        }
    }
    if(S5 == 0)  //秒表清零
    {
        DelayK(100);
        if(S5 == 0)
        {
            t_m = 0;
            t_s = 0;
            t_005s = 0;
            while(S5 == 0)
            {
                DisplayTime();
            }
        }
    }
}

//初始化系统
void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

void main()
{
    InitTimer0();
    Initsys();
    while(1)
    {
        DisplayTime();
        ScanKeys();
    }
}

10.PWM

51基础入门知识_第12张图片
#include 

typedef unsigned int uint;
typedef unsigned char uchar;

sbit L1 = P0^0;
sbit S7 = P3^0;

//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:    //LED
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:    //蜂鸣器和继电器
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:    //位码
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:    //段码
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

//*********定时相关函数*********
uchar count = 0;
uchar pwm_duty = 0;
void InitTimer0()
{
    TMOD = 0x01;
    TH0 = (65535 - 100) / 256;
    TL0 = (65535 - 100) % 256;
    
    ET0 = 1;
    EA = 1;
}

void ServiceTimer0() interrupt 1 
{
    TH0 = (65535 - 100) / 256;
    TL0 = (65535 - 100) % 256;
    
    count++;
    if(count == pwm_duty)
    {
        L1 = 1;
    }
    else if(count == 100)
    {
        L1 = 0;
        count = 0;
    }
}
//*******按键相关函数********
uchar stat = 0;
void Delay(uint t)
{
    while(t--);
}


void ScanKeys()
{
    if(S7 == 0)
    {
        Delay(100);
        if(S7 == 0)
        {
            switch(stat)
            {
                case 0:
                    L1 = 0;
                    TR0 = 1;  //打开寄存器
                    pwm_duty = 10;
                    stat = 1;
                break;
                case 1:
                    pwm_duty = 50;
                    stat = 2;
                break;
                case 2:
                    pwm_duty = 90;
                    stat = 3;
                break;
                case 3:
                    L1 = 1;
                    TR0 = 0;
                  stat = 0;
                break;
            }
            while(S7 == 0);
        }
    }
}

//初始化系统
void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

void main()
{
    InitTimer0();
    Initsys();
    SelectHC138(4);
    while(1)
    {
        ScanKeys();
    }
}


11.串口通信

51基础入门知识_第13张图片
#include 

sfr AUXR = 0x8e;

typedef unsigned int uint;
typedef unsigned char uchar;

uchar urdat;

void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:    //LED
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:    //蜂鸣器和继电器
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:    //位码
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:    //段码
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
    }
}

void InitUart()
{
    TMOD = 0X20;
    TH1 = 0XFD;
    TL1 = 0XFD;
    TR1 = 1;
    
    SCON = 0X50;
    AUXR = 0X00;
    
    ES = 1;
    EA = 1;
}



void SendByte(uchar dat)
{
    SBUF = dat;
    while(TI == 0);
    TI = 0;
}

void ServiceUart() interrupt 4
{
    if(RI == 1)
    {
        RI = 0;
        urdat = SBUF;
        SendByte(urdat + 1);
    }
}

//初始化系统
void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

void main()
{
    InitUart();
    Initsys();
    SendByte(0xa5);      
    SendByte(0x5a);
    while(1);
}

12.串口进阶

51基础入门知识_第14张图片
#include 

sfr AUXR = 0X8e;

typedef unsigned int uint;
typedef unsigned char uchar;

//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:    //LED
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:    //蜂鸣器和继电器
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:    //位码
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:    //段码
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
        case 0:    //锁住所有寄存器
            P2 = (P2 & 0X1F) | 0X00;
        break;
    }
}

void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

//*******************************
void InitUart()
{
    TMOD = 0x20;
    TH1 = 0XFD;  //在12兆下,9600波特率
    TL1 = 0XFD;
    TR1 = 1;
    
    SCON = 0X50; //模式1
    AUXR = 0X00;
    
    ES = 1;
    EA = 1;
}

uchar command = 0;
void ServiceUart() interrupt 4
{
    if(RI == 1)
    {
        command = SBUF;
        RI = 0;
    }
}

void SendByteUart(uchar dat)
{
    SBUF = dat;
    while(TI == 0);
    TI = 0;
}

void SendStringUart(uchar *str)
{
    while(*str != '\0')
    {
        SendByteUart(*str);
        str++;
    }
}
//*******************************
void Working()
{
     SelectHC138(4);
     if(command != 0)
     {
         switch(command & 0xf0)  //提取高四位
         {
             case 0xA0:
                 P0 = (P0 | 0X0F) & (~command | 0XF0); //保留高4位,反转低四位
                 command = 0X00;
             break;
             case 0xB0:
                 P0 = (P0 | 0XF0) & (~command << 4| 0X0F);
                 command = 0X00;
             break;
             case 0xC0:
                 SendStringUart("The System is Runing……\r\n");
                 command = 0X00;
             break;
         }
     }
}


void main()
{
    Initsys();
    InitUart();
    SendStringUart("Hello world");
    while(1)
    {
        Working();
    }
}

13.存储映射

51基础入门知识_第15张图片
51基础入门知识_第16张图片
51基础入门知识_第17张图片
#include 
#include "absacc.h"

typedef unsigned int uint;
typedef unsigned char uchar;

void Delay(uint t)
{
    while(t--);
    while(t--);
}

void LEDRuning()
{
    XBYTE[0X8000] = 0XF0;
    Delay(60000);
    Delay(60000);
    XBYTE[0X8000] = 0X0F;
    Delay(60000);
    Delay(60000);
    XBYTE[0X8000] = 0XFF;
    Delay(60000);
    Delay(60000);
}

void SMGRuning()
{
    uchar i;
    for(i = 0; i < 8; i++)
    {
        XBYTE[0XC000] = 0X01 << i;
        XBYTE[0XE000] = 0X00;
        Delay(60000);
        Delay(60000);
    }
    XBYTE[0XE000] = 0XFF;
    Delay(60000);
    Delay(60000);
}

void main()
{
    while(1)
    {
        LEDRuning();
        SMGRuning();
    }
}

14.基础技能综合实训案例解析

51基础入门知识_第18张图片
#include 

typedef unsigned int uint;
typedef unsigned char uchar;

sfr AUXR = 0X8e;

sbit S5 = P3^2;
sbit S4 = P3^3;

sbit L7 = P0^6;
sbit L8 = P0^7;

uchar code SMG_duanma[18] = 
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
     0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

uchar time_005s = 0;
uchar time_s = 0;
uchar time_m = 0;
uchar time_h = 0;
     
void SMGDisplay();
     
//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:    //LED
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:    //蜂鸣器和继电器
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:    //位码
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:    //段码
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
        case 0:    //锁住所有寄存器
            P2 = (P2 & 0X1F) | 0X00;
        break;
    }
}

//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value)
{
    SelectHC138(6);
    P0 = 0X01 << pos;
    SelectHC138(7);
    P0 = value;
}

void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

void Delay_CL_CSMG(uint t)
{
    while(t--);
    while(t--);
}
void CheckSys()
{
    //检测LED
    uchar i;
    SelectHC138(4);
    for(i = 1; i <= 8; i++)
    {
        P0 = 0XFF << i;
        Delay_CL_CSMG(60000);
        Delay_CL_CSMG(60000);
    }
    for(i = 1; i <= 8; i++)
    {
        P0 = ~(0XFF << i);
        Delay_CL_CSMG(60000);
        Delay_CL_CSMG(60000);
    }
    //检测数码管
    for(i = 0; i < 8; i++)
    {
        DisplaySMG_Bit(i, 0X00);
        Delay_CL_CSMG(60000);
        Delay_CL_CSMG(60000);
    }
    P0 = 0XFF;
}
//*********************************
//本地控制
void Delay_K(uint t)
{
    while(t--);
}

void LocalCheck()
{
    SelectHC138(4);
    if(S4 == 0)
    {
        Delay_K(100);
        if(S4 == 0)
        {
            L8 = 0;
        }
        while(S4 == 0)
        {
            SMGDisplay();
        }
        L8 = 1;
    }
    if(S5 == 0)
    {
        Delay_K(100);
        if(S5 == 0)
        {
            L7 = 0;
        }
        while(S5 == 0)
        {
            SMGDisplay();
        }
        L7 = 1;
    }
}
//*********************************

//*********************************
//定时器
void InitTimer0()
{
    TMOD = 0X01;
    TH0 = (65535 - 50000) / 256;
    TL0 = (65535 - 50000) % 256;
    
    ET0 = 1;
    EA = 1;
    TR0 = 1;
}

void ServiceTimer0() interrupt 1
{
    TH0 = (65535 - 50000) / 256;
    TL0 = (65535 - 50000) % 256;
    time_005s++;
    if(time_005s == 20)
    {
        time_005s = 0;
        time_s++;
        if(time_s == 60)
        {
            time_s = 0;
            time_m++;
        }
        if(time_m == 60)
        {
            time_m = 0;
            time_h++;
        }
        if(time_h == 99)
        {
            time_h = 0;
        }
    }
}
//*********************************

//数码管显示
void Delay_SMG(uint t)
{
    while(t--);
}

void SMGDisplay()
{
    DisplaySMG_Bit(0, SMG_duanma[time_h / 10]);
    Delay_SMG(500);
    DisplaySMG_Bit(1, SMG_duanma[time_h % 10]);
    Delay_SMG(500);
    DisplaySMG_Bit(2, SMG_duanma[16]);
    Delay_SMG(500);
    
    DisplaySMG_Bit(3, SMG_duanma[time_m / 10]);
    Delay_SMG(500);
    DisplaySMG_Bit(4, SMG_duanma[time_m % 10]);
    Delay_SMG(500);
    DisplaySMG_Bit(5, SMG_duanma[16]);
    Delay_SMG(500);

    DisplaySMG_Bit(6, SMG_duanma[time_s / 10]);
    Delay_SMG(500);
    DisplaySMG_Bit(7, SMG_duanma[time_s % 10]);
    Delay_SMG(500);
    P0 = 0XFF;
}
//************************************
//串口通信
uchar command = 0;

void InitUart()
{
    TMOD = 0X20;
    TH1 = 0XFD;
    TL1 = 0XFD;
    TR1 = 1;
    
    SCON = 0X50; //模式1
    AUXR = 0X00;
    
    ES = 1;
    EA = 1;
}

void ServiceUart() interrupt 4
{
    if(RI == 1)
    {
        command = SBUF;
        RI = 0;
    }
}

void SendByteUart(uchar dat)
{
    SBUF = dat;
    while(TI == 0);
    TI = 0;
}


void UartWorking()
{
    if(command != 0)
    {
        SelectHC138(4);
        switch(command & 0XF0)
        {
             case 0xA0:
                 P0 = (P0 | 0X0F) & (~command | 0XF0); //保留高4位,反转低四位
                 command = 0X00;
             break;
             case 0xB0:
                 SendByteUart((time_h / 10 << 4) | (time_h % 10));
               SendByteUart((time_m / 10 << 4) | (time_m % 10));
               SendByteUart((time_s / 10 << 4) | (time_s % 10));
                 command = 0X00;
             break;
        }
    }
}
//************************************
void main()
{
    Initsys();
    CheckSys();
    InitTimer0();
    InitUart();
    InitUart();
    while(1)
    {
        LocalCheck();
        SMGDisplay();
        UartWorking();
    }
}

15.DS18B20

51基础入门知识_第19张图片
#include "reg52.h"
#include "onewire.h"

typedef unsigned int uint;
typedef unsigned char uchar;

uint temp = 0;

uchar SMG_duanma[18] = 
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
     0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

uchar SMGDot_AC[10] = 
     {0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10};
     
//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:    //LED
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:    //蜂鸣器和继电器
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:    //位码
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:    //段码
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
        case 0:    //锁住所有寄存器
            P2 = (P2 & 0X1F) | 0X00;
        break;
    }
}
     
void Delay_SMG(uint t)
{
    while(t--);
}


//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value)
{
    SelectHC138(6);
    P0 = 0X01 << pos;
    SelectHC138(7);
    P0 = value;
}

void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

void Display_All(uchar dat)
{
    SelectHC138(6);
    P0 = 0XFF;
    SelectHC138(7);
    P0 = dat;
}

void DisplaySMG_Temp()
{
    DisplaySMG_Bit(7,SMG_duanma[temp % 10]);
    Delay_SMG(100);
    DisplaySMG_Bit(6,SMGDot_AC[(temp % 100) / 10]);
    Delay_SMG(100);
    DisplaySMG_Bit(5,SMG_duanma[temp / 100]);
    Delay_SMG(100);
    
    DisplaySMG_Bit(4,0XFF);
    Delay_SMG(100);
    DisplaySMG_Bit(3,0XFF);
    Delay_SMG(100);
    DisplaySMG_Bit(2,0XFF);
    Delay_SMG(100);
    DisplaySMG_Bit(1,0XFF);
    Delay_SMG(100);
    DisplaySMG_Bit(0,0XFF);
    Delay_SMG(100);
    Display_All(0XFF);
}

void Delay_temp(uint t)
{
    while(t--)
    {
        DisplaySMG_Temp();
    }
}

//**********************读取温度***************************/

void read_DS18B20_temp()
{
    uchar LSB,MSB;      //LSB低八位,MSB高八位
    //温度转换
    init_ds18b20();     //初始化DS18B20
    Write_DS18B20(0xcc);    //跳过ROM
    Write_DS18B20(0x44);    //开始温度转换
    Delay_OneWire(200);   //等待温度转换完成
    //读温度
    init_ds18b20();     //初始化DS18B20
    Write_DS18B20(0xcc);    //跳过ROM
    Write_DS18B20(0xbe);    //开始读温度
    //读温度
    
    LSB = Read_DS18B20();   //读取低八位数据
    MSB = Read_DS18B20();   //读取高八位数据
    
    temp = MSB;
    temp <<= 8;     //高八位右移8位
    temp = temp | LSB;  //将独立的高、低8位合并为16位
    
    if((temp & 0xf800) == 0x0000)    //判断temp的高五位来判断温度的正负,高5位全为0表示为正,全为1表示负
    {
        temp >>= 4;
        temp = temp * 10;   //temp先右移四位在乘上10相当于整数部分乘以0.625倍
        temp = temp + (LSB & 0x0f) * 0.625;  //整数部分 * 0.625 + 小数部分 *0.625
    }
}

void main()
{
    Initsys();
    while(1)
    {
        read_DS18B20_temp();
        DisplaySMG_Temp();
    }
}

16.DS12302

51基础入门知识_第20张图片
#include "reg52.h"
#include "ds1302.h"

typedef unsigned int uint;
typedef unsigned char uchar;

uchar Write_DS1302_adrr[7] = {0X80, 0X82, 0X84, 0X86, 0X88, 0X8A, 0X8C};
uchar Read_DS1302_adrr[7] = {0X81, 0X83, 0X85, 0X87, 0X89, 0X8B, 0X8D};
uchar Timer[7] = {0X24, 0X59, 0X23, 0X18, 0X04, 0X06, 0X20};

uchar code SMG_duanma[18] = 
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
     0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

void DS1302_Config()
{
    char i;
    
    Write_Ds1302(0X81, 0X00);  //打开保护
    for(i = 0; i < 7; i++)
    {
        Write_Ds1302(Write_DS1302_adrr[i], Timer[i]);
    }
    
}

void Read_DS1302_Timer()
{
    uchar i;
    for(i = 0; i < 7; i++)
    {
        Timer[i] = Read_Ds1302(Read_DS1302_adrr[i]);
    }
}

//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:    //LED
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:    //蜂鸣器和继电器
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:    //位码
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:    //段码
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
        case 0:    //锁住所有寄存器
            P2 = (P2 & 0X1F) | 0X00;
        break;
    }
}

void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value)
{
    SelectHC138(6);
    P0 = 0X01 << pos;
    SelectHC138(7);
    P0 = value;
}

void Delay_SMG(uint t)
{
    while(t--);
}


void Display_DS1302()
{
    DisplaySMG_Bit(0, SMG_duanma[Timer[2]/16]);
    Delay_SMG(100);
    DisplaySMG_Bit(1, SMG_duanma[Timer[2]%16]);
    Delay_SMG(100);
    DisplaySMG_Bit(2, SMG_duanma[16]);
    Delay_SMG(100);
    
    DisplaySMG_Bit(3, SMG_duanma[Timer[1]/16]);
    Delay_SMG(100);
    DisplaySMG_Bit(4, SMG_duanma[Timer[1]%16]);
    Delay_SMG(100);
    DisplaySMG_Bit(5, SMG_duanma[16]);
    Delay_SMG(100);
    
    DisplaySMG_Bit(6, SMG_duanma[Timer[0]/16]);
    Delay_SMG(100);
    DisplaySMG_Bit(7, SMG_duanma[Timer[0]%16]);
    Delay_SMG(100);
}

void main()
{
    Initsys();
    DS1302_Config();
    while(1)
    {
        Read_DS1302_Timer();
        Display_DS1302();
    }
}

17.555

51基础入门知识_第21张图片
#include 

typedef unsigned int uint;
typedef unsigned char uchar;

uint count_f = 0;   //计算频率
uint dat_f = 0;     //缓存上一秒频率
uchar count_t = 0;  //计算时间

uchar code SMG_duanma[18] = 
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
     0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

//配置HC138
void SelectHC138(uchar channel)
{
    switch(channel)
    {
        case 4:    //LED
            P2 = (P2 & 0X1F) | 0X80;
        break;
        case 5:    //蜂鸣器和继电器
            P2 = (P2 & 0X1F) | 0Xa0;
        break;
        case 6:    //位码
            P2 = (P2 & 0X1F) | 0Xc0;
        break;
        case 7:    //段码
            P2 = (P2 & 0X1F) | 0Xe0;
        break;
        case 0:    //锁住所有寄存器
            P2 = (P2 & 0X1F) | 0X00;
        break;
    }
}

void Initsys()
{
    SelectHC138(5);
    P0 = 0X00;//关闭蜂鸣器和继电器
    SelectHC138(4);
    P0 = 0XFF;//关闭LED
}

void Delay_SMG(uint t)
{
    while(t--);
}

//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value)
{
    SelectHC138(6);
    P0 = 0X01 << pos;
    SelectHC138(7);
    P0 = value;
}

void Init_Timer()
{
    //定时器0用作计数,8位重装
    TH0 = 0XFF;
    TL0 = 0XFF;
    
    //定时器1用作定时,定时时间50ms
    TH1 = (65535 - 50000 + 1) / 256;
    TL1 = (65535 - 50000 + 1) % 256;
    
    TMOD = 0X16;//0001 0110
    
    ET0 = 1;
    ET1 = 1;
    EA = 1;
    
    TR0 = 1;
    TR1 = 1;
}

void Service_T0() interrupt 1
{
    count_f++;
}

void Service_T1() interrupt 3
{
    TH1 = (65535 - 50000 + 1) / 256;
    TL1 = (65535 - 50000 + 1) % 256;
    
    count_t++;
    if(count_t == 20)
    {
        dat_f = count_f;
        count_f = 0;
        count_t = 0;
    }
}

void Display_SMG_F()
{
    DisplaySMG_Bit(0, SMG_duanma[15]);
    Delay_SMG(100);
    
    DisplaySMG_Bit(1, 0XFF);
    Delay_SMG(100);
    
    DisplaySMG_Bit(2, 0XFF);
    Delay_SMG(100);
    
    if(dat_f > 9999)
    {
        DisplaySMG_Bit(3, SMG_duanma[dat_f / 10000]);
        Delay_SMG(100);
    }
    if(dat_f > 999)
    {
        DisplaySMG_Bit(4, SMG_duanma[(dat_f / 1000) % 10]);
        Delay_SMG(100);
    }
    if(dat_f > 99)
    {
        DisplaySMG_Bit(5, SMG_duanma[(dat_f / 100) % 10]);
        Delay_SMG(100);
    }
    if(dat_f > 9)
    {
        DisplaySMG_Bit(6, SMG_duanma[(dat_f / 10) % 10]);
        Delay_SMG(100);
    }
    DisplaySMG_Bit(7, SMG_duanma[dat_f % 10]);
    Delay_SMG(100);
    
    SelectHC138(6);
    P0 = 0XFF;
    SelectHC138(7);
    P0 = 0XFF;
}

void main()
{
    Initsys();
    Init_Timer();
    while(1)
    {
        Display_SMG_F();
    }
}

18.IIC

PCF8591

光敏电阻

//Light_read(0X01)光敏//0x03  RB2(AD)

uchar Light_read(uchar add)
{
    uchar temp;
    IIC_Start();
    IIC_SendByte(0X90);
    IIC_WaitAck();
    IIC_SendByte(add);
    IIC_WaitAck();
    IIC_Stop();
    
    IIC_Start();
    IIC_SendByte(0X91);
    IIC_WaitAck();
    temp = IIC_RecByte();
    IIC_Stop();
    return temp;
}

AT24C02

EEPROM

/*
EEPROM_write(0X01, 126);
delayms(5) //延时5ms
EEPROM_read(0X01)
*/

//读取
uchar EEPROM_read(uchar add)
{
    uchar temp;
    IIC_Start();
    IIC_SendByte(0XA0);
    IIC_WaitAck();
    IIC_SendByte(add);
    IIC_WaitAck();
    IIC_Stop();
    
    IIC_Start();
    IIC_SendByte(0XA1);
    IIC_WaitAck();
    temp = IIC_RecByte();
    IIC_Stop();
    return temp;
}


//写入
void EEPROM_write(uchar add, uchar dat)
{
    unsigned char temp;
    IIC_Start();
    IIC_SendByte(0XA0);
    IIC_WaitAck();
    IIC_SendByte(add);
    IIC_WaitAck();
    IIC_SendByte(dat);
    IIC_WaitAck();
    IIC_Stop();
}

DS1302

//Init_DS1302()就是初始化
//uchar shijian[] = {0, 30, 8, 0, 0, 0, 0};
//Read_DS1302()while里写

void Init_DS1302()
{
    uchar i, add;
    add = 0X80;  //写
    Write_Ds1302(0X8e, 0X00);
    for(i = 0; i < 7; i++)
    {
        Write_Ds1302(add, shijian[i]);
        add = add + 2;
    }
    Write_Ds1302(0X8e, 0X80);
}

void Read_DS1302()
{
    uchar i, add;
    add = 0X81;  //读
    Write_Ds1302(0X8e, 0X00);
    for(i = 0; i < 7; i++)
    {
        shijian[i] = Read_Ds1302(add);
        add = add + 2;
    }
    Write_Ds1302(0X8e, 0X80);
}

你可能感兴趣的:(蓝桥,单片机)