我对我的c51教材最后一节内容十分不明白,高手帮下忙好吗?
这部分内容是有关EEPROM的内容,不懂EEPROM_CLK要不停地切换(我水平差,最好其它方面再给我提点一下)
EEPROM_CLK = 1;
EEPROM_CLK = 0;
整段程序是这样的:
#include <at89x52.h>
#define EEPROM_CS P1_5
#define EEPROM_CLK P1_6
#define EEPROM_DI P1_7
#define EEPROM_DO P1_4
#define SEG_PORT P0
#define COMM_PORT P1
#define EEPROM_EWEN 0x9f
#define WRITTEN_DATA 0xab
#define ADDRESS 0x05
void EEPROM_Write_Enable(void)
{
//give EWEN command to 93c46, allowing write of 93c46
unsigned char i;
EEPROM_CS = 0;
EEPROM_CLK = 0;
EEPROM_CS = 1;
for(i=0; i<8; i++)
{
EEPROM_DI = (EEPROM_EWEN>>(7-i))&1;
EEPROM_CLK = 1;
EEPROM_CLK = 0;
}
EEPROM_CLK = 1;
EEPROM_CLK = 0;
EEPROM_CLK = 1;
EEPROM_CLK = 0;
EEPROM_DI = 0;
EEPROM_CS = 0;
}
void EEPROM_Write(unsigned char addr, unsigned char writtenData)
{
//write to 93c46, only the lower 8 bits of the address will be written
unsigned char i;
addr |= 0x80;
//only lower 7 bits of address is valid according to 93c46's datasheet.
//so the highest bit is used as the second bit of write instruction
EEPROM_CS = 0;
EEPROM_DI = 0;
EEPROM_CS = 1;
EEPROM_DI = 1;
EEPROM_CLK = 1;
EEPROM_CLK = 0;
EEPROM_DI = 0;
EEPROM_CLK = 1;
EEPROM_CLK = 0;
for(i=0; i<8; i++)
{
EEPROM_DI = (addr>>(7-i))&1;
EEPROM_CLK = 1;
EEPROM_CLK = 0;
}
for(i=0; i<8; i++)
{
EEPROM_DI = (writtenData>>(7-i))&1;
EEPROM_CLK = 1;
EEPROM_CLK = 0;
}
EEPROM_DI = 0;
EEPROM_CS = 0;
EEPROM_CS = 1;
while(!EEPROM_DO);
EEPROM_CS = 0;
}
unsigned char EEPROM_Read(unsigned char addr)
{
//read 93c46, only the lower 7 bits of the address will be output
unsigned char i;
unsigned char readData = 0;
EEPROM_DO = 1; //set as input
addr &= 0x7f;
EEPROM_DI = 0;
EEPROM_CLK = 0;
EEPROM_CS = 1;
EEPROM_DI = 1;
EEPROM_CLK = 1;
EEPROM_CLK = 0;
EEPROM_CLK = 1;
EEPROM_CLK = 0;
for(i=0; i<8; i++)
{
EEPROM_DI = (addr>>(7-i))&1;
EEPROM_CLK = 1;
EEPROM_CLK = 0;
}
for(i=0; i<8; i++)
{
EEPROM_CLK = 1;
if(EEPROM_DO)
{
readData |= 1<<(7-i);
}
EEPROM_CLK = 0;
}
EEPROM_DI = 0;
EEPROM_CS = 0;
return readData;
}
void initial(void)
{
EEPROM_DO = 1; //for reading
EEPROM_Write_Enable(); //enable write of 93c46
}
void main(void)
{
unsigned char read;
unsigned char i;
unsigned char code SEG_CODE[] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xa3,0xa1,0x86,0x8e}; //0~9、a~f的笔段码
unsigned char code COMM[2] = {0x01,0x02};
initial();
EEPROM_Write(ADDRESS, WRITTEN_DATA);
read = EEPROM_Read(ADDRESS);
COMM_PORT & 0xf0; //数码管的4个共阳极同时置0,
//且不改变COMM_PORT端口的其它4个引脚的电平
while(1)
{
COMM_PORT | COMM[0]; //选通(置1)最左边的数码管
//且不改变COMM_PORT端口的其它7个引脚的电平
SEG_PORT = SEG_CODE[(read&0xf0)>>4]; //显示读出值的高4位
for(i=0; i<200; i++);
COMM_PORT & ~COMM[0]; //把刚才选通的数码管取消选通(置0)
//且不改变COMM_PORT端口的其它7个引脚的电平
COMM_PORT | COMM[1]; //选通右边的数码管(第二位数码管)
//且不改变COMM_PORT端口的其它7个引脚的电平
SEG_PORT = SEG_CODE[read&0x0f]; //显示读出值的低4位
for(i=0; i<200; i++);
COMM_PORT & ~COMM[1]; //把刚才选 通的数码管取消选通
//且不改变COMM_PORT端口的其它7个引脚的电平
}
}
[解决办法]
构造读写需要的时序而已 无他
[解决办法]
百度一下