//* ****************************************************************************
//
//    210623 4x4 Keypad.C
//
//	  PIC16F18345, LM35 (0.01V/degC)
//
//    2021/06/23
//
//    inja
//
//* ****************************************************************************

#include    <16f18345.h>
#device     ADC=10
#fuses      HS,NOMCLR
#use        delay (clock = 20000000)

#include    "210708 Key LM35.h"

//******************************************************************************
//*** Sub Functions ************************************************************
int8    keyValue ;
int8    chrData [10] ;

void hw_01 (void)
{   int8 xp ;
    int8 no_char ;
    
    printf ("$#CLR_LCD;") ;
    printf ("$2,1,char :  ;") ;
    printf ("$2,2,Chars:  ;") ;
    
    xp = 2 ;
    no_char = 0 ;
    while (1) {
        keyValue = read_key () ; delay_ms (1) ;
        
        if (keyValue != NO_KEY) {
            if (keyValue != '#') {
                chrData [no_char] = keyValue ;
                printf ("$9,1,%c / %2d;", chrData [no_char], no_char+1) ;
                no_char ++ ;
            }
            else {
                if (no_char >= 4) {
                    no_char = 0 ;
                    printf ("$9,1,       ;") ;
                    printf ("$9,2,%c%c%c%c%c;", chrData[0],chrData[1],
                        chrData[2],chrData[3],chrData[4]) ;
                }
            }
            while (read_key () != NO_KEY) ;
            delay_ms (1) ;
        }
    }
}

void hw_02 (void)
{   int8 xp ;
    int1 flgRead ;
    
    printf ("$#CLR_LCD;") ;
    printf ("$2,1,char :  ;") ;
    printf ("$2,2,Chars:  ;") ;
    
    xp = 9 ;
    flgRead = 0 ;
    
    while (1) {
        keyValue = read_key () ; delay_ms (1) ;
        if (keyValue != NO_KEY) {
            printf ("$9,1, %c/%d;", keyValue, xp-9) ;        
        
            if (keyValue == '*') flgRead = 1 ;
            else if (keyValue == '#') {
                flgRead = 0 ;
                xp = 9 ;
                printf ("$9,1,       ;") ;
                printf ("$9,2,       ;") ;
            }
            else {
                if (flgRead == 1) {
                    if ((xp-9) < 5) {
                        printf ("$%d,2,%c;", xp, keyValue) ;
                        xp ++ ;
                    }
                }
            }

            while (read_key () != NO_KEY) ;
            delay_ms (10) ;
        }
    }
}

void readKey_writeChar (void)
{   int8 xp ;

    printf ("$#CLR_LCD;") ;
    
    xp = 2 ;
    while (1) {
        keyValue = read_key () ;
        
        if (keyValue == '#') {
            printf ("$#CLR_LCD;") ;
            xp = 2 ;
        }
        else if (keyValue != NO_KEY) {
            if (xp <= 6) {
                printf ("$%d,1,%c;", xp, keyValue) ;
                xp ++ ;
                while (1) {
                    if (read_key () == NO_KEY) break ;
                }
            }
        }
        
        printf ("$2,2,2. Temp: %4.1f C ;", read_temp ()) ; 
        delay_ms (10) ;
    }
}

void readKey_readTemp (void)
{      
    printf ("$#CLR_LCD;") ;
    printf ("$2,1,1. Key : -   -   ;") ;
    while (1) {
        keyValue = read_key () ;        
        if (keyValue != NO_KEY) 
            printf ("$12,1, %c;", keyValue) ;

        printf ("$2,2,2. Temp: %4.1f C ;", read_temp ()) ; 
        delay_ms (10) ;
    }
}

//******************************************************************************
//*** Main Function ************************************************************
void main (void)
{
    set_tris_c (0x0F) ;         //* Define In/Out, O O O O I I I I, O:Out, I:In
    output_c (0x00) ;
    
    set_adc () ;
    delay_ms (1000) ;    
    
    printf ("$#BL_80;") ;
    printf ("$1,1,**  Key Test **;") ; delay_ms (1) ;
    printf ("$1,2,    2021/07/08 ;") ; delay_ms (2000) ;
    
    readKey_readTemp () ;
    //readKey_writeChar () ;
    //hw_01 () ;
    //hw_02 () ;
}
//******************************************************************************
//*** End Function *************************************************************






"Key_LM35.h" header file
//* ****************************************************************************
//
//    Key_LM35.h
//
//	  PIC16F18345, 8x8 KeyPad and LM35 (0.01V/degC)
//
//    2021/07/08
//
//    inja
//
//* ****************************************************************************


//*** Declear Pin for UART Communication ***************************************
#pin_select		U1TX = PIN_B7
#pin_select		U1RX = PIN_B5
#use			rs232 (baud=19200, parity=N, xmit=PIN_B7, rcv=PIN_B5)

#define         SW_PIN      PIN_A1
#define         RD_SW       input (SW_PIN)
#define         NO_KEY      0xFF

int8 read_key (void)
{   int8 byKey = NO_KEY ;
    
    output_high (PIN_C7) ;
        if (input (PIN_C0) == 1) byKey = 'A' ;
        if (input (PIN_C1) == 1) byKey = '3' ;
        if (input (PIN_C2) == 1) byKey = '2' ;
        if (input (PIN_C3) == 1) byKey = '1' ;
    output_low (PIN_C7) ;
    
    output_high (PIN_C6) ;
        if (input (PIN_C0) == 1) byKey = 'B' ;
        if (input (PIN_C1) == 1) byKey = '6' ;
        if (input (PIN_C2) == 1) byKey = '5' ;
        if (input (PIN_C3) == 1) byKey = '4' ;
    output_low (PIN_C6) ;
        
    output_high (PIN_C5) ;
        if (input (PIN_C0) == 1) byKey = 'C' ;
        if (input (PIN_C1) == 1) byKey = '9' ;
        if (input (PIN_C2) == 1) byKey = '8' ;
        if (input (PIN_C3) == 1) byKey = '7' ;
    output_low (PIN_C5) ;
        
    output_high (PIN_C4) ;
        if (input (PIN_C0) == 1) byKey = 'D' ;
        if (input (PIN_C1) == 1) byKey = '#' ;
        if (input (PIN_C2) == 1) byKey = '0' ;
        if (input (PIN_C3) == 1) byKey = '*' ;
    output_low (PIN_C4) ;
    
    return (byKey) ;
}

void set_adc (void)
{	setup_adc_ports (sAN2, VSS_FVR) ;
    setup_vref (VREF_ON | VREF_ADC_1v024) ;     //* set voltage refernce (1.024V))
	setup_adc (ADC_CLOCK_INTERNAL) ;
    set_adc_channel (2) ;
}

float read_temp (void)                          //* ADC 10bit, VREF_ADC_1v024
{   int16   adc_value, volt_mv ;
    int8    idx_avg ;
    int32   sum_value ;
    float   ret_temp ;
        
    sum_value = 0 ;
    for (idx_avg = 0;idx_avg <= 9;idx_avg++) {        
        sum_value = sum_value + read_adc () ;
    }
    adc_value = sum_value / 10 ;

    volt_mv = (float)adc_value * (1024. / 1023.) ;
    ret_temp = (float)volt_mv / 10 ;                      //* volt_mv = 0mV + 10.0mV/C

    return (ret_temp) ;
}