程序员书籍笔记 程序员书籍笔记
  • HTML
  • CSS
  • JavaScript
  • 前端知识
  • Vue
  • MarkDown
  • git
  • Node.js
  • Linux
  • 51单片机
  • 四级
  • 第一学期课程
  • 操作系统
  • 计算机网络
  • 数据结构
  • 计算机组成原理
  • HTML5
  • Electron
  • 日记便签
  • 前端导航
GitHub (opens new window)
  • HTML
  • CSS
  • JavaScript
  • 前端知识
  • Vue
  • MarkDown
  • git
  • Node.js
  • Linux
  • 51单片机
  • 四级
  • 第一学期课程
  • 操作系统
  • 计算机网络
  • 数据结构
  • 计算机组成原理
  • HTML5
  • Electron
  • 日记便签
  • 前端导航
GitHub (opens new window)
  • Linux

  • MySQL

  • 51单片机

  • exam

    • 自用文档
      • 快速使用
      • LED显示
        • led点亮函数
      • 串口显示
        • 初始化
        • 发送字符串
      • 通用工具
        • 延时函数
      • 外部资源
        • LED1602显示
        • LED12864显示
        • 独立键盘
        • C提供的标准库头文件
      • 非接口代码块
        • 共阴极0-9显示
        • 共阳极显示位选码
        • 共阴极显示为选码
        • 倒计时及重置程序
        • 定时器
        • 计数器
        • 中断程序
        • 串口通信
      • test
        • 串口接收代码块
        • 3S自动关闭
        • 警报
        • exam2
        • 12864显示
  • 系统和硬件
  • exam
yuadh
2022-02-03
目录

自用文档

# 快速使用

将 making.h 等文件导入到项目目录下开始使用

加入头文件

#include "making"
1

注意:常用51头文件已经加入making头文件非必要不需要自己加头文件

# LED显示

# led点亮函数

