我做的这个程序是想实现断电后上电,数码管加一,,利用了eeprom的掉电数据不丢失的性能,利用结构体定义一个标志位,每次上电检测这个标志位,如果和定义的标志位相同,就加一,如果不相同,就把定义的标志位赋给结构体中得那个标志位,然后数码管显示为一。。不胜感激!!!具体程序如下(多文件程序#include "main.h"
#include "eeprom.h"
void iic_start()
{
SDA=1;
_nop_();
SCL=1;
nops();
SDA=0;
nops();
SCL=0;
}
void iic_stop()
{
SDA=0;
_nop_();
SCL=0;
nops();
SCL=1;
nops();
SDA=1;
}
void iic_write_byte(uint8 dat)
{
uint8 i=0;
for(i=0;i<8;i++)
{
if(dat&0x80==0)
{
SDA=0;
}
else
{
SDA=1;
}
SCL=1;
dat<<=1;
nops();
SCL=0;
}
}
uint8 iic_read_byte()
{
uint8 dat,i=0;
for(i=0;i<8;i++)
{
SCL=1;
nops();
dat<<=1;
if(SDA)
{
dat|=0x01;
}
SCL=0;
nops();
}
return dat;
}
void iic_ack(bit ck)
{
if(ck)
{
SDA=0;
}
else
{
SDA=1;
}
SCL=1;
nops();
SCL=0;
nops();
}
bit waitack()
{
SDA=1;
nops();
SCL=1;
if(SDA)
{
SCL=0;
iic_stop();
return 1;
}
else
{
SCL=0;
return 0;
}
}
bit iic_write(uint8 addr,uint8 dat)
{
iic_start();
iic_write_byte(0xa0);
if(waitack()==1)
{
return 1;
}
iic_write_byte(addr);
if(waitack()==1)
{
return 1;
}
iic_write_byte(dat);
if(waitack()==1)
{
return 1;
}
iic_stop();
return 0;
}
bit iic_read(uint8 addr,uint8*dat)
{
iic_start();
iic_write_byte(0xa0);
if(waitack()==1)
{
return 1;
}
iic_write_byte(addr);
if(waitack()==1)
{
return 1;
}
iic_start();
iic_write_byte(0xa1);
if(waitack()==1)
{
return 1;
}
*dat=iic_read_byte();
iic_ack(0);
iic_stop();
return 0;
}
bit iic_write_buf(uint8 *buf,uint8 addr,uint8 len)
{
while(len--)
{
if(iic_write(addr++,*buf++))
{
return 1;
}
}
return 0;
}
bit iic_read_buf(uint8 *buf,uint8 addr,uint8 len)
{
while(len--)
{
if(iic_read(addr++,buf++))
{
return 1;
}
}
return 0;
}//这是eeprom.c
#include "main.h"
#include "eeprom.h"
code uint8 a[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
code uint8 b[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
uint8 c[10];
uint8 i=0;
POWER_UP Power_up;
void delay(uint8 n)
{
while(n--);
}
void main()
{
while(1)
{
iic_read_buf((uint8*)&Power_up,0x00,sizeof(POWER_UP));
if(Power_up.flag!=P0WER_UP_MARK)
{
Power_up.flag=P0WER_UP_MARK;
Power_up.times=1;
}
else
{
Power_up.times++;
}
iic_write_buf((uint8*)&Power_up,0x00,sizeof(POWER_UP));
c[0]=a[Power_up.times%10];
c[1]=a[Power_up.times/10%10];
c[2]=a[Power_up.times/100%10];
c[3]=a[Power_up.times/1000%10];
c[4]=a[Power_up.times/10000%10];
c[5]=a[Power_up.times/100000%10];
c[6]=a[Power_up.times/1000000%10];
c[7]=a[Power_up.times/10000000%10];
for(i=0;i<8;i++)
{
P0=c[i];
P1=b[i];
delay(150);
}
}
}//这是main.c
#ifndef _MAIN_H_
#define _MAIN_H_
#include <reg51.h>
#include <intrins.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
sbit SDA=P2^1;
sbit SCL=P2^0;
typedef struct{
uint32 times;
uint8 flag;
}POWER_UP;
#define P0WER_UP_MARK 0xAB
#endif//这是main.h
#ifndef _EEPROM_H_
#define _EEPROM_H_
#define nops() do{_nop_();_nop_();_nop_();_nop_();}while(0)
bit iic_write_buf(uint8*buf,uint8 addr,uint8 len);
bit iic_read_buf(uint8*buf,uint8 addr,uint8 len);
#endif//这是eeprom.h
) |