|
引入該頭文件,直接調用uchar readbyte (uint add) void writebyteseq(uint add,uchar *ptr,uint writelen) 函數即可
注意FM_DATA引腳必須接上拉電阻,否則不能正常讀取數據,數據時和時鐘線接51單片機準雙向口。
#include<reg52.h>
#include<intrins.h>
#include "stc12c5a16s2.h"
#define IIC_READ 0XA1 //定義讀指令
#define IIC_WRITE 0XA0 //定義寫指令
#define uchar unsigned char //定義一下方便使用
#define uint unsigned int
#define NOP _nop_()
/***************************************************************/
uchar idata buff[32]; //save the byte read out form iic device in test operation
uchar idata readbuff[32];//測試數組,暫存寫入讀出數據
//uchar writebuff[32];
//===============================
sbit SDA = P4^5; //THE SDA BIT IS PORT 4 BIT 2
sbit SCL = P4^4; //THE SCL BIT IS PORT 1 BIT 0
//===============================
//define a bit_operation byte to use in shift operation
//use this mode can achieve high operation speed
uchar bdata bbyte;//定義位操作用數組,采用此方法可提高位操作速度
sbit a0 = bbyte^0;
sbit a1 = bbyte^1;
sbit a2 = bbyte^2;
sbit a3 = bbyte^3;
sbit a4 = bbyte^4;
sbit a5 = bbyte^5;
sbit a6 = bbyte^6;
sbit a7 = bbyte^7;
//========================================
bit IFACK; //record the SDA state to confirm if ACK has happened
bit NO_ACK; //no ack flag
/***************************************************************/
//FUNCTION:ROUTES TO PROVIDE A START SIGNAL
void start(void)
{
SCL=0;SDA=1;SCL=1;SDA=0;SCL=0;
}
//=======================================
//FUNCTION:ROUTES TO PROVIDE A STOP SIGNAL
void stop(void)
{
SCL=0;SDA=0;SCL=1;SDA=1;SCL=0;
}
//=====================================
//FUNCTION:ROUTES TO PROVIDE ACK SINGAL
void ack(void)
{
SCL=0;SDA=0;SCL=1;SCL=0;
}
//=====================================
//FUNCTION:ROUTES TO RELEASE THE SDA TO RECEIVE A ACK SIGNAL
// OR TO PROVIDE A NO_ACK SIGNAL
//type=1等待應答信號
//type=0 產生無應答信號
void nack(uchar type)
{
SCL=0;SDA=1;SCL=1;
IFACK=SDA; SCL=0;
if(type)
{
if(IFACK)//如果無應答信號,則置標志位NO_ACK,程序中止
{
NO_ACK=1;//用戶可以加入自己的異常處理程序
//while(1);
}
else NO_ACK=0;
}
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT OUT A BYTE TO THE MASTER
unsigned char inbyte(void)
{ //從IIC器件中讀出數據
unsigned char get_value,i;
get_value=0;
SCL=0;
for(i=0;i<8;i++)
{
SDA=1;
SCL = 1;
get_value <<= 1;//首先接收到的為最高位
if(SDA==1)get_value += 1;
SCL = 0;
}
return get_value;
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT IN A BYTE FROM THE MASTER
void outbyte(unsigned char outdata) {//將數據寫入IIC器件
unsigned int i;
SCL=0;
for(i=0;i<8;i++)
{
if(outdata & (0x80>>i)) // 1000 0000
{
SDA=1;
SCL=1; //高電平數據有效
SCL=0;
}
else
{
SDA=0;
SCL=1; //高電平數據有效
SCL=0;
}
}
}
//======================================================
//FUNCTION:BYTE WRITE. 'add' THE WRITE ADDRESS, 'wbyte' THE DATA WANT TO WRITE
void writebyte(uint add,uchar wbyte)//add為寫入地址,wbyte為寫入數據
{
uchar temp;
temp=IIC_WRITE; //A0
if(add>>8){temp|=0x02;} //add = 0x01,temp= A0 else temp = A2 10000000
start(); //開始信號
outbyte(temp); //寫命令
nack(1); //等待應答
outbyte((uchar)add); //寫地址
nack(1); //等待應答
outbyte(wbyte); //寫數據
nack(1); //等待應答
stop(); //停止信號
}
//=====================================================
//FUNCTION:RANDOM READ.'add' IS THE ADDRESS WANT TO READ
uchar readbyte (uint add)//add為讀地址
{
uchar temp,tempr;
temp=IIC_WRITE; // A0;
tempr=IIC_READ;
if(add>>8){temp|=0x02;tempr|=0x02;}
start(); //開始信號
outbyte(temp); //寫命令
nack(1); //等待應答
outbyte((uchar)add); //寫地址
nack(1); //等待應答
start(); //開始信號
outbyte(tempr); //讀命令
nack(1); //等待應答
temp=inbyte(); //讀數據
nack(0); //無應答
stop(); //停止信號 */
return(temp);
}
//=================================================
//連寫函數
//add為讀起始地址,ptr數據保存指針,writelen為寫入數據長度
void writebyteseq(uint add,uchar *ptr,uint writelen)
{
uchar temp;
uint i;
temp=IIC_WRITE;
if(add>>8){temp|=0x02;}
start();
outbyte(temp);
nack(1);
outbyte((uchar)add);
nack(1);
for(i=0;i<writelen;i++)
{
outbyte(*(ptr+i));
nack(1);
}
stop();
}
//=================================================
//連讀函數
//add為讀起始地址,ptr數據保存指針,writelen為讀出數據長度
void readbyteseq(uint add,uchar *ptr,uint readlen)
{
uchar temp,tempr;
uint i;
temp=IIC_WRITE;
tempr=IIC_READ;
if(add>>8){temp|=0x02;tempr|=0x02;}
start();
outbyte(temp);
nack(1);
outbyte((uchar)add);
nack(1);
start();
outbyte(tempr);
nack(1);
for(i=0;i<readlen-1;i++)
{
*(ptr+i)=inbyte();
ack();
}
*(ptr+readlen-1)=inbyte();
nack();
stop();
}
//=======================================================
|
-
電路圖
-
-
fm24cl04b.zip
2020-3-1 00:21 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
1.87 KB, 下載次數: 9, 下載積分: 黑幣 -5
評分
-
查看全部評分
|