[code]
#include <main3.h>
#include <flex_lcd.c>
#define GREEN_LED1 PIN_B8
#define COUNT 500 //@ 8MHz = 125uS.
#define DELAY for(i=0;i<COUNT;i++)
#define OPENSW 1024 //Unpressed switch value
#define TRIP 150 //Difference between pressed //and unpressed switch
#define HYST 65 //amount to change //from pressed to unpressed
#define PRESSED 1
#define UNPRESSED 0
#use rs232(baud=9600,BRGH1OK,UART1,parity=N,bits=8)
#word CTMUCON1 = 0x35A
#bit CTMUEN_bit = CTMUCON1.15
#bit IDISSEN_bit = CTMUCON1.9
#bit TGEN_bit = CTMUCON1.12
#bit EDGEN_bit = CTMUCON1.11
#bit EDGSEQEN_bit = CTMUCON1.10
#bit CTTRIG_bit = CTMUCON1.8
#bit CTMUSIDL_bit = CTMUCON1.13
#word CTMUCON2 = 0x35c
//#bit EDG2SEL_bit = CTMUCON2.2
//#bit EDG1SEL_bit = CTMUCON2.10
#bit EDG1SEL_3_bit = CTMUCON2.13
#bit EDG1SEL_2_bit = CTMUCON2.12
#bit EDG1SEL_1_bit = CTMUCON2.11
#bit EDG1SEL_0_bit = CTMUCON2.10
#bit EDG2POL_bit = CTMUCON2.6
#bit EDG2SEL_3_bit = CTMUCON2.5
#bit EDG2SEL_2_bit = CTMUCON2.4
#bit EDG2SEL_1_bit = CTMUCON2.3
#bit EDG2SEL_0_bit = CTMUCON2.2
#bit EDG1POL_bit = CTMUCON2.14
#bit EDG1STAT_bit = CTMUCON2.8
#bit EDG2STAT_bit = CTMUCON2.9
#word AD1CON1 = 0x340
#bit SAMP_bit = AD1CON1.1
#bit DONE_bit = AD1CON1.0
#bit FORM_1_bit = AD1CON1.9
#bit FORM_0_bit = AD1CON1.8
#bit ADON_bit = AD1CON1.15
#word AD1CON2 = 0x342
#word AD1CON3 = 0x344
#word AD1CSSL = 0x350
#word ADC1BUF0 = 0x300
//#word AD1PCFG = 0x32C
#word AD1CHS = 0x328
#word IFSO = 0x084
#bit AD1IF_bit = IFSO.13
//#byte CTMUICON = 0x35E
#word CTMUICON = 0x35E
#bit ITRIM_bit = CTMUICON.10
#bit IRNG_0_bit = CTMUICON.8
#bit IRNG_1_bit = CTMUICON.9
#word ANSB = 0x4E2
#bit ANSB0_bit = ANSB.0
#bit ANSB1_bit = ANSB.1
#bit ANSB2_bit = ANSB.2
#bit ANSB3_bit = ANSB.3
#bit ANSB4_bit = ANSB.4
#bit ANSB5_bit = ANSB.5
#bit ANSB9_bit = ANSB.9
#bit ANSB10_bit = ANSB.10
#bit ANSB11_bit = ANSB.11
#bit ANSB12_bit = ANSB.12
#bit ANSB13_bit = ANSB.13
#bit ANSB14_bit = ANSB.14
#bit ANSB15_bit = ANSB.15
#word TRISB = 0x2C8
#bit TRISB0_bit = TRISB.0
#bit TRISB1_bit = TRISB.1
#bit TRISB2_bit = TRISB.2
#bit TRISB3_bit = TRISB.3
#bit TRISB4_bit = TRISB.4
#bit TRISB5_bit = TRISB.5
#bit TRISB6_bit = TRISB.6
#bit TRISB7_bit = TRISB.7
#bit TRISB8_bit = TRISB.8
#bit TRISB9_bit = TRISB.9
#bit TRISB10_bit = TRISB.10
#bit TRISB11_bit = TRISB.11
#bit TRISB12_bit = TRISB.12
#bit TRISB13_bit = TRISB.13
#bit TRISB14_bit = TRISB.14
#bit TRISB15_bit = TRISB.15
#word LATB = 0x2CC
#bit LATB0_bit = LATB.0
#bit LATB1_bit = LATB.1
#bit LATB2_bit = LATB.2
#bit LATB3_bit = LATB.3
#bit LATB4_bit = LATB.4
#bit LATB5_bit = LATB.5
#bit LATB6_bit = LATB.6
#bit LATB7_bit = LATB.7
#bit LATB8_bit = LATB.8
#bit LATB9_bit = LATB.9
#bit LATB10_bit = LATB.10
#bit LATB11_bit = LATB.11
#bit LATB12_bit = LATB.12
#bit LATB13_bit = LATB.13
#bit LATB14_bit = LATB.14
#bit LATB15_bit = LATB.15
#word PORTB = 0x2CA
#bit PORTB0_bit = PORTB.0
#bit PORTB1_bit = PORTB.1
#bit PORTB2_bit = PORTB.2
#bit PORTB3_bit = PORTB.3
#bit PORTB4_bit = PORTB.4
#bit PORTB5_bit = PORTB.5
#bit PORTB6_bit = PORTB.6
#bit PORTB7_bit = PORTB.7
#bit PORTB8_bit = PORTB.8
#bit PORTB9_bit = PORTB.9
#bit PORTB10_bit = PORTB.10
#bit PORTB11_bit = PORTB.11
#bit PORTB12_bit = PORTB.12
#bit PORTB13_bit = PORTB.13
#bit PORTB14_bit = PORTB.14
#bit PORTB15_bit = PORTB.15
#word AD1CTMENL = 0x362
#bit CTMEN10_bit = AD1CTMENL.10
// variables de uso general
char c;
int inc,incG,vecesP,vecesG;
int unit=0;
int i;
// Funciones
void sw_delay(unsigned long value)
{
while(value--);
}
void InitCTMU(void)
{
TRISB14_bit=1;
//setup CTMU
//CTMU CONFIGURATION
CTMUEN_bit = 0; //make sure CTMU is disabled
CTMUSIDL_bit = 0; //CTMU continues to run in idle mode
TGEN_bit = 0; //disable edge delay generation mode of the CTMU
EDGEN_bit = 0; //edges are blocked
EDGSEQEN_bit = 0; //edge sequence not needed
IDISSEN_bit = 0; //Do not ground the current source
CTTRIG_bit = 0; //Trigger Output is disabled
EDG2POL_bit = 0;
//CTMUCONbits.EDG2SEL = 0x3; //Edge2 Src = OC1
EDG2SEL_0_bit = 1;
//EDG2SEL_1_bit = 1;
EDG1POL_bit = 0;
//CTMUCONbits.EDG1SEL = 0x3; //Edge1 Src = OC1
EDG1SEL_0_bit = 1;
//EDG1SEL_1_bit = 1;
//CTMUICON
IRNG_0_bit = 1; //0x02:-> 10 x 0.55uA. 0x03:-> 100 x 0.55uA.
IRNG_1_bit = 1;
//ADC CONFIGURATION
//AD1PCFGL = 0x0000;
ANSB = 0x0000;
AD1CON1 = 0x0000;
AD1CHS = 10; //select the analog channel 10
FORM_0_bit = 0x0; //Unsigned int format
FORM_1_bit = 0x0;
AD1CON3 = 0x0000; //ADRC = Clock derived from system clock //GC4
AD1CON2 = 0x0000;
ADON_bit = 1;
SAMP_bit = 1;
CTMUEN_bit = 1;
IDISSEN_bit = 1; // Drain any charge on the circuit
sw_delay(100);
IDISSEN_bit = 0;
sw_delay(100);
}
unsigned int ReadCTMU(int Index)
{
unsigned long total = 0;
int chrd;
const int samples=16;
unsigned int immediateValue;
unsigned int switchState;
// Get the raw sensor reading.
for(chrd = 0; chrd < samples; chrd++)
{
//AD1PCFGL = 0xFFFF; // A/D mux must connect to channel for CTMU to drain charge
ANSB = 0xFFFF;
TRISB14_bit=0;
LATB14_bit=0;
DELAY //wait for the capacitors to be discharged
DELAY
TRISB14_bit=1;
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//AD1PCFGL = 0; //configure again as analog inputs
//ANSB = 0x0000;
AD1CHS = Index;
ITRIM_bit=0;
AD1IF_bit = 0;
SAMP_bit = 1; // Manually start the conversion
EDG2STAT_bit = 0; // Make sure edge2 is 0
EDG1STAT_bit = 1; // Set edge1 - Start Charge
DELAY // Delay for CTMU charge time
DELAY
EDG1STAT_bit = 0; // Clear edge1 - Stop Charge
AD1IF_bit = 0;
SAMP_bit = 0;
while(!AD1IF_bit);
// Wait for the A/D conversion to finish
immediateValue = ADC1BUF0; // Read the value from the A/D conversion
SAMP_bit = 0;
AD1IF_bit = 0;
DONE_bit = 0;
//End of CTMU read
total = total + immediateValue;
}
immediateValue = total / samples;
if(immediateValue < OPENSW - TRIP)
{ switchState = PRESSED; }
else if(immediateValue > OPENSW - TRIP + HYST) { switchState = UNPRESSED; }
return switchState;
}
// Función principal
void main()
{
//setup_adc_ports(sAN9|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_32);
setup_adc(ADC_CLOCK_INTERNAL);
setup_oscillator(OSC_INTERNAL);
// TODO: USER CODE!!
Delay_ms(500); //
while(TRUE)
{
while(1)
{
InitCTMU();
unsigned int key_value;
key_value= ReadCTMU(10); //read touch channel 10
if(key_value)
{
printf(LCD_PUTC
, "\f\ntecla pulsada"); }
delay_ms(200);
delay_ms(5);
}
}
}