打印

菜农用LabWindows写的I2C程序,望白沙烟酒同志笑纳

[复制链接]
3858|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
HotPower|  楼主 | 2007-6-18 13:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
HRESULT CVIFUNC I2C_Start(void)
{
    HRESULT __result = S_FALSE;
    int i;
    unsigned char ack;
    for (i = 0; i < I2C_waitStartDelay;i ++) {
        ack = I2C_GetSDA();//释放数据总线读数据总线 
        I2C_SetSCL();//首先拉高SCL, 保证SDA在SCL=1时跳变,拉高时钟(必须在SDA=1时)
        I2C_ClrSDA();//首先拉低SDA.必须按次序!!!
        I2C_ClrSCL();//然后拉低SCL,构成I2C从机内部中断并唤醒
        if (ack) {//测试成功!!!总线已释放
            __result = S_OK;//
            break;
        }
    }
    if (__result == S_OK)
    {
        Hot_I2CErrorState = I2C_START;//已发送起始条件
    }
    else
    {
        Hot_I2CErrorState = -1;//未发送起始条件
        I2C_Stop ();
    }
    return __result;
}

HRESULT CVIFUNC I2C_ReStart(unsigned char addrss)
{
    HRESULT __result;
//入口:SDA=SCL=0
    I2C_SetSDA();//首先拉高SDA, 这样才能保证在SCL=1时SDA下跳构成再次复位条件即复位
    I2C_SetSCL();//然后拉高SCL, 保证SDA在SCL=1时跳变,拉高时钟(必须在SDA=1时)
    I2C_ClrSDA();//首先拉低SDA.必须按次序!!!
    I2C_ClrSCL();//然后拉低SCL,构成I2C从机内部中断并唤醒
    I2C_ReadWriteByte (addrss | 1);//强行发送I2C读地址,SDA=SCL=0
    __result =I2C_GetAck();//取从机ACK信号
    if (__result == S_OK) 
    {
        Hot_I2CErrorState = I2C_MT_SLA_ACK;//已发送SLA+W,且已接收ACK
    }
    else
    {
        Hot_I2CErrorState = I2C_MT_SLA_NACK;//已发送SLA+W,且未接收ACK
        I2C_Stop ();
    }
    return __result;
}

void CVIFUNC I2C_Stop (void)
{
    I2C_ClrSCL();
    I2C_ClrSDA();
      I2C_SetSCL();//在SCL为高时准备Stop信号,拉高时钟(必须在SDA=1时,故本例形成Stop())
      I2C_SetSDA();//STOP信号结束
}

void CVIFUNC I2C_WriteAck(unsigned char ack) 
{
    if (ack) {
        I2C_ClrSDA();//给从机ACK信号
    }
    else
    {
        I2C_SetSDA();//给从机nACK信号 
    }
    I2C_SetSCL();//拉高时钟信号,拉高时钟(必须在SDA=1时)
    I2C_ClrSCL();//拉低时钟信号,防止START信号产生
    I2C_ClrSDA();//给从机ACK信号
}


HRESULT CVIFUNC I2C_GetAck(void)
{
    HRESULT __result = S_FALSE;
    int i;
    for (i = 0; i < I2C_waitAckDelay; i ++)
    {
        if (I2C_GetSDA() == 0) //释放数据总线读数据总线
        {
            __result = S_OK;
            break;
        }
    }
    I2C_SetSCL();//拉高时钟(必须在SDA=1时)???
    I2C_ClrSCL();//拉低时钟
    I2C_ClrSDA();//保证SDA=SCL=0///pwy(拉低SDA便于发送停止喜欢)
    return __result;//SDA=1,SCL=0
}

/*-----------------------------------------------------------
    I2C一体化读写程序
-------------------------------------------------------------*/
unsigned char CVIFUNC I2C_ReadWriteByte(unsigned char value)
{
    int i;
    for (i = 0; i < 8; i ++)//每个字节8位
    {
        if (value >= 0x80) //D7位=1
        {
             value <<= 1;//I2C协议先出D7位
            I2C_SetSDA();
            if (I2C_GetSDA())//GetSDA()内带释放数据总线SetSDA()
            {
                value ++;//读回1位I2C数据1
            }
        }
        else//D7=0
        {
             value <<= 1;//I2C协议先出D7位 
            I2C_ClrSDA();//写入1位I2C数据0
        }
        I2C_SetSCL();//拉高I2C时钟
        I2C_ClrSCL();//拉低I2C时钟
    }
    I2C_ClrSDA();//保证出口SDA=SCL=0
    return value;//返回数据
}

