' HL-K18 with PIC18F46K22 AND Lcd1602_I2CLib (PCF8574)
' By COS, 2025/04/19, 2025/01, PIC18 OSHONSOFT v5.73
' *************************************************************************************
' 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: VPP, VCCP, GND, DATA, CLK, LOW
'
' Description:
' A simple example to control the LCD2004/PCF8574 on the HL-K18 development board.
' *************************************************************************************

' 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 "_Pic18F46K22Lib.bas"
Include "I2CLib.bas"
Include "Lcd1602_I2CLib.bas"
' -------------------------------------------------------------------------
' Main program
Main:
	'Clock internal 16Mhz x 4PLL.
	OSCCON = %01110000 	'Call _Setup_Oscillator(_OSC_16MHz_HF)
	OSCTUNE.PLLEN = 1 	'Call _SET_PLL4(_PLL_ON)
	RCON.IPEN = 0		'Call _Interrupts_Mode(_DISABLE_PRIORITY)
	'Call _SlewRate(_Enable) 'Enable by default.
	'**********************************************************************
    AllDigital  ' Set all pins to digital mode

    ' Set all ports as input
    TRISA = 0xFF  ' Set all PORTA pins as input
    TRISB = 0xFF  ' Set all PORTB pins as input
    TRISC = 0xFF  ' Set all PORTC pins as input
    TRISD = 0xFF  ' Set all PORTD pins as input
    TRISE = 0xFF  ' Set all PORTE pins as input

    ' ---------------------------------------------------------------------
    ' Initialize UART at 115200 baud rate (using RC6 for TX and RC7 for RX)
    UART1_Init 115200
    WaitMs 1  ' Delay for stabilization
    ' ---------------------------------------------------------------------
    ' Send initial message through UART
    UART1_Write 0x0C  ' ASCII control character to clear screen
    WaitMs 100  ' Wait mSec.
    UART1_Write "Ready", CrLf  ' Send "Ready" message with carriage return and line feed
    WaitMs 500 ' Wait mSec.

	'Initialize I2C 400kHZ (64Mhz)
	Init_I2C(400, 64)
	WaitMs 5
    ' Initialize the LCD2004 without a cursor
    LCD_Init()
    WaitMs 50
    LCD_Clear()
    WaitMs 50
    ' Display a welcome message on the LCD
    LCD_Out(1, 1, "HELLO WORLD...")
    WaitMs 3000
    'LCD_BackLight(False)
    WaitMs 1000
    LCD_BackLight(False)
    WaitMs 1000
    LCD_BackLight(True)
    LCD_Cursor(True)
    LCD_CursorBlink(True)
    WaitMs 3000
	LCD_CursorBlink(False)
    LCD_Cursor(False)
	LCD_Line1Clear() 'Line 1 clear

	WaitMs 500
    LCD_Out(1, 4, "HELLO WORLD...")
    WaitMs 500
	LCD_Out(4, 1, "0123456789-ABCDEFGHI")
	WaitMs 500

    ' Variable declaration
    Dim Temp As Single  ' Define Counter as a single-precision variable
    Temp = -22.5  ' Initialize Counter with a negative value
	While True
		LCD_OutBigNum(2, 5, #Temp + "ºC   ")
		Temp = Temp + 0.1
		WaitMs 300
	Wend
End