微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于51的EEPROM C语言程序

基于51的EEPROM C语言程序

时间:11-22 来源:互联网 点击:
#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);
}
}


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top