?? 24c256.c
字號:
#include "Cabe.H"
/*---------------------------------------------
讀寫24c256標準程序段
作者:dasheng
Modified by:rgh111,for FM24C256. 04-8-16 14:08
I have deleted the page read &write subroutine.
-------------------------------------------*/
#define SCL P16
#define SDA P17
/*-----------------------------------------------
調用方式:void start_bit(void)
函數說明:開始位
-----------------------------------------------*/
void start_bit(void)
{
SCL = 1;
SDA = 1;
_nop_();
SDA = 0;
_nop_();
SCL = 0;
_nop_();
}
/*-----------------------------------------------
調用方式:void stop_bit(void)
函數說明:停止位
-----------------------------------------------*/
void stop_bit(void)
{
SDA = 0;
_nop_();
SCL = 1;
_nop_();
SDA = 1;
_nop_();
SCL = 0;
}
/*-----------------------------------------------
調用方式:void mast_ack(void)
函數說明:主答函數
-----------------------------------------------*/
/*
void mast_ack(void)
{
SCL=0;
_nop_();
SDA=0;
_nop_();
SCL=1;
_nop_();
SCL=0;
_nop_();
SDA=1;
_nop_();
}
*/
/*-----------------------------------------------
調用方式:void ack(void)
函數說明:應答函數
-----------------------------------------------*/
void ack(void)
{
SDA=0;
_nop_();
SCL = 1;
_nop_();
SCL = 0;
//while(SDA){;} //This may make the CPU crash. 04-3-18 17:16
}
/*-----------------------------------------------
調用方式:void no_ack(void)
函數說明:無需應答位,在讀程序中用到
-----------------------------------------------*/
void no_ack(void)
{
SDA=1;
_nop_();
SCL=1;
_nop_();
//while(SDA){;} //This may make the CPU crash. 04-3-18 17:16
SCL=0;
}
/*-----------------------------------------------
調用方式:Bit WaitAck(void)
函數說明:需應答位,在寫程序中用到
-----------------------------------------------*/
Bit WaitAck(void)
{
Byte errtime=255;//因故障接收方無ACK,超時值為255。
SDA=1;
_nop_();
SCL=1;
_nop_();
while(SDA)
{
errtime--;
if (!errtime)
{
return false;
}
}
SCL=0;
return true;
}
/*-----------------------------------------------
調用方式:void write_8bit(uchar ch)
函數說明:寫一個字節(8位)數據
-----------------------------------------------*/
void write_8bit(Byte ch)
{
Byte i=8;
EA = 0;
SCL=0;
_nop_();
while(i--)
{
SDA=(bit)(ch&0x80);
_nop_();
ch<<=1;
SCL=1;
_nop_();
SCL=0;
_nop_();
}
EA = 1;
}
/*-----------------------------------------------
調用方式:void Read_8bit(uchar ch)
函數說明:讀一個字節(8位)數據
-----------------------------------------------*/
Byte Read_8bit(void)
{
Byte data i = 8,rdata = 0;;
SDA = 1;
_nop_();
EA = 0;
while(i--)
{
rdata<<=1;
SCL=0;
_nop_();
SCL=1;
if(SDA) rdata|=0x01;
}
SCL = 0;
EA = 1;
return rdata;
}
/*----------------------------------------------
調用方式:Byte read24c256(uint address)
函數說明:讀24c256指定地址數據(字節讀)
-----------------------------------------------*/
Byte ReadCharFm24c256(Word address)
{
Byte data rdata,Addr;
Byte i = 8;
//EA=0;//避免與串口通訊等中斷沖突
start_bit();
if(address < 0x8000)
{
write_8bit(0xA0);
Addr = 0xA0 + 1;
}
else
{
write_8bit(0xA2);
Addr = 0xA2 + 1;
}
WaitAck();
write_8bit(address/256);
WaitAck(); //偽寫
write_8bit((Byte)address);
WaitAck();
start_bit();
write_8bit(Addr);
WaitAck();
while(i--)
{
rdata<<=1;
SCL=0;
_nop_();
SCL=1;
if(SDA) rdata|=0x01;
}
no_ack();
stop_bit();
// EA=1;
return rdata;
}
/*--------------------------------------------------------------
調用方式:void ReadFm24c256(Word Addr,Word Lenth,Byte *buffer)
函數說明:從指定的地址讀出Lenth長度的數據放入buffer中
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -