#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define OP_WRITE 0xa0 // 器件地址以及写入操作
#define OP_READ 0xa1 // 器件地址以及读取操作
uchar code display[16][4]={
0xf7,0xc0,0x0,0x0,
0x94,0x40,0x0,0x0,
0xa4,0x40,0x0,0x0,
0xc7,0xc0,0x0,0x0,
0xc4,0x40,0x0,0x0,
0xa4,0x40,0x0,0x0,
0x97,0xc0,0x0,0x0,
0x94,0x40,0x0,0x0,
0x94,0x40,0x0,0x0,
0xd4,0x40,0x0,0x0,
0xa4,0x40,0x0,0x0,
0x84,0xc0,0x0,0x0,
0x88,0x40,0x0,0x0,
0x00,0x00,0x0,0x0,
0x00,0x00,0x0,0x0,
0x00,0x00,0x0,0x0,
};
uchar dis_data[16][4]={
0x06,0x30,0x03,0xc0,
0x0f,0x78,0x04,0x20,
0x0f,0xf8,0x04,0x30,
0x0f,0xf8,0x09,0x70,
0x07,0xf0,0x70,0x70,
0x03,0xe0,0x60,0x7c,
0x01,0xc2,0x20,0x73,
0x00,0x83,0x1e,0x20,
0x00,0x0e,0x81,0x00,
0x00,0x10,0x41,0x00,
0x00,0x30,0x41,0xc3,
0x00,0x58,0x61,0xfd,
0x00,0xc0,0x91,0x81,
0x00,0xa0,0x11,0x81,
0x00,0x80,0x11,0x81,
0x00,0x7f,0xe3,0x06
};
/************74HC595控制*****************/
sbit STCP = P1^2; // 存储寄存器时钟输入
sbit SHCP = P1^1; // 移位寄存器时钟输入
sbit DS = P1^0; // 串行数据输入
/************AT24LC64控制*****************/
sbit SDA = P3^7;
sbit SCL = P2^7;
sbit WP = P2^6;
void start();
void stop();
uchar shin();
bit shout(uchar write_data);
void write_byte( uchar addr, uchar write_data);
void fill_byte(uchar fill_size,uchar fill_data);
void delayms(uint ms);
uchar read_current();
uchar read_random(uchar random_addr);
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
void delay()
{
uchar i;
for(i=0; i<3; i++);
}
/**********向移位寄存器中存入8个数据*************/
void shift_dat(uchar dat)
{
uchar i;
dat = ~dat;
for(i=0; i<8; i++)
{
SHCP = 0;
delay();
dat<<=1;
DS = CY;
SHCP = 1;
delay();
}
}
/**********向移位寄存器移入一行数据************/
void shift_row(uchar n)
{
STCP = 0;
shift_dat(dis_data[n][0]);
shift_dat(dis_data[n][1]);
shift_dat(dis_data[n][2]);
shift_dat(dis_data[n][3]);
SHCP = 0;
delay();
STCP = 1;
delay();
STCP = 0;
}
/***********显示全屏图案************/
void dis_image()
{
uchar i;
for(i=0; i<16; i++)
{
shift_row(i);
P2=(P2&0xf0)|i;
}
}
/**********************************************************/
main(void)
{
uchar i;
SDA = 1;
SCL = 1;
fill_byte(64,0x33); // 将前64字节填充0xff
for(i = 0 ; i < 64; i++) //写入显示代码到AT24LC64
{
write_byte(i,display[i/4][i%4]);
}
for(i =0 ;i <64 ; i++) //从AT24LC64移出数据送到P0口显示
{
dis_data[i/4][i%4] = read_random(i);
delayms(350);
}
while(1)
{
dis_image();
}
}
/**********************************************************/
void start() //开始位
{
SDA = 1;
SCL = 1;
delayNOP();
SDA = 0;
delayNOP();
SCL = 0;
}
/**********************************************************/
void stop() // 停止位
{
SDA = 0;
delayNOP();
SCL = 1;
delayNOP();
SDA = 1;
}
/**********************************************************/
uchar shin() // 从AT24LC64移出数据到MCU
{
uchar i,read_data;
for(i = 0; i < 8; i++)
{
SCL = 1;
read_data <<= 1;
read_data |= SDA;
SCL = 0;
}
return(read_data);
}
/**********************************************************/
bit shout(uchar write_data) // 从MCU移出数据到AT24LC64
{
uchar i;
bit ack_bit;
for(i = 0; i < 8; i++) // 循环移入8个位
{
SDA = (bit)(write_data & 0x80);
_nop_();
SCL = 1;
delayNOP();
SCL = 0;
write_data <<= 1;
}
SDA = 1; // 读取应答
delayNOP();
SCL = 1;
delayNOP();
ack_bit = SDA;
SCL = 0;
return ack_bit; // 返回AT24C02应答位
}
/**********************************************************/
void write_byte(uchar addr, uchar write_data) // 在指定地址addr处写入数据write_data
{
start();
shout(OP_WRITE);
shout(0x00); // 发送地址高位
shout(addr); // 发送地址低位
shout(write_data);
stop();
delayms(10); // 写入周期
}
/**********************************************************/
void fill_byte(uchar fill_size,uchar fill_data) // 填充数据fill_data到EEPROM内fill_size字节
{
uchar i;
for(i = 0; i < fill_size; i++)
{
write_byte(i, fill_data);
}
}
/**********************************************************/
uchar read_current() // 在当前地址读取
{
uchar read_data;
start();
shout(OP_READ);
read_data = shin();
stop();
return read_data;
}
/**********************************************************/
uchar read_random(uchar random_addr) // 在指定地址读取
{
start();
shout(OP_WRITE);
shout(0x00); // 写入读写高8位地址
shout(random_addr); // 写入读写低8位地址
return(read_current());
}
/**********************************************************/
void delayms(uint ms) // 延时子程序
{
uchar k;
while(ms--)
{
for(k = 0; k < 120; k++);
}
}