' *******************************************************************
' Test example for I2CPROM AT24C32 (I2C HARDWARE).
' Microcontroller: PIC18F46K22, OshonSoft PIC18 BASIC Compiler v6.10
' Author: COS (dogflu66@yahoo.es)
' Date: 2025/04/16
' *******************************************************************
' *******************************************************************
' PCB HL-K18 Jumper Configuration:
' General Jumpers on HL-K18: VCC, GND, PGC, PGD, OSC2, OSC1, VPP
' RS232 Interface: TX (Transmit), RX (Receive), BZ (Buzzer)
' Specific Jumpers for this setup: NONE
' Switches S2 and S4 should both be set to OFF.
' PICKIT2 Jumpers on HL-K18: Not used, external programmer.
' ********************************************************************
' Configuration of microcontroller fuses
' Setting up internal oscillator with 4x PLL to achieve 64MHz system clock,
' MCLR enabled for external reset <-----------------------------------
#define CONFIG1L = 0x00  ' Low Power Timer and memory settings
#define CONFIG1H = 0x38  ' External oscillator with PLL enabled
#define CONFIG2L = 0x1E  ' Brown-out Reset, Power-Up Timer
#define CONFIG2H = 0x3C  ' Watchdog Timer setup
#define CONFIG3L = 0x00  ' Low-voltage programming disabled
#define CONFIG3H = 0xBF  ' Port configuration options
#define CONFIG4L = 0x80  ' Stack overflow and underflow reset enabled
#define CONFIG4H = 0x00  ' Additional configuration bits
#define CONFIG5L = 0x0F  ' Code protection
#define CONFIG5H = 0xC0  ' Data EEPROM code protection
#define CONFIG6L = 0x0F  ' Write protection settings
#define CONFIG6H = 0xE0  ' Configuration protection bits
#define CONFIG7L = 0x0F  ' Boot block protection settings
#define CONFIG7H = 0x40  ' Configuration for block protection
' General configuration
#define CLOCK_FREQUENCY = 64		' System clock frequency in MHz
#define SINGLE_DECIMAL_PLACES = 1	' Number of decimal places for floating-point operations
#define STRING_MAX_LENGTH = 30		' Maximum length For String variables
'#define SIMULATION_WAITMS_VALUE = 0 'Activate only in simulation.
' *************************************************
' Include required libraries
'*************************************************
Include "_Pic18F46K22Lib.bas" ' Include library for PIC18F26K22 functions
Include "_I2CLib.bas" ' Include I2C communication library
'Include "DS1307Lib.bas" ' Include library for DS1307 RTC (commented out)
Include "_AT24C32Lib.bas" ' Include library for AT24C32 EEPROM functions
'*************************************************
main:
    ' Configure the internal clock to 64MHz
	Call _Setup_Oscillator(_OSC_16MHz_HF) ' Set the oscillator to 16MHz HF
	Call _SET_PLL4(_PLL_ON) ' Enable the PLL to multiply frequency by 4
	Call _Interrupts_Mode(_DISABLE_PRIORITY) ' Disable interrupt priority
	'*********************************************
	'Basic configuration for Amicus18 card.
	AllDigital ' Set all pins to digital mode
	TRISA = 0 ' Configure PORTA as output
	TRISB = 0 ' Configure PORTB as output
	TRISC = 0 ' Configure PORTC as output
	TRISE = 0 ' Configure PORTE as output
	UART1_Init 115200   ' Initialize UART1 at 115200 baud rate
	'UART2_Init 4800    ' Initialize UART2 at 4800 baud rate (commented out)
	WaitMs 1            ' Wait 1 millisecond for stabilization
	UART1_Write 12 		' Clear the screen (new page)
	WaitMs 100          ' Wait 100 milliseconds
	UART1_Write "Ready", CrLf ' Send "Ready" message via UART1 with newline
	'*********************************************
	TRISB.1 = 0 ' Configure PORTB pin 1 as output
	TRISB.2 = 0 ' Configure PORTB pin 2 as output
	Symbol LedRed = LATB.1 ' Assign LedRed to PORTB.1 (output control)
	Symbol ledOrange = LATB.2 ' Assign ledOrange to PORTB.2 (output control)
	LedRed = 0 ' Turn off the red LED
	ledOrange = 0 ' Turn off the orange LED
	'*********************************************
	' Initialize I2C
	Call Init_I2C(100, 64) ' Initialize I2C module in master mode at 100KHz, 64MHz system clock
	WaitMs 10              ' Wait 10 milliseconds for I2C stabilization

	' Test EEPROM by writing values to it
	Dim i As Word  ' Variable to store the memory address
	Dim x As Byte  ' Variable to store the data to be written
	x = 0          ' Initialize data to 0
	For i = 0x00 To 0x0FFF  ' Loop through EEPROM addresses from 0 to 4095
		Call EEPROM_WriteByte(i, x) ' Write the value of x to the current EEPROM address
		x++ ' Increment x after each write
	Next i

	' Infinite loop
'	While True
		' Read EEPROM and send values via UART1
		For i = 0x00 To 0x0FFF  ' Loop through EEPROM addresses from 0 to 4095
			UART1_Write "i: ", #i, " Ram: ", #EEPROM_ReadByte(i), CrLf ' Read and output EEPROM values
		Next i

		' Wait 1 second before repeating
		'WaitMs 1000
'	Wend
End ' End of the program
