蓝桥杯单片机必备知识-----(11)EEPROM

蓝桥杯单片机必备知识-----(11)EEPROM

EEPROM

由于EPROM操作的不便,后来出的主板上BIOS ROM芯片大部分都采用EEPROM(Electrically Erasable Programmable ROM,电可擦除可编程ROM)。EEPROM的擦除不需要借助于其它设备,它是以电子信号来修改其内容的,而且是以Byte为最小修改单位,不必将资料全部洗掉才能写入,彻底摆脱了EPROM Eraser和编程器的束缚。EEPROM在写入数据时,仍要利用一定的编程电压,此时,只需用厂商提供的专用刷新程序就可以轻而易举地改写内容,所以,它属于双电压芯片。借助于EEPROM芯片的双电压特性,可以使BIOS具有良好的防毒功能,在升级时,把跳线开关打至“on”的位置,即给芯片加上相应的编程电压,就可以方便地升级;平时使用时,则把跳线开关打至“off”的位置,防止CIH类的病毒对BIOS芯片的非法修改。所以,仍有不少主板采用EEPROM作为BIOS芯片并作为自己主板的一大特色。
IIC.H添加

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

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;
}

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();

测试结果:

实现功能:记录开机次数。
蓝桥杯单片机必备知识-----(11)EEPROM_第1张图片

整个代码粘贴

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 
#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;
}

你可能感兴趣的:(蓝桥杯单片机)