24LC526 EEPROM Help

Hello.

I am trying to get this code I have to work that I am writing in C. I am trying to get this record and playback working. But for some when I record more than about 665 bytes, the playback no longer works. But any smaller than it works just fine.

If you need more provided please let me know.



void recordSnapshots(uint32_t currentMillis) {
	if (currentMillis - lastSnapshotTime >= SNAPSHOT_INTERVAL) {
		struct Snapshot snapshot;
		snapshot.time = currentMillis - recordingStartTime;
		snapshot.buttonStates = 0;

		// Capture button states
		if (!(PIND & (1 << Button1_Pin))) snapshot.buttonStates |= (1 << 0);
		if (!(PIND & (1 << Button2_Pin))) snapshot.buttonStates |= (1 << 1);
		if (!(PIND & (1 << Button3_Pin))) snapshot.buttonStates |= (1 << 2);
		if (!(PIND & (1 << Button4_Pin))) snapshot.buttonStates |= (1 << 3);

		// Calculate address and manage bank switching
		uint32_t address = eventCount * sizeof(struct Snapshot) + 2;

		if (address >= 0x8000) {  // If Bank 1 is full, move to Bank 2
			address = (address - 0x8000) + 0x8000;
		}

		// Check if EEPROM capacity is exceeded
		if (address + sizeof(struct Snapshot) > EEPROM_SIZE) {
			PORTB |= (1 << Recording_LED_Pin) | (1 << Playback_LED_Pin); // Indicate error
			stopRecording();
			return;
		}

		// Save the snapshot to EEPROM
		saveSnapshotToEEPROM(address, &snapshot);
		eventCount++;
		lastSnapshotTime = currentMillis;
	}
}


//Playback snapshots function.
void playBackSnapshots(void) {
	uint32_t playbackStartTime = millis();
	PORTB |= (1 << Recording_LED_Pin);

	for (uint32_t i = 0; i < eventCount; i++) {
		struct Snapshot snapshot;
		uint32_t address = i * sizeof(struct Snapshot) + 2;

		if (address >= 0x8000) {  // Adjust address for Bank 2
			address = (address - 0x8000) + 0x8000;
		}

		loadSnapshotFromEEPROM(address, &snapshot);

		while (millis() - playbackStartTime < snapshot.time) {}
		PORTB = (PORTB & ~((1 << LED1_Pin) | (1 << LED2_Pin) | (1 << LED3_Pin) | (1 << LED4_Pin))) |
		((snapshot.buttonStates & (1 << 0)) ? (1 << LED1_Pin) : 0) |
		((snapshot.buttonStates & (1 << 1)) ? (1 << LED2_Pin) : 0) |
		((snapshot.buttonStates & (1 << 2)) ? (1 << LED3_Pin) : 0) |
		((snapshot.buttonStates & (1 << 3)) ? (1 << LED4_Pin) : 0);
	}

	PORTB &= ~((1 << LED1_Pin) | (1 << LED2_Pin) | (1 << LED3_Pin) | (1 << LED4_Pin));
	PORTB &= ~(1 << Recording_LED_Pin);
}

I suspect that without seeing full code it will be very difficult for people to advise.

I'm not sure if I understand that. If address greater than or equal to 0x8000, subtract and add 0x8000?

1 Like

Please post an annotated schematic showing exactly how you have wired your project. Be sure to include all hardware components, connections, and links to the relevant technical documentation or datasheets for the components used.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.