Blue Bridge Cup MCU | Special training case [Advanced 03] Basic read and write operations of 24C02

【1】Title requirements

Create a new project, write code in I/O mode , and implement the following functions on the CT107D MCU comprehensive training platform:

1. Correctly transplant the underlying driver code file of the IIC bus into the project.

2. After the system is powered on, read data from the three address units of 0x01 , 0x03 and 0x05 of the 24C04 memory .

3. Add 1 to the data read from unit 0x01 . If the calculation result is greater than 10, reset to 0, and then write the result back to unit 0x01.

4. Add 2 to the data read from unit 0x03 . If the calculation result is greater than 20, reset to 0, and then write the result back to unit 0x03.

5. Add 3 to the data read from unit 0x05 . If the calculation result is greater than 30, reset to 0, and then write the result back to unit 0x05.

6. Display the current data of the three units on the nixie tube in turn from left to right, and separate the numbers with " - ".

【hint】:

The basic principle of IIC interface can be seen in this blog: "[Blue Bridge Cup MCU Advanced Strengthening-01] IIC Bus Interface Technology Basics" .

The basic principle of 24C02 can be seen in this blog: "[Blue Bridge Cup MCU Advanced Enhancement-03] Basic Principle and Application of 24C02 Memory"

[2] Core source code reference

/*==================蓝桥杯单片机特训==================
【进阶03】:24C02的基本读写操作
**平  台:CT107D单片机综合实训平台
**模  式:IO模式
**底层驱动文件:2022年竞赛资源数据包提供的文件
**设  计:欧浩源(小蜜蜂老师,[email protected])
**时  间:2022-04-01
**更多详见:www.xmf393.com
====================================================*/

#include "reg52.h"
#include "iic.h"

//定义动态显示中单个数码管点亮时长
#define TSMG	500
//-------共阳数码管的段码编码表(无小数点)--------
//0 1 2 3 4 5 6 7 8 9 A B C D E F - .
unsigned char code SMG_NoDot[18]={0xc0,0xf9,
	  0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
    0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};

//定义3个计数变量
unsigned char dat1 = 0, dat2 = 0, dat3 = 0;

/*====锁存器设置和数码管显示的代码参考前面的案例====*/
//===================读取值显示函数===================
void DisplaySMG_24C02()
{
	//数码管右起第0位
	DisplaySMG_Bit(7,SMG_NoDot[dat3	% 10]);
	//数码管右起第1位
	DisplaySMG_Bit(6,SMG_NoDot[dat3 / 10]);		
	//数码管右起第2位
	DisplaySMG_Bit(5,SMG_NoDot[16]);				
	
	//数码管右起第3位
	DisplaySMG_Bit(4,SMG_NoDot[dat2	% 10]);
	//数码管右起第4位
	DisplaySMG_Bit(3,SMG_NoDot[dat2 / 10]);		
	//数码管右起第5位
	DisplaySMG_Bit(2,SMG_NoDot[16]);				
	
	//数码管右起第6位
	DisplaySMG_Bit(1,SMG_NoDot[dat1 % 10]);				
	//数码管右起第7位
	DisplaySMG_Bit(0,SMG_NoDot[dat1 / 10]);				
}
//================24C02单字节写入=================
void Write_24C02(unsigned char addr, unsigned char dat)
{
	IIC_Start();			//起始信号
	IIC_SendByte(0xa0);		//EEPROM的写设备地址
	IIC_WaitAck();		    //等待从机应答
	IIC_SendByte(addr);		//内存单元地址
	IIC_WaitAck();			//等待从机应答
	IIC_SendByte(dat);		//内存写入数据
	IIC_WaitAck();			//等待从机应答
	IIC_Stop();				//停止信号
}
//================24C02单字节读取=================
unsigned char Read_24C02(unsigned char addr)
{
	unsigned char tmp;
	//首先,进行一个伪写操作
	IIC_Start();			//起始信号
	IIC_SendByte(0xa0);		//EEPROM的写设备地址
	IIC_WaitAck();			//等待从机应答
	IIC_SendByte(addr);		//内存单元地址
	IIC_WaitAck();			//等待从机应答
	//然后,开始字节读操作
	IIC_Start();			//起始信号
	IIC_SendByte(0xa1);		//EEPROM的读设备地址
	IIC_WaitAck();			//等待从机应答
	tmp = IIC_RecByte();	//读取内存中的数据
	IIC_SendAck(1);			//产生非应答信号
	IIC_Stop();				//停止信号
	return tmp;
}
//==============24C02数据读取与写入===============
void Read_Write_24C02()
{
	dat1 = Read_24C02(0x01);	//从24C02读取dat1
	dat2 = Read_24C02(0x03);	//从24C02读取dat2
	dat3 = Read_24C02(0x05);	//从24C02读取dat3
	
	dat1 = dat1 + 1;			//第1个计数变量+1
	dat2 = dat2 + 2;			//第2个计数变量+2
	dat3 = dat3 + 3;			//第3个计数变量+3
	if(dat1 > 10)
		dat1 = 0;
	if(dat2 > 20)
		dat2 = 0;
	if(dat3 > 30)
		dat3 = 0;
	
	Write_24C02(0x01, dat1);
	DelaySMG(1000);				//等待字节写入完成
	Write_24C02(0x03, dat2);
	DelaySMG(1000);				//等待时间不够会影响dat3写入
	Write_24C02(0x05, dat3);
	DelaySMG(1000);
}

//===================系统初始化===================
void Init_sys()
{
	Set_HC573(0, 0x00);			//关闭所有锁存器
	Set_HC573(5, 0x00);			//关闭蜂鸣器和继电器
	Set_HC573(4, 0xff);			//关闭全部LED灯
	DisplaySMG_All(0xff);		//关闭全部数码管
}
//=======================主函数=======================
void main()
{
	Init_sys();					//系统初始化
	Read_Write_24C02();			//读写24C02的三个单元的数据
	while(1)
	{
		DisplaySMG_24C02();		//循环动态刷新数码管
	}
}

[Note]: For more information about the Blue Bridge Cup MCU preparation content, please refer to the "Blue Bridge Cup MCU Design and Development" Little Bee Special Training Manual , which can be downloaded from this site, and related more Blue Bridge Cup case complete source code and study preparations Competition notes, welcome to the official account of " Little Bee Notes ".

Guess you like

Origin blog.csdn.net/ohy3686/article/details/123900843