个人感觉这次的题不是特别难,主要就是用的东西比较多,三个驱动函数全部都用上了,功能全部都实现了,应该没有什么问题。欢迎大家指出错误。
main.c
#include"reg52.h"
#include"intrins.h"
#include"ds1302.h"
#include"iic.h"
#include"onewire.h"
char code write_add[]={0x80,0x82,0x84};
char code read_add[]={0x81,0x83,0x85};
char time[]={0x50,0x59,0x16};
int Rb1;
int temperature;
int all_mode=0;
int smg_mode=0;
int set_mode=0;
int set_hour=17;
int set_temperature=25;
int set_led=4;
int led=0xff;
int L3_mode=0;
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;
void smg_display();
char code xianshi[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0xc6,0x86,0x8c,0x7f};
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]);
}
}
//==================================
//==================================光敏and温度
void get_guang()
{
Rb1=guang();
}
void get_temperature()
{
temperature=rd_temperature();
}
//==================================
//==================================按键
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void key_board()
{
R3=0;R2=R1=R4=1;
C1=C2=C3=C4=1;
//S5
if(C1==0)
{
Delay10ms();
if(C1==0)
{
if(all_mode==0)
{
smg_mode++;
if(smg_mode>=3)
{
smg_mode=0;
}
}
if(all_mode==1)
{
set_mode++;
if(set_mode>=3)
{
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_hour++;
if(set_hour>23)
{
set_hour=23;
}
}
if(set_mode==1)
{
set_temperature++;
if(set_temperature>99)
{
set_temperature=99;
}
}
if(set_mode==2)
{
set_led++;
if(set_led>8)
{
set_led=8;
}
}
}
while(!C2)
{
smg_display();
}
}
R4=0;R2=R1=R3=1;
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==1)
{
Delay10ms();
if(C2==0&&all_mode==1)
{
if(set_mode==0)
{
set_hour--;
if(set_hour<0)
{
set_hour=0;
}
}
if(set_mode==1)
{
set_temperature--;
if(set_temperature<0)
{
set_temperature=0;
}
}
if(set_mode==2)
{
set_led--;
if(set_led<4)
{
set_led=4;
}
}
}
while(!C2)
{
smg_display();
}
}
}
//==================================
//==================================数码管
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,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,temperature%10);
SMG(2,(temperature%100)/10);
SMG(3,(temperature%1000)/100);
SMG(2,15);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,10);
SMG(8,12);
}
if(smg_mode==2)
{
if(Rb1<100)
{
SMG(1,1);
}
else
{
SMG(1,0);
}
SMG(2,10);
SMG(3,10);
SMG(4,Rb1%10);
SMG(5,(Rb1%100)/10);
SMG(6,(Rb1%1000)/100);
SMG(6,15);
SMG(7,10);
SMG(8,13);
}
}
if(all_mode==1)
{
if(set_mode==0)
{
SMG(1,set_hour%10);
SMG(2,set_hour/10);
SMG(3,10);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,1);
SMG(8,14);
}
if(set_mode==1)
{
SMG(1,set_temperature%10);
SMG(2,set_temperature/10);
SMG(3,10);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,2);
SMG(8,14);
}
if(set_mode==2)
{
SMG(1,set_led);
SMG(2,10);
SMG(3,10);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,3);
SMG(8,14);
}
}
}
//==================================
//==================================中断函数
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;int count_2;int count_3;
void time0_service() interrupt 1
{
count_1++;
count_2++;
if(count_1>150)
{
count_1=0;
get_temperature();
}
if(count_2>80)
{
count_2=0;
get_guang();
}
if(Rb1<100)
{
count_3++;
if(count_3>=600)
{
L3_mode=1;
}
}
else
{
count_3=0;
L3_mode=0;
}
}
//==================================
//==================================led
void led_work()
{
//L1
if(set_hour>time[2]%16+(time[2]/16)*10)
{
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(temperature<set_temperature*10)
{
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(L3_mode==1)
{
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~L8
if(Rb1<100)
{
if(set_led==4)
{
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);
}
if(set_led==5)
{
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(set_led==6)
{
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);
}
if(set_led==7)
{
led=(led&0xbf)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xbf)|0x40;
choose_573(4);
P0=led;
choose_573(0);
}
if(set_led==8)
{
led=(led&0x7f)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0x7f)|0x80;
choose_573(4);
P0=led;
choose_573(0);
}
}
else
{
led=(led&0x07)|0xf8;
choose_573(4);
P0=led;
choose_573(0);
}
}
//==================================
void main()
{
init_system();
write_time();
Timer0Init();
while(1)
{
read_time();
key_board();
smg_display();
led_work();
}
}
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 guang()
{
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;
}
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 guang();
#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
onewire.c
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
t=t*12;
while(t--);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
unsigned int rd_temperature(void)
{
int HBS;int LBS;int temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LBS=Read_DS18B20();
HBS=Read_DS18B20();
temp=(HBS<<8)|LBS;
temp=temp*0.625;
return temp;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned int rd_temperature(void); //; ;
void Delay_OneWire(unsigned int t); //STC89C52RC
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
#endif