蓝桥杯大模板

工程文件夹的建立

capture_20240312194726442

首先建立文件夹,包含两个文件夹

接着new project,命名文件为LED,选择芯片

capture_20240312194901674

魔术棒勾选生成HEX文件魔术棒选择头文件路径Driver

capture_20240312195554597

底层文件的编写

1.第一个底层:init(初始化)

两个文件

init.c和init.h

.h文件引用自身的头文件

capture_20240312200807732

capture_20240312200800202

双击driver,把init.c引用进文件夹。并在c引用h自身,h文件引用单片机型号头文件

初始化函数(关闭继电器蜂鸣器)

1
2
3
4
5
6
7
8
9
10
#include <init.h>
void system_init()
{
P0 = 0xff;//开锁存器
P2 = P2 & 0x1f | 0x80;
P2 &= 0x1f;//用完即关
P0 = 0x00;
P2 = P2 & 0x1f | 0xa0;
P2 &= 0x1f;
}

接着在.h里声明该函数:

capture_20240312203553010

第二个底层:LED.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <LED.h>
void LED_disp(unsigned char addr,enable)

{
static unsigned char temp = 0x00;
static unsigned char temp_old = 0xff;
if(enable)
temp |= 0x01 << addr;
else
temp &=~(0x01 << addr);
if(temp != temp_old)
{
P0 = ~temp;
P2 = P2 & 0x1f | 0x80;
P2 = P2 & 0x1f;
temp_old = temp;
}
}

仍然是两个文件。

双击driver,把LED.c引用进文件夹。并在c引用h自身,h文件引用单片机型号头文件

capture_20240312205801226

第三个底层:独立按键和矩阵键盘

.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
#include <Key.h>

unsigned char Key_Read()
{
unsigned char temp = 0;
P44 = 0;P42 = 1;P35 = 1;P34 = 1;//第一列(0为选中)
if(P33 == 0) temp = 4;
if(P32 == 0) temp = 5;
if(P31 == 0) temp = 6;
if(P30 == 0) temp = 7;
P44 = 1;P42 = 0;P35 = 1;P34 = 1;//第二列
if(P33 == 0) temp = 8;
if(P32 == 0) temp = 9;
if(P31 == 0) temp = 10;
if(P30 == 0) temp = 11;
P44 = 1;P42 = 1;P35 = 0;P34 = 1;第三列
if(P33 == 0) temp = 12;
if(P32 == 0) temp = 13;
if(P31 == 0) temp = 14;
if(P30 == 0) temp = 15;
P44 = 1;P42 = 1;P35 = 1;P34 = 0;//第四列
if(P33 == 0) temp = 16;
if(P32 == 0) temp = 17;
if(P31 == 0) temp = 18;
if(P30 == 0) temp = 19;
return temp;

}

需要注意的是,独立按键和矩阵按键是通过开发板的跳线帽进行选择的。

capture_20240312215145294

但是仿真的芯片并非蓝桥比赛的板子,矩阵按键有些端口和蓝桥不一样,这里我们以蓝桥的板子为主。

第四个底层:共阳极数码管

.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <Seg.h>

unsigned char seg_dula[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x88};
unsigned char seg_wela[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

void Seg_Disp(unsigned char wela,dula,point)
{
P0 = 0xff;
P2 = P2 & 0x1f | 0xe0;
P2 &= 0x1f;

P0 = seg_wela[wela];
P2 = P2 & 0x1f | 0xc0;
P2 &= 0x1f;

P0 = seg_dula[dula];
if(point)
P0 &= 0x7f;
P2 = P2 & 0x1f | 0xe0;
P2 &= 0x1f;
}

熟练使用ISP烧录工具,内含部分范例程序。

主函数建立

分别引用之前的两个底层到主函数里,我们测试led模块和初始化是否正常:

capture_20240312213044098

第一位是第几个(第一位是0,第二位是1.)第二位是是否亮(1为亮)

放入仿真里运行

capture_20240312213229762

十分稳健。