' HL-K18 with PIC18F46K22 and TM1637Lib (Test TM1637_4&6dIGS_6kEIS.bas)
' By COS, 2025/07/19, PIC18 OSHONSOFT v5.90
' *****************************************************************************************
' 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.
'
' Description:
' Test TM1637: TM1637 modules with 7-segment digits + keypad.
' ****************************************************************************************

' 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 (PIN MCLR ENABLE)
#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 "TM1637Lib.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

	'*********************************************
	Dim counter As Long
	Dim counter1 As Single
	counter = 0
	counter1 = 0
	Dim Temperature As Short
	Temperature = -85
	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
	hours = 0
	minutes = 0
	seconds = 0
	'*********************************************
	'Pin
	TRISA.1 = 0
	TRISA.2 = 1
	TM1637_Init()
	WaitMs 500

	'Displays the string on the display

	TM1637_WriteString("23:59")
	WaitMs 2000
	'Displays 123456 on the display
	TM1637_WriteChar(TM1637_Digit(1), 0)
	TM1637_WriteChar(TM1637_Digit(2), 1)
	TM1637_WriteChar(TM1637_Digit(3), 2)
	TM1637_WriteChar(TM1637_Digit(4), 3)
	TM1637_WriteChar(TM1637_Digit(5), 4)
	TM1637_WriteChar(TM1637_Digit(6), 5)

	'Shows the preset brightness in the library.
	TM1637_Bright(TM1637_LOW_BRIGHT)
	WaitMs 500
	TM1637_Bright(TM1637_MID_BRIGHT)
	WaitMs 500
	TM1637_Bright(TM1637_MAX_BRIGHT)
	WaitMs 500
	TM1637_Bright(TM1637_MID_BRIGHT)

	'Displays the number 2456 with the colon of the second segment.
	TM1637_Write4Char(TM1637_Digit(2), TM1637_Digit(4) Or 0x80, TM1637_Digit(5), TM1637_Digit(6)) 'Colon = :
	WaitMs 500

	While True

		'Scans the keyboard
		UART_Write "ScanKey: ", #TM1637_ScanKey(), CrLf
		'TM1637_WriteNumber(TM1637_ScanKey())
		TM1637_WriteString("    " + #TM1637_ScanKey())

		''Display 4 digits: counter from 0 To 9999.
		'TM1637_Write4Char(TM1637_Digit((counter/1000)%10), TM1637_Digit((counter/100)%10), TM1637_Digit((counter/10)%10), TM1637_Digit(counter%10))
		'counter = counter++ Mod 9999 +1

		''Display 6 digits: counter from 0 To 999999.
		'TM1637_WriteNumber(counter)
		'counter = counter++ Mod 999999 +1

		''4 digit display: Temperature
		'TM1637_WriteString(" " + #Temperature + "C")
		'Temperature--
		'If Temperature < -99 Then Temperature = 99

		''6 digit display: Temperature
		'TM1637_WriteString("    " + #Temperature + "C")
		'Temperature--

		''6 digit display: counter with decimals
		'TM1637_WriteString(#counter1)
		'counter1 = counter1 + 0.001
		'If counter1 > 999.999 Then counter1 = 0

		''6 digit display: Points
		'TM1637_WriteString(" ......")

		''6 digit display: Clock
		'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

		'TM1637_WriteString(_hours + "." + _minutes + "." + _seconds)

		'seconds++
		'If seconds = 60 Then
			'seconds = 0
			'minutes++
			'If minutes = 60 Then
				'minutes = 0
				'hours++
				'If hours = 24 Then hours = 0
			'Endif
		'Endif

        'WaitMs 100
       'Toggle PORTA.1
       ' Toggle PORTA.2

     Wend
End