您的位置 首页 5G

根据51的EEPROM C言语程序

includereg52h单片机51头文件,存放着单片机的寄存器includeintrinsh>defineucharunsignedchardefineuintunsigned

#include “reg52.h”//单片机51头文件,存放着单片机的寄存器

#include
#define uchar unsigned char
#define uint unsigned int
uchar code tab[]=”0123456789″;
sbit SID=P2^0; //数据信号
sbit SCLK=P2^1; //时钟信号
uint tcnt; //守时中止次数
bit write=0; //写24C08的标志;
sbit scl=P3^7; //24c08 SCL
sbit sda=P3^6; //24c08 SDA
uchar a,b,c,d,e,f;

void delay(uint t)
{
uint i,j;
for(i=0; i for(j=0; j<10; j++);
}

void sendbyte(uchar zdata)
{
uint i;
for(i=0; i<8; i++)
{
if((zdata << i) & 0x80)
{
SID = 1;
}
else
{
SID = 0;
}
SCLK = 0;
SCLK = 1;
}
}

void write_com(uchar cmdcode)
{
sendbyte(0xf8); //11111abc a假如高表明从LCD读数据,低表明往LCD写数据。 b 高表明显现数据,低表明控制指令 c 固定为0
sendbyte(cmdcode & 0xf0); //取cmdcode的高四位
sendbyte((cmdcode << 4) & 0xf0); //取cmdcode的第四位
delay(2);
}

void write_data(unsigned char Dispdata)
{
sendbyte(0xfa);
sendbyte(Dispdata & 0xf0);
sendbyte((Dispdata << 4) & 0xf0);
delay(2);
}

void lcdinit()
{
delay(200);
write_com(0x30);
delay(50);
write_com(0x0c);
delay(50);
write_com(0x01);
delay(50);
}

void hzkdis(unsigned char code *s)
{
while(*s > 0)
{
write_data(*s);
s++;
delay(50);
}
}
void delay1(unsigned char x)
{ unsigned int i;
for(i=0;i}
void flash()
{ ; ; }

void x24c02_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(uchar j) //写一个字节
{ uchar 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();
}
uchar readx() //读一个字节
{
uchar 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总线时钟
{
uchar i=0;
scl=1; flash();
while ((sda==1)&&(i<255))i++;
scl=0; flash();
}
uchar x24c02_read(uchar address)
{
uchar i;
start(); writex(0xa0);
clock(); writex(address);
clock(); start();
writex(0xa1); clock();
i=readx(); stop();
delay1(10);
return(i);
}
void x24c02_write(uchar address,uchar info)
{

start(); writex(0xa0);
clock(); writex(address);
clock(); writex(info);
clock(); stop();
delay1(50);
}
void yejing()
{
write_com(0x90);
delay(10);
write_data(tab[a]);
delay(10);
write_data(tab[b]);
//delay(10);
//write_com(0x91);
delay(10);
write_data(tab[c]);
//write_com(0x90);
delay(10);
write_data(tab[d]);
delay(10);
write_data(tab[e]);
delay(10);
write_data(tab[f]);
}
void main(void)
{
lcdinit();
x24c02_init();
while(1)
{
a=x24c02_read(0);//读出保存的数据赋于sec
b=x24c02_read(1);
c=x24c02_read(2);
d=x24c02_read(3);
e=x24c02_read(4);
f=x24c02_read(5);
yejing();
x24c02_write(0,8);delay(10);
x24c02_write(1,7);delay(10);
x24c02_write(2,0);delay(10);
x24c02_write(3,2);delay(10);
x24c02_write(4,0);delay(10);
x24c02_write(5,8);delay(10);
delay(1000);
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/5g/262354.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部