51单片机

一些没有分类的知识:
数据类型

点亮led灯

led灯模块

点亮一个led灯

1
2
3
4
5
6
7
8
9
10
#include <REGX52.H>

void main()
{
P2=0x00;// 0101 0101
while(1)
{

}
}

0x是一个前缀,代表后面数字为十六进制
2025-9-8
2025-9-8日,第一次完成一个单片机小任务,点亮了一个led灯,非常有成就感。目前进程,江协51单片机第四个视频。

led灯闪烁

STV-ISP中有个功能叫软件延时计时器,可以通过这个功能生成延时代码写入程序当中。延时代码是一个函数,直接在主函数中调用即可。

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
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms(void) //@12.000MHz
{
unsigned char data i, j, k;

_nop_();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{

while(1)
{
P2=0xFE;// 1111 1110
Delay500ms();
P2=0XFD; // 1111 1101
Delay500ms();


}
}

流水线式灯光

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
#include <REGX52.H>
#include <INTRINS.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char data i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
Delay1ms(500);
while(1)
{
P2=0xFE;// 1111 1110
Delay1ms(100);
P2=0XFD; // 1111 1101
Delay1ms(100);
P2=0xFB;// 1111 1011
Delay1ms(100);
P2=0xF7;// 1111 0111
Delay1ms(100);
P2=0xEF;// 1110 1111
Delay1ms(100);
P2=0xDF;// 1101 1111
Delay1ms(100);
P2=0xBF;// 1011 1111
Delay1ms(100);
P2=0x7F;// 0111 1111
Delay1ms(100);


}
}

独立按键

独立按键电路图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <REGX52.H>
#include <INTRINS.H>
void main()
{
while(1)
{
if(P3_1==0)
{
P2_0=0;
}
else
{
P2_0=1;
}
}

}

这行代码表示,按下第一个按键,可以是第一个led灯亮。
按键抖动

用独立按键控制led灯的状态

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
#include <REGX52.H>
#include <INTRINS.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char data i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
while(1)
{
if(P3_1==0)
{
Delay1ms(20);
while(P3_1==0);
Delay1ms(20);

P2_1=~P2_1;

}

}
}

独立按键控制led显示二进制
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
#include <REGX52.H>
#include <INTRINS.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char data i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
while(1)
{
if(P3_1==0)
{
Delay1ms(20);
while(P3_1==0);
Delay1ms(20);

P2++;

}

}
}

表示二进制

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
#include <REGX52.H>
#include <INTRINS.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char data i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
unsigned char led=0;
while(1)
{
if(P3_1==0)
{
Delay1ms(20);
while(P3_1==0);
Delay1ms(20);

led++;
P2=~led;
}

}
}

数码管

译码器
数码管

静态数码管显示

第四个数码管上显示6;

1
2
3
4
5
6
7
8
9
10
11
12
#include <REGX52.H>
#include <INTRINS.H>
void main()
{
P2_4=1;
P2_3=0;
P2_2=0;
P0=0x7D;
while(1)
{
}
}

数码管显示代码完整版

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
#include <REGX52.H>
#include <INTRINS.H>

unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void Nixie(unsigned char Location,Number)
{
switch(Location)
{
case 1:P2_4=1;P2_3=1;P2_2=1;break;
case 2:P2_4=1;P2_3=1;P2_2=0;break;
case 3:P2_4=1;P2_3=0;P2_2=1;break;
case 4:P2_4=1;P2_3=0;P2_2=0;break;
case 5:P2_4=0;P2_3=1;P2_2=1;break;
case 6:P2_4=0;P2_3=1;P2_2=0;break;
case 7:P2_4=0;P2_3=0;P2_2=1;break;
case 8:P2_4=0;P2_3=0;P2_2=0;break;
}
P0=NixieTable[Number];
}
void main()
{
Nixie(1,9);
while(1)
{

}
}

动态数码管显示

数码管的消影
需要在每一个字段之后做一个清零操作;

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
#include <REGX52.H>
#include <INTRINS.H>

void Delay(unsigned int xms) //@12.000MHz
{
unsigned char data i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}

}


unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void Nixie(unsigned char Location,Number)
{
switch(Location)
{
case 1:P2_4=1;P2_3=1;P2_2=1;break;
case 2:P2_4=1;P2_3=1;P2_2=0;break;
case 3:P2_4=1;P2_3=0;P2_2=1;break;
case 4:P2_4=1;P2_3=0;P2_2=0;break;
case 5:P2_4=0;P2_3=1;P2_2=1;break;
case 6:P2_4=0;P2_3=1;P2_2=0;break;
case 7:P2_4=0;P2_3=0;P2_2=1;break;
case 8:P2_4=0;P2_3=0;P2_2=0;break;
}
P0=NixieTable[Number];
Delay(1);
P0=0x00;
}
void main()
{
while(1)
{
Nixie(4,5);
Nixie(2,7);
Nixie(3,3);
Nixie(5,5);
Nixie(6,6);
Nixie(7,0);
Nixie(8,8);
}
}

模块化编程

LCD1602A液晶屏

液晶屏

矩阵键盘

MatrixKey.c

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
#ifndef __MatrixKey_H__
#define __MatrixKey_H__
#include <REGX52.H>
#include "Delay.h"
unsigned char MatixKey()
{
unsigned char KeyNumber=0;

P1=0xFF;
P1_3=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=13;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=9;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=5;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=1;}

P1=0xFF;
P1_2=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=14;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=10;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=6;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=2;}

P1=0xFF;
P1_1=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=15;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=11;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=7;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=3;}

P1=0xFF;
P1_0=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=16;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=12;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=8;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=4;}

return KeyNumber;
}

#endif

MatrixKey.h

1
unsigned char MatixKey();

main()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <REGX52.H>
#include "LCD1602.h"
#include "MatrixKey.h"
unsigned char KeyNum;

void main()
{
LCD_Init();
LCD_ShowString(1,1,"Hello,World");
while(1)
{
KeyNum=MatixKey();
if(KeyNum)
{
LCD_ShowNum(2,1,KeyNum,2);
}
}
}

电子锁

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
#include <REGX52.H>
#include "LCD1602.h"
#include "MatrixKey.h"
unsigned char KeyNum;
unsigned int password,count;
void main()
{
LCD_Init();
LCD_ShowString(1,1,"password:");
while(1)
{
KeyNum=MatixKey();
if(KeyNum)
{
if(KeyNum<=10)
{
if(count<4)
{
password*=10;
password+=KeyNum%10;
count++;
}
LCD_ShowNum(2,1,password,4);
}
if(KeyNum==11)
{
if(password==210)
{
LCD_ShowString(1,14,"OK ");
password=0;
count=0;
LCD_ShowNum(2,1,password,4);
}
else
{
LCD_ShowString(1,14,"ERR");
password=0;
count=0;
LCD_ShowNum(2,1,password,4);
}
}
if(KeyNum==12)
{
password=0;
count=0;
LCD_ShowNum(2,1,password,4);
}
}
}
}

定时器

STM32