基于普中科技的单片机开发实验仪编写的数字表
1、项目简单说明
一、显示日期,对ds1302芯片进行计时所得的日期用数码管进行显示;
二、显示温度,对18B20芯片测量所得的温度用数码管进行显示;
三、进行计时,从零开始进行计时,用定时器中断对秒进行加一操作,数码管显示计时时间;
四、进行倒计时,运用矩阵键盘进行倒计时时间的设置,用定时器中断对秒进行减一操作,数码管显示倒计时时间,时间为零时蜂鸣器响起,响起后需按下相关按键结束;
五、对时间进行设置,运用矩阵键盘进行当前时间的设置,数码管显示设置时间,设置完成后ds1302芯片会从设置时间开始重新计时;
六、对日期进行设置,即对当前日期进行设置。
使用此数字表时,应先对日期进行设置,再对时间进行设置,这样才能得到当前的正确时间。
基本框图如下:
2、代码编写
main函数
#include<reg52.h>
#include"type.h"
#include"temperature.h"
#include"digital.h"
#include"count.h"
#include"ds1302.h"
#include"key.h"
#include"ring.h"
void main(){
init_ds1302(); //初始化ds1302
init_counter(); //初始化定时器1
digital_flag = close;
EA= 1;
while(1){
if(digital_flag == open){
function(); //功能显示
if(count_up_flag == close)
main_key_check(); //独立按键检测
}
if(ring_flag == open)
player(); //蜂鸣器唱歌
switch_check(); //数码管显示开关
}
}
1、type
type.h
#ifndef _TYPE_H
#define _TYPE_H
typedef unsigned char uchar;
typedef unsigned int uint;
#define open 1
#define close 0
void Delay1ms(uint x); //延时x ms
void delay(uint x); //延时
#endif
type.c
#include"type.h"
void Delay1ms(uint x){
uint i, j;
for(; x > 0; x--)
for(i = 199; i > 0; i--)
for(j = 1; j > 0; j--) ;
}
void delay(uint x){
uint i, j;
for(i = x; i > 0; --i)
for(j = 110; j > 0; --j) ;
}
2、temperature
temperature.h
#ifndef _TEMPERATURE_H
#define _TEMPERATURE_H
#include<reg52.h>
#include"type.h"
extern uint temperature;
sbit temp = P3^7; //数据发收位
void init_temp(); //初始化温感芯片
uchar read_temp(); //读取芯片数据
void write_temp(uchar temp_data); //对芯片写入命令
void temp2digital(); //将读取的数据转换为十进制
#endif
temperature.c
#include"temperature.h"
#include"digital.h"
uint temperature = 10;
void init_temp(){ //初始化芯片
temp = 0;
delay(1);
temp = 1;
show_temp();
}
uchar read_temp(){ //读取数据
uchar temp_byte, temp_bit;
uint i, j;
for(i = 0; i < 8; ++i){
temp = 0;
j++;
temp = 1;
j++; j++;
temp_bit = temp;
temp_byte = (temp_byte >> 1) | (temp_bit << 7);
j = 4;
while(j--);
}
return temp_byte;
}
void write_temp(uchar temp_data){ //写入数据
uint i, j;
for(i = 0; i < 8; ++i){
temp = 0;
j++;
temp = temp_data & 0x01;
j= 6;
while(j--);
temp = 1;
temp_data >>= 1;
}
}
void temp2digital(){ //将获取的温度转化为可用数码管显示的数据
uchar tempL, tempH;
uint i = 10;
init_temp();
delay(1);
write_temp(0xcc);
write_temp(0x44);
while(i--) show_temp();
init_temp();
delay(1);
write_temp(0xcc);
write_temp(0xbe);
tempL = read_temp();
tempH = read_temp();
//将温度的高8位与低8位组合乘以0.0625即为温度值,后面乘以100是为了保留两位小数位, 加上0.5四舍五入
temperature = ((tempH * 256) + tempL) * 0.0625 * 100 + 0.5;
}
3、digital
digital.h
#ifndef _DIGITAL_H
#define _DIGITAL_H
#include<reg52.h>
#include"type.h"
#define SMG7 P0
sbit LS_A = P2^2; //数码管位选
sbit LS_B = P2^3;
sbit LS_C = P2^4;
sbit digital_flag = P1^0; //数码管显示标志
extern uchar time_show[]; //时间显示数组
extern uchar temp_show[]; //温度显示数组
extern uchar time_temp; //时间暂存
extern bit flash_flag; //闪烁标志
void get_time_now(uint hour, uint min, uintsec); //获取当前时间
void get_time_count(uint hour, uint min,uint sec); //获取计时时间
void get_temp(uint temp); //获取显示温度
void flash(uint flash_bit); //选定位闪烁
void show_time(); //显示时间
void show_temp(); //显示温度
void show_func(uchar show); //显示功能序号
#endif
digital.c
#include"digital.h"
#include"count.h"
uchar digital_show[] = {0x3f, 0x06, 0x5b,0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x40, 0x00, 0x39};
// 0 1 2 3 4 5 6 7 8 9 - C
uchar time_show[] = {0, 0, 10, 0, 0, 10, 0,0}; //数码管显示时间数组
uchar temp_show[] = {11, 11, 11, 0, 0, 0,0, 12}; //数码管显示温度数组
bit flash_flag = open;
uchar time_temp = 0;
void get_time_now(uint hour, uint min, uintsec){ //获取现在的时间
time_show[0] = hour / 16;
time_show[1] = hour % 16;
time_show[3] = min / 16;
time_show[4] = min % 16;
time_show[6] = sec / 16;
time_show[7] = sec % 16;
}
void get_time_count(uint hour, uint min,uint sec){ //获取计时的时间
time_show[0] = hour / 10;
time_show[1] = hour % 10;
time_show[3] = min / 10;
time_show[4] = min % 10;
time_show[6] = sec / 10;
time_show[7] = sec % 10;
}
void get_temp(uint temp){ //获取温度
temp_show[3] = temp % 10000 / 1000;
temp_show[4] = temp % 1000 / 100;
temp_show[5] = temp % 100 / 10;
temp_show[6] = temp % 10;
}
void show_time(){ //显示时间
LS_A = 0; LS_B = 0; LS_C = 0;
SMG7 = digital_show[time_show[7]]; delay(1);
LS_A= 1; LS_B = 0; LS_C = 0;
SMG7 = digital_show[time_show[6]]; delay(1);
LS_A = 0; LS_B = 1; LS_C &#