' *******************************************************************
' Test example for MAX7219 with an 8-digit LED (7-segment display).
' Microcontroller: PIC18F46K22, OshonSoft PIC18 BASIC Compiler v5.91
' Author: COS (dogflu66@yahoo.es)
' Date: 2024/12/12
' *******************************************************************
' Description:
' Controls LED 7-segment displays (Spi library by software).
' *******************************************************************
' *******************************************************************
' 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
#define STRING_MAX_LENGTH = 35  	' Maximum length for string variables
'#define SIMULATION_WAITMS_VALUE = 1 'Activate only in simulation.
' *************************************************
' Include required libraries
' *************************************************
'Include "_Pic18F46K22Lib.bas"
'Include "CLib.bas"
'Include "I2CLib.bas"
'Include "I2C_Scan.bas"
'Include "SPI_Lib.bas" 'Hardware SPI
'Include "MAX7219_SPI_DisplayLib.bas" 'Hardware SPI
Include "MAX7219_DisplayLib.bas" 'Software SPI
' *************************************************
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
    ' Initialize hardware SPI
	'_SPI1_Init(_SPI_MODE2, _SPI_MASTER_SPEED1 | _SPI_ENABLE) 'CLK 4Mhz (64mHZ)
    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
	' ----------------------------------------------------------------------
    ' *********************************************
    ' Initialize the MAX7219 module
    MAX7219_Init()
    ' MAX7219_Clear() ' Clear the display (optional)
    MAX7219_Full() ' Light up all segments on all digits
    MAX7219_SetIntensity(MAX7219_MAX_BRIGHT) ' Set maximum brightness
    WaitMs 500
    MAX7219_SetIntensity(3)'MID_BRIGHT) ' Set medium brightness
    WaitMs 500
    MAX7219_SetIntensity(MAX7219_LOW_BRIGHT) ' Set low brightness
    WaitMs 2000
    ' MAX7219_Send(1, MAX7219_Digits(0)) ' Display "0" on the first digit (optional)
    MAX7219_DisplayString("-123456.7") ' Display "-123456.7" on the 7-segment display
	WaitMs 1000
    ' *********************************************
    ' Counter demonstration
    Dim counter As Single ' Declare a single-precision floating-point counter
    counter = -123456.7 ' Initialize the counter to -10250.0

    While True
        ' Display the counter value on the 7-segment display
        MAX7219_DisplayString("     " + #counter)
        counter = counter + 0.1 ' Increment the counter by 0.1
		'WaitMs 100
    Wend
End