I2C(24C64)驅(qū)動(dòng)程序
/*
********************************************************************
********************************************************************
*/
//總結(jié): SCL為高電平時(shí), SDA上的數(shù)據(jù)才有效
// 傳輸數(shù)據(jù) SCL = 1; SDA = 1 或 SDA = 0 要求是穩(wěn)定的數(shù)據(jù)
// 啟動(dòng)信號(hào) SCL = 1; SDA = 1 ---> 0
// 停止信號(hào) SCL = 1; SDA = 0 ---> 1
// 主要用proteus調(diào)試查看每次總線上的數(shù)據(jù)
// 主要調(diào)試i2cstar() 以及i2cstop()這兩個(gè)函數(shù), 只要這兩個(gè)函數(shù)正確,一般就正確了。
//
//關(guān)于地址的說(shuō)明
// 無(wú)論是寫入還是讀出,被操作器件容量在256字節(jié)以內(nèi)時(shí),一個(gè)字節(jié)(8位)的尋址范圍即可滿足要求。當(dāng)容量為512字節(jié)
// (2頁(yè)). 1k字節(jié)(4頁(yè))和2k字節(jié)(8頁(yè))時(shí),采用占用器件引腳地址(A2,A1,A0)的辦法,將引腳地址做為頁(yè)地址(占用的引腳地址線懸空).
// 當(dāng)容量在4K字節(jié)以上時(shí),存儲(chǔ)單元地址字節(jié)將用兩個(gè)字節(jié)表示(高8位在前)
//
/* 應(yīng)用例子
void main( void )
{
uchar ReadBuf[6];
LCD_init();
LCD_write_string( 1, 3, " I2C test " ); //測(cè)度液晶
I2cWriteDataToAddr( 0xa0, 0x0007, 'h' ); //在器件地址為0xa0的IIC器件上的0x0007這個(gè)地址寫入'h'這個(gè)字符
I2cWriteDataToAddr( 0xa0, 0x0008, 'e' );
I2cWriteDataToAddr( 0xae, 0x0007, 'b' );
I2cWriteDataToAddr( 0xae, 0x0008, 'e' );
I2cWriteDataToAddr( 0xae, 0x0009, 'i' ); //器件地址 0xae, 字節(jié)地址 0x0009, 寫入 'i'
LCD_write_char( 2, 1, I2cReadDataFromAddr( 0xa0, 0x0007 ) ); //把器件地址為0xa0的0x0007這個(gè)地址上的字節(jié)數(shù)據(jù)讀出來(lái)
LCD_write_char( 2, 2, I2cReadDataFromAddr( 0xa0, 0x0008 ) );
LCD_write_char( 2, 3, I2cReadDataFromAddr( 0xae, 0x0007 ) );
LCD_write_char( 2, 4, I2cReadDataFromAddr( 0xae, 0x0008 ) );
LCD_write_char( 2, 5, I2cReadDataFromAddr( 0xae, 0x0009 ) );
I2c_Write_n( 0xa0, 0x0001, "0123456789", 10 ); //連續(xù)寫10個(gè)字節(jié)
LCD_write_char( 2, 0, I2cReadDataFromAddr( 0xa0, 0x0001 ) );
LCD_write_char( 2, 1, I2cReadDataFromAddr( 0xa0, 0x0002 ) );
LCD_write_char( 2, 2, I2cReadDataFromAddr( 0xa0, 0x0003 ) );
LCD_write_char( 2, 3, I2cReadDataFromAddr( 0xa0, 0x0004 ) );
LCD_write_char( 2, 4, I2cReadDataFromAddr( 0xa0, 0x0005 ) );
LCD_write_char( 2, 5, I2cReadDataFromAddr( 0xa0, 0x0006 ) );
LCD_write_char( 2, 6, I2cReadDataFromAddr( 0xa0, 0x0007 ) );
LCD_write_char( 2, 7, I2cReadDataFromAddr( 0xa0, 0x0008 ) );
LCD_write_char( 2, 8, I2cReadDataFromAddr( 0xa0, 0x0009 ) );
LCD_write_char( 2, 9, I2cReadDataFromAddr( 0xa0, 0x000a ) );
I2c_Read_n( 0xa0, 0x0002, ReadBuf, 5 ); //連續(xù)讀5個(gè)字節(jié)
LCD_write_array( 1, 4, ReadBuf );
while( 1 );
}
*/
#ifndef _24C64_H_
#define _24C64_H_
#include
#include
//數(shù)據(jù)類型說(shuō)明
#define uchar unsigned char
#define uint unsigned int
//5us延時(shí)宏定義
#define NOP_5 _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
//管腳連接信息
#define SCL P3_6
#define SDA P3_7
//定義讀出數(shù)據(jù)緩沖區(qū)
//#define I2CSIZE 16 //定義16個(gè)字節(jié) 盡量不要太大,節(jié)省空間
//uchar xdata I2cBuffer[I2CSIZE];
//注意,在寫函數(shù)中加入了5ms延時(shí),如果晶振有變,則適當(dāng)修改。
void delay_5ms();
//IIC函數(shù)
void I2cStart( void );//啟動(dòng)
void I2cStop( void );//終止
uchar WaitAsk( void );//等待應(yīng)答
void SendAsk( void );//發(fā)送應(yīng)答
void SendNoAsk( void );//發(fā)送非應(yīng)答
void I2cWriteByte( uchar wbyte );//寫字節(jié)
void I2cWriteDataToAddr( uchar DeviceAddress, uint ByteAddress, uchar Wdata );//寫字節(jié)到某地址
uchar I2cReadByte( void );//讀字節(jié)
uchar I2cReadDataFromAddr( uchar DeviceAddress, uint ByteAddress );//從某器件讀字節(jié)
void I2c_Write_n( uchar DeviceAddress, uint ByteAddress, uchar *Wdata, uchar n );//寫n個(gè)
void I2c_Read_n( uchar DeviceAddress, uint ByteAddress, uchar *rdatabuf, uchar n );//讀n個(gè)
#endif // <24c64.h>
#define _24c64_c_
#include "24c64.h"
/*
********************************************************************
** 函數(shù)名:5ms延時(shí)函數(shù)
** 注意 :
** 說(shuō)明 :
**
********************************************************************
*/
void delay_5ms( void )
{
uchar i;
uchar j;
uchar t;
for( t = 0; t < 10; t++ )
{
for( j = 0; j < 15; j ++ )
{
for( i = 0; i < 21; i++ )
{;}
}
}
}
/*
********************************************************************
** 函數(shù)名:i2c啟動(dòng)
** 注意 :
** 說(shuō)明 :
********************************************************************
*/
void I2cStart( void )
{
//scl=1時(shí)
//sda由1-->0
SDA = 1; //準(zhǔn)備下降沿
SCL = 1;
NOP_5;
SDA = 0;
NOP_5;
SCL = 0; //一定要
//SDA = 0;
}
/*
********************************************************************
** 函數(shù)名:i2c停止
** 注意 :
** 說(shuō)明 :
********************************************************************
*/
void I2cStop( void )
{
//scl=1時(shí)
//sda由0-->1
SDA = 0; //準(zhǔn)備上升沿
SCL = 1;
NOP_5;
SDA = 1;
NOP_5;
//SCL = 0; //本來(lái)書上說(shuō)要此句,但發(fā)覺(jué)加入后仿真不正確...
//SDA = 0;
}
/*
********************************************************************
** 函數(shù)名:查詢應(yīng)答信號(hào)
** 注意 :
** 說(shuō)明 :如果有應(yīng)答返回 1
********************************************************************
*/
uchar WaitAsk( void )
{
uchar askflag = 0;
SDA = 1; //SDA置為輸入
SCL = 1;
NOP_5; //5us后檢測(cè)
if( SDA == 0 ) //檢測(cè)sda線
askflag = 1; //有應(yīng)答返回 1 表示成功
SCL = 0;
return askflag;
}
/*
********************************************************************
** 函數(shù)名:發(fā)送應(yīng)答信號(hào)
** 注意 :
** 說(shuō)明 :SCL = 1 , SDA = 0;
********************************************************************
*/
void SendAsk( void )
{
SDA = 0;
SCL = 1;
NOP_5;
SCL = 0; //在scl為高時(shí),sda為0
SDA = 1;
}
/*
********************************************************************
** 函數(shù)名:發(fā)送非應(yīng)答信號(hào)
** 注意 :
** 說(shuō)明 :SCL = 1 , SDA = 1
********************************************************************
*/
void SendNoAsk( void )
{
SDA = 1;
SCL = 1;
NOP_5;
SCL = 0; //在scl為高時(shí), sda為1
SDA = 0;
}
/*
********************************************************************
** 函數(shù)名 :寫一個(gè)字節(jié)數(shù)據(jù)
** 入口參數(shù): 字節(jié)數(shù)據(jù)wbyte
** 注意 :
** 說(shuō)明 :可以用來(lái)寫地址字節(jié),也可以用來(lái)寫數(shù)據(jù)字節(jié)
********************************************************************
*/
void I2cWriteByte( uchar wbyte )
{
uchar i;
for(i = 0; i < 8; i++ )
{
if( ( wbyte & 0x80 ) == 0x80 ) //!!!! 先發(fā)送高位,再發(fā)送低位.., 在數(shù)據(jù)傳輸時(shí)一定要注意此處細(xì)節(jié)
SDA = 1;
else
SDA = 0; //因?yàn)閿?shù)據(jù)傳輸時(shí)要保持?jǐn)?shù)據(jù)穩(wěn)定, 因此要先準(zhǔn)備好SDA上的數(shù)據(jù)才能進(jìn)行SCL的變化
SCL = 1;
NOP_5;
SCL = 0;
wbyte = wbyte << 1;
}
}
/*
********************************************************************
** 函數(shù)名 :寫一個(gè)字節(jié)數(shù)據(jù)到某器件某地址
** 入口參數(shù): 器件地址DeviceAddress 字節(jié)地址ByteAddress 要寫的字節(jié)數(shù)據(jù)Wdata
** 注意 :里面加有5ms延時(shí)。
** 說(shuō)明 :I2cWriteDataToAddr( 0xa0, 0x08, 'a' );
********************************************************************
*/
void I2cWriteDataToAddr( uchar DeviceAddress, uint ByteAddress, uchar Wdata )
{
I2cStart();
I2cWriteByte( DeviceAddress );
WaitAsk(); //等待應(yīng)答
I2cWriteByte( ByteAddress >> 8 ); //高8位
WaitAsk();
I2cWriteByte( ByteAddress ); //低8位
WaitAsk();
I2cWriteByte( Wdata );
WaitAsk();
I2cStop();
delay_5ms(); //發(fā)覺(jué)這里要加一小段延時(shí),如果不加,則可以在外面加。
}
/*
********************************************************************
** 函數(shù)名 :讀一個(gè)字節(jié)數(shù)據(jù)
** 入口參數(shù): 無(wú)
** 注意 :
** 說(shuō)明 :
********************************************************************
*/
uchar I2cReadByte( void )
{
uchar rbyte = 0;
uchar i = 0;
for(i = 0; i < 8; i++ )
{
rbyte = rbyte << 1; //非常注意...此語(yǔ)句不放在循環(huán)體內(nèi)最后.
SDA = 1; //SDA為輸入
SCL = 1;
NOP_5;
if( SDA == 1 )
rbyte = rbyte | 0x01;
SCL = 0;
}
return rbyte;
}
/*
********************************************************************
** 函數(shù)名 :寫一個(gè)字節(jié)數(shù)據(jù)到某器件某地址
** 入口參數(shù): 器件地址DeviceAddress 字節(jié)地址ByteAddress
** 出口參數(shù): 讀到的字節(jié)數(shù)據(jù)rdata
** 注意 :
** 說(shuō)明 :I2cWriteDataToAddr( 0xa0, 0x08, 'a' );
********************************************************************
*/
uchar I2cReadDataFromAddr( uchar DeviceAddress, uint ByteAddress )
{
uchar rdata;
I2cStart();
I2cWriteByte( DeviceAddress );
WaitAsk(); //等待應(yīng)答
I2cWriteByte( ByteAddress >> 8 ); //高8位
WaitAsk();
I2cWriteByte( ByteAddress ); //低8位
WaitAsk();
I2cStart();
I2cWriteByte( DeviceAddress | 0x01 ); //讀
WaitAsk();
rdata = I2cReadByte();
SendNoAsk(); //不發(fā)送應(yīng)答信號(hào)
I2cStop();
return rdata;
}
/*
********************************************************************
** 函數(shù)名 :連續(xù)寫字節(jié)數(shù)據(jù)到某器件某地址之后的好幾個(gè)單元
** 入口參數(shù): 器件地址DeviceAddress 字節(jié)地址ByteAddress 要寫的字節(jié)數(shù)據(jù)取址*Wdata 字節(jié)數(shù)據(jù)的個(gè)數(shù)n
** 注意 :里面加有5ms延時(shí)。
** 說(shuō)明 :I2cWriteDataToAddr( 0xa0, 0x08, "hebei is a big pig!", 20 );
********************************************************************
*/
void I2c_Write_n( uchar DeviceAddress, uint ByteAddress, uchar *Wdata, uchar n )
{
uchar i = 0;
I2cStart();
I2cWriteByte( DeviceAddress );
WaitAsk(); //等待應(yīng)答
I2cWriteByte( ByteAddress >> 8 ); //高8位
WaitAsk();
I2cWriteByte( ByteAddress ); //低8位
WaitAsk();
for( i = 0; i < n; i++ )
{
I2cWriteByte( *Wdata );
WaitAsk();
Wdata++;
}
I2cStop();
delay_5ms(); //發(fā)覺(jué)這里要加一小段延時(shí),如果不加,則可以在外面加。
}
/*
********************************************************************
** 函數(shù)名 :寫一個(gè)字節(jié)數(shù)據(jù)到某器件某地址
** 入口參數(shù): 器件地址DeviceAddress 字節(jié)地址ByteAddress
** 出口參數(shù): 讀到的字節(jié)數(shù)據(jù)rdata
** 注意 :
** 說(shuō)明 :I2c_Read_n( 0xa0, 0x0003, a, 10 ) //uchar a[10];
********************************************************************
*/
void I2c_Read_n( uchar DeviceAddress, uint ByteAddress, uchar *rdatabuf, uchar n )
{
uchar i = 0;
I2cStart(); //啟動(dòng)總線
I2cWriteByte( DeviceAddress );
WaitAsk(); //等待應(yīng)答
I2cWriteByte( ByteAddress >> 8 ); //高8位
WaitAsk();
I2cWriteByte( ByteAddress ); //低8位
WaitAsk();
I2cStart();//重新啟動(dòng)
I2cWriteByte( DeviceAddress | 0x01 ); //讀
WaitAsk();
for( i = 0; i < n; i++ )
{
*rdatabuf = I2cReadByte();
SendAsk(); //連續(xù)發(fā)送應(yīng)答信號(hào)
rdatabuf++;
}
I2cStop();
}