|
89c52外挂24c02跟一个PT2259;
程序就发送一个地址码,接收ACK信号,并把接收到的ACK信号用液晶显示出来;
发送 0x50 24c02有应答信号;
但我按PT2259资料写的四个地址:0x88;0x89;0x8D;0x8C 都测试一边都没有收到应答信号!
这是什么回事啊?是不是24C02跟PT2259的寻址方式不同?
程序:
LCD1206.C
///////////////////////////////////////////////////////////////////////////////////////////////////////
#include <reg52.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P1^2;
sbit LCD1602_RW = P1^1;
sbit LCD1602_E = P1^0;
/* 等待液晶准备好 */
void LcdWaitReady()
{
unsigned char sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do {
LCD1602_E = 1;
sta = LCD1602_DB; //读取状态字
LCD1602_E = 0;
} while (sta & 0x80); //bit7等于1表示液晶正忙,重复检测直到其等于0为止
}
/* 向LCD1602液晶写入一字节命令,cmd-待写入命令值 */
void LcdWriteCmd(unsigned char cmd)
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* 向LCD1602液晶写入一字节数据,dat-待写入数据值 */
void LcdWriteDat(unsigned char dat)
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* 设置显示RAM起始地址,亦即光标位置,(x,y)-对应屏幕上的字符坐标 */
void LcdSetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if (y == 0) //由输入的屏幕坐标计算显示RAM的地址
addr = 0x00 + x; //第一行字符地址从0x00起始
else
addr = 0x40 + x; //第二行字符地址从0x40起始
LcdWriteCmd(addr | 0x80); //设置RAM地址
}
/* 在液晶上显示字符串,(x,y)-对应屏幕上的起始坐标,str-字符串指针 */
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
LcdSetCursor(x, y); //设置起始地址
while (*str != '\0') //连续写入字符串数据,直到检测到结束符
{
LcdWriteDat(*str++);
}
}
/* 初始化1602液晶 */
void InitLcd1602()
{
LcdWriteCmd(0x38); //16*2显示,5*7点阵,8位数据接口
LcdWriteCmd(0x0C); //显示器开,光标关闭
LcdWriteCmd(0x06); //文字不动,地址自动+1
LcdWriteCmd(0x01); //清屏
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
main.c
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <reg52.h>
#include <intrins.h>
#define I2CDelay() {_nop_();_nop_();_nop_();_nop_();}
sbit I2C_SCL = P3^6;
sbit I2C_SDA = P3^7;
extern void InitLcd1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
bit I2CAddresing(unsigned char addr);
void main()
{
bit ack = 0;
unsigned char str[10];
InitLcd1602(); //初始化液晶
ack = I2CAddresing(0x88);
str[0] = '5';
str[1] = '0';
str[2] = ':';
str[3] = (unsigned char)ack + '0';
str[4] = '\0';
LcdShowStr(0, 0, str);
////////////////////////////////////////////////////////////////////////
ack = I2CAddresing(0x89);
str[0] = '6';
str[1] = '2';
str[2] = ':';
str[3] = (unsigned char)ack + '0';
str[4] = '\0';
LcdShowStr(8, 0, str);
////////////////////////////////////////////////////////////////////
while (1);
}
void I2CStart()
{
I2C_SDA = 1;
I2C_SCL = 1;
I2CDelay();
I2C_SDA = 0;
I2CDelay();
I2C_SCL = 0;
}
void I2CStop()
{
I2C_SCL = 0;
I2C_SDA = 0;
I2CDelay();
I2C_SCL = 1;
I2CDelay();
I2C_SDA = 1;
I2CDelay();
}
bit I2CWrite(unsigned char dat)
{
bit ack = 0;
unsigned char mask;
for(mask=0x80; mask!=0; mask>>=1)
{
if((mask&dat) == 0)
I2C_SDA = 0;
else
I2C_SDA = 1;
I2CDelay();
I2C_SCL = 1;
I2CDelay();
I2C_SCL = 0;
}
I2C_SDA = 1;
I2CDelay();
I2C_SCL = 1;
ack = I2C_SDA;
I2CDelay();
I2C_SCL = 0;
return ack;
}
bit I2CAddresing(unsigned char addr)
{
bit ack = 0;
I2CStart();
ack = I2CWrite(addr << 1);
I2CStop();
return ack;
}
|
|