Use of EEPROM of Lanqiao Cup MCU

1. Experimental tasks

EEPROM power-down storage

2. Principle analysis

There is not much introduction here, if you need to understand the principle, you can move to https://editor.csdn.net/md/?articleId=112068955, MCU review.

3. Experimental process

1. Set up the framework (the absacc header file is added here, a simple framework, which can be skipped)

#include<stc15f2k60s2.h>
#include<intrins.h>
#include "absacc.h"
#define uchar unsigned char
#define uint unsigned int

uchar tab[]={
    
    0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,
							0X40,0X79,0X1A,0X30,0X19,0X12,0X02,0X78,0X00,0X10,0XBF,0XFF};
uchar yi,er,san,si,wu,liu,qi,ba;

void Allinit(void);
void Delayms(int ms);
void Display1(uchar yi,uchar er);
void Display2(uchar san,uchar si);
void Display3(uchar wu,uchar liu);
void Display4(uchar qi,uchar ba);

void main(void)
{
    
    
	Allinit();
	yi=21;er=21;san=21;si=21;wu=21;liu=21;qi=21;ba=21;
	while(1)
	{
    
    
		Display1(yi,er);
		Display2(san,si);
		Display3(wu,liu);
		Display4(qi,ba);
	}
}


void Display1(uchar yi,uchar er)
{
    
    
	P2=0XC0;
	P0=0X01;
	P2=0XE0;
	P0=tab[yi];
	Delayms(1);
	
	P2=0XC0;
	P0=0X02;
	P2=0XE0;
	P0=tab[er];
	Delayms(1);
}

void Display2(uchar san,uchar si)
{
    
    
	P2=0XC0;
	P0=0X04;
	P2=0XE0;
	P0=tab[san];
	Delayms(1);
	
	P2=0XC0;
	P0=0X08;
	P2=0XE0;
	P0=tab[si];
	Delayms(1);
}

void Display3(uchar wu,uchar liu)
{
    
    
	P2=0XC0;
	P0=0X10;
	P2=0XE0;
	P0=tab[wu];
	Delayms(1);
	
	P2=0XC0;
	P0=0X20;
	P2=0XE0;
	P0=tab[liu];
	Delayms(1);
}

void Display4(uchar qi,uchar ba)
{
    
    
	P2=0XC0;
	P0=0X40;
	P2=0XE0;
	P0=tab[qi];
	Delayms(1);
	
	P2=0XC0;
	P0=0X80;
	P2=0XE0;
	P0=tab[ba];
	Delayms(1);
}

void Allinit(void)
{
    
    
	P2=0XA0;//打开控制蜂鸣器的573
	P0=0X00;//关闭蜂鸣器继电器
	
	P2=0XC0;//打开控制数码管位选的573
	P0=0XFF;//选中所有数码管
	P2=0XE0;//打开控制数码管段选的573
	P0=0XFF;//关闭所有的数码管
	
	P2=0X80;//打开控制LED的573
	P0=0XFF;//关闭所有的LED
}

void Delayms(int ms)
{
    
    
	int i,j;
	for(i=0;i<ms;i++)
		for(j=845;j>0;j--);
}


2. Load the driver, the official driver code will be provided, if these driver codes are under the main function, remember to declare in advance.


//总线启动条件
void IIC_Start(void)
{
    
    
	SDA = 1;
	SCL = 1;
	somenop;
	SDA = 0;
	somenop;
	SCL = 0;	
}

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

//应答位控制
void IIC_Ack(bit ackbit)
{
    
    
	if(ackbit) 
	{
    
    	
		SDA = 0;
	}
	else 
	{
    
    
		SDA = 1;
	}
	somenop;
	SCL = 1;
	somenop;
	SCL = 0;
	SDA = 1; 
	somenop;
}

//等待应答
bit IIC_WaitAck(void)
{
    
    
	SDA = 1;
	somenop;
	SCL = 1;
	somenop;
	if(SDA)    
	{
    
       
		SCL = 0;
		IIC_Stop();
		return 0;
	}
	else  
	{
    
     
		SCL = 0;
		return 1;
	}
}

//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
    
    
	unsigned char i;
	for(i=0;i<8;i++)
	{
    
       
		if(byt&0x80) //1010 0000 &  1000 0000 
		{
    
    	
			SDA = 1;
		}
		else 
		{
    
    
			SDA = 0;
		}
		somenop;
		SCL = 1;
		byt <<= 1;
		somenop;
		SCL = 0;
	}
}

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

3. Add newly defined functions and variables (the functions here are written by myself, understandable, if you can’t understand, just memorize them)

uchar num[8];
uchar Save[8];
uchar Start;//变量,用于存储掉电次数

//总线引脚定义
sbit SDA = P2^1; //注意:这里的时钟线定义名和DS1302的时钟线变量名一样,在同时使用时注意修改。
sbit SCL = P2^0;  /* 时钟线 */

uchar EEPROM_read(uchar add);
void EEPROM_write(uchar add,uchar dat);

uchar EEPROM_read(uchar add)//读取存储空间的数据
{
    
    
	unsigned char i;
	IIC_Start();//启动
	IIC_SendByte(0XA0);//发送数据,相当于通知他准备接受地址数据
	IIC_WaitAck();//等待
	IIC_SendByte(add);//发送地址
	IIC_WaitAck();//等待
	IIC_Stop();//停止
	
	IIC_Start();//启动
	IIC_SendByte(0XA1);//发送数据,相当于通知他准备发送上一次给他那个地址的数据
	IIC_WaitAck();//等待
	temp=IIC_RecByte();
	IIC_Stop();//停止
	return temp;
}
void EEPROM_write(uchar add,uchar dat)
{
    
    

	IIC_Start();//启动
	IIC_SendByte(0XA0);//发送数据,相当于通知他准备接受地址数据
	IIC_WaitAck();//等待
	IIC_SendByte(add);//发送地址
	IIC_WaitAck();//等待
	IIC_SendByte(dat);//发送数据
	IIC_WaitAck();//等待
	IIC_Stop();//停止
}

4. To realize the function, just call and display it in the main function

void main(void)
{
    
    
	Allinit();
	Start=EEPROM_read(0xc0);Delayms(2);
	EEPROM_write(0xc0,Start+1);Delayms(2);

	yi=num[1]/10;er=num[1]%10;san=11;
	si=num[5]/10;wu=num[5]%10;liu=11;
	qi=num[7]/10;ba=num[7]%10;
	while(1)
	{
    
    
		Display1(yi,er);
		Display2(san,si);
		Display3(wu,liu);
		Display4(qi,ba);
	}
}

Note:
In this article, I learned the use of EEPROM, the pin definition should pay attention to the conflict with the clock, remember to modify it, and pay attention to the driver, if the driver is a 51 driver, you need to increase the waiting time in the driver by 8-12 times, because the speed of the 15 chip is about 12 times that of the 51 chip. If the clock displays garbled characters, and you still can’t find the reason after excluding all factors, check whether a variable name (especially SDA) in your driver has been changed incorrectly.

—————————————————————————————————————————
This article is purely original, if there is any infringement, please contact to delete, if there is any error, please criticize and correct, thank you all.

Guess you like

Origin blog.csdn.net/G_Shengn/article/details/116072319