📄 24c08.c
字号:
/**********************************************************
24c08掉电保护试验程序
功能描述:从00-99计数,中间因意外断电,当再次来电时
数码管将接着掉电前的数字继续计数
***********************************************************/
#include <reg52.H>
#include <stdio.h>
#include <absacc.h>
unsigned char code table[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0x40};
unsigned char sec; //定义计数值,每过1 秒,sec加1
unsigned int tcnt; //定时中断次数
bit write=0; //写24C08 的标志;
sbit gewei=P2^0; //个位选通定义
sbit shiwei=P2^1; //十位选通定义
/*********************************************************
与单片机连接引脚定义
*********************************************************/
sbit scl=P3^7; // 24c08 SCL
sbit sda=P3^6; // 24c08 SDA
/*********************************************************
延时
*********************************************************/
void delay1(unsigned char x)
{ unsigned int i;
for(i=0;i<x;i++);
}
/*************************************************
**************************************************/
void flash()
{ ; ; }
/******************************************************
24c08 初始化子程序
******************************************************/
void x24c08_init()
{scl=1;
flash();
sda=1;
flash();
}
/******************************************************
启动(I2C)总线
******************************************************/
void start()
{ sda=1;
flash();
scl=1;
flash();
sda=0;
flash();
scl=0;
flash();
}
/*********************************************************
停止(I2C)总线
*********************************************************/
void stop()
{sda=0;
flash();
scl=1;
flash();
sda=1;
flash();}
/*********************************************************
写一个字节程序
*********************************************************/
void writex(unsigned char j)
{ unsigned char i,temp;
temp=j;
for (i=0;i<8;i++)
{temp=temp<<1;
scl=0;
flash();
sda=CY;
flash();
scl=1;
flash();
}
scl=0;
flash();
sda=1;
flash();
}
/*********************************************************
读一个字节程序
*********************************************************/
unsigned char readx()
{
unsigned char i,j,k=0;
scl=0; flash(); sda=1;
for (i=0;i<8;i++)
{
flash(); scl=1; flash();
if (sda==1) j=1;
else j=0;
k=(k<<1)|j;
scl=0;}
flash(); return(k);
}
/*********************************************************
(I2C)线时钟
*********************************************************/
void clock()
{
unsigned char i=0;
scl=1;
flash();
while ((sda==1)&&(i<255))i++;
scl=0;
flash();
}
/*********************************************************
从24c02 的地址address 中读取一个字节数据
*********************************************************/
unsigned char x24c08_read(unsigned char address)
{
unsigned char i;
start(); writex(0xa0);
clock(); writex(address);
clock(); start();
writex(0xa1); clock();
i=readx(); stop();
delay1(10);
return(i);
}
/*********************************************************
向24c02 的address地址中写入一字节数据info
*********************************************************/
void x24c08_write(unsigned char address,unsigned char info)
{
EA=0;
start(); writex(0xa0);
clock(); writex(address);
clock(); writex(info);
clock(); stop();
EA=1;
delay1(50);
}
/*********************************************************
延时程序
*********************************************************/
void Delay(unsigned int tc)
{
while( tc != 0 )
{unsigned int i;
for(i=0; i<100; i++);
tc--;}
}
/*********************************************************
LED显示函数
*********************************************************/
void LED()
{
shiwei=0; P0=table[sec/10]; Delay(8); shiwei=1;
gewei=0; P0=table[sec%10]; Delay(5); gewei=1;
}
void t0(void) interrupt 1 using 0 //定时中断服务函数
{
TH0=(65536-50000)/256; //对TH0 TL0 赋值
TL0=(65536-50000)%256; //重装计数初值
tcnt++; //每过250ust tcnt 加一
if(tcnt==20) //计满20次(1 秒)时
{
tcnt=0; //重新再计
sec++;
write=1; //1 秒写一次24C08
if(sec==100) //定时100 秒,在从零开始计时
{sec=0;}
}
}
void main(void)
{
TMOD=0x01; //定时器工作在方式1
ET0=1; EA=1;
x24c08_init(); //初始化24C08
sec=x24c08_read(2);//读出保存的数据赋于sec
TH0=(65536-50000)/256; //对TH0 TL0 赋值
TL0=(65536-50000)%256; //使定时器0.05 秒中断一次
TR0=1; //开始计时
while(1)
{
LED();
if(write==1) //判断计时器是否计时一秒
{
write=0; //清零
x24c08_write(2,sec); //在24c08 的地址2 中写入数据sec
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -