'*****************************************************************************************
' Test program for LED&KEY-TM1638 with TM1638Lib.bas
' HL-K18 with PIC18F46K22 and TM1638LIB.bas
' By COS, dogflu66@yahoo.es, 2024/12/01, PIC18 OSHONSOFT v5.91
'*****************************************************************************************
' 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.
'
' Description:
' TEST PCB LED&KEY-TM1638
' ****************************************************************************************

' 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 = 35  	' Maximum length for string variables
'#define SIMULATION_WAITMS_VALUE = 1 'Activate only in simulation.
' -------------------------------------------------------------------------
'Include "_Pic18F46K22Lib.bas"
'Include "CLib.bas"
'Include "I2CLib.bas"
'Include "I2C_Scan.bas"
Include "TM1638Lib.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 hardware UART at 115200 baud rate (using RC6 for TX and RC7 for RX)
    UART_Init 115200
    WaitMs 1  ' Delay for stabilization
    ' ---------------------------------------------------------------------
    ' Send initial message through UART
    UART_Write 0x0C  ' ASCII control character to clear screen
    WaitMs 100  ' Wait
    UART_Write "Ready", CrLf  ' Send "Ready" message with carriage return and line feed
	WaitMs 500 ' Wait
	'*********************************************
	'Assigning aliases to keys.
	Dim keys As Byte
	Symbol SW1 = keys.0
	Symbol SW2 = keys.1
	Symbol SW3 = keys.2
	Symbol SW4 = keys.3
	Symbol SW5 = keys.4
	Symbol SW6 = keys.5
	Symbol SW7 = keys.6
	Symbol SW8 = keys.7
	'Assigning name to LEDs
	Const LED1 = 0
	Const LED2 = 1
	Const LED3 = 2
	Const LED4 = 3
	Const LED5 = 4
	Const LED6 = 5
	Const LED7 = 6
	Const LED8 = 7
	'Assigning name to digits
	Const DIG1 = 0
	Const DIG2 = 1
	Const DIG3 = 2
	Const DIG4 = 3
	Const DIG5 = 4
	Const DIG6 = 5
	Const DIG7 = 6
	Const DIG8 = 7

	Dim counter As Long
	Dim Temperature As Single
	Dim _hours[2] As String
	Dim _minutes[2] As String
	Dim _seconds[2] As String
	Dim hours As Byte
	Dim minutes As Byte
	Dim seconds As Byte
	counter = 0
	keys = 0
	Temperature = -10
	hours = 24
	minutes = 59
	seconds = 59
	'**********************************************
	'User pin
	TRISC.0 = 0
	Symbol LED = LATC.0
	LED = 0

	'Initialize TM1638 (all on and at maximum brightness).
	SmgDisplayTM1638() 'LED&KEY-TM1638 card by default.
	WaitMs 2000

	'Shows the preset brightness in the library.
	BrightTM1638(MAX_BRIGHT)
	WaitMs 200
	BrightTM1638(MID_BRIGHT)
	WaitMs 200
	BrightTM1638(LOW_BRIGHT)
	WaitMs 200

	'Displays a string on the display in 24-hour format.
	If hours < 10 Then
		_hours = "0" + #hours
	Else
		_hours = #hours
	Endif
	If minutes < 10 Then
		_minutes = "0" + #minutes
	Else
		_minutes = #minutes
	Endif
	If seconds < 10 Then
		_seconds = "0" + #seconds
	Else
		_seconds = #seconds
	Endif
	DisplayStringTM1638(" " + _hours + ":" + _minutes + ":" + _seconds + " ")
	WaitMs 2000

	'Displays individual digits: displays 01234567 on the display
	writeRawDigitTM1638(DigitsTM1638(0), DIG1)
	writeRawDigitTM1638(DigitsTM1638(1), DIG2)
	writeRawDigitTM1638(DigitsTM1638(2), DIG3)
	writeRawDigitTM1638(DigitsTM1638(3), DIG4)
	writeRawDigitTM1638(DigitsTM1638(4), DIG5)
	writeRawDigitTM1638(DigitsTM1638(5), DIG6)
	writeRawDigitTM1638(DigitsTM1638(6), DIG7)
	writeRawDigitTM1638(DigitsTM1638(7), DIG8)
	WaitMs 2000

	'Turns off the LEDs on the card sequentially.
	For counter = 7 To 0 Step -1
		writeRawLedTM1638(0, counter)
		WaitMs 200
	Next counter

	counter = 0
	'Display individual digits.
	writeRawDigitTM1638(DigitsTM1638(counter), DIG1)
	writeRawDigitTM1638(DigitsTM1638(0), DIG2)
	writeRawDigitTM1638(DigitsTM1638(0), DIG3)
	writeRawDigitTM1638(DigitsTM1638(0), DIG4)
	writeRawDigitTM1638(DigitsTM1638(0), DIG5)
	writeRawDigitTM1638(DigitsTM1638(0), DIG6)
	writeRawDigitTM1638(DigitsTM1638(0), DIG7)
	writeRawDigitTM1638(DigitsTM1638(counter), DIG8)
	WaitMs 1000

	'Displays all digits formatted as integers (Byte by digit).
	While counter < 10
		displayValuesTM1638(DigitsTM1638(counter), DigitsTM1638("-"), DigitsTM1638("-"), DigitsTM1638("-"), DigitsTM1638("-"), DigitsTM1638("-"), DigitsTM1638("-"), DigitsTM1638(counter))
		counter++
		WaitMs 200
	Wend

	'Signed decimal numbers
	While Temperature < 10
		DisplayStringTM1638("       " + #Temperature)
		Temperature = Temperature + 0.1
		WaitMs 50
	Wend

	'Scans the keyboard
	While True
		Toggle LED 'User led.

		keys = ScanKeyTM1638() 'Reads keyboard and assigns it to the variable Keys, each bit of the variable represents the state of a key.
		DisplayStringTM1638(#keys) 'Sends key presses to the display in decimal.

		If (SW1) Then
			writeRawLedTM1638(1, LED1) 'LED1 a on
		Else
			writeRawLedTM1638(0, LED1) 'LED1 a off
		Endif

		If (SW2) Then
			writeRawLedTM1638(1, LED2) 'LED2 a on
		Else
			writeRawLedTM1638(0, LED2) 'LED2 a off
		Endif

		If (SW3) Then
			writeRawLedTM1638(1, LED3)
		Else
			writeRawLedTM1638(0, LED3)
		Endif

		If (SW4) Then
			writeRawLedTM1638(1, LED4)
		Else
			writeRawLedTM1638(0, LED4)
		Endif

		If (SW5) Then
			writeRawLedTM1638(1, LED5)
		Else
			writeRawLedTM1638(0, LED5)
		Endif

		If (SW6) Then
			writeRawLedTM1638(1, LED6)
		Else
			writeRawLedTM1638(0, LED6)
		Endif

		If (SW7) Then
			writeRawLedTM1638(1, LED7)
		Else
			writeRawLedTM1638(0, LED7)
		Endif

		If (SW8) Then
			writeRawLedTM1638(1, LED8)
		Else
			writeRawLedTM1638(0, LED8)
		Endif

		WaitMs 100
     Wend
End