|
串行EEPROM存储器驱动
以AT93C66(已停产,替代为93C66A)为例。对93c86将地址改为11位即可。
管脚排列(extracted from datasheet):

ORG接地,为8位模式。
写操作时DO做检测位,输入完成后变低,写入完成后变高。平时高阻,所以应加上拉电阻(或用51管脚内的)。在检测到DO变高后CS需要复位以开始下一次操作。
SK上升沿读取数据。
指令集(extracted from datasheet)

C51编程:
文件头
//串行存储芯片声明开始----------------
//定义三线制Microware串行总线管脚 Three-wire Serial Interface TSI
sbit CS=P3^2;
sbit SK=P3^3;
sbit DI=P3^4;
sbit DO=P3^5;
//定义指令
#define IS93c86 0 //如果为93c86,置1
#if IS93c86 //6位,用于93c86,最高位地址加在最后3位
#define TSI_READ 0x30
#define TSI_EWEN 0x27
#define TSI_EWDS 0x20
#define TSI_WRITE 0x28
#else //4位,用于93c66,最高位地址加在最后1位
#define TSI_READ 0x0C
#define TSI_EWEN 0x09
#define TSI_EWDS 0x08
#define TSI_WRITE 0x0A
#endif
//定义数据类型(三线串行通信指针)
union intbyte{
unsigned char c[2];
unsigned short int i;
};
//函数声明
void tsiwrite(unsigned short int address,unsigned char value);
unsigned char tsiread(unsigned short int address);
void tsien(unsigned char en);
void tsisendcmd(unsigned char cmd);
void tsisendbyte(unsigned char value);
unsigned char tsireadbyte();
//存储位置指针变量
unsigned short int tsiptr;
//串行存储芯片声明结束----------------
主程序
//存储芯片初始化开始---------------
tsien(1);
//存储芯片初始化结束---------------
调用
......
tsiptr=200;
for(i=0;i<8;i++){
while(flag_uart_busy){
;}
flag_uart_busy=1;
SBUF=tsiread(tsiptr);
tsiptr=tsiptr+1;
.......
tsiptr=200;
for(i=0;i<8;i++){
c=clk[i];
tsiwrite(tsiptr,c);
tsiptr=tsiptr+1;
}
......
子程序
//存储芯片驱动代码开始----------------
/***************************************************
Three-wire Serial Interface TSI
发送四位指令,最后一位是地址最高位
完成后 CS处于置位状态,需要后续的sendbyte或readbyte复位
****************************************************/
void tsisendcmd(unsigned char cmd) {
unsigned char i;
DO=1;
SK=0;
CS=0;
CS=1;
while(!DO){ //检测上次写入是否完成
;
}
CS=0;
CS=1;
#if(IS93c86)
for(i=0;i<6;i++){
SK=0;
if(0x20&cmd) DI=1;
#else
for(i=0;i<4;i++){
SK=0;
if(0x08&cmd) DI=1;
#endif
else DI=0;
SK=1;
cmd<<=1;
}
SK=0;
}
/***************************************************
Three-wire Serial Interface TSI
发送1个字节
完成后 CS处于置位状态,
****************************************************/
void tsisendbyte(unsigned char value){
unsigned char i;
for(i=0;i<8;i++){
SK=0;
if(0x80&value) DI=1;
else DI=0;
SK=1;
value<<=1;
}
SK=0;
DI=0;
}
/***************************************************
Three-wire Serial Interface TSI
读1个字节
完成后 CS处于置位状态,
****************************************************/
unsigned char tsireadbyte(){
unsigned char i,res=0;
SK=0;
for(i=0;i<8;i++){
SK=1;
res*=2;
if (DO==1) res++;
SK=0;
}
return res;
}
/***************************************************
Three-wire Serial Interface TSI
允许/禁止tsi
***************************************************/
void tsien(unsigned char en){
DO=1;
if(en){
tsisendcmd(TSI_EWEN);
tsisendbyte(0xFF);
}
else{
tsisendcmd(TSI_EWDS);
tsisendbyte(0);
}
CS=0;
}
/***************************************************
Three-wire Serial Interface TSI
在地址address处写入value
***************************************************/
void tsiwrite(unsigned short int address,unsigned char value){
union intbyte add;
add.i=address;
#if(IS93c86)
add.c[0]&=0x07;//高字节在前
#else
add.c[0]&=0x01;//高字节在前
#endif
tsisendcmd(TSI_WRITE+add.c[0]);
tsisendbyte(add.c[1]);
tsisendbyte(value);
CS=0;
}
/***************************************************
Three-wire Serial Interface TSI
从地址address读出一个字节
返回读出值
***************************************************/
unsigned char tsiread(unsigned short int address){
union intbyte add;
unsigned char res;
add.i=address;
#if(IS93c86)
add.c[0]&=0x07;//高字节在前
#else
add.c[0]&=0x01;//高字节在前
#endif
tsisendcmd(TSI_READ+add.c[0]);
tsisendbyte(add.c[1]);
res=tsireadbyte();
CS=0;
return res;
}
//存储芯片驱动代码结束----------------
|