' *******************************************************************
' Test example for RTC DS1302 (Real-time clock with calendar).
' Microcontroller: PIC18F46K22, OshonSoft PIC18 BASIC Compiler v5.99
' Author: COS (dogflu66@yahoo.es)
' Date: 2026/02/22, 2024/9
' *******************************************************************
' Description:
' Controls RTC DS1302 (Software serial library).
' *******************************************************************
' *******************************************************************
' 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 the PIC18F46K22 library
Include "_DS1302Lib.bas"      ' Include DS1302 RTC library
'*************************************************
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 PCB HL-K18.
	AllDigital
	TRISA = 0
	TRISB = 0
	TRISC = 0
	TRISE = 0
	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
   ' Send initial message through UART
    UART1_Write 0x0C			' ASCII control character to clear screen
    WaitMs 100 					' Wait for 100 mSeg.
    UART1_Write "Ready", CrLf	' Send "Ready" message with carriage return and line feed
	WaitMs 500 ' Wait for 500 mSeg.
	'*********************************************
	Dim _DOW[9] As String

	' Initialize the DS1302 RTC module
	Call Init_DS1302()        ' Initialize DS1302
	Call Set_DS1302_24h()     ' Set the DS1302 to 24-hour mode
	'Call Set_DS1302_12h()    ' Set the DS1302 to 12-hour mode
	'Call Set_DS1302_AM_PM(AM_ON) ' Set the AM mode
	'Call Set_DS1302_AM_PM(PM_ON) ' Set the PM mode


	' Declare variables for date and time
	Dim day, mth, year, DOW, hr, min, sec As Byte
	day = 16	' Set day to 16
	mth = 2		' Set month to 2 (September)
	year = 26	' Set year to 26
	DOW = 1		' Set day of week to 1 (Monday)
	hr = 22		' Set hour To 22
	min = 0		' Set minute to 0

	'******************************************************************************************
	'	Enable the line To configure the RTC.
	'	Disable And recompile To retain date And time.
	'
	'Call RTC_Set_DateTime(day, mth, year, dow, hr, min) ' Seet the date and time in the DS1302
	'******************************************************************************************


	' Test NVRam by writing values to it
	'Dim addr As Byte
	'For addr = 0 To 30
		'Call RTC_Write_NVRam(addr, addr) ' Write the index value to NVRam
	'Next addr

	' Infinite loop
	While True

		' Read date and time from DS1302
		Call RTC_Get_Date(day, mth, year, DOW) ' Get the current date
		Call RTC_Get_Time(hr, min, sec)        ' Get the current time

		' Set day of week
		Select Case DOW
			Case 1
				_DOW = "Monday"
			Case 2
				_DOW = "Tuesday"
			Case 3
				_DOW = "Wednesday"
			Case 4
				_DOW = "Thursday"
			Case 5
				_DOW = "Friday"
			Case 6
				_DOW = "Saturday"
			Case 7
				_DOW = "Sunday"
			Case Else
				_DOW = "Null"
		EndSelect

		' Send date and time via UART1
		UART1_Write #hr, ":", #min, ":", #sec, " - ", #day, "/", #mth, "/", #year, " - ", _DOW, CrLf

		' Read NVRam and send values via UART1
		'For addr = 0 To 30
			'UART1_Write "Addr", #addr, " Nvr: ", #RTC_Read_NVRam(addr), CrLf ' Output NVRam values
		'Next addr

		'Wait 1 second before repeating
		WaitMs 1000
	Wend
End ' End of the program