第一次写国赛题,写得非常不流畅,断断续续写了两天左右,而且那个超声波测距感觉还是有点问题,但是大部分应该没有问题了,所以大家可以看看,如果发现问题,欢迎提出!
题目:
源代码:
main.c
#include"reg52.h"
#include"intrins.h"
#include"ds1302.h"
#include"iic.h"
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;
sbit TX=P1^0;
sbit RX=P1^1;
sfr AUXR=0x8e;
unsigned int code write_add[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned int code read_add[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned int code xianshi[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
unsigned int time[]={0x00,0x59,0x06};
int mima_wei=0;
int mima[]={6,5,4,3,2,1};
int suru_mima[]={0,0,0,0,0,0};
int new_mima[]={0,0,0,0,0,0};
int smg_mode=1;
int door=0;
int distance=0;
int sure=0;
int error_time=0;
int alarm=0;
int control=0x00;
int temp1=654321;
void choose_573(int n)
{
switch(n)
{
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);
P0=0xff;
}
//=================================ds1302
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]);
}
}
//=================================
//=================================keyboard
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void key_board()
{
R1=0;R2=R3=R4=1;
C1=C2=C3=C4=1;
if(C1==0)
{
Delay10ms();
if(C1==0)
{
if(smg_mode==1)
{
suru_mima[mima_wei]=0;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=0;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C1);
}
if(C2==0)
{
Delay10ms();
if(C2==0)
{
if(smg_mode==1)
{
suru_mima[mima_wei]=1;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=1;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C2);
}
if(C3==0)
{
Delay10ms();
if(C3==0)
{
if(smg_mode==1)
{
suru_mima[mima_wei]=2;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=2;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C3);
}
if(C4==0)
{
Delay10ms();
if(C4==0)
{
if(smg_mode==1)
{
suru_mima[mima_wei]=3;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=3;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C4);
}
R2=0;R1=R3=R4=1;
C1=C2=C3=C4=1;
if(C1==0)
{
Delay10ms();
if(C1==0)
{
if(smg_mode==1)
{
suru_mima[mima_wei]=4;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=4;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C1);
}
if(C2==0)
{
Delay10ms();
if(C2==0)
{
if(smg_mode==1)
{
suru_mima[mima_wei]=5;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=5;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C2);
}
if(C3==0)
{
Delay10ms();
if(C3==0)
{
if(smg_mode==1)
{
suru_mima[mima_wei]=6;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=6;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C3);
}
if(C4==0)
{
Delay10ms();
if(C4==0)
{
Delay10ms();
if(smg_mode==1)
{
suru_mima[mima_wei]=7;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=7;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C4);
}
R3=0;R1=R2=R4=1;
C1=C2=C3=C4=1;
if(C1==0)
{
Delay10ms();
if(C1==0)
{
if(smg_mode==1)
{
suru_mima[mima_wei]=8;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=8;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C1);
}
if(C2==0)
{
Delay10ms();
if(C2==0)
{
if(smg_mode==1)
{
suru_mima[mima_wei]=9;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
if(smg_mode==2)
{
new_mima[mima_wei]=9;
mima_wei++;
if(mima_wei==8)
{
mima_wei=0;
}
}
}
while(!C2);
}
//setting
if(C3==0)
{
int i;
Delay10ms();
if(C3==0)
{
smg_mode=2;
mima_wei=0;
for(i=0;i<6;i++)
{
suru_mima[i]=0;
new_mima[i]=0;
}
}
while(!C3);
}
//restart
if(C4==0)
{
Delay10ms();
if(C4==0)
{
mima[0]=6;mima[1]=5;mima[2]=4;mima[3]=3;mima[4]=2;mima[5]=1;
mima_wei=0;
smg_mode=1;
}
while(!C4);
}
R4=0;R1=R2=R3=1;
C1=C2=C3=C4=1;
//sure
if(C3==0)
{
Delay10ms();
if(C3==0)
{
if(smg_mode==1)
{
int i;int temp=0;
for(i=0;i<6;i++)
{
if(suru_mima[i]!=mima[i])
{
temp=1;
}
}
for(i=0;i<6;i++)
{
suru_mima[i]=0;
}
if(temp==0)
{
door=1;
error_time=0;
}
else
{
door=0;
temp=0;
error_time++; //alarm
}
if(error_time>=3) //alarm
{
alarm=1;
error_time=0;
}
mima_wei=0;
}
if(smg_mode==2)
{
int i=0;
for(i=0;i<6;i++)
{
mima[i]=new_mima[i];
}
sure=1;
mima_wei=0;
temp1=new_mima[5]+new_mima[4]*10+new_mima[3]*100+new_mima[2]*1000+new_mima[1]*10000+new_mima[0]*100000;
write_EEPROM(0x01,temp1);
}
}
while(!C3);
}
//quit
if(C4==0&&sure==1)
{
int i;
Delay10ms();
if(C4==0&&sure==1)
{
sure=0;
smg_mode=1;
for(i=0;i<6;i++)
{
new_mima[i]=0;
}
}
while(!C4);
}
}
//=================================
//=================================distance
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 = 0x00; //设置定时初始值
TH1 = 0x28; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 0; //
TL1=0x00;
TH1=0x00;
TR1=1;
send_wave();
while(RX==1&&TF1==0);
TR1=0;
if(TF1==0)
{
time=TH1;
time=(time<<8)|TL1;
distance=time*0.017;
}
}
void distance_door()
{
if(smg_mode==0)
{
get_distance();
if(distance<=30)
{
door=1;
}
}
}
//=================================
//=================================smg_mode
void setting_smg()
{
if((time[2]%16+(time[2]/16)*10)>=7&&(time[2]%16+(time[2]/16)*10)<=21)
{
smg_mode=0;
}
// else if((time[2]%16+(time[2]/16)*10)<7&&(time[2]%16+(time[2]/16)*10)>21&&smg_mode==2)
// {
// smg_mode=2;
// }
// else
// {
// smg_mode=1;
// }
}
//=================================
//=================================smg
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(smg_mode==0)
{
SMG(1,time[0]%16);
SMG(2,time[0]/16);
SMG(3,11);
SMG(4,time[1]%16);
SMG(5,time[1]/16);
SMG(6,11);
SMG(7,time[2]%16);
SMG(8,time[2]/16);
}
if(smg_mode==1)
{
SMG(1,suru_mima[5]);
SMG(2,suru_mima[4]);
SMG(3,suru_mima[3]);
SMG(4,suru_mima[2]);
SMG(5,suru_mima[1]);
SMG(6,suru_mima[0]);
SMG(7,11);
SMG(8,11);
}
if(smg_mode==2&&sure==0)
{
SMG(1,new_mima[5]);
SMG(2,new_mima[4]);
SMG(3,new_mima[3]);
SMG(4,new_mima[2]);
SMG(5,new_mima[1]);
SMG(6,new_mima[0]);
SMG(7,11);
SMG(8,10);
}
if(sure==1&&smg_mode==2)
{
SMG(1,10);
SMG(2,10);
SMG(3,10);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,10);
SMG(8,11);
}
}
//=================================
//=================================door and alarm
int door_control_close=0x00;
int door_control_open=0x10;
void door_work()
{
if(door==1)
{
control=(control&0xef)|0x10;
choose_573(5);
P0=control;
choose_573(0);
P0=0xff;
}
if(door==0)
{
control=(control&0xef)|0x00;
choose_573(5);
P0=control;
choose_573(0);
P0=0xff;
}
}
int alarm_control_open=0x40;
int alarm_control_close=0x00;
void alarm_work()
{
if(alarm==1)
{
control=(control&0xbf)|0x40;
choose_573(5);
P0=control; //open alarm
choose_573(0);
P0=0xff;
}
if(alarm==0)
{
control=(control&0xbf)|0x00;
choose_573(5);
P0=control; //close alarm
choose_573(0);
P0=0xff;
}
}
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=0;int count_2=0;
void Time0_server() interrupt 1
{
if(door==1)
{
count_1++;
if(count_1>=1000)
{
door=0;
count_1=0;
}
}
if(alarm==1)
{
count_2++;
if(count_2>=600)
{
count_2=0;
alarm=0;
}
}
}
//=================================
void read()
{
temp1=read_EEPROM(0x01);
mima[0]=temp1/100000;
mima[1]=(temp1%100000)/10000;
mima[2]=(temp1%10000)/1000;
mima[3]=(temp1%1000)/100;
mima[4]=(temp1%100)/10;
mima[5]=(temp1%10)/1;
}
void main()
{
init_system();
write_time();
Timer0Init();
read();
while(1)
{
key_board();
get_distance();
distance_door();
read_time();
smg_display();
setting_smg();
door_work();
alarm_work();
}
}
ds1302.h
/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include <reg52.h>
#include <intrins.h>
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
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;
}
void write_EEPROM(int add,int dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
int temp;
int read_EEPROM(int add)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
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);
void write_EEPROM(int add,int dat);
int read_EEPROM(int add);
#endif