unsigned char CVIFUNC I2C_ReadByte (unsigned char ack)
{
    unsigned char value;
    value = I2C_ReadWriteByte (0xff);//取数据
    I2C_WriteAck(ack);//向从机发送ACK/nACK信号  
    return value;//返回接收数据
}

unsigned short CVIFUNC I2C_ReadWord (unsigned char ack)
{
    unsigned short value;
    unsigned char ch, cl;
    cl = I2C_ReadByte (I2C_ACK);
    if (ack) ch = I2C_ReadByte (I2C_ACK);
    else ch = I2C_ReadByte (I2C_nACK);
    value = (ch << 8) | cl;
    return value;
}

void CVIFUNC I2C_ReadBlock (unsigned char *str, int size)
{
    int i;
    for (i = 0; i < size; i ++)
    {
        if (i == size - 1) 
        {
            str = I2C_ReadByte(I2C_nACK);
        }
        else
        {
            str = I2C_ReadByte(I2C_ACK);
        }
    }
}

HRESULT CVIFUNC I2C_WriteByte (unsigned char value)
{
    HRESULT __result = S_FALSE;
    I2C_ReadWriteByte (value);//写数据,SDA=SCL=0 
    __result = I2C_GetAck();//取从机ACK信号
    if (__result == S_OK) 
    {
        Hot_I2CErrorState = I2C_MT_DATA_ACK;//已发送I2DAT 中的数据字节,且已接收ACK
    }
    else
    {
        Hot_I2CErrorState = I2C_MT_DATA_NACK;//已发送I2DAT 中的数据字节,且未接收ACK
        I2C_Stop();
    }
    return __result;
}

HRESULT CVIFUNC I2C_WriteWord (unsigned short value)
{
    HRESULT __result = S_FALSE;
    unsigned char ch, cl;
    ch = value  >> 8;
    cl = value & 0xff;
    __result = I2C_WriteByte (cl);
    if (__result == S_OK)
    {
        __result = I2C_WriteByte (ch);
    }
    return __result;
}

HRESULT CVIFUNC I2C_WriteBlock (unsigned char *str, int size)
{
    HRESULT __result = S_FALSE;
    int i;
    for (i = 0; i < size; i ++)
    {
        __result = I2C_WriteByte(str);
        if (__result != S_OK) break; 
    }
    return __result;
}

附该程序效果图:
1.读出EEPROM数据文件


2.写入EEPROM数据文件 


3.单/双字节EEPROM读出


4.单/双字节EEPROM写入


5.整片EEPROM读出


6.整片EEPROM写入 

相关帖子

沙发
雁舞白沙| | 2007-6-18 13:48 | 只看该作者

运行后,一样读不出来!

郁闷

使用特权

评论回复
板凳
hotpower| | 2007-6-18 18:50 | 只看该作者

只要发送0xa0地址收不到ACK信号,就和MCU说拜拜吧~~~

我倒~~~找张教主讨个说法...

使用特权

评论回复
地板
农民讲习所| | 2007-6-18 18:58 | 只看该作者

以前不是说过吗?把读ACK改为空读不检测

因为读ACK是硬件级别的,软件模拟时序受线程影响会读丢。

使用特权

评论回复
5
Richard741| | 2007-6-18 19:11 | 只看该作者

不会啊。这是我的从打印口读写24C02A的C51 I2C程序

/*-----------------------------------------------
  24C02a Read/Write With LPT(Printer Port)
  Page Size=8
  Oscilator:12Mhz
------------------------------------------------*/

#include <Virtual51.h>
#define uint unsigned int
#define uchar unsigned char   

uchar bdata Msec,Sec,Min,Hour,Day,Week,Month,Year,Count=0,Flags=0;
uchar xdata Buf[0x4000];

sbit  To_5ms=Flags^0;
sbit  GF=Flags^1;  
sbit  AF=Flags^2;

#define PWR     LPT_Pin16
#define SDI     LPT_Pin15
#define SDO     LPT_Pin01
#define SCL     LPT_Pin14

