【单片机】DS2431,STM32,EEPROM读取与写入

芯片介绍: https://qq742971636.blog.csdn.net/article/details/132164189

接线

串口结果:

部分代码:

go 复制代码
#include "sys.h"
#include "DS2431.h"

unsigned char	serialNb[8];
unsigned char	write_data[128];
unsigned char	read_data[128];
unsigned char	status;

unsigned char	position[5] = { 12, 41, 65, 89, 12 };
unsigned char	position_data[5];

int main( void )
{
	int idx = 0;
	/* 初始化write_data 装ascii */
	for ( idx = 0; idx < 128; idx++ )
	{
		write_data[idx] = idx;
	}

	Stm32_Clock_Init( 9 );  /* 系统时钟设置 */
	uart_init( 72, 9600 );  /* 串口初始化为9600 */
	delay_init( 72 );       /* 延时初始化 */

	status = DS2431_Init();
	printf( "DS2431_Init status: %d\r\n", status );

	if ( DS2431_ReadRom( serialNb ) == 0 )
	{
		printf( "DS2431 found with serial number: %02X %02X %02X %02X %02X %02X %02X %02X\r\n", serialNb[0], serialNb[1],
			serialNb[2], serialNb[3], serialNb[4], serialNb[5], serialNb[6], serialNb[7] );
	} else {
		printf( "DS2431_ReadRom failed,请检查硬件连线 \r\n" );
		while ( 1 )
			;
	}

	/* Check serial number CRC */
	if ( crc8( serialNb, 7 ) != serialNb[7] )
	{
		printf( "A DS2431 was found but the serial number CRC is invalid.\r\n" );
		while ( 1 )
			;
	} else {
		printf( "CRC8 校验通过,器件处于正常工作中。 \r\n" );
	}


	/* 随机挑选几个位置写入数据0x55 */
	for ( idx = 0; idx < 5; idx++ )
	{
		status = DS2431_WriteByte( position[idx], 0x55 );
		if ( status == 0 )
		{
			printf( "ds2431的第%d个位置写入成功,写入数据为0x55。 \r\n", position[idx] );
		} else {
			printf( "ds2431的第%d个位置写入失败,错误码为%d。 \r\n", position[idx], status );
		}
	}
	/* 读取这几个位置的数据 */
	for ( idx = 0; idx < 5; idx++ )
	{
		status = DS2431_ReadByte( position[idx], position_data + idx );
		if ( status == 0 )
		{
			printf( "ds2431的第%d个位置读取成功,读取到的数据为0x%02X。 \r\n", position[idx], position_data[idx] );
		} else {
			printf( "ds2431的第%d个位置读取失败,错误码为%d。 \r\n", position[idx], status );
		}
	}

	delay_ms( 100 );
	printf( "************************************************ \r\n" );
	printf( "************************************************ \r\n" );
	printf( "读取128个字节中。。。 \r\n" );
	status = DS2431_ReadMemory( 0, 128, read_data ); /* 从地址0开始读取,读取128个字节 */
	/* 打印出读取到的数据 */
	if ( status == 0 )
	{
		printf( "读取到的数据为: \r\n" );
		for ( idx = 0; idx < 128; idx++ )
		{
			printf( "%d ", read_data[idx] );
		}
		printf( "\r\n" );
	} else {
		printf( "读取失败,错误码为%d。 \r\n", status );
	}
	/* 清空read_data */
	for ( idx = 0; idx < 128; idx++ )
	{
		read_data[idx] = 0;
	}


	delay_ms( 100 );
	printf( "************************************************ \r\n" );
	printf( "************************************************ \r\n" );
	printf( "写入128个字节中。。。 \r\n" );
	for ( idx = 0; idx < 16; idx++ )
	{
		status = DS2431_WriteMemory( idx, write_data + idx * 8 );
		if ( status == 0 )
		{
			printf( "ds2431的第%d个块写入成功,每个块有8个字节。 \r\n", idx );
		} else {
			printf( "ds2431的第%d个块写入失败,错误码为%d。 \r\n", idx, status );
		}
	}


	delay_ms( 100 );
	printf( "************************************************ \r\n" );
	printf( "************************************************ \r\n" );
	printf( "读取128个字节中。。。 \r\n" );
	status = DS2431_ReadMemory( 0, 128, read_data ); /* 从地址0开始读取,读取128个字节 */
	/* 打印出读取到的数据 */
	if ( status == 0 )
	{
		printf( "读取到的数据为: \r\n" );
		for ( idx = 0; idx < 128; idx++ )
		{
			printf( "%d ", read_data[idx] );
		}
		printf( "\r\n" );
	} else {
		printf( "读取失败,错误码为%d。 \r\n", status );
	}
	/* 清空read_data */
	for ( idx = 0; idx < 128; idx++ )
	{
		read_data[idx] = 0;
	}

    printf( "************************************************ \r\n" );
    printf( "************************************************ \r\n" );
    printf( "程序已经全部演示完毕,如果需要重新演示,请复位。 \r\n" );
	while ( 1 )
	{
	}
}

代码:https://github.com/xddun/blog_code_search

相关推荐
学嵌入式的小杨同学25 分钟前
STM32 进阶封神之路(二十四):低功耗实战全攻略 —— 电池供电传感器节点(RTC 唤醒 + DHT11 采集 + 功耗优化)
c++·stm32·单片机·嵌入式硬件·mcu·架构·硬件架构
电子工程师成长日记-C5131 分钟前
51单片机热敏电阻测温
单片机·嵌入式硬件·51单片机
熬夜有啥好33 分钟前
51 单片机基础架构与最小系统详解
单片机·嵌入式硬件·51单片机
ACP广源盛1392462567339 分钟前
ASW3810@ACP#4 路差分 2:1/1:2 双向多路复用 / 解复用器 产品规格与应用总结
大数据·单片机·嵌入式硬件·计算机外设·电脑
学嵌入式的小杨同学1 小时前
STM32 进阶封神之路(二十三):低功耗深度解析 —— 从睡眠模式到停机模式(底层原理 + 寄存器配置)
c++·stm32·单片机·嵌入式硬件·mcu·架构·硬件架构
我在人间贩卖青春3 小时前
SysTick 定时器
单片机·嵌入式硬件·滴答定时器·systick
llilian_163 小时前
IRIG-B码产生器立足用户痛点,提供精准授时解决方案
大数据·数据库·功能测试·单片机·嵌入式硬件·测试工具
busideyang10 小时前
为什么推挽输出不能接收串口数据,而准双向口可以?
c语言·stm32·单片机·嵌入式硬件·嵌入式
济61711 小时前
STM32定时器进阶:从模式控制器完全指南,一文学会TRGI/TRGO---STM32 HAL库专栏
stm32·单片机·嵌入式·stm32hal库编程
鲨辣椒1008611 小时前
单片机在线演绎《当幸福来敲门》------ 中断机制
单片机·嵌入式硬件