FM24C64汇编测试程序c语言(8052)
//THIS CODE WAS DESIGNED TO DEMONSTRATE HOW THE FM24C04/FM24C04A SERIAL FRAM COULD
//BE INTERFACED TO THE 8051 MICROCONTROLLER. THE INTERFACE USES 2 LINES
//FROM PORT 2 (P2.7 AND P2.6) TO COMMUNICATE.
//THE CODE SHOWN DEMONSTRATES A 'RANDOM READ' AND 'BYTE WRITE'. THE OTHER *
//MODES OF OPERATION CAN BE CREATED BY EXPANDING UPON THESE ROUTINES.
//深圳市华胄科技有限公司
//the first date writed:2003-11-20
//=============================
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define IIC_READ 0XA1 //定义读指令
#define IIC_WRITE 0XA0 //定义写指令
#define NOP _nop_()
//#define SDA INT0
//#define SCL T0
//==============================
uchar idata buff[32]; //save the byte read out form iic device in test operation
uchar idata readbuff[32];//测试数组,暂存写入读出数据
//uchar writebuff[32];
//===============================
sbit SDA=P1^4;//P2^7; //THE SDA BIT IS PORT 2 BIT 7
sbit SCL=P1^7;//P2^6; //THE SCL BIT IS PORT 2 BIT 6
//sbit WP=P2^5;
//sbit test=P1^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 confirn if ACK has happened
bit NO_ACK; //no ack flag
bit BUS_FAULT; //bus fault 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
uchar inbyte(void) {//从IIC器件中读出数据
SDA=1;
SCL=1;a7=SDA;SCL=0;
SCL=1;a6=SDA;SCL=0;
SCL=1;a5=SDA;SCL=0;
SCL=1;a4=SDA;SCL=0;
SCL=1;a3=SDA;SCL=0;
SCL=1;a2=SDA;SCL=0;
SCL=1;a1=SDA;SCL=0;
SCL=1;a0=SDA;SCL=0;
return(bbyte);
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT IN A BYTE FROM THE MASTER
void outbyte(uchar outdata) {//将数据写放IIC器件
bbyte=outdata;
SDA=a7;SCL=1;SCL=0;
SDA=a6;SCL=1;SCL=0;
SDA=a5;SCL=1;SCL=0;
SDA=a4;SCL=1;SCL=0;
SDA=a3;SCL=1;SCL=0;
SDA=a2;SCL=1;SCL=0;
SDA=a1;SCL=1;SCL=0;
SDA=a0;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 temph,templ;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
start(); //开始信号
outbyte(IIC_WRITE); //写命令
nack(1); //等待应答
outbyte(temph); //写地址
nack(1); //等待应答
outbyte(templ); //写地址
nack(1);
outbyte(wbyte); //写数据
nack(1); //等待应答
stop(); //停止信号
}
//=====================================================
//FUNCTION:RANDOM READ.'add' IS THE ADDRESS WANT TO READ
uchar readbyte (uint add)//add为读地址
{
uchar temp,temph,templ;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
start(); //开始信号
outbyte(IIC_WRITE); //写命令
nack(1); //等待应答
outbyte(temph); //写地址
nack(1); //等待应答
outbyte(templ); //写地址
nack(1);
start(); //开始信号
outbyte(IIC_READ); //读命令
nack(1); //等待应答
temp=inbyte(); //读数据
nack(0); //无应答
stop(); //停止信号
return(temp);
}
//=================================================
//连写函数
//add为读起始地址,ptr数据保存指针,writelen为写入数据长度
void writebyteseq(uint add,uchar *ptr,uint writelen)
{
//uchar temp;
uchar temph,templ;
uint i;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
start();
outbyte(IIC_WRITE);
nack(1);
outbyte(temph);
nack(1);
outbyte(templ);
nack(1);
for(i=0;i255){j=0;}
for(i=0;i<32;i++) readbuff[i]=j;
writebyteseq(0, readbuff, 32);
for(i=0;i<32;i++) readbuff[i]=0;
readbyteseq(0,readbuff,32);
//for(i=0;i<32;i++)
//writebyte(i,i);
for(i=0;i<32;i++)
buff[i]=readbyte(i);
i=0;
}
}
//BE INTERFACED TO THE 8051 MICROCONTROLLER. THE INTERFACE USES 2 LINES
//FROM PORT 2 (P2.7 AND P2.6) TO COMMUNICATE.
//THE CODE SHOWN DEMONSTRATES A 'RANDOM READ' AND 'BYTE WRITE'. THE OTHER *
//MODES OF OPERATION CAN BE CREATED BY EXPANDING UPON THESE ROUTINES.
//深圳市华胄科技有限公司
//the first date writed:2003-11-20
//=============================
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define IIC_READ 0XA1 //定义读指令
#define IIC_WRITE 0XA0 //定义写指令
#define NOP _nop_()
//#define SDA INT0
//#define SCL T0
//==============================
uchar idata buff[32]; //save the byte read out form iic device in test operation
uchar idata readbuff[32];//测试数组,暂存写入读出数据
//uchar writebuff[32];
//===============================
sbit SDA=P1^4;//P2^7; //THE SDA BIT IS PORT 2 BIT 7
sbit SCL=P1^7;//P2^6; //THE SCL BIT IS PORT 2 BIT 6
//sbit WP=P2^5;
//sbit test=P1^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 confirn if ACK has happened
bit NO_ACK; //no ack flag
bit BUS_FAULT; //bus fault 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
uchar inbyte(void) {//从IIC器件中读出数据
SDA=1;
SCL=1;a7=SDA;SCL=0;
SCL=1;a6=SDA;SCL=0;
SCL=1;a5=SDA;SCL=0;
SCL=1;a4=SDA;SCL=0;
SCL=1;a3=SDA;SCL=0;
SCL=1;a2=SDA;SCL=0;
SCL=1;a1=SDA;SCL=0;
SCL=1;a0=SDA;SCL=0;
return(bbyte);
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT IN A BYTE FROM THE MASTER
void outbyte(uchar outdata) {//将数据写放IIC器件
bbyte=outdata;
SDA=a7;SCL=1;SCL=0;
SDA=a6;SCL=1;SCL=0;
SDA=a5;SCL=1;SCL=0;
SDA=a4;SCL=1;SCL=0;
SDA=a3;SCL=1;SCL=0;
SDA=a2;SCL=1;SCL=0;
SDA=a1;SCL=1;SCL=0;
SDA=a0;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 temph,templ;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
start(); //开始信号
outbyte(IIC_WRITE); //写命令
nack(1); //等待应答
outbyte(temph); //写地址
nack(1); //等待应答
outbyte(templ); //写地址
nack(1);
outbyte(wbyte); //写数据
nack(1); //等待应答
stop(); //停止信号
}
//=====================================================
//FUNCTION:RANDOM READ.'add' IS THE ADDRESS WANT TO READ
uchar readbyte (uint add)//add为读地址
{
uchar temp,temph,templ;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
start(); //开始信号
outbyte(IIC_WRITE); //写命令
nack(1); //等待应答
outbyte(temph); //写地址
nack(1); //等待应答
outbyte(templ); //写地址
nack(1);
start(); //开始信号
outbyte(IIC_READ); //读命令
nack(1); //等待应答
temp=inbyte(); //读数据
nack(0); //无应答
stop(); //停止信号
return(temp);
}
//=================================================
//连写函数
//add为读起始地址,ptr数据保存指针,writelen为写入数据长度
void writebyteseq(uint add,uchar *ptr,uint writelen)
{
//uchar temp;
uchar temph,templ;
uint i;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
start();
outbyte(IIC_WRITE);
nack(1);
outbyte(temph);
nack(1);
outbyte(templ);
nack(1);
for(i=0;i
for(i=0;i<32;i++) readbuff[i]=j;
writebyteseq(0, readbuff, 32);
for(i=0;i<32;i++) readbuff[i]=0;
readbyteseq(0,readbuff,32);
//for(i=0;i<32;i++)
//writebyte(i,i);
for(i=0;i<32;i++)
buff[i]=readbyte(i);
i=0;
}
}
文章版权归西部工控xbgk所有,未经许可不得转载。