uchar code PIC[]=
{   
0x00,0x00,0x00,0x00,0x10,0x08,0x08,0x88,0x70,0x00,0x00,0x00,0xC0,0x30,0xF8,0x00,
0x00,0x00,0xE0,0x10,0x08,0x08,0x08,0x10,0x20,0x00,0x00,0xF0,0x08,0x08,0x08,0xF0,
0x00,0x00,0x10,0x08,0x08,0x88,0x70,0x00,0x00,0x40,0x20,0x20,0x20,0xC0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0x0C,0x0A,0x09,0x08,0x00,0x00,0x03,0x02,0x02,0x0F,0x02,
0x00,0x00,0x03,0x04,0x08,0x08,0x08,0x04,0x02,0x00,0x00,0x07,0x08,0x08,0x08,0x07,
0x00,0x00,0x08,0x0C,0x0A,0x09,0x08,0x00,0x00,0x06,0x09,0x09,0x05,0x0F,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xC0,0xB8,0x84,0xB8,0xC0,0x00,0x00,0xE0,0x10,0x10,0x20,0x00,
0x00,0xE0,0x10,0x10,0x20,0x00,0x00,0xE0,0x90,0x90,0x90,0xE0,0x00,0x00,0x60,0x90,
0x90,0x90,0x20,0x00,0x00,0x60,0x90,0x90,0x90,0x20,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x1C,0xE0,0x00,0xC0,0x38,0x04,0x38,0xC0,0x00,0xF0,0x0C,0x00,0xF4,0x00,0x10,
0xFC,0x10,0x00,0xFC,0x20,0x10,0x10,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x44,0x44,0x44,0x44,0x38,0x00,0x04,0x04,
0x04,0xFC,0x04,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x06,0x01,0x00,0x00,0x00,0x01,0x06,0x00,0x03,0x04,0x04,0x02,0x00,
0x00,0x03,0x04,0x04,0x02,0x00,0x00,0x03,0x04,0x04,0x04,0x02,0x00,0x00,0x02,0x04,
0x04,0x04,0x03,0x00,0x00,0x02,0x04,0x04,0x04,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x06,0x01,0x00,0x00,0x00,0x01,0x06,0x01,0x00,0x00,0x07,0x00,0x00,
0x07,0x04,0x00,0x07,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0x04,0x04,0x04,0x04,0x04,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xFE,0x22,0x22,0x22,0x02,0x00,0x00,0x08,0x04,0xFE,0x00,0x00,
0x00,0x00,0x08,0x00,0x00,0xFE,0x22,0x22,0x22,0x62,0xA2,0x1C,0x00,0x00,0xF0,0x48,
0x48,0x48,0x70,0x00,0x00,0x90,0x48,0x48,0x48,0xF0,0x00,0x00,0xF0,0x08,0x08,0x10,
0xFE,0x00,0x00,0x00,0x00,0x00,0xFE,0x22,0x22,0x22,0x02,0x00,0x00,0x04,0x02,0x82,
0x62,0x1C,0x00,0x00,0x08,0x00,0x0E,0xF0,0x00,0xE0,0x1C,0x02,0x1C,0xE0,0x00,0xF8,
0x06,0x00,0xF8,0x10,0x08,0x00,0xFA,0x00,0x08,0xFE,0x08,0x00,0xF0,0x48,0x48,0x48,
0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,
0x00,0x00,0x02,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x01,0x02,0x00,0x00,0x01,0x02,
0x02,0x02,0x01,0x00,0x00,0x01,0x02,0x02,0x01,0x03,0x00,0x00,0x01,0x02,0x02,0x02,
0x03,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x03,0x02,
0x02,0x02,0x00,0x00,0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x03,0x00,
0x00,0x00,0x03,0x00,0x00,0x00,0x03,0x00,0x00,0x03,0x02,0x00,0x01,0x02,0x02,0x02,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

uchar code Hint[]=
{
//OK!
0x00,0x00,0x00,0x00,0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x08,0xF8,0x08,0x80,
0x60,0x18,0x08,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x03,0x04,0x08,0x08,0x08,0x04,0x03,0x00,0x08,0x0F,0x09,0x01,
0x02,0x0C,0x08,0x00,0x00,0x00,0x00,0x0B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//Failed!
0x00,0x00,0x00,0x00,0x00,0x00,0x10,0xF0,0x10,0x10,0x90,0x10,0x30,0x00,0x00,0x80,
0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x80,0x80,0x90,0x00,0x00,0x00,0x00,0x00,0x00,
0x10,0xF0,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x80,
0x80,0x80,0x90,0xF0,0x00,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x1F,0x11,0x11,0x03,0x00,0x00,0x00,0x0C,0x12,
0x12,0x12,0x12,0x0F,0x10,0x00,0x00,0x10,0x10,0x1F,0x10,0x10,0x00,0x00,0x00,0x10,
0x10,0x1F,0x10,0x10,0x00,0x00,0x0F,0x12,0x12,0x12,0x12,0x12,0x0B,0x00,0x0F,0x10,
0x10,0x10,0x10,0x1F,0x10,0x00,0x00,0x00,0x00,0x17,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//Reading...
0x00,0x00,0x00,0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0xC0,0xA0,0xA0,0xA0,0xA0,
0xC0,0x00,0x00,0x40,0xA0,0xA0,0xA0,0xA0,0xC0,0x00,0x00,0xC0,0x20,0x20,0x20,0x44,
0xFC,0x00,0x00,0x00,0x20,0x20,0xE8,0x00,0x00,0x00,0x00,0x20,0xE0,0x40,0x20,0x20,
0xC0,0x00,0x00,0xC0,0x20,0x20,0x20,0x40,0xE0,0x20,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x04,0x07,0x04,0x00,0x01,0x02,0x04,0x00,0x03,0x04,0x04,0x04,0x04,
0x04,0x00,0x00,0x03,0x04,0x04,0x04,0x02,0x07,0x04,0x00,0x03,0x04,0x04,0x04,0x04,
0x07,0x04,0x00,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x00,0x04,0x07,0x04,0x00,0x04,
0x07,0x04,0x00,0x03,0x24,0x24,0x24,0x22,0x1F,0x00,0x00,0x00,0x00,0x06,0x06,0x00,
0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//Writing...
0x00,0x00,0x00,0x00,0x00,0x08,0xF8,0x08,0x80,0x60,0x80,0x08,0xF8,0x00,0x20,0xE0,
0x40,0x20,0x20,0x20,0x00,0x00,0x00,0x20,0x20,0xE8,0x00,0x00,0x00,0x00,0x20,0xF8,
0x20,0x20,0x20,0x20,0x00,0x00,0x00,0x20,0x20,0xE8,0x00,0x00,0x00,0x00,0x20,0xE0,
0x40,0x20,0x20,0xC0,0x00,0x00,0xC0,0x20,0x20,0x20,0x40,0xE0,0x20,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x04,0x03,0x00,0x03,0x04,0x03,0x00,0x04,0x07,
0x04,0x04,0x04,0x00,0x00,0x00,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x00,0x00,0x03,
0x04,0x04,0x04,0x02,0x00,0x00,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x00,0x04,0x07,
0x04,0x00,0x04,0x07,0x04,0x00,0x03,0x24,0x24,0x24,0x22,0x1F,0x00,0x00,0x00,0x00,
0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

void delay()  
{
uchar n=0;
while (n<16) //延时 n*0.1 us
{
  GF=SDI;//延时大约为 0.1-0.2 us,不能用NOP
  n++;
}
}

void Start_I2c()
{
  SDO=1;  
  SCL=1;
  delay();  
  SDO=0;   
  delay();     
  SCL=0;
  delay();
}

void Stop_I2c()
{
  SDO=0;  
  SCL=1;
  delay();
  SDO=1;
  delay();
  SCL=0;
  delay();
}

bit SendByte(uchar Dat)
{
uchar Cnt;

for(Cnt=0;Cnt<8;Cnt++)
    {
     if (Dat&0x80) SDO=1;
       else SDO=0;
     Dat<<=1;
     SCL=1;              
     delay();    
     SCL=0;
     delay();
    }
SDO=1;     
SCL=1;
delay();
AF=SDI;
SCL=0;
delay();
return(AF);
}
    
uchar  LoadByte()
{
  uchar Cnt,Dat=0;
  
  SDO=1;  
  for(Cnt=0;Cnt<8;Cnt++)
      {
        SCL=1;   
        delay();    
        Dat=(Dat<<1)| SDI;
        SCL=0;
        delay();
      }
  return(Dat);
}

void Ack_I2c()
{
  SDO=0;
  SCL=1;
  delay();  
  SCL=0;      
  delay();    
}

void ShowPICture(uchar byPICID)
{
    uchar col,pg;
    uchar code *pC;
    pC = PIC + (byPICID&0x03)*128*8;
    for(pg = 0; pg < 8; pg++)
    {
        LCDCMR = SET_PAGE | pg;
        LCDCMR = SET_COL  | 0;
        for(col = 0; col < 128; col++)
            LCDDR = *pC++;
    }
}

void ShowHint(uchar aNo)
{
    uchar col,pg;
    uchar code *pC;
    pC = Hint + (aNo&0x03)*32*8;
    for(pg = 6; pg < 8; pg++)
    {
        LCDCMR = SET_PAGE | pg;
        LCDCMR = SET_COL  | 0;
        for(col = 0; col < 128; col++)
            LCDDR = *pC++;
    }
}

void Beep()
{
  uchar n;
  TGCON=0x88;
  for (n=0;n<20;n++)
  {
   while (~To_5ms) {}
   To_5ms=0;
  }
  TGCON=0x80;
  for (n=0;n<20;n++)
  {
   while (~To_5ms) {}
   To_5ms=0;
  }
  TGCON=0x88;
  for (n=0;n<20;n++)
  {
   while (~To_5ms) {}
   To_5ms=0;
  }
  TGCON=0x80;
}

void main()
{
    uint  i;
    uchar n;
    TMOD=0x01;
    LCDCMR=Cls;
    LCDCMR=LCD_on;
    LCDCMR=INC_enb;
    EA=1;ET0=1;TR0=1;
    LPTCTR=0x00;
    LPTDTR=0xff;
    ShowPICture(0);
    while(1)
    {
     while (KEYPAD==0) {};
     switch(KEYPAD)
     {
      case 0x70://Read
           LPTCTR=0x04;  //打开电源
           ShowHint(2);
           for(i=0;i<256;i++)
            {
             if ((i&7)==0)
              {
               Start_I2c();  
               if (SendByte(0xA0)) break;
               if (SendByte(i)) break;
               Stop_I2c();
               Start_I2c();
               if (SendByte(0xA1)) break;
              }
             Buf=LoadByte();
             if ((i&7)==7) Stop_I2c();
               else  Ack_I2c();
             n=i;
            }
           Beep();
           LPTCTR=0x00;  //关闭电源
           if (n!=0xff) ShowHint(1);
             else ShowHint(0);
            break;
       
      case 0x71://Write
           LPTCTR=0x04;  //打开电源
           ShowHint(3);
           for(i=0;i<256;i++)
            {
             if ((i&7)==0)
              {
               Start_I2c();  
               if (SendByte(0xA0)) break;
               if (SendByte(i)) break;
              }
             if (SendByte(Buf)) break;
             if ((i&7)==7)
               {
                Stop_I2c();
                for(n=0;n<4;n++)  //Wait 15-20ms
                  {
                   while(~To_5ms){}
                   To_5ms=0;
                  }
               }
             n=i;
            }
           Beep();
           LPTCTR=0x00;  //关闭电源
           if (n!=0xff) ShowHint(1);
             else ShowHint(0);
            break;
      case 0x72://Clear Buffer
           for(i=0;i<256;i++) Buf=0;
           break;
         default:;
     }  
    while (KEYPAD) {};
    }
}

void timer0() interrupt 1
{  
    TL0=0x7B;   //5ms Interval,XTAL=12Mhz
       TH0=0xEC;
    To_5ms=1;
    if (++Count>49)
      {
       RTCON=Msec_BCD;
       Msec=RTCON;
       RTCON=Sec_BCD;
       Sec=RTCON;
       RTCON=Min_BCD;
       Min=RTCON;
       RTCON=Hou_BCD;
       Hour=RTCON;
       RTCON=Day_BCD;
       Day=RTCON;
       RTCON=Week_BCD;
       Week=RTCON;
       RTCON=Mon_BCD;
       Month=RTCON;
       RTCON=Year_BCD;
       Year=RTCON;
       Count=0;
      }
}

使用特权

评论回复
6
H0TP0WER.| | 2007-7-19 01:40 | 只看该作者

1

使用特权

评论回复
7
gyt| | 2007-7-19 06:42 | 只看该作者

2

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1460

主题

21619

帖子

506

粉丝