//LED点亮函数 指定点亮某引脚的led
void ledOn(uint i,uchar state){
    if(i==0){
        P0 = state;
    }else if(i==1){
        P1 = state;
    }else if(i==2){
        P2 = state;
    }else if(i==3){
        P3 = state;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

使用案例

ledOn(3,0x01);//点亮3端口的 0000 0001 灯
1
  • 注意仿真图的连接方式及共阴极还是共阳极

# 串口显示

# 初始化

void ck_init(uchar baud){
    TMOD |= 0x20;
    SCON = 0x50;    
    TH1 = baud;
    TL1 = baud;
    PCON=0X80; //波特率加倍
    EA = 1;
    TR1 = 1;
}
1
2
3
4
5
6
7
8
9

使用

ck_init(0xf4);
1
  • 最好使用4800波特率

# 发送字符串

void ck_WriteDat(uchar dat){
    SBUF = dat;
    while(!TI);
    TI = 0;
}
void ck_WriteStr(uchar *str){
    while(*str){
        ck_WriteDat(*str++);
    }
}
1
2
3
4
5
6
7
8
9
10

使用

 ck_WriteStr("测试");
1
  • 在串口助手中可以显示
  • 注意文件的字符集要和系统一致不然串口助手无法显示

# 通用工具

# 延时函数

void delayms(int m){
    int n=m*1000;
    TMOD |= 0X01;//0001 0000
    TR0 = 1;
    TH0 = (65536-n)/256;
    TL0 = (65536-n)%256;
    while (!TF0);
    TF0=0; 
}
1
2
3
4
5
6
7
8
9

使用案例

delayms(1000);//延时1s
1
  • 输入任意整型字符 n 延时n毫秒
  • 该定时器使用定时器 0

# 外部资源

# LED1602显示

# LED12864显示

void lineDisplay(uchar x1,uchar x2,uchar *x3,uchar x4,uchar x5){
    int i;
    uchar line = x1,colum = x2,length = x4,typ = x5;
    uchar *dat = x3;
    LCD12864_Init();
    for(i=0;i<length;i++){
        if(i<(length/2)){
            LCD12864_SelectScreen(0);
        }else {
            LCD12864_SelectScreen(1);
        }
        if(typ == 1){
            LCD12864_Show1616(line,colum,dat);
            dat += 32;
        }else {
            LCD12864_Show816(line,colum,dat);
            dat += 16;
        }
        colum += 16;
        if(colum > 63){
            colum = 0;
        }
    }
     for(i=0;i<300000;i++){
            _nop_();
        }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  • 使用
lineDisplay(0,0,WORDS,8,1);
1

# 独立键盘

# C提供的标准库头文件

# 字符串比较函数

#include <string.h> //头文件
strcmp(CKDAT, "ON"); //字符串相等返回0  
1
2

# 非接口代码块

# 共阴极0-9显示

uchar numbs[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
1

# 共阳极显示位选码

uchar count[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
1

# 共阴极显示为选码

0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
1

# 倒计时及重置程序

#include <reg51.h>
void delay(unsigned int i);
void start();
void restart();
sbit button=P3^2;
sbit reset=P3^3;
unsigned char numbs[]={0xC0,0xF9,0xA4,0xB0,0x99,
                       0x92,0x82,0xF8,0x80,0x90};
int i=0;int j=6;
int main(){
    //涓€鍒嗛挓鍊掕鏃剁▼搴?
    //delay(1000) 寤舵椂1s 60-00
    start();
}
void start(){
    if(!button){
        P1=numbs[i];
        P2=numbs[j];
        while(1){
            if(!reset){
                restart();
                P1=numbs[i];P2=numbs[j];
            }
            while(button);
            if(i!=9){
				delay(1000);
			}
            if(i==0&&j==0){
                P1=numbs[0];P1=numbs[0];
                restart();
                while(reset);
                continue;
            }
            
            if(i!=-1){
                 P1=numbs[i--];
            }else if(i==-1){//49
			 	i=9;
				P1=numbs[i];
                P2=numbs[--j];
            }
        }
    }
}
void restart(){
    i=0;j=6;
}
void delay(unsigned int  i)	
{
unsigned int j,k;
for(k=0;k<i;k++)
for(j=0;j<110;j++);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

# 定时器

# 工作方式1

void mode0_work1(){
    TMOD |= 0x01;
    TH0 = (65536-1000)/256;
    TL0 = (65536-1000)%256;
    TR0 = 1;
    while(!TF0);
    TF0 = 0;
}
1
2
3
4
5
6
7
8

# 工作方式二

# 计数器

# 工作方式2

void mode0_work2(){
    TMOD = 0X06;// 启动T0方式2    0000 0110
    TH0=TL0=256-1;// 按键按下一次数码管显示+1 0000 0000
    TR0=1; //启动计数器
    myShow();
    //检查按键是否按下  此处为计数器
    if(TF0==1){
       TF0=0;
       {//代码块}
    } 
}
1
2
3
4
5
6
7
8
9
10
11

# 中断程序

# 定时器中断

//60秒的秒表 使用中断

#include <regx52.h>
void ledOn();
sbit led = P1^0;
int flag = 0;
int i = 80;
//位选码 控制数位管的位显示数字 
int count[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
//段位码 对显示数字的控制 0-9 
int numbs[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
int main(){
    ledOn();
    return 0;
}

void ledOn(){
    //对1s进行显示
    TMOD = 0x01; //T0方式1
    TH0 = (65536-5000)/256;
    TL0 = (65536-5000)%256;
    EA = 1;
    ET0 = 1; //定时器中断
    TR0 = 1; //开启定时器
    while(1){//等待中断
        //5ms显示  
        if(flag%2==0){
           P2=count[7];
           P1=numbs[i%10];
        }else{
           P2=count[6];
           P1=numbs[i/10];
        }
    }
}

void ing() interrupt 1{
    TH0 = (65536-5000)/256;
    TL0 = (65536-5000)%256;
    flag++;
    if(flag==200){//200*5
        flag=0;
        if(i==0){
            i=80;
        }
        i--;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

# 外部中断

# 串口通信

设置波特率

void init(){
    EA = 1;//开启总中断
    TMOD |= 0x20;//设置计数器工作方式2
    TH1 = 0xf4;
    TL1 = 0xf4;
    TR1 = 1;//开启计数器
    SCON = 0x50;
}
1
2
3
4
5
6
7
8

# test

# 串口接收代码块

void main(){//exam程序 
   ES = 1;//开启串口中断
   ck_init(0xf4);
   while(1){
      _nop_();//延时函数等待
      if(!strcmp(CKDAT, "ON")){
         led = 1;
         // ck_WriteStr("LED灯亮了");
      }else if(!strcmp(CKDAT, "OFF")){
         led = 0;
         // ck_WriteStr("LED灯灭了");
      }
      _nop_();
      flag = 0;memset(CKDAT, 0, sizeof(CKDAT));
   };
}

void ck_service() interrupt 4{
   RI = 0;//没接收到一个数据就触发一次串口中断
   CKDAT[flag] = SBUF;
   SBUF = "\0";
   while(!TI);
   TI = 0;
   flag++;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 3S自动关闭

void main(){//exam程序 
   //利用定时器工作方式2 自动检测
   led = 1;
   TMOD = 0x02;
   TH0 = 6;
   TL0 = 6;
   EA = 1;//开启总中断
   ET0 = 1;//开启定时器中断
   TR0 = 1;//开启计数器
   while(1);
}
void dsj_service() interrupt 1{
   TF0 = 0;flag++;
   if(flag==12000){//12000*250 =3s
      if(led == 1){
         led = 0;
      }
      flag = 0;
   }
} 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 警报

void init(){
   TMOD |= 1;//设置定时器
   TR0 = 1;
   ET0 =1;//定时器中断
}
//警报
void beepservice() interrupt 1{
    TL0 = 64536%256;
    TH0 = 64536/256;
    if(beepTime){
        beepTime--;
        beep = ~beep;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# exam2

#include "making.h" 
sbit led = P1^0;
sbit show1 = P3^0;
sbit show2 = P3^1;
sbit key1 = P3^2;   
sbit key2 = P3^3;   
uchar numbs[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar CKDAT[101];
uint LedTime=0,flag=0,beepTime=0;
void init(){
   TMOD |= 1;//设置定时器
   TR0 = 1;
   // ET0 =1;//定时器中断
}
void main(){//exam程序  测试其它串口
   init();
   ES = 1;//开启串口中断
   ck_init(0xfd);//9600波特率
   while(1){
      if(!strcmp(CKDAT, "ON")){
         led = 1;
         ck_WriteStr("LED灯亮了");
      }else if(!strcmp(CKDAT, "OFF")){
         led = 0;
         ck_WriteStr("LED灯灭了");
         beepTime = 200;
      }
      flag = 0;memset(CKDAT, 0, sizeof(CKDAT));
      //定时器工作2
      TMOD |= 0x02;
      TH0 = 6;
      TL0 = 6;
      TF0 = 0;
      LedTime++;
      if(LedTime==6000){//12000*250 =3s
         if(led == 1){
            led = 0;
         }
         LedTime = 0;
      }
      TF0 = 0;
      //报警程序
      TMOD |= 0x01;
      TL0 = 64536%256;
      TH0 = 64536/256;
      if(beepTime){
         beepTime--;
         beep = ~beep;
      }
   };
}

void ck_service() interrupt 4{
   if(RI){//接收串口中断
     RI = 0;
     CKDAT[flag] = SBUF;
     SBUF = SBUF;
     while(!TI);
     TI = 0;
     flag++;
   }else if(TI){//发送串口中断
     TI = 0;
   }
}
// //警报
// void beepservice() interrupt 1{
//    TMOD = 0x01;
//    TL0 = 64536%256;
//    TH0 = 64536/256;
//    if(beepTime){
//        beepTime--;
//        beep = ~beep;
//    }
//    TMOD = 0x02;
//    TH0 = 6;
//    TL0 = 6;
//    TF0 = 0;
//    LedTime++;
//    if(LedTime==12000){//12000*250 =3s
//       if(led == 1){
//          led = 0;
//       }
//       LedTime = 0;
//    }
// }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

# 12864显示

int i;
	uchar line = 0,colum = 0,length = 8,typ = 1;
   uchar *dat;
   dat = WORDS;
   //  uchar *dat; dat = "test";
    LCD12864_Init();
    for(i=0;i<length;i++){
        if(i<(length/2)){
            LCD12864_SelectScreen(0);
        }else {
            LCD12864_SelectScreen(1);
        }
        if(typ == 1){
            LCD12864_Show1616(line,colum,dat);
            dat += 32;
        }else {
            LCD12864_Show816(line,colum,dat);
            dat += 16;
        }
        colum += 16;
        if(colum > 63){
            colum = 0;
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
编辑 (opens new window)
上次更新: 2023/02/07, 14:51:48
SD卡

← SD卡

Theme by Vdoing | Copyright © 2021-2023 yuadh
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×