📄 11.c
字号:
#include<reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
unsigned char sec; //定义计数值,每过 1 秒,sec 加 1
unsigned int tcnt; //定时中断次数
bit write=0; //写 24C08 的标志;
sbit sda=P2^0; //IIC 接口 SDA 定义
sbit scl=P2^1; //IIC 接口 SCL 定义
sbit dula=P2^6;
sbit wela=P2^7;
unsigned char j,k;
void delay(unsigned char i) //延时程序
{
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, //数码管编码
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void display(uchar bai_c,uchar sh_c) //显示程序
{
dula=0;
P0=table[bai_c]; //显示百位
dula=1;
dula=0;
wela=0;
P0=0x7e;
wela=1;
wela=0;
delay(5);
P0=0; //------------
dula=1; //该部分仿真必须要。
dula=0; //因为每次下一位的段选打开后,码值会先赋值给上一位,
P0=0x7e; //从而改变上一位的显示。然后再打开下一位的位选。
wela=1; //
wela=0; //------------
dula=0;
P0=table[sh_c]; //显示十位
dula=1;
dula=0;
wela=0;
P0=0x7d;
wela=1;
wela=0;
delay(5);
P0=0; //------------
dula=1; //该部分仿真必须要。
dula=0; //因为每次下一位的段选打开后,码值会先赋值给上一位,
P0=0x7d; //从而改变上一位的显示。然后再打开下一位的位选。
wela=1; //
wela=0; //------------
}
/////////24C02 读写驱动程序////////////////////
void delay1(unsigned char x) //延时子函数
{
unsigned int i;
for(i=0;i<x;i++);
}
void flash() //延时子函数
{ ; ; }
void x24c08_init() //24c02 初始化子程序
{ scl=1;
flash();
sda=1;
flash();
}
void start() //启动 I2C 总线
{
sda=1;
flash();
scl=1;
flash();
sda=0;
flash();
//scl=0;
//flash();
}
void stop() //停止 I2C 总线
{
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);
}
void clock() //I2C总线应答子函数
{
unsigned char i=0;
sda=1; // 仿真增加语句
flash(); // 仿真增加语句
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 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 秒写一次 24C02
if(sec==100) //定时 100 秒,在从零开始计时
{sec=0;}
}
}
void main() //主函数
{
unsigned char i;
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)
{
i=10;
while(i--)
{ display(sec/10,sec%10); }
if(write==1) //判断计时器是否计时一秒
{
write=0; //清零
x24c08_write(2,sec); //在 24c02 的地址 2 中写入数据 sec
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -