Essential knowledge of Lanqiao Cup MCU-----(11)EEPROM

Essential knowledge of Lanqiao Cup MCU-----(11)EEPROM

EEPROM

Due to the inconvenience of EPROM operation, most of the BIOS ROM chips on the later motherboards use EEPROM (Electrically Erasable Programmable ROM). The erasure of EEPROM does not need to resort to other equipment. It uses electronic signals to modify its content, and uses Byte as the minimum modification unit. It does not need to wash all the data to write, completely getting rid of the EPROM Eraser and programmer. Bondage. EEPROM still needs to use a certain programming voltage when writing data. At this time, the content can be easily rewritten with the dedicated refresh program provided by the manufacturer. Therefore, it is a dual-voltage chip. With the dual voltage characteristics of the EEPROM chip, the BIOS can have a good anti-virus function. When upgrading, turn the jumper switch to the "on" position, that is, add the corresponding programming voltage to the chip to easily upgrade; In normal use, turn the jumper switch to the "off" position to prevent CIH viruses from illegally modifying the BIOS chip. Therefore, many motherboards still use EEPROM as the BIOS chip and as a major feature of their own motherboards.
IIC.H added

void write_eeprom(unsigned char add,unsigned char val);
unsigned char read_eeprom(unsigned char add);

Added in IIC.C

// 写入
void write_eeprom(unsigned char add,unsigned char val)
{
    
    
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(val);
	IIC_WaitAck();
	IIC_Stop();
}

unsigned char read_eeprom(unsigned char add)
{
    
    
	unsigned char da;
	
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	da = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	
	return da;
}

Called in MAIN.C

void delay()		//10ms @11.0592MHz
{
    
    
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
    
    
		while (--j);
	} while (--i);
}

//数据的读写操作
// 	write_eeprom(0x00,0x00);  //EEPROM中存储的数据需要进行初始化
    reset_cnt = read_eeprom(0x00);  //从AT24C02地址0x00中读取数据
	delay();	//延时10ms
    write_eeprom(0x00,reset_cnt);  //向AT24C02地址0x00中写入数据
	delay();

Test Results:

Realization function: record the number of booting.
Insert picture description here

Paste the entire code

IIC.H

#ifndef _IIC_H
#define _IIC_H

void IIC_Start(void); 
void IIC_Stop(void);  
bit IIC_WaitAck(void);  
void IIC_SendAck(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
unsigned char IIC_RecByte(void); 

void write_eeprom(unsigned char add,unsigned char val);
unsigned char read_eeprom(unsigned char add);

#endif

IIC.C


#include "reg52.h"
#include "intrins.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */

void IIC_Delay(unsigned char i)
{
    
    
    do{
    
    _nop_();}
    while(i--);        
}
//总线启动条件
void IIC_Start(void)
{
    
    
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//总线停止条件
void IIC_Stop(void)
{
    
    
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//发送应答
void IIC_SendAck(bit ackbit)
{
    
    
    SCL = 0;
    SDA = ackbit;  					// 0:应答,1:非应答
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//等待应答
bit IIC_WaitAck(void)
{
    
    
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
    
    
    unsigned char i;

    for(i=0; i<8; i++)
    {
    
    
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
    
    
    unsigned char i, da;
    for(i=0; i<8; i++)
    {
    
       
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}




// 写入
void write_eeprom(unsigned char add,unsigned char val)
{
    
    
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(val);
	IIC_WaitAck();
	IIC_Stop();
}

unsigned char read_eeprom(unsigned char add)
{
    
    
	unsigned char da;
	
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	da = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	
	return da;
}

MAIN.C

#include <stc15f2k60s2.h>
#include "iic.h"

#define uchar unsigned char
#define uint unsigned char
	
uchar tab[] = {
    
    0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff};
uchar dspbuf[8] = {
    
    10,10,10,10,10,10,10,10};
uchar a1 = 10,b1 = 20,c1 = 0,a2,b2,c2;
//uchar s4 = 0,s5 = 0,s8 = 0,s9 = 0;
uchar s4 = 0,s5 = 0,s6 = 0,s7 = 0,s8 = 0,s9 = 0,s10 = 0;
uchar s11 = 0,s12 = 0,s13 = 0,s14 = 0,s15 = 0,s16 = 0,s17 = 0,s18 = 0,s19 = 0;


void load();
void display();
void read_key();
void delay()		//10ms @11.0592MHz
{
    
    
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
    
    
		while (--j);
	} while (--i);
}
void cls()
{
    
    
	P2 = (P2 & 0x1f) | 0x80;
	P0 = 0xff;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xa0;
	P0 = 0x00;
	P2 = 0x1f;
}

void main()
{
    
    
	cls();
	AUXR = 0xc0;
	TMOD = 0x00;
	TL0 = 0xcd;
	TH0 = 0xd4;
	TR0 = 1;
	ET0 = 1;
	EA = 1;
	c2 = read_eeprom(0x04);
	delay();
	c1 = c2 + 1;
	write_eeprom(0x04,c1);
	delay();
	dspbuf[4] = c2 / 10;
	dspbuf[5] = c2 % 10;
	while(1){
    
    }
}

void time0() interrupt 1
{
    
    
	display();
}



void display()
{
    
    
	static unsigned char dspcom = 0;
	
	
	P2 = (P2 & 0x1f) | 0xe0;
	P0 = 0xff;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xc0;
	P0 = 1 << dspcom;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xe0;
	P0 = tab[dspbuf[dspcom]];
	P2 = 0x1f;
	
	if(++dspcom == 8) dspcom = 0;
}

Guess you like

Origin blog.csdn.net/qq_43710889/article/details/110082885