答案仅供参考,有错误欢迎指正,非常感谢!!!
文件较多,代码较长,感谢查看!!!
目录
main.h
#ifndef __MAIN_H_
#define __MAIN_H_
#define uchar unsigned char
#define uint unsigned int
#include <STC15F2K60S2.H>
#include "Init.h"
#include "SMG.h"
#include "DSQ.h"
#include "YanShi.h"
#include "JZKey.h"
#include "ds1302.h"
#include "iic.h"
#include "CSB.h"
void CeLiang_JuLi_ShuChu_DianYa();
void SMG_XianShi();
void LED_Show();
#endif
main.c
#include "main.h"
uchar flag0=0;//定时器0标志位
uchar jie_main=0;//界面
uchar xian_shi=0;//显示
uchar can_shu=0;//参数
uchar mo_shi=0;//模式
uchar zhi_flag=0;//最大、最小、平均值的显示标志位
uint code ShiJian_CanShu[]={2000,3000,5000,7000,9000};//时间参数
uchar JuLi_CanShu=20;//距离参数
uint ju_li=0;//距离
uint time=0;//时间
uchar flag=0;//时间参数选择标志位
uint Max=0;//最大值
uint Min=0;//最小值
uint PingJunZhi=0;//平均值
long int sum=0;//距离的总和
uint ci_shu=0;//检测距离的次数
uint ShiJian_CanShu1=2000;//时间参数,确定值
uchar JuLi_CanShu1=20;//距离参数,确定值
uchar ADC_date=0;//采集到的光敏数据
uchar ADC_date_old=0;//上一次采集到的光敏数据
uchar ADC_flag=1;//采集光敏数据标志位,模拟中断
uchar GM_Liang_flag=0;//光敏处于亮环境的标志位
uchar CSB_CaiJi_flag=0;//在触发模式下,允许超声波数据采集标志位
int ju_li1=0;//连续3次采集的距离
int ju_li2=0;
int ju_li3=0;
uchar L5_LED_flag=0;//L5LED是否开启标志位
uchar date=0;//输出电压数据
void main(){
uchar key_num=0;
uchar key_num_old;//上一个键值
Init_BZ();
SMG_Init();
DSQ_0_Init();
JZKey_Init();
DS1302_XieDate(20,19,50);
while(1){
EA=0;
DS1302_GetDate();
EA=1;
key_num_old=key_num;
key_num=JZKey_GetKeynum();
if(key_num!=key_num_old){
if(key_num==4){
jie_main++;
jie_main=jie_main%2;
if(jie_main==1){//每次从数据显示界面进入参数设置界面,默认当前为采集时间设置。
can_shu=0;
}
if(jie_main==0){//每次从参数设置界面进入数据显示界面,默认当前为时间数据显示。所有参数均在退出参数界面时生效,参数调整过程中不生效。
xian_shi=0;
ShiJian_CanShu1=ShiJian_CanShu[flag];
JuLi_CanShu1=JuLi_CanShu;
}
}
else if(key_num==5){
if(jie_main){//参数设置界面
can_shu++;
can_shu=can_shu%2;
}
else{//数据显示界面
xian_shi++;
xian_shi=xian_shi%3;
if(xian_shi==2){//每次进入数据记录显示界面,默认为当前显示为测距最大值。
zhi_flag=0;
}
}
}
else if(key_num==8){
if(jie_main==0&&xian_shi==1){//图10,切换模式
mo_shi++;
mo_shi=mo_shi%2;
}
else if(jie_main==0&&xian_shi==2){//图11,切换显示
zhi_flag++;
zhi_flag=zhi_flag%3;
}
}
else if(key_num==9){
if(jie_main==1&&can_shu==0){//参数设置界面,采集时间参数,图12 采集时间参数调整
flag++;
flag=flag%5;
}
else if(jie_main==1&&can_shu==1){//参数设置界面,距离参数,图13 距离参数调整
JuLi_CanShu=JuLi_CanShu+10;
if(JuLi_CanShu>80){
JuLi_CanShu=10;
}
}
}
}
//距离采集
if(time>=ShiJian_CanShu1&&mo_shi==1){//中断标志位开启,处于定时采集距离模式
time=0;
CeLiang_JuLi_ShuChu_DianYa();
}
if(ADC_flag>=100){//光敏数据采集
ADC_date_old=ADC_date;
ADC_date=IIC_ADC_GetDate(1);
CSB_CaiJi_flag=0;
ADC_flag=0;
if(ADC_date>55){//光敏处于亮环境
GM_Liang_flag=1;
}
if(ADC_date_old>ADC_date&&ADC_date_old<55&&ADC_date<55&&GM_Liang_flag==1&&mo_shi==0){//由亮变暗,触发模式
GM_Liang_flag=0;
CSB_CaiJi_flag=1;
CeLiang_JuLi_ShuChu_DianYa();
}
}
IIC_DAC_XieDate(date);
}
}
void DSQ_0_ZD() interrupt 1{
flag0++;
ADC_flag++;
if(mo_shi==1){//如果在定时模式下
time++;
}
if(mo_shi==0){//触发模式
ADC_flag++;
}
if(flag0>=1){
flag0=0;
SMG_XianShi();
LED_Show();
}
}
void CeLiang_JuLi_ShuChu_DianYa(){//测量距离,输出电压
ju_li=(uint)CSB_GetJuLi();
if(ju_li!=999){//距离为有效值
if(mo_shi==1){
ju_li1=ju_li2;
ju_li2=ju_li3;
ju_li3=ju_li;
//“连续”测量到的3次距离数据在距离参数“附近”(±5cm),L5点亮,否则熄灭。
if(((ju_li1-JuLi_CanShu1)>=-5)&&((ju_li1-JuLi_CanShu1)<=5)&&((ju_li2-JuLi_CanShu1)>=-5)&&((ju_li2-JuLi_CanShu1)<=5)&&((ju_li3-JuLi_CanShu1)>=-5)&&((ju_li3-JuLi_CanShu1)<=5)){
L5_LED_flag=1;
}
else{
L5_LED_flag=0;
}
}
else{
L5_LED_flag=0;
}
if(ju_li<10){
date=51;
}
else if(ju_li>=10&&ju_li<=80){
date=((ju_li*204.0)/70.0)+51.0-(204.0/7.0);
}
else{
date=255;
}
if(ju_li>Max){//记录最大值
Max=ju_li;
}
if(ju_li<Min||Min==0){//记录最小值
Min=ju_li;
}
sum=sum+ju_li;
ci_shu++;
PingJunZhi=((sum*1.0)/(ci_shu*1.0))*10;//记录平均值
}
else{
L5_LED_flag=0;
date=255;
}
}
void SMG_XianShi(){//数码管显示
if(jie_main==0){//数据显示界面
switch(xian_shi){
case 0:{//时间显示
SMG_Show(shi/10,shi%10,17,fen/10,fen%10,17,miao/10,miao%10);
break;
}
case 1:{//距离显示
if(mo_shi==0){//触发模式
if(ju_li>=100){
SMG_Show(21,12,16,16,16,ju_li/100,(ju_li/10)%10,ju_li%10);
}
else if(ju_li>=10){
SMG_Show(21,12,16,16,16,16,ju_li/10,ju_li%10);
}
else if(ju_li>=0){
SMG_Show(21,12,16,16,16,16,16,ju_li%10);
}
}
else if(mo_shi==1){//定时模式
if(ju_li>=100){
SMG_Show(21,15,16,16,16,ju_li/100,(ju_li/10)%10,ju_li%10);
}
else if(ju_li>=10){
SMG_Show(21,15,16,16,16,16,ju_li/10,ju_li%10);
}
else if(ju_li>=0){
SMG_Show(21,15,16,16,16,16,16,ju_li%10);
}
}
break;
}
case 2:{//数据记录
switch(zhi_flag){
case 0:{//最大值
if(Max>=1000){
SMG_Show(18,43,16,16,Max/1000,(Max/100)%10,(Max/10)%10,Max%10);
}
else if(Max>=100){
SMG_Show(18,43,16,16,16,Max/100,(Max/10)%10,Max%10);
}
else if(Max>=10){
SMG_Show(18,43,16,16,16,16,Max/10,Max%10);
}
else if(Max>=0){
SMG_Show(18,43,16,16,16,16,16,Max%10);
}
break;
}
case 1:{//最小值
if(Min>=1000){
SMG_Show(18,45,16,16,Min/1000,(Min/100)%10,(Min/10)%10,Min%10);
}
else if(Min>=100){
SMG_Show(18,45,16,16,16,Min/100,(Min/10)%10,Min%10);
}
else if(Min>=10){
SMG_Show(18,45,16,16,16,16,Min/10,Min%10);
}
else if(Min>=0){
SMG_Show(18,45,16,16,16,16,16,Min%10);
}
break;
}
case 2:{//平均值
if(PingJunZhi>=1000){
SMG_Show(18,44,16,16,PingJunZhi/1000,(PingJunZhi/100)%10,((PingJunZhi/10)%10)+32,PingJunZhi%10);
}
else if(PingJunZhi>=100){
SMG_Show(18,44,16,16,16,PingJunZhi/100,((PingJunZhi/10)%10)+32,PingJunZhi%10);
}
else if(PingJunZhi>=0){
SMG_Show(18,44,16,16,16,16,(PingJunZhi/10)+32,PingJunZhi%10);
}
break;
}
}
break;
}
}
}
else if(jie_main==1){//参数设置界面
if(can_shu==0){//采集时间设置界面
SMG_Show(24,1,16,16,16,16,0,ShiJian_CanShu[flag]/1000);
}
else if(can_shu==1){//距离参数设置界面
SMG_Show(24,2,16,16,16,16,JuLi_CanShu/10,JuLi_CanShu%10);
}
}
}
void LED_Show(){//LEd显示
uchar L1_2_3=0xff;
uchar L4=0xff;
uchar L5=0xff;
uchar L6=0xff;
if(jie_main==0){//数据显示界面
switch(xian_shi){
case 0:{//时间显示 L1:处于时间数据显示界面,L1点亮,否则熄灭。
L1_2_3=0xfe;
break;
}
case 1:{//距离显示 L2:处于距离数据显示界面,L2点亮,否则熄灭。
L1_2_3=0xfd;
break;
}
case 2:{//数据记录 L3:处于数据记录显示界面,L3点亮,否则熄灭。
L1_2_3=0xfb;
break;
}
}
}
else{
L1_2_3=0xff;
}
if(mo_shi==0){//L4:处于触发模式下,L4点亮,否则熄灭。
L4=0xf7;
}
else{
L4=0xff;
}
if(L5_LED_flag){//L5:在定时模式下,“连续”测量到的3次距离数据在距离参数“附近”(±5cm),L5点亮,否则熄灭。
L5=0xef;
}
else{
L5=0xff;
}
if(ADC_date>55){//L6:当光敏电阻RD1采集的环境光线状态为“亮”,指示灯L6点亮,否则熄灭。
L6=0xdf;
}
else{
L6=0xff;
}
P0=L1_2_3&L4&L5&L6;
P2=(P2&0x1f)|0x80;
P2=(P2&0x1f)|0x00;
}
Init.h
#ifndef __INIT_H_
#define __INIT_H_
#include <STC15F2K60S2.H>
void Init_BZ();
#endif
Init.c
#include "Init.h"
void Init_BZ(){
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2=(P2&0x1f)|0x80;
P0=0xff;
}
SMG.h
#ifndef __SMG_H_
#define __SMG_H_
#include <STC15F2K60S2.H>
void SMG_Init();
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);
#endif
SMG.c
#include "SMG.h"
unsigned char code SMG_ZK[]={ //????
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46,0x01,0x40,0x08}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1.高.中.低
void SMG_Init(){
P2=(P2&0x1f)|0xc0;
P0=0xff;
P2=(P2&0x1f)|0xe0;
P0=0xff;
}
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8){
static unsigned char i=0;
i++;
i=i%8;
P2=(P2&0x1f)|0xc0;
switch(i){
case 1:{
P0=0x01;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n1];
break;
}
case 2:{
P0=0x02;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n2];
break;
}
case 3:{
P0=0x04;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n3];
break;
}
case 4:{
P0=0x08;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n4];
break;
}
case 5:{
P0=0x10;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n5];
break;
}
case 6:{
P0=0x20;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n6];
break;
}
case 7:{
P0=0x40;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n7];
break;
}
case 0:{
P0=0x80;
P2=(P2&0x1f)|0xe0;
P0=~SMG_ZK[n8];
break;
}
}
P2=(P2&0x1f)|0x00;
}
DSQ.h
#ifndef __DSQ_H_
#define __DSQ_H_
#include <STC15F2K60S2.H>
void DSQ_0_Init();
#endif
DSQ.c
#include "DSQ.h"
void DSQ_0_Init(){
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;
ET0=1;
}
YanShi.h
#ifndef __YANSHI_H_
#define __YANSHI_H_
#include <STC15F2K60S2.H>
#include "intrins.h"
void Delay10ms();
#endif
YanShi.c
#include "YanShi.h"
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
JZKey.h
#ifndef __JZKEY_H_
#define __JZKEY_H_
#include "YanShi.h"
void JZKey_Init();
unsigned char JZKey_GetKeynum();
#endif
JZKey.c
#include "JZKey.h"
void JZKey_Init(){
P30=1;
P31=1;
P32=1;
P33=1;
P34=0;
P35=0;
P42=0;
P44=0;
}
unsigned char JZKey_GetKeynum(){
unsigned char key_num=0;
if(P30==0){
P30=0;
P34=1;
P35=1;
P42=1;
P44=1;
if(P34==0){
Delay10ms();
if(P34==0){
key_num=19;
}
}
else if(P35==0){
Delay10ms();
if(P35==0){
key_num=15;
}
}
else if(P42==0){
Delay10ms();
if(P42==0){
key_num=11;
}
}
else if(P44==0){
Delay10ms();
if(P44==0){
key_num=7;
}
}
}
else if(P31==0){
P31=0;
P34=1;
P35=1;
P42=1;
P44=1;
if(P34==0){
Delay10ms();
if(P34==0){
key_num=18;
}
}
else if(P35==0){
Delay10ms();
if(P35==0){
key_num=14;
}
}
else if(P42==0){
Delay10ms();
if(P42==0){
key_num=10;
}
}
else if(P44==0){
Delay10ms();
if(P44==0){
key_num=6;
}
}
}
else if(P32==0){
P32=0;
P34=1;
P35=1;
P42=1;
P44=1;
if(P34==0){
Delay10ms();
if(P34==0){
key_num=17;
}
}
else if(P35==0){
Delay10ms();
if(P35==0){
key_num=13;
}
}
else if(P42==0){
Delay10ms();
if(P42==0){
key_num=9;
}
}
else if(P44==0){
Delay10ms();
if(P44==0){
key_num=5;
}
}
}
else if(P33==0){
P33=0;
P34=1;
P35=1;
P42=1;
P44=1;
if(P34==0){
Delay10ms();
if(P34==0){
key_num=16;
}
}
else if(P35==0){
Delay10ms();
if(P35==0){
key_num=12;
}
}
else if(P42==0){
Delay10ms();
if(P42==0){
key_num=8;
}
}
else if(P44==0){
Delay10ms();
if(P44==0){
key_num=4;
}
}
}
JZKey_Init();
return key_num;
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
#include <STC15F2K60S2.H>
#include <intrins.h>
extern unsigned char shi,fen,miao;
sbit SCK = P1^7;
sbit SDA1 = P2^3;
sbit RST = P1^3;
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s);
void DS1302_GetDate();
#endif
ds1302.c
#include "ds1302.h"
unsigned char shi,fen,miao;
//写字节
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK = 0;
SDA1 = temp&0x01;
temp>>=1;
SCK=1;
}
}
//向DS1302寄存器写入数据
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
//从DS1302寄存器读出数据
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA1)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA1=0; _nop_();
SDA1=1; _nop_();
return (temp);
}
void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s){
h=(h/10)*16+(h%10);
m=(m/10)*16+(m%10);
s=(s/10)*16+(s%10);
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(0x84,h);
Write_Ds1302_Byte(0x82,m);
Write_Ds1302_Byte(0x80,s);
Write_Ds1302_Byte(0x8e,0x80);
}
void DS1302_GetDate(){
shi=Read_Ds1302_Byte(0x85);
fen=Read_Ds1302_Byte(0x83);
miao=Read_Ds1302_Byte(0x81);
shi=((shi/16)*10)+(shi%16);
fen=((fen/16)*10)+(fen%16);
miao=((miao/16)*10)+(miao%16);
}
iic.h
#ifndef _IIC_H
#define _IIC_H
#include <STC15F2K60S2.H>
#include "intrins.h"
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
unsigned char IIC_ADC_GetDate(unsigned char di_zhi);
void IIC_DAC_XieDate(unsigned char date);
#endif
iic.c
#include "iic.h"
#define DELAY_TIME 5
//I2C总线内部延时函数
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//I2C总线启动信号
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//I2C总线停止信号
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答或非应答信号
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//I2C总线发送一个字节数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//I2C总线接收一个字节数据
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
unsigned char IIC_ADC_GetDate(unsigned char di_zhi){
unsigned char date;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(di_zhi);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
date=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return date;
}
void IIC_DAC_XieDate(unsigned char date){
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
CSB.h
#ifndef __CSB_H_
#define __CSB_H_
#define FS P10
#define JS P11
#include <STC15F2K60S2.H>
#include "intrins.h"
void Delay13us();
void CSB_FS_MC();
float CSB_GetJuLi();
#endif
CSB.c
#include "CSB.h"
void Delay13us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 36;
while (--i);
}
void CSB_FS_MC(){
unsigned char i=8;
while(i>0){
i--;
FS=1;
Delay13us();
FS=0;
Delay13us();
}
}
float CSB_GetJuLi(){
unsigned int time=0;
float ju_li;
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TH1=0;
TL1=0;
ET1=0;
CSB_FS_MC();
TR1=1;
while(JS==1&&TF1==0);
TR1=0;
if(JS==0&&TF1==0){
time=((unsigned int)TH1<<8)|(unsigned int)TL1;
ju_li=(time*1.0*0.017)/12.0;
}
else {
ju_li=999;
TF1=0;
}
TH1=0;
TL1=0;
return ju_li;
}