Sometimes, you need to measure analog signal and then switch something on or off according to the result. This short example shows, how to do it. Everything you need is DDC-SE-01 module as a sensor module and DDC-RE-01 as relay module. Of course you need DK-EVAL-04 or CK-USB-04 for both of them as a master module.
This example originates from IQRF contest Example 12. It was simplified and changed for TR-52D module.
Relay module source code
// *********************************************************************
#include "includes/template-basic.h"
// *********************************************************************
#define RE1_TRIS TRISC.5 // C8/SDO pin connected to the RE1
#define RE1_IO PORTC.5
#define RE2_TRIS TRISC.2 // C2/IO2 pin connected to the RE2
#define RE2_IO PORTC.2
// *********************************************************************
void APPLICATION()
{
RE1_IO = 0;
RE1_TRIS = 0; // Pin as output
RE2_IO = 0;
RE2_TRIS = 0; // Pin as output
pulseLEDG(); // Reset indication
waitDelay(10);
while (1)
{
clrwdt();
if (RFRXpacket())
{
if (bufferRF[0] == 'S')
{
switch (bufferRF[1])
{
case '1':
RE1_IO = 1;
break;
case '2':
RE1_IO = 0;
break;
case '3':
RE2_IO = 1;
break;
case '4':
RE2_IO = 0;
break;
}
}
}
}
}
Source code is very simple. Relays are switched on or off acording to received char.
Sensor module source code
// *********************************************************************
#include "includes/template-basic.h"
// *********************************************************************
#define TRESHOLD_PTM 15 // Means 1.5V from full range 0 - 3.0 V
// *********************************************************************
void InitADC(void);
void ReadAnalogInput(void);
// *********************************************************************
uns16 ADC_result, voltage;
// *********************************************************************
void APPLICATION()
{
TRISA.5 = 1; // Potentiometer pins C5 (AN4) as input
TRISC.6 = 1;
TRISB.4 = 1;
TRISA.0 = 1;
InitADC();
pulseLEDG();
waitDelay(10);
while(1)
{
//clrwdt();
waitDelay(5); // Read AN input every 50 ms
ReadAnalogInput();
voltage = ADC_result * 30;
voltage /= 1024;
bufferRF[0] = 'S';
if(voltage > TRESHOLD_PTM)
bufferRF[1] = '1';
else
bufferRF[1] = '2';
PIN = 0;
DLEN = 2;
RFTXpacket();
}
}
// *********************************************************************
void InitADC(void) // ADC init (for more info see PIC datasheet)
{
ANSELA.5 = 1; // pin C5 (AN4) as analog input
ADCON0 = 0b00010001; // ADC setting (AN4 channel)
ADCON1 = 0b10010000; // ADC result - right justified, Fosc/8
}
//----------------------------------------------------------------------
void ReadAnalogInput(void)
{
GO = 1; // start ADC
while(GO); // wait for ADC finish
ADC_result.high8 = ADRESH & 0x03; // 10b result is stored
ADC_result.low8 = ADRESL; // in ADRESH and ADRESL
}
//----------------------------------------------------------------------
Analog input C5 (AN4) is readed every 50ms. Resolution is 0.1V. So if you want get full scale value, you have to multiply ADC result by 30.
Tested on TR-52D, OS version 3.03.
Enjoy.