每次建好工程文件夹,里边包含User(放工程文件,mian.c,可以在这里写如同我这个文章的文本文档)、Driver(存放底层文件如Led.c,Led.h等)
新建的工程先搭建框架,可以先书写底层函数(此次书写了五个函数并包含相应的头文件共十个底层文件)
底层函数内容:
1.初始化底层驱动专用文件
比如先用3个IO口控制74HC138译码器,控制Y4为低电平;当Y4为低电平时,或非门74HC02控制Y4C为高电平,使74HC573的OE端口有效,OE端口有效时,可使用P0口控制LED的亮灭。
可以去多了解74HC138译码器,74HC02或非门,74HC573八路输出透明锁存器的相关内容会更好理解
#include <Init.h>
//关闭外设
void System_Init()
{
P0 = 0xff;
P2 = P2 & 0x1f | 0x80;
P2 &= 0x1f;
P0 = 0x00;
P2 = P2 & 0x1f | 0xa0;
P2 &= 0x1f;
}
//头文件
#include <STC15F2K60S2.H>
void System_Init();
2.Led底层驱动专用文件
与初始化底层驱动专用文件同理,需要了解对应的锁存器控制,可以在使用的芯片数据手册查看
#include <Led.h>
void Led_Disp(unsigned char addr,enable)//LED
{
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 &= 0x1f;
temp_Old = temp;
}
}
void Beep(unsigned char flag)//蜂鸣器
{
static unsigned char temp = 0x00;
static unsigned char temp_Old = 0xff;
if(flag)
temp |=0x40 ;
else
temp &= ~ 0x40 ;
if(temp != temp_Old)
{
P0 = ~ temp;
P2 = P2 & 0x1f | 0xa0;
P2 &= 0x1f;
temp_Old = temp;
}
}
void Relay(unsigned char flag)//继电器
{
static unsigned char temp = 0x00;
static unsigned char temp_Old = 0xff;
if(flag)
temp |= 0x10 ;
else
temp &= ~ 0x10 ;
if(temp != temp_Old)
{
P0 = ~ temp;
P2 = P2 & 0x1f | 0xa0;
P2 &= 0x1f;
temp_Old = temp;
}
}
//头文件
#include <STC15F2K60S2.H>
void Led_Disp(unsigned char addr,enable);
3.按键底层驱动专用文件
(板子上的按键从按键4开始到按键19,可根据实际硬件修改)
#include <Key.h>
unsigned char Key_Read()
{
unsigned char temp = 0;
P44 = 0;P42 = 1; P35 = 1;P34 = 1;//这个仿真没有P4口,不适用,但是实际运行使用这个
P37 = 0; P36 = 1; P35 = 1; P34 = 1;
if(P33 == 0) temp = 4;
if(P32 == 0) temp = 5;
if(P31 == 0) temp = 6;
if(P30 == 0) temp = 7;
P37 = 1; P36 = 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;
P37 = 1; P36 = 1; P35 = 0; P34 = 1;
if(P33 == 0) temp = 12;
if(P32 == 0) temp = 13;
if(P31