我们先把底层写好,后面就容易使用了
模板使用的为up西风的模板进行学习,对内容进行扩展,根据原理进行扩展,做到能根据所给资源包进行编写,后面的文件内容改为14届所使用资源包
1. 我们首先先把锁存器写好
#include "reg52.h"
#include "HC573.h"
void SelectHC573(unsigned char n)
{
switch(n)
{
case 4://LED
P2=(P2&0x1f)|0x80;
P2 &= 0x1f;
break;
case 5://蜂鸣器与继电器
P2=(P2&0x1f)|0xa0;
P2 &= 0x1f;
break;
case 6://数码管段位
P2=(P2&0x1f)|0xc0;
P2 &= 0x1f;
break;
case 7://数码管内容
P2=(P2&0x1f)|0xe0;
P2 &= 0x1f;
break;
case 0:
P2=(P2&0x1f)|0x00;
break;
}
}
2. 按键底层
按键的选择与题目和下面的跳线帽有关,跳线帽左,代表独立按键,右代表矩阵按键
#include <Key.h>
#include <STC15F2K60S2.H>
//unsigned char Key_Read()
//{
// unsigned char temp = 0;
// P44 = 0;P42 = 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;
// 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;
//}
unsigned char Key_Read()
{
unsigned char temp = 0;
if(P33 == 0) temp = 4; //S7
if(P32 == 0) temp = 5; //S6
if(P31 == 0) temp = 6; //S5
if(P30 == 0) temp = 7; //S4
return temp;
}
3. 数码管显示
#include <Seg.h>
#include "HC573.h"
/* 新蓝桥杯中断码会给出 *。
unsigned char seg_dula[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0xc6};
unsigned char seg_wela[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void Seg_Disp(unsigned char wela,dula,point)
{
/* 消隐 */
P0 = 0xff;
//位置
SelectHC573(7);
P0 = seg_wela[wela];
//内容
SelectHC573(6);
P0 = seg_dula[dula];
//小数点
if(point)
P0 &= 0x7f;
SelectHC573(7);
}
4. LED显示
#include <LED.h>
#include "HC573.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;
SelectHC573(4);
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;
SelectHC573(5);
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;
SelectHC573(5);
temp_old = temp;
}
}
5.init初始化函数
void System_Init()
{
P0 = 0xff;
SelectHC573(4);
P0 = 0x00;
SelectHC573(5);
}
整体大模板
/* 头文件包含 */
#include <STC15F2K60S2.H>
#include <Init.h>
#include <Led.h>
#include <Key.h>
#include <Seg.h>
#include "ds1302.h"
#include "onewire.h"
/* 变量声明 */
unsigned char Key_Val,Key_Down,Key_Old,Key_Up; //按键判断函数
unsigned char Key_Slow_Down; //按键减速函数
unsigned char Seg_Buf[8] = {10,10,10,10,10,10,10,10}; //数码管显示函数
unsigned char Seg_Point[8] = {0,0,0,0,0,0,0,0}; //数码管小数显示
unsigned char Seg_Pos; //数码管位显示,一直持续
unsigned int Seg_Slow_Down; //数码管减速函数
unsigned char ucLed[8] = {0,0,0,0,0,0,0,0}; //LED显示函数
unsigned char Alarm[3] = {0x00,0x00,0x00}; //时钟数组
unsigned char Alarm_Set[3] = {0x00,0x00,0x00};
unsigned char ucRtc[3] = {0x23,0x59,0x55}; //时钟初始化地址
unsigned char ucRtc_Set[3];
unsigned char ucRtc_Set_Index;
unsigned char dat; //读取AD数值
unsigned char* Set_Index[3] = {ucRtc,ucRtc_Set,Alarm_Set};
unsigned char Set_Flag;
unsigned char Temperature; //温度读取函数
// 标志位函数
bit Led_Star_Flag;
bit Seg_Star_Flag;
bit Seg_Disp_Mode;
bit Beep_Flag;
/* 按键处理函数 */
void Key_Proc()
{
unsigned char i;
if(Key_Slow_Down) return;
Key_Slow_Down = 1; //按键减速
Key_Val = Key_Read(); //按键读取
Key_Down = Key_Val & (Key_Old ^ Key_Val); //下降发生——按下后处理
Key_Up = ~Key_Val & (Key_Old ^ Key_Val); //上升发生——松开后处理
Key_Old = Key_Val; //持续按下
switch(Key_Down) //判断按下的按键
{
}
}
/* 数码管处理函数 */
void Seg_Proc()
{
unsigned char i;
if(Seg_Slow_Down) return; //数码管减速函数
Seg_Slow_Down = 1;
/* 信息获取函数 */
Read_Rtc(ucRtc); //读取时钟值
Temperature = rd_temperature(); //显示温度值
dat = Ad_Read(0x41); //读取0x01里的数据(1通道),0x03为3通道R2滑动变阻器,同时读取就把回去地址改变就可以
Da_Write(127); //直接写入DA
/* 数码管模式显示函数—— 由自己根据题目编写 */
// 只要在你需要的数码管位处写入你想要的数值即可,小数点也是相同的
/* 举例AD的读取 */
/* Seg_Buf[0] = dat / 100 % 10;
Seg_Buf[0] = dat / 100 % 10;
Seg_Buf[0] = dat / 100 % 10; */
}
/* LED处理函数 */
void Led_Proc()
{
/* 因为下面已经有显示,我们只需要在数组里面填入哪个灯亮就可以了 */
// ex :ucLed[0] = 1 ; 就代表第一个灯亮
}
/* 定时器初始化函数 */
void Timer0Init(void) //@12.000MHz
{
AUXR &= 0x7F;
TMOD &= 0xF0;
TL0 = 0x18;
TH0 = 0xFC;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
/* 定时器服务函数 */
void Timer0Server() interrupt 1
{
/* 减速部分 */
if(++Key_Slow_Down == 10) Key_Slow_Down = 0;
if(++Seg_Slow_Down == 500) Seg_Slow_Down = 0;
if(++Seg_Pos == 8) Seg_Pos = 0;
/* 显示部分 */
Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Seg_Point[Seg_Pos]);
Led_Disp(Seg_Pos,ucLed[Seg_Pos]);
/* 在这部分内容里面,上面的显示不需要再改,只需要在下面再补上一些计时功能即可
}
/* Main */
void main()
{
Set_Rtc(ucRtc);
System_Init();
Timer0Init();
while (1)
{
Key_Proc();
Seg_Proc();
Led_Proc();
}
}