// *********************************************************************
// *                           DDC-SE-01-ptm                           *
// *                           Demo example                            *
// *********************************************************************
// Using of Analog-to-Digital Converter with potentiometer connected.
//
// Intended for:
//   HW: - TR-72G + DK-EVAL-04x development kit with DDC-SE-01 connected
//
//   OS: - v4.06G
//
// Description:
//   AD conversion is called in 1s period. RF packet is sent on every
//   change of result (out of tolerance). It can be received by example 
//   E03-TR.c and displayed in terminal of IQRF IDE.
//   For more info about ADC setting see PIC datasheet
//
// ---------------------------------------------------------------------
// Online IQRF OS Reference Guide: https://doc.iqrf.org/IQRF-OS-Reference-guide
//
// Copyright (c) IQRF Tech s.r.o.
//
// File:    DDC-SE-01-ptm.c
// Version: v3.00                                   Revision: 16/06/2021
//
// Revision history:
//   v3.00: 16/06/2021  First release for TR-7xG.
//
// *********************************************************************
// Default IQRF include (modify the path according to your setup)
#include "IQRF.h"

// *********************************************************************
#define TOLERANCE       2

// *********************************************************************
void InitADC(void);
void ReadAnalogInput(void);

// *********************************************************************
uns16 ADC_result, H_limit, L_limit, voltage, tmp;

// *********************************************************************

void APPLICATION()                  
{
    TRISA.5 = 1;                            // potentiometer pins C5 (ANA5) as input 
    TRISC.6 = 1;
    TRISB.4 = 1;
    InitADC();
    pulseLEDG();                            // reset indication
    waitDelay(10);
    H_limit = 0x0FFF;
    L_limit = 0x0FFF;
    
    while(1)
    {   
        waitDelay(100);                     // reading of AN input is every 1s
        ReadAnalogInput();
                                            
        if ((ADC_result > H_limit) || (ADC_result < L_limit))
        {                                   // AD result has changed more then +- tolerance
                                            //   from last measurement
                                            
            if (ADC_result < TOLERANCE)     // new limits setting
            {
                H_limit = TOLERANCE;
                L_limit = 0;    
            }
            else
            {                               
                H_limit = ADC_result + TOLERANCE;
                L_limit = ADC_result - TOLERANCE;
            }
                                            // resolution is tenth volts because
            voltage = ADC_result * 30;      //   FREE complirer supports max. 16bit variables
            voltage /= 1024;
            tmp = voltage / 10;
            
            bufferRF[0] = tmp;
            bufferRF[0] += '0'; 
            bufferRF[1]  = '.';             
            bufferRF[2] = voltage % 10;
            bufferRF[2] += '0';
            bufferRF[3] = 'V';  

            pulseLEDR();
            PIN  = 0;
            DLEN = 4;
            RFTXpacket(); 
        }           
    }   
}
// *********************************************************************
// Note:
// If a packet received the A/D converter control registers are changed so InitADC
// must be called again (see RFRXpacket function side effects in IQRF Reference Guide).
// This is not case of this example because it does not call RFRXpacket function.

void InitADC(void)          // ADC init (for more info see PIC datasheet)
{
    ANSELA.5 = 1;                           // pin C5 (ANA5) as analog input 
    setADPCH(0b00000101);                   // ADC setting (ANA5 channel)
                                            //   This register (ADPCH) can not be written directly due to OS restriction
    ADCLK = 0b00000111;                     // Fosc/16
    ADCON0 = 0b10000100;                    // ADC result - right justified
}
//----------------------------------------------------------------------

void ReadAnalogInput(void)
{
    ADGO = 1;                               // start ADC
    while(ADGO);                            // wait for ADC finish
    
    ADC_result.high8 = ADRESH & 0x03;       // 10b result is stored
    ADC_result.low8  = ADRESL;              //   in ADRESH and ADRESL
}
//----------------------------------------------------------------------
