' ************************************************************************
' Practice: HTU21D - temperature And humidity via I2C (MSSP).
' Program: Test For HTU21D control in No Hold Master mode
' And Hold Master mode.
' Microcontroller: PIC18F46K22, OshonSoft PIC18 BASIC Compiler v6.10
' Author: COS (dogflu66@yahoo.es)
' Date: 2026/04/03
' ************************************************************************
' ************************************************************************
' 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 = 30		' Maximum length for String variables
'#define SIMULATION_WAITMS_VALUE = 0 'Activate only in simulation.
'*************************************************
'*************************************************
Include "_Pic18F46K22Lib.bas"
Include "_I2CLib.bas"
Include "_HTU21DLib.bas"
'*************************************************
main:
    ' Configure the internal clock to 64MHz
    Call _Setup_Oscillator(_OSC_16MHz_HF) ' Set the oscillator to 16MHz HF
    Call _SET_PLL4(_PLL_ON) ' Enable the PLL to multiply frequency by 4
    Call _Interrupts_Mode(_DISABLE_PRIORITY) ' Disable interrupt priority
    '*********************************************
    'Basic configuration.
    AllDigital
    TRISA = 0
    TRISB = 0
    TRISC = 0
    TRISE = 0
    UART1_Init 115200	' Initialize UART1 at 115200 baud rate
    'UART2_Init 4800    ' Initialize UART2 at 4800 baud rate (commented out)
    WaitMs 1            ' Wait 1 millisecond
    UART1_Write 0x0C 	' Clear screen (new page)
    WaitMs 500
    UART1_Write "Ready", CrLf
    WaitMs 500
    '*********************************************
    '*********************************************
    ' Initialize I2C
    Init_I2C(100, 64) 		' Initialize MSSP1 module in I2C master mode at 100kHz
    WaitMs 10

	HTU21D_Init()

	WaitMs 200

	Dim temperature As Single
	Dim humidity As Single
	Dim _count As Word
	_count = 0

	UART_Write "Pratice HTU21D", CrLf
	UART_Write "Initializing sensor...", CrLf

	While 1

		UART_Write 0x0C ' Clear screen (new page)

		'' HTU21D Mode: Hold Master
		'UART_Write "HDTU21D mode: Hold Master:", CrLf
		'Clock_Stretch_I2C(50) 		' The slave can lock the clock for up to 50 ms.
		'temperature = HTU21D_ReadTemp(HTU21D_CMD_TEMP_HM)
		'Clock_Stretch_I2C(16)		' The slave can lock the clock for up to 16 ms.
		'humidity = HTU21D_ReadHum(HTU21D_CMD_HUM_HM)
		'Clock_Stretch_I2C(0) 		' Disable

		' HDTU21D Mode: no Hold Master
		UART_Write "HDTU21D mode: no Hold Master:", CrLf
		HTU21D_RequestMeasurement(HTU21D_CMD_TEMP)
		WaitMs 50
		temperature = HTU21D_ReadTemp(HTU21D_CMD_TEMP)
		HTU21D_RequestMeasurement(HTU21D_CMD_HUM)
 		WaitMs 16
		humidity = HTU21D_ReadHum(HTU21D_CMD_HUM)

		UART_Write "Temperature = "
		UART_Write "Dec ", #temperature
		UART_Write " C"
		UART_Write CrLf

		UART_Write "Humidity    = "
		UART_Write "Dec ", #humidity
		UART_Write " %RH"
		UART_Write CrLf
		UART_Write "Reading: ", #_count
		++_count

		WaitMs 1000

	Wend
End



