本帖最后由 **kfu 于 2013-5-2 09:47 编辑
20130322sn8p2711_eeprom_test(C program).rar
(85.77 KB)
这个是个简单的eeprom读写测试程序的完整工程。你可以试试。
/**
******************************************************************************
* @filesn8p_eeprom.c
* [url=home.php?mod=space&uid=187600]@author[/url] gdw
* [url=home.php?mod=space&uid=895143]@version[/url] V1.0
* [url=home.php?mod=space&uid=212281]@date[/url] 2013.3.22
* [url=home.php?mod=space&uid=247401]@brief[/url] eeprom read&wirte
******************************************************************************
* @attention
* sn8p2711 Fcpu=Fosc/4=16MHz/4=4MHz 测试通过
* 通信速率:100KHz
******************************************************************************
*/
#include"sn8p_eeprom.h"
bit error;
static void delay5us(void)
{
__asm{
JMP $+1;
JMP $+1;
JMP $+1;
JMP $+1;
JMP $+1;
JMP $+1;
JMP $+1;
}
}
static void start(void) //开始信号
{
sdam=1;
sclm=1;
sda=1;
scl=1;
delay5us();
sda=0;
delay5us();
scl=0;
}
static void stop(void) //停止
{
sdam=1;
sclm=1;
sda=0;
delay5us();
scl=1;
delay5us();
sda=1;
delay5us();
}
static void ack(void) //应答
{
uchar i=0;
sdam=1;
sclm=1;
sda=1;//释放sda数据总线
sdam=0;
delay5us();
scl=1;
delay5us();
while((sda==1)&&(i<8))
{
i++;
}
if(i>5)
{
i=0;
error=true;
//SLEEPMODE;
}
scl=0;
}
static void MasterAck(void)
{
sclm = 1;
sdam = 1;
scl = 0;
sda = 0;//MasterAck
delay5us();
scl = 1;
delay5us();
// sda = 1; //出现问题点
sdam =0;
scl = 0;
}
static void Write_Byte(uchar data)
{
uchar i;
sclm=1;
sdam=1;
for(i=0;i<8;i++)
{
if(data&0x80>>i)
{
sda=1;
}
else
{
sda=0;
}
delay5us();
scl=1;
delay5us();
scl=0;
}
}
static uchar Read_Byte(void)
{
uchar i,b=0;
sdam=0;
sclm=1;
for(i=0;i<8;i++)
{
scl=1;
delay5us();
b=(b<<1)|sda;
scl=0;
delay5us();
}
sda =0;
sdam=1;
return b;
}
/****************************************************************************
* _______ _ _ _ _ _ _ _ _ _ ______
*SCL \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/
*
* start| 1 0 1 0 0 0 0 0 |ack |stop
* ____ ___ ___ ___
*SDA \___/ \___/ \___________________/___\___/
*
*****************************************************************************
* _______ _ _ _ _ _ _ _ _ _____
*SCL \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/
*
* start| 1 0 1 0 0 0 0 |ack |stop
* ____ _____ _____ ___
*SDA \____/ \_____/ \_______________________/_____\_____/
*
*****************************************************************************
*/
/*
function: 页写入
in: DeviceAddress 设备地址
DataAddress 数据写入的起始地址
DataBuffer 数据所在的buffer地址
DataSize 数据大小
out:
*/
void ByteWrite(uchar DeviceAddress , uchar DataAddress , uchar data)
{
start();
Write_Byte(DeviceAddress + 0);//Write
ack();
Write_Byte(DataAddress);
ack();
Write_Byte(data);
ack();
stop();
}
/*
function: 页写入
in: DeviceAddress 设备地址
DataAddress 数据写入的起始地址
DataBuffer 数据所在的buffer地址
DataSize 数据大小
out:
*/
void PageWrite(uchar DeviceAddress , \
uchar DataAddress , uchar *DataBuffer ,uchar DataSize)
{
uchar p_offset;
start();
Write_Byte(DeviceAddress);
ack();
Write_Byte(DataAddress);
ack();
for(p_offset=0;p_offset<DataSize;p_offset++)
{
Write_Byte(*(DataBuffer+p_offset));
ack();
}
stop();
}
/*
function: 连续地址读
in: data ,保存数据的buffer地址
out:
*/
void SequentialRead(uchar DeviceAddress , \
uchar DataAddress , uchar *DataBuffer, uchar NumDataRead)
{
uchar i;
start();
Write_Byte(DeviceAddress+0);//Write
ack();
Write_Byte(DataAddress);
ack();
start();
Write_Byte(DeviceAddress + 1);//Read
ack();
for(i=0;i<NumDataRead;i++)
{
*(DataBuffer+i)=Read_Byte();
MasterAck();
}
//NoAck
stop();
}
/*
function: 任意地址读
in: address
out: data
*/
uchar RandomRead(uchar DeviceAddress , uchar DataAddress)
{
uchar data;
start();
Write_Byte(DeviceAddress+0);//Write
ack();
Write_Byte(DataAddress);
ack();
start();
Write_Byte(DeviceAddress+1);//Read
ack();
data=Read_Byte();
//NO ACK
stop();
return data;
}
/******************************sn8p_eeprom.c END*********************************/
|