192
#include <xc.h> // Configuration bits: Adjust these according to your specific needs #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled) #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled) #pragma config LVP = OFF // Low-Voltage Programming Enable (RB3 is digital I/O) #pragma config CPD = OFF // Data EEPROM Memory Code Protection (off) #pragma config WRT = OFF // Flash Program Memory Write Enable (off) #pragma config CP = OFF // Flash Program Memory Code Protection (off) #define _XTAL_FREQ 4000000 // Assume 4MHz crystal frequency // Function prototypes void EEPROM_Write(unsigned char address, unsigned char data); unsigned char EEPROM_Read(unsigned char address); void main() { // Set up the ports ADCON1 = 0x06; // Configure all pins as digital I/O TRISB = 0x00; // Set PORTB as output (for red LEDs) TRISD = 0x00; // Set PORTD as output (for green LEDs) TRISA = 0xFF; // Set PORTA as input (for button) PORTB = 0; // Initialize PORTB PORTD = 0; // Initialize PORTD // Read initial value from EEPROM and display on PORTD PORTD = EEPROM_Read(5); // Read from EEPROM at address 5 while(1) { PORTB++; // Increment PORTB value (light up red LEDs) __delay_ms(100); // 100ms delay // Check if MEMO button (RA2) is pressed if (PORTAbits.RA2) { EEPROM_Write(5, PORTB); // Write current PORTB value to EEPROM PORTD = EEPROM_Read(5); // Read the value back and display on PORTD // Wait for button release to avoid multiple writes while(PORTAbits.RA2); // Wait for button release __delay_ms(20); // Simple debounce } } }