|
代码如下:
#include
#include "intrins.h"
sbit led = P2^0;
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char BYTE;
typedef unsigned char byte;
typedef unsigned int word;
void int_0(); //外部中断0
////////////////////////////////////////
///////////////////////////////////////
#define NADDR 7 //for 7 -- 93C46
sbit ROM_CS = P1^0; //93c46的片选端
sbit SK = P1^1; //93c46的时钟端
sbit D_I = P1^5; //93c46的数据端
sbit D_O = P1^6; //93c46的数据端
//////////////////////////////////////////
void delay_nop(void)
{
_nop_(); _nop_();
_nop_();_nop_();
}
byte shin(void)
{
byte i,d_out;
SK=0;
d_out= 0;
for (i = 0;i<8;i++)
{
delay_nop();
SK=1;
d_out<<=1;
D_O = 1; //引脚寄存器置1
delay_nop();
if(D_O)
{
d_out+=0x01;
}
delay_nop();
SK=0;
}
return d_out;
}
//the d_out is the data goen out.
void shout(byte num,byte ch)
{
byte i;
if(num <8)
{
ch<<=(8-num);
}
for(i = 0; i
{
delay_nop();
SK = 0;
delay_nop();
D_I= (ch&0x80)? 1:0;
SK = 1;
ch<<=1;
}
SK = 0;
}
// 'ch' is the data coming in, the num is the number of the byte for this data.
// return 0--ok 1--error
byte status(void)
{
byte i,j,flag;
D_O=1;
ROM_CS=1;
_nop_();
_nop_();
for (i=255;i;i--)
{
for (j=40;j;j--)
{
;
}
if (D_O == 1)
{
flag = 0;
break;
}
flag = 1;
}
return flag;
}
//擦除数据子程序
byte erase(word addr)
{
ROM_CS=1;
delay_nop();
shout(3,0x07); //0x111b erase byte command
shout(NADDR,(byte)addr);
ROM_CS=0; //disable
SK = 1;
return status();
}
//写使能函数(擦除或写操作前必须执行)子程序
void ewen(void)
{
word ch,temp;
byte i;
ROM_CS=1;
delay_nop();
ch = 0x0013; //write enable command 100,11
ch <<= (NADDR-2);
temp = ch;
temp>>=8;
i = (byte)temp;
shout(NADDR-5,i);
i = (byte)ch;
shout(8,i);
ROM_CS=0; //disable
}
//写禁止函数子程序
void ewds(void)
{
word ch,temp;
byte i;
ROM_CS=1;
delay_nop();
ch = 0x0010; //write all command 100,00
ch <<= (NADDR-2);
temp = ch;
temp>>=8;
i = (byte)temp;
shout(NADDR-5,i);
i = (byte)ch;
shout(8,i);
ROM_CS=0; //disable
}
byte read_eeprom(word addr)
{
byte ch;
ROM_CS=1;
delay_nop();
SK = 0;
shout(3,0x06); //0x06 110 read command
shout(NADDR,(byte)addr);
ch = shin();
ROM_CS=0; //disable
SK = 1;
D_O= 1;
return ch;
}
byte write(word addr,byte dat)
{
ROM_CS=1;
delay_nop(); //enable
shout(3,0x05); //101
shout(NADDR,(byte)addr);
shout(8,dat);
ROM_CS=0; //disable
D_O = 1;
SK = 1;
return status();
}
byte write_eeprom(word addr,byte dat)
{
byte i,ch;
for(i = 0;i<10;i++)
{
ewen();
erase(addr);
ewen();
write(addr,dat);
ewds();
ch = read_eeprom(addr);
if(ch == dat)
{
return 0;
}
}
return 1;
}
//////////////////////////////////////////
void delay1(unsigned int num)
{while(num!=0){num=num-1;}
}
void t0(void)interrupt 0
{
led=~led;
}
main()
{
unsigned int aa;
int ii;
EA=1; //开启0号中断
EX0=1;
IT0=1;
for(ii = 18;ii;ii--)
{
aa = read_eeprom(ii);
}
}
这是抄的网上的一段代码,我用的是11.0592的晶振,93C46的ORG脚悬空,现在问题是程序老是不能正确读出数据,请大家帮我看看。
|
|