' ************************************************************************
' * Test_VK16K33_MATRIX_16x8.bas
' * Test for VK16K33 + two 8x8 LED matrices (I2C).
' * Logical display size: 16x8 pixels.
' * PIC18 Oshonsoft BASIC Compiler v6.10
' * V2026/10/06
' ************************************************************************
' ************************************************************************
' ************************************************************************
' ************************************************************************
' 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 = 0xf8  ' 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 = 0xbd  ' 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 = 40		' Maximum length for String variables
'#define SIMULATION_WAITMS_VALUE = 0 'Activate only in simulation.
'*************************************************
'*************************************************
Include "_Pic18F46K22Lib.bas"
Include "CLib.bas"
Include "_I2CLib.bas"
Include "Font_01.bas"
Include "VK16K33_I2C_MATRIX_16x8.bas"
'*************************************************
Main:
	'Clock internal 64Mhz.
	Call _Setup_Oscillator(_OSC_16MHz_HF)
	Call _SET_PLL4(_PLL_ON)
	Call _Interrupts_Mode(_DISABLE_PRIORITY)
	Call _SlewRate(_Disable) 'Enabled by default.
	'*********************************************
	AllDigital
	UART1_Init 115200
	'UART2_Init 4800
	WaitMs 1
	'*********************************************
	UART1_Write 0x0c
	WaitMs 100
	UART1_Write "Ready", CrLf
	WaitMs 100

	Init_I2C(400, 64) ' Initialize MSSP1 module in I2C master mode at 400KHz
    WaitMs 10

	VK16K33_Init()
	VK16K33_Set_Brightness(0)
	VK16K33_Clear()
	VK16K33_SetRotation(VK16K33_ROTATION_0)
	VK16K33_ScrollReset()
	WaitMs 100

	Dim m As Byte
	Dim x As Byte
	Dim y As Byte
	Const Delayms = 45

	' Bitmap: smiley face
	Dim graphic(8) As Word
	graphic(0) = %0000011111100000
	graphic(1) = %0000100000010000
	graphic(2) = %0001001001001000
	graphic(3) = %0001000000001000
	graphic(4) = %0001010000101000
	graphic(5) = %0001001111001000
	graphic(6) = %0000100000010000
	graphic(7) = %0000011111100000

	m = 0
	For x = 0 To 7
		VK16K33_RAM(m) = Reverse_Byte(graphic(x).HB)
		UART_Write Get_ToBin(VK16K33_RAM(m), 8), "-"

		VK16K33_RAM(m + 1) = Reverse_Byte(graphic(x).LB)
		UART_Write Get_ToBin(VK16K33_RAM(m + 1), 8), CrLf

		m = m + 2
	Next x

	VK16K33_Update()
	Delay_ms(1000)

	For y = 0 To 100
		VK16K33_RotateLeft()
		VK16K33_Update()
		Delay_ms(Delayms)
	Next y

	For y = 0 To 100
		VK16K33_RotateRight()
		VK16K33_Update()
		Delay_ms(Delayms)
	Next y

	' Vertical line
	For x = 0 To 15
		VK16K33_LineV(x, 1)
		VK16K33_Update()
		Delay_ms(Delayms)
	Next x

	' Vertical line.
	For x = 15 To 0 Step -1
		VK16K33_LineV(x, 0)
		VK16K33_Update()
		Delay_ms(Delayms)
	Next x

	' Simple horizontal line.
	For y = 0 To 15
		VK16K33_LineH(y, 0, 1)
		VK16K33_Update()
		Delay_ms(Delayms)
	Next y

	' Simple horizontal line.
	For y = 0 To 15
		VK16K33_LineH(y, 1, 1)
		VK16K33_Update()
		Delay_ms(Delayms)
	Next y

	' Double horizontal line.
	For y = 7 To 0 Step -1
		VK16K33_LineDH(y, 0)
		VK16K33_Update()
		Delay_ms(Delayms)
	Next y

	' Write a character at the specified coordinates.
	x = 1
	VK16K33_Clear()
	VK16K33_PutC("0", x, 1)
	VK16K33_PutC("1", x=x+5, 1)
	VK16K33_PutC("2", x=x+5, 1)
	VK16K33_Update()
	Delay_ms(1000)

	' Left shift of a string.
	While True
		VK16K33_ScrollLeft("0123456789 * Hello World * ")
		VK16K33_Update()
		Delay_ms(Delayms)
	Wend
End

' Double horizontal line
Function VK16K33_LineDH(ypos As Byte, fill As Bit) As Byte
	Dim x As Byte
	For x = 0 To 15
		VK16K33_Pixel(x, ypos, fill)
	Next x
End Function

'Horizontal line
Function VK16K33_LineH(ypos As Byte, matrix As Byte, fill As Bit) As Byte
	Dim x As Byte
	If matrix > 0 Then matrix = 8
	For x = 0 + matrix To 7 + matrix
		VK16K33_Pixel(x, ypos, fill)
	Next x
End Function

'Vertical line
Function VK16K33_LineV(xpos As Byte, fill As Bit) As Byte
	Dim y As Byte
	For y = 0 To 7
		VK16K33_Pixel(xpos, y, fill)
	Next y
End Function
' Delay_ms
Function Delay_ms(delay As Word) As Word
	WaitMs delay
End Function


















