/************ CTMU.c *********/
#include "CTMU.h"
#include "stdio.h"
/** struct where the values for every touch sensor saves its own values **/
typedef struct _TOUCH_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 SensorState; // Actual Sensor State.
unsigned int SensorRead; // ADC value.
} TOUCH_SENSOR;
volatile TOUCH_SENSOR sensors [ NUMBER_OF_TOUCH_SENSORS ];
/*******************************************************************/
/* -SetUpCTMU ( Edges Configurtion, Current Source,
Edge 2 Polarity, Edge 1 Polarity,
Current Source Range )
/* -Edges Configurtion
/* -Current Source
/* -Edge 2 Polarity
/* -Edge 1 Polarity
/* -Current Source Range
/* *configures the CTMU Port.
/*
/* -SetUpTouchSensor ( TouchSensorNumber, Threshold
/* Trip, Hysteresis,
/* ChargeDelay, DischargeDelay )
/* -TouchSensorNumber
/* -Threshold
/* -Trip
/* -Hysteresis
/* -ChargeDelay
/* -DischargeDelay
/* *sets the Touch Sensor Channel to be read.
/*
/* -DecodeTouchSensor ( TouchSensorNumber )
/* -TouchSensorNumber
/* *Returns the value stored in ptrSensor -> SensorState
/* if pressed = 1; else = 0.
/*
/* -DebbugTouchSensor ( TouchSensorNumber, TouchSensorNumber )
/* -TouchSensorNumber
/* -TouchSensorNumber
/* *Prints out the value of any two touch sensors
/*
/* -SliderTouchSensor ( TouchSensorNumber0, TouchSensorNumber1)
/* -TouchSensorNumber0
/* -TouchSensorNumber1
/* *Returns the unsigned int value ( 16bit) of the Slider Sensor.
/* *Require two sensors.
/******************************************************************/
/********* Configures the CTMU Module Register ********************/
void SetUpCTMU ( unsigned char CTMUConfig0, unsigned char CTMUConfig1,
unsigned char CTMUConfig2, unsigned char CTMUConfig3,
unsigned char CTMUConfig4 )
{
CTMUCONH = 0x00; // Set to POR/BOR values.
CTMUCONL = 0x00;
CTMUICON = 0x00;
CTMUCONHbits . EDGEN = CTMUConfig0; // Edges are blocked.
CTMUCONHbits . IDISSEN = CTMUConfig1; // Analog current source output is not grounded.
CTMUCONLbits . EDG2POL = CTMUConfig2; // Edge 2 is programmed for a positive edge response.
CTMUCONLbits . EDG1POL = CTMUConfig3; // Edge 1 is programmed for a positive edge response.
CTMUICONbits . IRNG = CTMUConfig4; // Current Source Range Select.
CTMUCONHbits . CTMUEN = ENABLE; //Enable the CTMU.
}
/********* Configure the Touch Sensor to use ********************/
void SetUpTouchSensor ( unsigned char TouchSensorNumber, unsigned int Threshold,
unsigned char Trip, unsigned char Hysteresis,
unsigned char ChargeDelay, unsigned char DischargeDelay )
{
TOUCH_SENSOR* ptrSensor;
ptrSensor = ( TOUCH_SENSOR* ) sensors + TouchSensorNumber; // Pointing to the Touch Sensor
// to be Set it.
ptrSensor -> Threshold = Threshold; // Pasing the values to
ptrSensor -> Trip = Trip; // the Sensors
ptrSensor -> Hysteresis = Hysteresis;
}
/********* Decode the Touch Sensor Selsected; verified if it is Pressed or not ********************/
int DecodeTouchSensor ( unsigned char TouchSensorNumber )
{
TOUCH_SENSOR* ptrSensor;
ptrSensor = ( TOUCH_SENSOR* ) sensors + TouchSensorNumber; // Pointing to the Touch Sensor
// to be Decode it.
CTMUCONL = CTMUCONL & 0xFC; // 0xFC = 0b11111100.
ADCON0 &= 0b10000011; // Cleaning up the previous channel.
ADCON0 |= TouchSensorNumber << 0x02; // Setting the AD channel to read
// without changing the ADCON0 value.
CTMUCONHbits . IDISSEN = START; // Drain charge on the circuit.
__delay_us ( DEFAULT_DISCHARGE_TIME );
CTMUCONHbits . IDISSEN = STOP; // End drain of circuit.
CTMUCONLbits . EDG1STAT = START; // Begin charging the circuit
// using CTMU current source.
__delay_us ( DEFAULT_CHARGE_TIME );
CTMUCONLbits . EDG1STAT = STOP; // Stop charging circuit.
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.
CTMUCONHbits . IDISSEN = START; // Drain charge on the circuit.
if ( ptrSensor -> SensorRead < DEFAULT_THRESHOLD - DEFAULT_TRIP )
{
ptrSensor -> SensorState = PRESSED;
}
else if ( ptrSensor -> SensorRead > DEFAULT_THRESHOLD - DEFAULT_TRIP + \
DEFAULT_HYST )
{
ptrSensor -> SensorState = UNPRESSED;
}
return ptrSensor -> SensorState;
}
/********* Debug the touch Sensor; debug propouse ********************/
void DebbugTouchSensor ( unsigned char TouchSensorNumber0, unsigned char TouchSensorNumber1 )
{
TOUCH_SENSOR* ptrSensor0;
ptrSensor0 = ( TOUCH_SENSOR* ) sensors + TouchSensorNumber0; // Pointing to the Touch Sensor
// to be Decode it.
TOUCH_SENSOR* ptrSensor1;
ptrSensor1 = ( TOUCH_SENSOR* ) sensors + TouchSensorNumber1; // Pointing to the Touch Sensor
// to be Decode it.
printf ( " \n%d,%d", ptrSensor0
-> SensorRead
, ptrSensor1
-> SensorRead
); // Printing out the actual value // on the sensor selected.
}
/********* Return the Slider Value Selected ********************/
unsigned int CurrentSliderVal;
int SliderTouchSensor ( unsigned char SlideSensorNumber0, unsigned char SlideSensorNumber1 )
{
unsigned int SliderTouchSensorValue, SliderTempVal;
unsigned char Sensor0, Sensor1;
Sensor0 = DecodeTouchSensor ( SlideSensorNumber0 );
Sensor1 = DecodeTouchSensor ( SlideSensorNumber1 );
TOUCH_SENSOR* ptrSlideSensor0;
ptrSlideSensor0 = ( TOUCH_SENSOR* ) sensors + SlideSensorNumber0; // Pointing to the Touch Sensor
// to be Read.
TOUCH_SENSOR* ptrSlideSensor1;
ptrSlideSensor1 = ( TOUCH_SENSOR* ) sensors + SlideSensorNumber1; // Pointing to the Touch Sensor
// to be Read.
SliderTouchSensorValue = CurrentSliderVal;
if ( ( Sensor0 ) || ( Sensor1 ) )
{
SliderTempVal = ( ptrSlideSensor0 -> SensorRead * 3 ) - ( ptrSlideSensor1 -> SensorRead );
SliderTouchSensorValue = SliderTempVal / FACTOR_10BIT_RESOLUTION
- DIFFERENTIAL;
CurrentSliderVal = SliderTempVal / FACTOR_10BIT_RESOLUTION
- DIFFERENTIAL;
}
return ( SliderTouchSensorValue );
}