/************ CVD.c *********/
#include "CVD.h"
#include "stdio.h"
/** struct where the values for every cvd touch sensor saves its own values **/
typedef struct _CVD_SENSOR
{
unsigned int Threshold; // Un-pressed Sensor value.
unsigned char Trip; // Difference between pressed and un-pressed switch.
unsigned char Hysteresis; // Amount to change.
unsigned char PinRef; // Charging Pin to Reference.
unsigned char Tris; // SFR Tris Register.
unsigned char Port; // SFR Port Register.
unsigned char SensorState; // Actual Sensor State.
unsigned int SensorRead; // ADC value.
} CVD_TOUCH_SENSOR;
volatile CVD_TOUCH_SENSOR cvd_sensors [ NUMBER_OF_CVD_SENSORS ];
/*******************************************************************/
/* -SetUpCVDTouch ( ReferencePin )
/* -ReferencePin
/* *configures the Pin which is a reference for touch sensors.
/* should be high (VDD) to charge and pass voltage to the sensors
/* internally.
/*
/* -SetUpCVDTouchSensor ( CVDSensorNumber, Threshold
/* Trip, Hysteresis,
/* ChargingPin, Tris, Port )
/* -TouchSensorNumber
/* -Threshold
/* -Trip
/* -Hysteresis
/* -ChargingPin (reference pin)
/* -Tris
/* -Port
/* *sets the Touch Sensor Channel to be read.
/*
/* -CVDTouchSensorNumber ( CVDTouchSensorNumber )
/* -CVDTouchSensorNumber
/* *Returns the value stored in ptrSensor -> SensorState
/* if pressed = 1; else = 0.
/*
/* -DebbugCVDTouchSensor ( TouchSensorNumber, TouchSensorNumber )
/* -TouchSensorNumber
/* -TouchSensorNumber
/* *Prints out the value of any two touch sensors, int (16-bit).
/*
/******************************************************************/
/********* Configure the CVD Touch Pin Reference ********************/
void SetUpCVDTouch ( unsigned char ReferencePin )
{
switch ( ReferencePin )
{
case CVD_CH_0:
REFERENCE_TRIS_PIN_0 = AS_OUTPUT; // Chargin pin as output
REFERENCE_PIN_0 = TO_VDD; // and logic high.
break;
case CVD_CH_1:
REFERENCE_TRIS_PIN_1 = AS_OUTPUT; // Chargin pin as output
REFERENCE_PIN_1 = TO_VDD; // and logic high.
break;
case CVD_CH_2:
REFERENCE_TRIS_PIN_2 = AS_OUTPUT; // Chargin pin as output
REFERENCE_PIN_2 = TO_VDD; // and logic high.
break;
case CVD_CH_3:
REFERENCE_TRIS_PIN_3 = AS_OUTPUT; // Chargin pin as output
REFERENCE_PIN_3 = TO_VDD; // and logic high.
break;
// case CVD_CH_4:
// REFERENCE_TRIS_PIN_4 = AS_OUTPUT; // Chargin pin as output
// REFERENCE_PIN_4 = TO_VDD; // and logic high.
// break;
case CVD_CH_5:
REFERENCE_TRIS_PIN_5 = AS_OUTPUT; // Chargin pin as output
REFERENCE_PIN_5 = TO_VDD; // and logic high.
break;
default:
break;
}
}
/********* Configure the CVD Touch Sensor to use ********************/
void SetUpCVDTouchSensor ( unsigned char CVDSensorNumber, unsigned int Threshold,
unsigned char Trip, unsigned char Hysteresis,
unsigned char ChargingPin, unsigned char Tris, unsigned char Port )
{
CVD_TOUCH_SENSOR* ptrSensor;
ptrSensor = ( CVD_TOUCH_SENSOR* ) cvd_sensors + CVDSensorNumber; // Pointing to the Touch Sensor
// to be Set it.
ptrSensor -> Threshold = Threshold; // Pasing the values to
ptrSensor -> Trip = Trip; // the Sensors
ptrSensor -> Hysteresis = Hysteresis;
ptrSensor -> PinRef = ChargingPin;
ptrSensor -> Tris = Tris;
ptrSensor -> Port = Port;
}
/********* Decode the Touch Sensor Selsected; verified if it is Pressed or not ********************/
int DecodeCVDTouchSensor ( unsigned char CVDTouchSensorNumber )
{
CVD_TOUCH_SENSOR* ptrSensor;
ptrSensor = ( CVD_TOUCH_SENSOR* ) cvd_sensors + // Pointing to the Touch Sensor
CVDTouchSensorNumber; // to be Decode it.
#if defined ( _PIC18F26K80_H_ ) || defined ( _PIC18F45K22_H_ )
ADCON0 &= 0b10000011; // Cleaning up the previous channel.
#elif defined ( _PIC16F887_H_ ) || defined ( _PIC16F886_H_ )
ADCON0 &= 0b11000011; // Cleaning up the previous channel.
#elif defined ( _PIC12F683_H_ )
ADCON0 &= 0b11110011; // Cleaning up the previous channel.
#endif
ADCON0 |= ptrSensor -> PinRef << 2 ; // Setting the AD channel to read
// without changing the ADCON0 value.
__delay_us ( 20 );
switch ( CVDTouchSensorNumber )
{
case CVD_CH_0:
CVD_SENSOR_TRIS_0 = AS_OUTPUT; // Set Sensing pin as output
CVD_SENSOR_PORT_0 = TO_GND; // and logic low (discharging it).
CVD_SENSOR_TRIS_0 = AS_INPUT; // Sensing pin as input.
break;
case CVD_CH_1:
CVD_SENSOR_TRIS_1 = AS_OUTPUT; // Set Sensing pin as output
CVD_SENSOR_PORT_1 = TO_GND; // and logic low (discharging it).
CVD_SENSOR_TRIS_1 = AS_INPUT; // Sensing pin as input.
break;
case CVD_CH_2:
CVD_SENSOR_TRIS_2 = AS_OUTPUT; // Set Sensing pin as output
CVD_SENSOR_PORT_2 = TO_GND; // and logic low (discharging it).
CVD_SENSOR_TRIS_2 = AS_INPUT; // Sensing pin as input.
break;
case CVD_CH_3:
CVD_SENSOR_TRIS_3 = AS_OUTPUT; // Set Sensing pin as output
CVD_SENSOR_PORT_3 = TO_GND; // and logic low (discharging it).
CVD_SENSOR_TRIS_3 = AS_INPUT; // Sensing pin as input.
break;
// case CVD_CH_4:
// CVD_SENSOR_TRIS_4 = AS_OUTPUT; // Set Sensing pin as output
// CVD_SENSOR_PORT_4 = TO_GND; // and logic low (discharging it).
//
// CVD_SENSOR_TRIS_4 = AS_INPUT; // Sensing pin as input.
// break;
case CVD_CH_5:
CVD_SENSOR_TRIS_5 = AS_OUTPUT; // Set Sensing pin as output
CVD_SENSOR_PORT_5 = TO_GND; // and logic low (discharging it).
CVD_SENSOR_TRIS_5 = AS_INPUT; // Sensing pin as input.
break;
default:
break;
}
// ADCON0 = 0b00000101; // imediatly pointing ADC to Sense pin.
// SetADCChannel ( CH_AN2 );
#if defined ( _PIC18F26K80_H_ ) || defined ( _PIC18F45K22_H_ )
ADCON0 &= 0b10000011; // Cleaning up the previous channel.
#elif defined ( _PIC16F887_H_ ) || defined ( _PIC16F886_H_ )
ADCON0 &= 0b11000011; // Cleaning up the previous channel.
#elif defined ( _PIC12F683_H_ )
ADCON0 &= 0b11110011; // Cleaning up the previous channel.
#endif
ADCON0 |= CVDTouchSensorNumber << 2; // Setting the AD channel to read
// without changing the ADCON0 value.
ADCON0bits . GO = 0x01; // Starts the reading of the capacitive sensor.
while ( ADCON0bits . GO ); // Wait until convertion is done.
ptrSensor -> SensorRead = ( ( ( unsigned int ) ADRESH ) << 8 ) |
( ADRESL ); // Return the value from two Bytes.
if ( ptrSensor -> SensorRead < DEFAULT_CVD_THRESHOLD - DEFAULT_CVD_TRIP )
{
ptrSensor -> SensorState = PRESSED;
}
else if ( ptrSensor -> SensorRead > DEFAULT_CVD_THRESHOLD - DEFAULT_CVD_TRIP + \
DEFAULT_CVD_HYST )
{
ptrSensor -> SensorState = UNPRESSED;
}
return ptrSensor -> SensorState;
}
/********* Debug the CVD touch Sensor; debug propouse ********************/
void DebbugCVDTouchSensor ( unsigned char TouchSensorNumber0, unsigned char TouchSensorNumber1 )
{
CVD_TOUCH_SENSOR* ptrSensor0;
ptrSensor0 = ( CVD_TOUCH_SENSOR* ) cvd_sensors + TouchSensorNumber0; // Pointing to the Touch Sensor
// to be Decode it.
CVD_TOUCH_SENSOR* ptrSensor1;
ptrSensor1 = ( CVD_TOUCH_SENSOR* ) cvd_sensors + TouchSensorNumber1; // Pointing to the Touch Sensor
// to be Decode it.
printf ( "%d,%d\n", ptrSensor0
-> SensorRead
, ptrSensor1
-> SensorRead
); // Printing out the actual value // on the sensor selected.
}