EEPROM読み書き(メモ)
code:c
GPIO_InitTypeDef GPIO_InitStruct;
void SystemClock_Config(void);
void Error_Handler(void);
void DS2431_Init(void);
void DS2431_WriteByte(uint8_t data);
uint8_t DS2431_ReadByte(void);
void DS2431_ReadROM(uint8_t *rom);
int main(void) {
HAL_Init();
SystemClock_Config();
DS2431_Init();
DS2431_ReadROM(rom);
printf("ROM: ");
for(int i = 0; i < 8; i++) {
}
printf("\n");
while (1) {
// Infinite loop
}
}
void DS2431_Init(void) {
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin */
GPIO_InitStruct.Pin = DS2431_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DS2431_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(DS2431_PORT, DS2431_PIN, GPIO_PIN_SET);
}
void DS2431_WriteByte(uint8_t data) {
for (uint8_t i = 0; i < 8; i++) {
HAL_GPIO_WritePin(DS2431_PORT, DS2431_PIN, GPIO_PIN_RESET);
HAL_Delay(2); // Adjust this delay according to your requirements
HAL_GPIO_WritePin(DS2431_PORT, DS2431_PIN, (data & 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_Delay(60); // Adjust this delay according to your requirements
HAL_GPIO_WritePin(DS2431_PORT, DS2431_PIN, GPIO_PIN_SET);
HAL_Delay(10); // Adjust this delay according to your requirements
data >>= 1;
}
}
uint8_t DS2431_ReadByte(void) {
uint8_t data = 0;
for (uint8_t i = 0; i < 8; i++) {
HAL_GPIO_WritePin(DS2431_PORT, DS2431_PIN, GPIO_PIN_RESET);
HAL_Delay(2); // Adjust this delay according to your requirements
HAL_GPIO_WritePin(DS2431_PORT, DS2431_PIN, GPIO_PIN_SET);
HAL_Delay(2); // Adjust this delay according to your requirements
if (HAL_GPIO_ReadPin(DS2431_PORT, DS2431_PIN) == GPIO_PIN_SET) {
data |= (1 << i);
}
HAL_Delay(60); // Adjust this delay according to your requirements
}
return data;
}
void DS2431_ReadROM(uint8_t *rom) {
DS2431_WriteByte(0x33); // ROM Read Command
for (uint8_t i = 0; i < 8; i++) {
romi = DS2431_ReadByte(); }
}