' ********************************************************************************
' v20260718 - PIC18 simulation utility - by COS - dogflu66@yahoo.es
'
' Utility to simplify the creation of characters and symbols for TM1622-based
' 16-segment LCD modules.
'
' Define the required segments in "mask". The resulting hexadecimal mask and
' its 16-bit binary representation are displayed on the serial console.
'
' IMPORTANT:
' The segment-to-bit mapping depends on how the LCD glass is connected to the
' TM1622 and may therefore vary between display manufacturers or modules.
'
' The decimal points (DP) are not part of the 16-segment character map on the
' tested module. They are controlled independently through different RAM bits.
' ********************************************************************************

#define STRING_MAX_LENGTH = 40
#define SIMULATION_WAITMS_VALUE = 0

Include "CLib.bas"

AllDigital

UART1_Init 115200
'UART2_Init 4800

WaitMs 1

' ------------------------------------------------------------------------------
' Serial console initialization.
' ------------------------------------------------------------------------------

UART1_Write 0x0c
WaitMs 100

UART1_Write "Ready", CrLf
WaitMs 100

' ------------------------------------------------------------------------------
' Segment bit masks for the tested TM1622 LCD module.
'
' Each constant represents the bit assigned to one physical segment of a
' 16-segment character.
' ------------------------------------------------------------------------------

Const _G1 = 0x8000
Const _F  = 0x4000
Const _H  = 0x2000
Const _A1 = 0x1000
Const _D1 = 0x0800
Const _E  = 0x0400
Const _M  = 0x0200
Const _N  = 0x0100
Const _K  = 0x0080
Const _B  = 0x0040
Const _J  = 0x0020
Const _A2 = 0x0010
Const _D2 = 0x0008
Const _C  = 0x0004
Const _L  = 0x0002
Const _G2 = 0x0001

Dim mask As Word
Dim str[16] As String
Dim i As Byte

' ------------------------------------------------------------------------------
' Example: generate the segment mask for character "8".
'
' Required segments:
' A1, A2, B, C, D1, D2, E, F, G1 and G2
'
' Result:
'
'   "8" -> mask: 0xDC5D
'
'   Segment: G1  F  H A1 D1  E  M  N  K  B  J A2 D2  C  L G2
'   Bit:      1  1  0  1  1  1  0  0  0  1  0  1  1  1  0  1
' ------------------------------------------------------------------------------

mask = _A1 | _A2 | _B | _C | _D2 | _D1 | _E | _F | _G1 | _G2

UART_Write "8 -> ", "Mask: 0x", HexStr(mask), CrLf

' Convert the 16-bit mask to a binary string for easier visual verification.

str = Get_ToBin(mask, 16)

UART_Write "G1  F  H A1 D1  E  M  N  K  B  J A2 D2  C  L G2", CrLf

UART_Write " "

CFor (i = 0; i <= 15; ++i)
    UART_Write str(i), "  "
CNext

End

' ==============================================================================
' 16-SEGMENT LCD REFERENCE
' ==============================================================================
'
' Segment-to-bit mapping for the tested LCD glass.
'
' Physical bit order, from Bit 15 to Bit 0:
'
'   Bit:     15  14  13  12  11  10   9   8   7   6   5   4   3   2   1   0
'   Segment: G1   F   H  A1  D1   E   M   N   K   B   J  A2  D2   C   L  G2
'
' In compact form:
'
'   G1-F-H-A1-D1-E-M-N-K-B-J-A2-D2-C-L-G2
'
'
'                       A1       A2
'                  ********* *********
'                  * *      *      * *
'                  *  *     J     *  *
'                F *   H    *    K   * B
'                  *    *   *   *    *
'                  *     *  *  *     *
'                  **G1***   ***G2**
'                  *     *  *  *     *
'                  *    *   *   *    *
'                E *   N    *    L   * C
'                  *  *     M     *  *
'                  * *      *      * *
'                  ********* *********  * DP
'                       D1       D2
'
' NOTE:
' The decimal point (DP) is controlled independently and is not included in
' the 16-bit character mask shown above.
'
' Module reference:
' https://es.aliexpress.com/item/1005004325147080.html
' ===================================================================================



