图片中前面两位数由按下S5加一,按下S4减一,最后按下S6储存在eeprom中,不按不储存。而图片后面两位数是自动储存上电次数,每上电加一,按下S7清零。 main.c文件
#include "stc15f2k60s2.h"
#include "iic.h"
typedef unsigned char u8;
typedef unsigned int u16;
u8 code duan[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0xff,0xbf};
u8 temp=0;
u8 dat=0;
void delay(u16 i)
{
while(i--);
}
void Chan_hc573(chose)
{
switch(chose)
{
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;
}
P2 = ( P2 & 0x1f ) | 0x00;
}
void Smg_display(u8 com,u8 dat)
{
P0=0xff;
Chan_hc573(7);
P0=0x01<<com;
Chan_hc573(6);
P0=dat;
Chan_hc573(7);
}
void Close_smg()
{
P0=0x00;
Chan_hc573(6);
P0=0xff;
Chan_hc573(7);
}
void Display_temp()
{
Smg_display(0,duan[dat/10]); delay(500);
Smg_display(1,duan[dat%10]); delay(500);
Smg_display(2,duan[10]); delay(500);
Smg_display(3,duan[10]); delay(500);
Smg_display(4,duan[10]); delay(500);
Smg_display(5,duan[10]); delay(500);
Smg_display(6,duan[temp/10]); delay(500);
Smg_display(7,duan[temp%10]); delay(500);
Close_smg();
}
void key_down()
{
if(P30==0)
{
delay(500);
if(P30==0)
{
while(P30==0) {Display_temp();}
temp=0;
eeprom_wire(0x00,temp);
}
}
if(P31==0)
{
delay(500);
if(P31==0)
{
while(P31==0) {Display_temp();}
eeprom_wire(0x01,dat);
}
}
if(P32==0)
{
delay(500);
if(P32==0)
{
while(P32==0) {Display_temp();}
if(dat!=99) {dat++;}
}
}
if(P33==0)
{
delay(500);
if(P33==0)
{
while(P33==0) {Display_temp();}
if(dat!=0) {dat--;}
}
}
}
void Init_system()
{
P0=0x00;
Chan_hc573(5);
P0=0xff;
Chan_hc573(4);
Close_smg();
}
void main()
{
Init_system();
temp=eeprom_read(0x00);
dat=eeprom_read(0x01);
temp++;
eeprom_wire(0x00,temp);
while(1)
{
key_down();
Display_temp();
}
}
iic.c文件
#include "reg52.h"
#include "intrins.h"
#include "iic.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;
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;
}
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;
}
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 eeprom_read(unsigned char addr)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return dat;
}
void eeprom_wire(unsigned char addr,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
iic.h文件
#ifndef _IIC_H
#define _IIC_H
unsigned char eeprom_read(unsigned char addr);
void eeprom_wire(unsigned char addr,unsigned char dat);
#endif
|