感觉这次题有点小难,太多的限制条件了,而且感觉逻辑很绕,现在这份代码只能说我觉得应该全部功能实现了。
main.c
#include"reg52.h"
#include"intrins.h"
#include"ds1302.h"
#include"iic.h"
sbit TX=P1^0;
sbit RX=P1^1;
sfr AUXR=0x8e;
sfr P4=0xC0;
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C4=P3^4;
sbit C3=P3^5;
sbit C2=P4^2;
sbit C1=P4^4;
char code write_add[3]={0x80,0x82,0x84};
char code read_add[3]={0x81,0x83,0x85};
char time[3]={0x01,0x20,0x20};
int last_distance;
int all_mode;
int smg_mode;
int set_mode;
int set_time=2;
int set_distance=20;
int distance_mode=0;
int num_mode=0;
int rb1;
int led=0xff;
int distance_work=0;
int alarm_work=0;
int max;int min;int avg;
char rom_distance[50];
int distance_num=0;
char code xianshi[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xc7,0x8e,0xc6,0x89,0xfe,0xbf,0xf7,0x8c,0x7f};
void find_max();
void find_min();
void find_avg();
void smg_display();
void choose_573(int i)
{
switch(i)
{
case(0):P2=(P2&0x1f)|0x00;break;
case(4):P2=(P2&0x1f)|0x80;break;
case(5):P2=(P2&0x1f)|0xa0;break;
case(6):P2=(P2&0x1f)|0xc0;break;
case(7):P2=(P2&0x1f)|0xe0;break;
}
}
void init_system()
{
choose_573(4);
P0=0xff;
choose_573(5);
P0=0x00;
choose_573(0);
}
//=========================================时间
void write_time()
{
int i;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<3;i++)
{
Write_Ds1302_Byte(write_add[i],time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void read_time()
{
int i;
for(i=0;i<3;i++)
{
time[i]=Read_Ds1302_Byte(read_add[i]);
}
}
//=========================================
//=========================================距离
void Delay12us() //@11.0592MHz
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 30;
while (--i);
}
void send_wave()
{
int i;
for(i=0;i<8;i++)
{
TX=1;
Delay12us();
TX=0;
Delay12us();
}
}
void get_distance()
{
int time;
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xAE; //设置定时初始值
TH1 = 0xFB; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 0; //定时器1开始计时
TL1 = 0x00; //设置定时初始值
TH1 = 0x00; //设置定时初始值
send_wave();
TR1=1;
while(RX==1&&TF1==0);
TR1=0;
if(TF1==0)
{
time=TH1;
time=(time<<8)|TL1;
last_distance=time*0.017;
last_distance=last_distance/12;
}
else
{
last_distance=999;
TF1=0;
}
TL1 = 0x00; //设置定时初始值
TH1 = 0x00; //设置定时初始值
}
//=========================================
//=========================================按键
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void key_board()
{
R1=R2=R4=1;R3=0;
C1=C2=C3=C4=1;
//S5
if(C1==0)
{
Delay10ms();
if(C1==0)
{
if(all_mode==0)
{
smg_mode++;
if(smg_mode==2)
{
num_mode=0;
find_max();
find_min();
find_avg();
}
if(smg_mode>=3)
{
smg_mode=0;
}
}
if(all_mode==1)
{
set_mode++;
if(set_mode>=2)
{
set_mode=0;
}
}
}
while(!C1);
{
smg_display();
}
}
//S9
if(C2==0&&all_mode==1)
{
Delay10ms();
if(C2==0&&all_mode==1)
{
if(set_mode==0)
{
set_time++;
if(set_time>9)
{
set_time=2;
}
}
if(set_mode==1)
{
set_distance=set_distance+10;
if(set_distance>80)
{
set_distance=10;
}
}
}
while(!C2)
{
smg_display();
}
}
R1=R2=R3=1;R4=0;
C1=C2=C3=C4=1;
//S4
if(C1==0)
{
Delay10ms();
if(C1==0)
{
if(all_mode==0)
{
all_mode=1;
set_mode=0;
}
else
{
all_mode=0;
smg_mode=0;
}
}
while(!C1)
{
smg_display();
}
}
//S8
if(C2==0&&all_mode==0)
{
Delay10ms();
if(C2==0&&all_mode==0)
{
if(smg_mode==1)
{
if(distance_mode==0)
{
distance_mode=1;
}
else
{
distance_mode=0;
}
}
if(smg_mode==2)
{
num_mode++;
if(num_mode>=3)
{
num_mode=0;
}
}
}
while(!C2)
{
smg_display();
}
}
}
//=========================================
//=========================================光敏and距离获得
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初始值
TH0 = 0x28; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
int count_1;
void timer0_server() interrupt 1
{
if(distance_mode==1)
{
count_1++;
if(count_1>=(set_time*200))
{
distance_work=1;
count_1=0;
}
}
else
{
count_1=0;
}
}
void get_rb1()
{
rb1=re_rb1();
}
int temp_mode_1=0;
void judge_distance()
{
if(distance_mode==0)//触发模式
{
if(rb1<=100&&temp_mode_1==0)
{
temp_mode_1=1;
get_distance();
rom_distance[distance_num]=last_distance;
distance_num++;
}
if(rb1>100)
{
temp_mode_1=0;
}
}
if(distance_mode==1)//定时模式
{
if(distance_work==1)
{
get_distance();
distance_work=0;
rom_distance[distance_num]=last_distance;
distance_num++;
if(distance_num>2)
{
if(rom_distance[distance_num-1]<(set_distance+5)&&
rom_distance[distance_num-1]>(set_distance-5)&&
rom_distance[distance_num-2]<(set_distance+5)&&
rom_distance[distance_num-2]>(set_distance-5)&&
rom_distance[distance_num-3]<(set_distance+5)&&
rom_distance[distance_num-3]>(set_distance-5))
{
alarm_work=1;
}
else
{
alarm_work=0;
}
}
}
}
}
//=========================================
//=========================================计算平均值,寻找最大最小值
void find_max()
{
int i=0;
for(i=1;i<distance_num;i++)
{
if(rom_distance[i-1]>=rom_distance[i])
{
max=rom_distance[i-1];
}
else
{
max=rom_distance[i];
}
}
}
void find_min()
{
int i=0;
for(i=1;i<distance_num;i++)
{
if(rom_distance[i-1]>=rom_distance[i])
{
min=rom_distance[i];
}
else
{
min=rom_distance[i-1];
}
}
}
void find_avg()
{
int i;int sum;
for(i=0;i<distance_num;i++)
{
sum=sum+rom_distance[i]*10;
}
avg=sum/distance_num;
}
//=========================================
//=========================================数码管
void Delay400us() //@11.0592MHz
{
unsigned char i, j;
i = 5;
j = 74;
do
{
while (--j);
} while (--i);
}
void SMG(int wei,int dat)
{
choose_573(6);
P0=0x80>>(wei-1);
choose_573(7);
P0=xianshi[dat];
choose_573(0);
Delay400us();
}
void smg_display()
{
if(all_mode==0)
{
if(smg_mode==0)
{
SMG(1,time[0]%16);
SMG(2,time[0]/16);
SMG(3,16);
SMG(4,time[1]%16);
SMG(5,time[1]/16);
SMG(6,16);
SMG(7,time[2]%16);
SMG(8,time[2]/16);
}
if(smg_mode==1)
{
SMG(1,last_distance%10);
if(last_distance>=10)
{
SMG(2,(last_distance%100)/10);
}
else
{
SMG(2,10);
}
if(last_distance>=100)
{
SMG(3,(last_distance%1000)/100);
}
else
{
SMG(3,10);
}
SMG(4,10);
SMG(5,10);
// SMG(5,rb1/100);//测试
SMG(6,10);
if(distance_mode==0)
{
SMG(7,13);
}
else
{
SMG(7,12);
}
SMG(8,11);
}
if(smg_mode==2)
{
if(num_mode==0)
{
SMG(1,max%10);
SMG(2,max/10);
SMG(3,10);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,15);
SMG(8,14);
}
if(num_mode==1)
{
SMG(1,min%10);
SMG(2,min/10);
SMG(3,10);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,16);
SMG(8,14);
}
if(num_mode==2)
{
SMG(1,avg%10);
SMG(2,19);
SMG(2,(avg%100)/10);
SMG(3,(avg%1000)/100);
SMG(4,10);
SMG(5,10);
SMG(6,10);
// SMG(6,distance_num);//测试
SMG(7,17);
SMG(8,14);
}
}
}
if(all_mode==1)
{
if(set_mode==0)
{
SMG(1,set_time%10);
SMG(2,set_time/10);
SMG(3,10);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,(set_mode+1));
SMG(8,18);
}
if(set_mode==1)
{
SMG(1,set_distance%10);
SMG(2,set_distance/10);
SMG(3,10);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,(set_mode+1));
SMG(8,18);
}
}
}
//=========================================
//=========================================led
void led_work()
{
if(all_mode==0)
{
//L1
if(smg_mode==0)
{
led=(led&0xfe)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xfe)|0x01;
choose_573(4);
P0=led;
choose_573(0);
}
//L2
if(smg_mode==1)
{
led=(led&0xfd)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xfd)|0x02;
choose_573(4);
P0=led;
choose_573(0);
}
//L3
if(smg_mode==2)
{
led=(led&0xfb)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xfb)|0x04;
choose_573(4);
P0=led;
choose_573(0);
}
//L4
if(smg_mode==1&&distance_mode==0)
{
led=(led&0xf7)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xf7)|0x08;
choose_573(4);
P0=led;
choose_573(0);
}
//L5
if(alarm_work==1)
{
led=(led&0xef)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xef)|0x10;
choose_573(4);
P0=led;
choose_573(0);
}
if(rb1>=100)
{
led=(led&0xdf)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xdf)|0x20;
choose_573(4);
P0=led;
choose_573(0);
}
}
}
//=========================================
//=========================================
void DA_out()
{
if(last_distance<=10)
{
DA(51);
}
if(last_distance>=80)
{
DA(255);
}
if(last_distance>10&&last_distance<80)
{
DA(((0.057*last_distance)+1)*51);
}
}
//=========================================
void main()
{
init_system();
write_time();
Timer0Init();
while(1)
{
read_time();
key_board();
smg_display();
// get_distance();//测试
get_rb1();
judge_distance();
led_work();
DA_out();
}
}
iic.c
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
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; // 0:应答,1:非应答
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;
}
int re_rb1()
{
int temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x01);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
void DA(int dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
iic.h
#ifndef _IIC_H
#define _IIC_H
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);
int re_rb1();
void DA(int dat);
#endif
ds1302.c
/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include
#include
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
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;
}
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(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
#endif