24c08控制液晶显示,但写进的数据和读出的不一样。液晶显示绝对没问题。
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCL = P2^7; //eerom时钟线
sbit SDA = P2^6; //eeromIO口
sbit LCD_WR=P1^2; //LCD 读写位(此处只写)
sbit LCD_DA=P1^3; //LCD 数据输入
sbit LCD_CS=P1^0; //LCD Chip Selection读写选择
sbit Wr = P3^6; //74HC573 Latch Enable LE控制位
sbit Bell=P0^6; //蜂鸣器控制位
uchar Flag_normal;
uchar code C_num[] =
{0xf0,0x00,0xb0,0x90,0x40,0xd0,0xf0,0x80,0xf0,0xd0,0xe0,0x70,0xf0,0x20,0xf0,0xe0};//字码表
uchar code C_num1[] =
{0xa0,0xa0,0xc0,0xe0,0xe0,0x60,0x60,0xa0,0xe0,0xe0,0xe0,0x60,0x00,0xe0,0x60,0x40};//字码表1
uchar C_buff[]=
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//缓冲
uchar C_buff1[]=
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//缓冲1
void Delay_us()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void DelayMS()
{
uchar x,y;
for(x=0;x<100;x++)
for(y=0;y<89;y++);
}
void IIC_start() //IIC开始信号(EEPROM)
{
SCL = 1;
SDA = 1;
Delay_us();
SDA = 0;
Delay_us();
SCL = 0;
}
void IIC_stop() //IIC截止信号(EEPROM)
{
SDA = 0;
SCL = 1;
Delay_us();
SDA = 1;
Delay_us();
SCL= 0;;
}
void EEPROM_ack() //IIC应答信号(EEPROM)
{
SDA=0;
SCL=1;
Delay_us();
SCL=0;
SDA=1;
}
void EEPROM_No_ack() //IIC非应答信号(EEPROM)
{
SDA=1;
SCL=1;
Delay_us();
SCL=0;
SDA=0;
}
void Cheek_Ack() //检测应答
{
uchar i=0;
SCL = 1;
while((SDA==1)&&(i<250))
{
i++;
}
SCL = 0;
Delay_us();
}
void W_bit(uchar b) //位写(LCD)
{
LCD_CS=0;
_nop_();
LCD_DA=b; //写数据
LCD_WR=0;
Delay_us(); //上升沿触发
LCD_WR=1;
Delay_us();
}
void W_code(uchar cd) //写代码CODE(LCD)
{
uchar n;
for(n = 0; n < 4; n++)
{
W_bit(cd & 0x80);
cd <<= 1;
}
}
void Write(uchar com) //写命令(LCD)
{
uchar num;
if(Flag_normal==1) //如果 Flag_normal=1则写Normal
{
W_bit(1);
Flag_normal=0;
}
else //写其他条件
W_bit(0);
for(num = 0; num < 8; num++)
{
W_bit(com & 0x80);
com <<= 1;
}
}
void LCD_clr(uchar nums) //连续写函数(LCD)
{
uchar num,j=0,i=0;
uchar addr = 0;
LCD_CS = 0;
W_bit(1);
W_bit(0);
W_bit(1);
for(num = 6; num > 0; num--)
{
W_bit(0);
}
while(nums--) //连续写代码(地址自动+1)
{
W_code(C_buff[i]);
W_code(C_buff1[j]);
i++;
j++;
}
LCD_CS = 1;
}
void Write_A_Byte(uchar IIC_code) //位写函数(EEPROM)
{
uchar i,temp;
temp = IIC_code;
for(i=0;i<8;i++)
{
IIC_code <<= 1;
SDA = CY; //位由CY给SDA
SCL = 1;
Delay_us();
SCL=0;
Delay_us();
}
SDA = 1;
}
void Byte_Write(uchar addr,uchar dat) //写数据函数(EEPROM)
{
IIC_start();
Write_A_Byte(0xa0);
Cheek_Ack();
Write_A_Byte(addr);
Cheek_Ack();
Write_A_Byte(dat);
Cheek_Ack();
IIC_stop();
}
uchar Read_A_Byte() //位读函数(EEPROM)
{
uchar i,temp = 0;
for(i=0;i<8;i++)
{
SCL = 1;
temp <<= 1;
//Delay_us();
if(SDA==1)
temp|=0x01; //读入数据
else
temp&=0xfe;
_nop_();
_nop_();
SCL = 0;
Delay_us();
}
return temp;
}
uchar Random_Read(address) //读数据函数(EEPROM)
{
uchar dat;
IIC_start();
Write_A_Byte(0xa0);
Cheek_Ack();
Write_A_Byte(address);
Cheek_Ack();
IIC_start();
Write_A_Byte(0xa1);
Cheek_Ack();
dat=Read_A_Byte();
EEPROM_No_ack();
IIC_stop();
return dat;
}
void init() //初始
{
Wr=0;
Bell=1;
Wr=1;
Flag_normal=0;
LCD_CS=1;
LCD_CS=0;
W_bit(1);
W_bit(0);
W_bit(0);
Write(0x02);
Write(0x30);
Write(0x52);
Flag_normal=1;
Write(0xc6);
Write(0x06);
LCD_CS=1;
LCD_clr(16);
}
void main()
{
uchar dat;
init(); //初始化与24c08无关
while(1)
{
Byte_Write(0x03,1);
DelayMS();
dat=Random_Read(0x03);
C_buff[0]=C_num[dat];
C_buff1[0]=C_num1[dat];
LCD_clr(12); //显示
}
} |