?? i2c_24c64.c
字號(hào):
/***********************************************************
**模塊名稱:24C64的讀寫
**功能描述:24C64儲(chǔ)存開機(jī)次數(shù)實(shí)驗(yàn)
**該試驗(yàn)功能是單片機(jī)復(fù)位一次, 自動(dòng)從24C64中讀取數(shù)據(jù)
**然后加1,最終數(shù)碼管中的數(shù)據(jù)就是開機(jī)的次數(shù),具有一定的實(shí)用意義
**燒寫后用手按復(fù)位鍵可以看到數(shù)碼管每按一下加一,也可以斷電再開機(jī)
**********************************************************/
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
code unsigned char seg7code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0xff}; //共陽數(shù)碼管段碼
sbit SDA=P3^5; //定義數(shù)據(jù)線
sbit SCL=P3^4; //定義時(shí)鐘線
bit flag;
uint idata ucSendBuffer[1]=0;
uint idata ucReceData;
uint idata ucReceiveBuffer; //從器件中讀出的1字節(jié)數(shù)據(jù)暫存區(qū)
void delay(void);
void delay_10ms(void);
void ACK();
void NoACK();
/**************************************************************/
void delay(void)
{
uint i;
for(i=100;i>0;i--)
_nop_();
}
void delay1ms()
{
uchar i;
for(i=124;i>0;i--); //延時(shí)124*8+10=1002us
}
/*********************************************************
**名稱:I2C_Start
**功能:啟動(dòng)I2C
**輸入:無
**返回:無
*********************************************************/
void I2C_Start()
{
SDA=1;
delay();
SCL=1;
delay();
SDA=0;
delay();
SCL=0; //鉗位I2C總線,準(zhǔn)備發(fā)送數(shù)據(jù)
}
/**********************************************************
**名稱:I2C_Stop
**功能:停止I2C
**輸入:無
**返回:無
**********************************************************/
void I2C_Stop()
{
SDA=0;
delay();
SCL=1;
delay();
SDA=1;
delay();
}
/**********************************************************
**名稱:Ack
**功能:應(yīng)答信號(hào)
**輸入:無
**返回:無
**********************************************************/
void Ack()
{
SDA=0;
delay();
SCL=1;
delay();
SCL=0;
delay();
SDA=1;
delay();
}
/********************************************************
**名稱:NoAck
**功能:發(fā)送非應(yīng)答信號(hào)
**輸入:無
**返回:無
********************************************************/
void NoAck()
{
SDA=1;
delay();
SCL=1;
delay();
SCL=0;
delay();
SDA=0;
delay();
}
/********************************************************
**名稱:Test_Ack()
**功能:檢測應(yīng)答位
**輸入:無
**返回:flag,有應(yīng)答時(shí)flag為0,無應(yīng)答時(shí)flag為1
*********************************************************/
bit Test_Ack()
{
SCL=0;
SDA=1;//讀入數(shù)據(jù)
_nop_();_nop_();_nop_();_nop_();
SCL=1;
_nop_();_nop_();_nop_();_nop_();
if(SDA==0)
flag=1;
else flag=0;
SCL=0;
return(flag);
}
/********************************************************
**名稱:SendData()
**功能:發(fā)送一字節(jié)數(shù)據(jù)
**輸入:buffer
**返回:
*******************************************************/
void SendData(uint buffer)
{
uint BitCnt=8;//一字節(jié)8位
uint temp=0;
do
{
temp=buffer;
SCL=0;
delay();
if((temp&0x80)==0) //判斷最高位是0還是1
SDA=0;
else
SDA=1;
delay();
SCL=1;
temp=_crol_(buffer,1);//將buffer中的數(shù)據(jù)左移一位
buffer=temp;
BitCnt--;
}
while(BitCnt);
SCL=0;
}
/**************************************************************
**名稱:uint ReceiveData()
**功能:接收一字節(jié)數(shù)據(jù)
**輸入:
**返回:ucReceData
**說明:將接收的數(shù)據(jù)存放在ucReceData中
**************************************************************/
uint ReceiveData()
{
uint BitCnt=8;
uint temp=0;
SDA=1;//讀入數(shù)據(jù)
do
{
SCL=0;
delay();
SCL=1;
delay();
if(SDA==1)
ucReceData=ucReceData|0x01; //低位置1
else
ucReceData=ucReceData&0x0fe; //低位清0
if(BitCnt-1)
{
temp=_crol_(ucReceData,1); //數(shù)據(jù)左移一位
ucReceData=temp;
}
BitCnt--;
}
while(BitCnt);
SCL=0;
return(ucReceData);
}
/*************************************************************
**名稱:bit WriteNByte()
**功能:主機(jī)向24C64中寫入多字節(jié)數(shù)據(jù)
**輸入:
**返回:
**說明:sla-器件地址, suba-數(shù)據(jù)高8位地址,subab-數(shù)據(jù)低8位地址,*s-寫入的數(shù)據(jù),n-寫入的字節(jié)數(shù)(n<=32)
**************************************************************/
bit WriteNByte(uint sla,uint suba,uint subab,uint *s,uint n)
{
uint i;
I2C_Start();//啟動(dòng)I2C
SendData(sla);//發(fā)送器件地址
Test_Ack();
if(flag==0) return(0);
SendData(suba);
Test_Ack();
if(flag==0) return(0);
SendData(subab);
Test_Ack();
if(flag==0) return(0);
for(i=0;i<n;i++)//寫入32字節(jié)數(shù)據(jù)
{
SendData(*(s+i));
Test_Ack();
if(flag==0) return(0);
}
I2C_Stop();
return(1);
}
/*************************************************************
**名稱:bit ReadNByte()
**功能:主機(jī)從24C64中讀出N字節(jié)數(shù)據(jù)(n<=32)
**輸入:
**返回:
**說明:隨機(jī)地址讀操作
**************************************************************/
bit ReadNByte(uint sla,uint suba,uint subab,uint *p,uint n)
{
uint i;
I2C_Start();//啟動(dòng)I2C
SendData(sla);//發(fā)送器件地址
Test_Ack();
if(flag==0) return(0);
SendData(suba);//發(fā)送器件內(nèi)部高8位地址
Test_Ack();
if(flag==0) return(0);
SendData(subab);//發(fā)送器件內(nèi)部低8位地址
Test_Ack();
if(flag==0) return(0);
I2C_Start();
SendData(sla+1);
Test_Ack();
if(flag==0) return(0);
for(i=0;i<n-1;i++)//讀取字節(jié)數(shù)據(jù)
{
*(p+i)=ReceiveData();//讀取數(shù)據(jù)
ACK();
}
*(p+n-1)=ReceiveData();
NoACK();
I2C_Stop();
return(1);
}
/***************************************************************
**名稱:main()
**功能:
**輸入:
**返回:
**說明:
****************************************************************/
void main()
{
ReadNByte(0xa0,0x00,0xff,ucSendBuffer,1);
ucSendBuffer[0]++;
WriteNByte(0xa0,0x00,0xff,ucSendBuffer,1);
while(1)
{
P1=0xfd; //P1.1=0,選通第二位
P2=seg7code[ucSendBuffer[0]%1000/100]; //百位
delay1ms();
P2=0xff; //消隱
P1=0xfb; //P1.3=0,選通第三位
P2=seg7code[ucSendBuffer[0]%100/10]; //十位
delay1ms();
P2=0xff; //消隱
P1=0xf7; //P1.3=0,選通第四位
P2=seg7code[ucSendBuffer[0]%10]; //個(gè)位
delay1ms();
P2=0xff; //消隱
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -