DS24B33

DS24B33
DS24B33特性4096位非易失EEPROM划分为16个256位存储页读、写操作向下兼容于DS2433256位暂存器采用严格的读/写协议操作保证数据传输完整性唯一的、由工厂编程的64位注册码保证器件选择无误确保每个器件绝对识别切换点滞回可以在嘈杂环境下获得最佳性能通过1-Wire协议以15.4kbps或125kbps与主机通信低成本插装和SMD封装工作范围2.8V至5.25V-40°C至85°CIO引脚具有IEC 1000-4-2 4级ESD保护(±8kV接触放电模式、±15kV气隙放电模式典型值)产品详情DS24B33是一款4096位1-Wire® EEPROM存储器分为16页每页256位。数据先写入32字节暂存器经验证后拷贝到EEPROM存储器。DS24B33通过1-Wire总线通信该通信符合标准1-Wire协议。每个器件都有不可更改且唯一的64位注册码该注册码由工厂编程到芯片内。在多点1-Wire网络环境下该注册码可以用作器件地址。DS24B33软件兼容于DS2433。应用电路板识别医用传感器校准数据存储智能电缆校准系数存储储存产品的修订状态电路STM32F407DS24B33串口3 printf显示#include DS24B33.h /*| 命令HEX | 名称 | 作用 | | ------- | ---------------- | ------------------------------- | | 0x33 | Read ROM | 读 64-bit 注册号FamilySerialCRC | | 0xCC | Skip ROM | 不发送 64-bit 匹配码直接操作总线上唯一器件 | | 0x55 | Match ROM | 后面跟 64-bit ROM选中指定器件 | | 0x0F | Write Scratchpad | 写暂存器必须 32 B 整页 | | 0xAA | Read Scratchpad | 读回暂存器内容TA1/TA2/ESCRC16 | | 0x55 | Copy Scratchpad | 把暂存器复制到 EEPROM真正落盘 | | 0xF0 | Read Memory | 从指定地址开始连续读 EEPROM | DS24B33 的 功能命令 和 64 位 ROM 序列号、EEPROM 存储地址 是 三套完全独立的寄存器/空间 互不占位也分开发送。 64 位 ROM 序列号——“我是谁” 工厂激光刻录只读格式 1 Byte Family Code(0x23) 6 Byte Serial 1 Byte CRC8 位于 ROM 空间与 EEPROM 物理隔离用 0x33 命令读取。 EEPROM 存储地址——“数据写在哪” 容量 512 B16 页 × 32 B 地址范围 0x0000–0x01FF 页首地址 页号 × 32page 5 只在 Write/Read Memory 或 Copy Scratchpad 命令之后由主机再发 2 字节 TA1/TB2 给出 与 ROM 序列号不在同一空间也不会互相覆盖 */ /* DS2433引脚定义 */ #define DS2433_DQ_PIN GPIO_PIN_9 #define DS2433_DQ_PORT GPIOE /* 延时函数 */ /* 引脚控制宏 */ #define DS2433_DQ_OUT_LOW() HAL_GPIO_WritePin(DS2433_DQ_PORT, DS2433_DQ_PIN, GPIO_PIN_RESET) #define DS2433_DQ_OUT_HIGH() HAL_GPIO_WritePin(DS2433_DQ_PORT, DS2433_DQ_PIN, GPIO_PIN_SET) #define DS2433_DQ_IN() HAL_GPIO_ReadPin(DS2433_DQ_PORT, DS2433_DQ_PIN) void delay_us(uint32_t us) { uint32_t ticks us * 168; // 168 周期 / μs uint32_t start DWT-CYCCNT; while ((DWT-CYCCNT - start) ticks) __NOP(); } //等待ds2433响应 /* 引脚方向控制函数/* 把 DQ 切到推挽输出模式 */ static void DS2433_IO_OUT(void) { GPIO_InitTypeDef GPIO_InitStruct {0}; GPIO_InitStruct.Pin DS2433_DQ_PIN; GPIO_InitStruct.Mode GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull GPIO_NOPULL; GPIO_InitStruct.Speed GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(DS2433_DQ_PORT, GPIO_InitStruct); } //把 DQ 切到浮空输入模式 static void DS2433_IO_IN(void) { GPIO_InitTypeDef GPIO_InitStruct {0}; GPIO_InitStruct.Pin DS2433_DQ_PIN; GPIO_InitStruct.Mode GPIO_MODE_INPUT; GPIO_InitStruct.Pull GPIO_NOPULL; HAL_GPIO_Init(DS2433_DQ_PORT, GPIO_InitStruct); } //等待DS2433的回应 //返回1:未检测到DS2433的存在 //返回0:存在 /* 采样存在脉冲芯片会在 15-60 μs 内拉低总线 60-240 μs */ uint8_t DS2433_Check(void) { uint8_t retry0; DS2433_IO_IN(); //SET PG11 INPUT while (DS2433_DQ_IN() retry200) { retry; delay_us(1); }; if(retry200)return 1; else retry0; while (!DS2433_DQ_IN() retry240) { retry; delay_us(1); }; if(retry240)return 1; return 0; } /* 产生 750 μs 复位脉冲 → 等待存在脉冲 → 返回 0芯片存在 1不存在 */ void DS2433_Rst(void) { DS2433_IO_OUT(); // 设置引脚为输出模式 DS2433_DQ_OUT_LOW(); // 拉低DQ线 delay_us(750); // 保持低电平750us DS2433_DQ_OUT_HIGH(); // 释放总线 delay_us(15); // 等待15us } uint8_t DS2433_Init(void) { __HAL_RCC_GPIOE_CLK_ENABLE(); // 初始化引脚为输出模式 DS2433_IO_OUT(); DS2433_DQ_OUT_HIGH(); // 输出高电平 // 复位DS2433 DS2433_Rst(); // 检查DS2433是否存在 return DS2433_Check(); } /* 封装一次复位存在检测返回 0成功 1失败 */ int OWTouchReset(void) { int result; DS2433_Rst();//复位 resultDS2433_Check(); return result;// 0芯片在, 1芯片不在 } // Write 1-Wire data byte void OWWriteByte(int data) { int loop; // Loop to write each bit in the byte, LS-bit first for (loop 0; loop 8; loop) { OWWriteBit(data 0x01); // shift the data byte for the next bit data 1; } } // Read a bit from the 1-Wire bus and return it. Provide 10us recovery time. // // Read 1-Wire data byte and return it // // Send a 1-Wire write bit. Provide 10us recovery time. /* 字节写LSb 先出 */ /* 写 1 个 bit1-slot 或 0-slot */ void OWWriteBit(int bit) { DS2433_IO_OUT(); //SET PG11 OUTPUT if (bit)// Write-1 { // Write 1 bit HAL_GPIO_WritePin(GPIOE, GPIO_PIN_9, GPIO_PIN_RESET); delay_us(6); // 1-15 μs HAL_GPIO_WritePin(GPIOE, GPIO_PIN_9, GPIO_PIN_SET);// Releases the bus delay_us(60); // // 剩余 60 μs Complete the time slot and 10us recovery } else// Write-0 { // Write 0 bit HAL_GPIO_WritePin(GPIOE, GPIO_PIN_9, GPIO_PIN_RESET); // GPIO_ResetBits(GPIOB,GPIO_Pin_11);// Drives DQ low delay_us(60); // 60 μs 低 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_9, GPIO_PIN_SET); // GPIO_SetBits(GPIOB, GPIO_Pin_11);// Releases the bus delay_us(10);// 10 μs 恢复 } } /* 字节读LSb 先收 */ int OWReadByte(void) { int loop, result0; for (loop 0; loop 8; loop) { // shift the result to get it ready for the next bit result 1; // if result is one, then set MS bit if (OWReadBit()) result | 0x80; } return result; } /* 读 1 个 bit先拉低 6 μs → 释放 → 9 μs 后采样 */ int OWReadBit(void) { int result; DS2433_IO_OUT(); //SET PG11 OUTPUT // GPIO_ResetBits(GPIOB,GPIO_Pin_11);// Drives DQ low HAL_GPIO_WritePin(GPIOE, GPIO_PIN_9, GPIO_PIN_RESET); delay_us(6); // GPIO_SetBits(GPIOB, GPIO_Pin_11);// Releases the bus HAL_GPIO_WritePin(GPIOE, GPIO_PIN_9, GPIO_PIN_SET);// 释放 delay_us(9); DS2433_IO_IN(); // // 切输入 result HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_9);// // 采样Sample the bit value from the slave delay_us(55); // // 凑满 70 μsComplete the time slot and 10us recovery return result; } //命令层Skip ROM 读写存储器 /* 读 512 字节整片按 8 字节一块发 0xF0 TA1/TA2 连续读 */ void read_2433_alldata(uint8_t *page_data) //所有存储空间512字节全部读出 { uint16_t page0; uint8_t i,j; for(page0;page64;page)// // 64×8512 { // select the device if (OWTouchReset()) // // 无芯片Reset the 1-Wire bus { return ; // Return if no devices found } OWWriteByte(0xCC); // Send Skip ROM command to select single device OWWriteByte(0xf0); // Read Authentication command OWWriteByte(((page 3)) 0xFF); //TA1地址低8位左移位数与每次读取长度相关 OWWriteByte(page5); // TA2 地址高八位 for (i 0; i 8; i) //每起始地址连续读8字节 page_data[page*8i] OWReadByte(); if (OWTouchReset()) // Reset the 1-Wire bus { return ; // Return if no devices found } } } //向ds2433中写入512字节数据 /* 写 512 字节整片按 8 字节一块先写 scratchpad → 读回验证 → copy */ void write_test(unsigned char *page_data,uint8_t *Oid) { int i,page,j; unsigned char TA1,TA2,E_S; for(j0;j64;j) { if(OWTouchReset()) // Reset the 1-Wire bus { return ; // Return if no devices found } OWWriteByte(0xCC); // Send Skip ROM command to select single device OWWriteByte(0x0F); // Write Scratchpad Read Authentication command OWWriteByte((j3)0xFF); //TA1 OWWriteByte((j5)0xFF); //TA2 for (i 0; i 8; i) { OWWriteByte(page_data[j*8i]); } // select the device if (OWTouchReset()) // Reset the 1-Wire bus { return; // Return if no devices found } OWWriteByte(0xCC); // Send Skip ROM command to select single device OWWriteByte(0xAA); TA1 OWReadByte(); TA2 OWReadByte(); E_S OWReadByte(); delay_us(10); for(i0;i8;i) { Oid[i]OWReadByte(); // if (Oid) Oid[i] rd; // 只有需要时才保存 } // select the device if (OWTouchReset()) // Reset the 1-Wire bus { return; // Return if no devices found } OWWriteByte(0xCC); // Send Skip ROM command to select single device OWWriteByte(0x55); // Read Authentication command OWWriteByte(TA1 0xFF ); OWWriteByte(TA2); // TA2 OWWriteByte(E_S); delay_us(15000); // 等待 t_PROG ≤ 12 ms // select the device if (OWTouchReset()) // Reset the 1-Wire bus { return; // Return if no devices found } } } void DS24B33_WritePage(uint8_t page, uint8_t *data32) { uint8_t TA1, TA2, ES; if (page 15) return; if (OWTouchReset()) return; OWWriteByte(0xCC); OWWriteByte(0x0F); // Write Scratchpad OWWriteByte(page 5); // TA1 page * 32 OWWriteByte(0x00); // TA2 0 for (int i 0; i 32; i) OWWriteByte(data32[i]); if (OWTouchReset()) return; OWWriteByte(0xCC); OWWriteByte(0xAA); TA1 OWReadByte(); TA2 OWReadByte(); ES OWReadByte(); if (OWTouchReset()) return; OWWriteByte(0xCC); OWWriteByte(0x55); // Copy Scratchpad OWWriteByte(TA1); OWWriteByte(TA2); OWWriteByte(ES); delay_us(5000); OWTouchReset(); } //read 2433 product code /* 读 ROM0x33 CRC8 检查返回 1成功 0失败 */ uint8_t read_2433(uint8_t *RomCode) { int i; if (OWTouchReset()) // 1 无芯片 return 0; // 0 表示失败 OWWriteByte(0x33); // Read ROM delay_us(60); for (i 0; i 8; i) RomCode[i] OWReadByte(); /* CRC 正确 → 成功返回 1 */ return dscrcCheck(RomCode, 8) ? 0 : 1; } ////* 单字节 CRC8x^8 x^5 x^4 1 */CRC check uint8_t crc8(uint8_t* d,uint8_t len) { uint8_t bit0,cbit,i,j,byte,temp; temp0; for(j0;jlen;j) { byted[j]; for(i0;i8;i) { cbit temp 0x01; bit0 byte 0x01; temptemp1; if( (cbit^bit0) ) temp^0x8c; byte1; } } return temp; // 0 CRC 正确 } // CRC校验函数 uint8_t dscrcCheck(uint8_t *p, uint8_t len) { uint8_t bit0, cbit, i, j, byte, temp; temp 0; for (j 0; j len; j) { byte p[j]; for (i 0; i 8; i) { cbit temp 0x01; bit0 byte 0x01; temp temp 1; if ((cbit ^ bit0)) temp ^ 0x8C; byte 1; } } return temp; }#ifndef __DS24B33_H__ #define __DS24B33_H__ #ifdef __cplusplus extern C { #endif /* Includes ------------------------------------------------------------------*/ #include main.h void DS2433_Rst(void); void OWWriteBit(int bit); void OWWriteByte(int data); void delay_us(uint32_t us); void write_test(unsigned char *page_data,uint8_t *Oid); void read_2433_alldata(uint8_t *page_data); //所有存储空间512字节全部读出 int OWReadBit(void); int OWReadByte(void); int OWTouchReset(void); uint8_t DS2433_Check(void) ; uint8_t DS2433_Init(void); uint8_t dscrcCheck(uint8_t *p, uint8_t len); uint8_t read_2433(uint8_t *RomCode); // void write_test(unsigned char *page_data,uint8_t *Oid); void DS24B33_WritePage(uint8_t page, uint8_t *data32); #ifdef __cplusplus } #endif #endifmainint main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART3_UART_Init(); DS2433_Init(); uint8_t RomCode[8]; uint8_t i; // 读取设备序列号 if (read_2433(RomCode)) { printf(success: ); for (i 0; i 8; i) { printf(%02X , RomCode[i]); } printf(\r\n); } else { printf(fail1\r\n); } while (1) { HAL_Delay(1000); } }