/*********************************************************************************************************
** 函数名称: WriteChar() , ReadChar() , WriteString() , ReadString() , Erase() , Eral()
** 功能描述: 93c46读取字符和字符串函数
** 输 入: 写入一个字符: address , InData
写入地址 , 写入数据
读出一个字符: address
读出相应地址的数据
写入字符串 : *RamAddress ,RomAddress , Number
读入数组指针 , 93c46写入起始地址 , 写入字节个数
读出字符串 : *RamAddress ,RomAddress , Number
读入数组指针 , 93c46写入起始地址 , 写入字节个数
擦除一个字节: address
要擦除的存储区地址
擦除所有存储区: 无输入函数
** 输 出: ...
** 全局变量: 无
** 调用模块: 无
** 作 者: 王恒智 (修改 程序匠人源码)
** 日 期: 2009年5月13日
********************************************************************************************************/
#ifndef __93C46_H__
#define __93C46_H__
sbit CS=P1^0;
sbit SK=P1^1;
sbit DI=P1^2;
sbit DO=P1^3;
/*
extern void WriteChar(unsigned char address,unsigned char InData);
extern unsigned char ReadChar(unsigned char address);
extern void WriteString(unsigned char data *RamAddress,unsigned char RomAddress,unsigned char Number);
extern void ReadString(unsigned char data *RamAddress,unsigned char RomAddress,unsigned char Number);
extern void Erase(unsigned char address)
extern void Eral(void)
*/
// Write enable must precede all programming modes.
void Ewen(void)
{
unsigned char temp,InData;
CS=0;
SK=0;
CS=1;
InData=0x98; // 10011XXXX
for(temp=9;temp!=0;temp--) { // 9
DI=InData&0x80;
SK=1; SK=0;
InData<<=1;
}
CS=0;
}
// Disables all programming instructions.
void Ewds(void)
{
unsigned char temp,InData;
CS=0;
SK=0;
CS=1;
InData=0x80; // 10000XXXX
for(temp=9;temp!=0;temp--) { // 9
DI=InData&0x80;
SK=1; SK=0;
InData<<=1;
}
CS=0;
}
// Reads data stored in memory, at specified address.
unsigned int Read(unsigned char address)
{
unsigned char temp;
unsigned int result;
Ewen(); //擦除和写入允许
SK=0; DI=1; // 110 A5-A0
CS=0; CS=1;
SK=1; SK=0; // 1
address=address&0x3f|0x80;
for(temp=8;temp!=0;temp--) // 8
{
DI=address&0x80;
SK=1; SK=0;
address<<=1;
}
DO=1;
for(temp=16;temp!=0;temp--)// 16
{
SK=1;
result=(result<<1)|DO;
SK=0;
}
CS=0;
Ewds(); //擦除和写入禁止
return(result);
}
// Writes memory location An - A0.
void Write(unsigned char address,unsigned int InData) {
unsigned char temp;
Ewen();
SK=0; DI=1; // 101 A5-A0
CS=0; CS=1;
SK=1; SK=0; // 1
address=address&0x3f|0x40;
for(temp=8;temp!=0;temp--) { // 8
DI=address&0x80;
SK=1; SK=0;
address<<=1;
}
for(temp=16;temp!=0;temp--) { // 16
DI=InData&0x8000;
SK=1; SK=0;
InData<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(DO==0) { // busy test
SK=0; SK=1;
}
SK=0; CS=0;
Ewds();
}
// Erase memory location An - A0.
void Erase(unsigned char address) {
unsigned char temp;
Ewen();
SK=0; DI=1; // 111 A5-A0
CS=0; CS=1;
SK=1; SK=0; // 1
address|=0xc0;
for(temp=8;temp!=0;temp--) { // 8
DI=address&0x80;
SK=1; SK=0;
address<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(DO==0) {
SK=0; SK=1;
}
SK=0; CS=0;
Ewds();
}
// Erases all memory locations. Valid only at VCC = 4.5V to 5.5V.
void Eral(void) {
unsigned char temp,InData;
Ewen();
CS=0;
SK=0;
CS=1;
InData=0x90; // 10010XXXX
for(temp=9;temp!=0;temp--) { // 9
DI=InData&0x80;
SK=1; SK=0;
InData<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(DO==0) {
SK=0; SK=1;
}
SK=0; CS=0;
Ewds();
}
/*
// Writes all memory locations. Valid only at VCC = 4.5V to 5.5V.
void Wral(unsigned int InData) {
unsigned char temp,address;
Ewen();
CS=0;
SK=0;
CS=1;
address=0x88; // 10001XXXX
for(temp=9;temp!=0;temp--) { // 9
DI=address&0x80;
SK=1; SK=0;
address<<=1;
}
for(temp=16;temp!=0;temp--) { // 16
DI=InData&0x8000;
SK=1; SK=0;
InData<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(DO==0) {
SK=0; SK=1;
}
SK=0; CS=0;
Ewds();
}
*/
unsigned char ReadChar(unsigned char address) {
unsigned char temp=address>>1;
if(address&0x01) return((unsigned char)(Read(temp)>>8));
else return((unsigned char)(Read(temp)));
}
void WriteChar(unsigned char address,unsigned char InData) {
unsigned char temp=address>>1;
if(address&0x01) Write(temp,(unsigned int)(Read(temp)&0x00ff|(InData<<8)));
else Write(temp,(unsigned int)(Read(temp)&0xff00|InData));
}
void ReadString(unsigned char *RamAddress,unsigned char RomAddress,unsigned char Number) {
while(Number!=0) {
*RamAddress=ReadChar(RomAddress);
RamAddress++;
RomAddress++;
Number--;
}
}
void WriteString(unsigned char *RamAddress,unsigned char RomAddress,unsigned char Number) {
unsigned int temp;
if(Number==0) return;
if(RomAddress&0x01)
{
WriteChar(RomAddress,*RamAddress);
RamAddress++;
RomAddress++;
Number--;
}
if(Number==0) return;
while(Number>>1)
{
temp=*RamAddress;
RamAddress++;
temp=temp|(*RamAddress)<<8;
RamAddress++;
Write(RomAddress>>1,temp);
RomAddress++;
RomAddress++;
Number--;
Number--;
}
if(Number) WriteChar(RomAddress,*RamAddress);
}
#endif
这是对93c46设计的头文件,是一些示例程序。我测试了,可以在板子上跑。但是我没有弄明白里面的result是指什么。 |