' *******************************************************************
' Test example for RTC DS1302 (Real-time clock with calendar).
' Program: master I2C and DS1307 (I2C HARDWARE).
' Microcontroller: PIC18F46K22, OshonSoft PIC18 BASIC Compiler v6.10
' Author: COS (dogflu66@yahoo.es)
' Date: 2026/03/21, 2024/10
' *******************************************************************
' *******************************************************************
' 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 "_I2CLib.bas"
Include "_DS1307Lib.bas"
'Include "_AT24C32Lib.bas"
'*************************************************
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
    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
    UART1_Write 12 ' Clear screen (new page)
    WaitMs 100
    UART1_Write "Ready", CrLf
    '*********************************************
    TRISB.1 = 0
    TRISB.2 = 0
    Symbol LedRed = LATB.1
    Symbol ledOrange = LATB.2
    LedRed = 0
    ledOrange = 0
    '*********************************************
    ' Initialize I2C
    Init_I2C(100, 64) ' Initialize MSSP1 module in I2C master mode at 100kHz
    WaitMs 10
    ' Initialize the DS1307 RTC
    Call DS1307_Init()
    WaitMs 10

    ' Declare variables for date and time
    Dim day, mth, year, dow, hr, min, sec As Byte
    day = 12    ' Set day to 7
    mth = 03   ' Set month to 10 (October)
    year = 26  ' Set year to 2024
    dow = 4    ' Set day of the week (1 = Sunday)
    hr = 21     ' Set hour to 1
    min = 17   ' Set minutes to 28
    Call DS1307_Set_24h() ' Set 24-hour mode
    'Call DS1307_Set_12h() ' Set 12-hour mode
    'Call DS1307_Set_DateTime(day, mth, year, dow, hr, min) ' Set the date and time in the DS1307
    WaitMs 10

    ' Test the RAM by writing values to it
    Dim data As Byte
    Dim adress As Byte
	data = 55
    'For adress = 0 To 55
        'Call DS1307_Write_RAM(adress, data) ' Write the index value to RAM
		'--data
    'Next adress

   ' Read the RAM and send the values via UART1
   For adress = 0 To 55
		UART1_Write "Adress: ", #adress, " RAM: ", #DS1307_Read_RAM(adress), CrLf ' Output RAM values
   Next adress

   Call DS1307_SquareWave(DS1307_4096hz)

    ' Infinite loop
    While True
        ' Read date and time from the DS1307
        Call DS1307_Get_Time(hr, min, sec)        ' Get the current time
        Call DS1307_Get_Date(day, mth, year, dow) ' Get the current date

		' Set day of week
		Dim _Dow[9] As String
		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

		Dim th[4] As String
		Select Case DS1307_Get_AM_PM_24h()
			Case 0
				th = "AM"
			Case 1
				th = "PM"
			Case -1
				th = "24h"
			Case Else
				th = "Null"
		EndSelect

        ' Send date and time via UART1
        UART1_Write #hr, ":", #min, ":", #sec, "   ", #day, "/", #mth, "/", #year, "-", _Dow, " ", th, CrLf

        ' Wait 1 second before repeating
        WaitMs 1000
    Wend
End ' End of the program
