Ecco il codice completo:
`
#define PRESSED 1
#define UNPRESSED 0
#byte xxxxCONH = 0xFB3
#byte xxxxCONL = 0xFB2
#byte xxxxICON = 0xFB1
#byte ANCON0 = 0xF48
#byte ANCON1 = 0xF49
#BYTE ADCON1 = 0xFC1
#BYTE ADCON0 = 0xFC2
#BYTE PIR1 = 0xF9E
#BYTE ADRESH = 0xFC4
#BYTE ADRESL = 0xFC3
void setup(void)
{ //xxxxCON – xxxx Control register
xxxxCONH = 0x00; //make sure xxxx is disabled
xxxxCONL = 0x90;
//xxxx continues to run when emulator is stopped,xxxx continues
//to run in idle mode,Time Generation mode disabled, Edges are blocked
//No edge sequence order, Analog current source not grounded, trigger
//output disabled, Edge2 polarity = positive level, Edge2 source =
//source 0, Edge1 polarity = positive level, Edge1 source = source 0,
//xxxxICON – xxxx Current Control Register
xxxxICON = 0x01; //0.55uA, Nominal – No Adjustment
//bit_set (ANCON1, 7);
/**************************************************************************/
//Setup AD converter;
/**************************************************************************/
setup_adc_ports (NO_ANALOGS);
bit_clear (ADCON1, 5);
bit_clear (ADCON1, 4);
bit_set (ADCON1, 3);
setup_adc (ADC_CLOCK_DIV_32);
setup_adc_ports(sAN11 | VSS_VDD);
set_adc_channel(11);
bit_set (ADCON0, 0);
}
//#use delay(clock=48000000)
void xxxx_startup(void)
{
unsigned int16 OPENSW = 0;
unsigned int16 Vread = 0;
int1 switchState;
int pressed_count, unpressed_count;
while (TMR1_ov < 69) // FOSC = 48 MHz / 4 3 secondi
{
setup();
bit_set (xxxxCONH,7); //xxxxCONHbits.xxxxEN = 1; // Enable the xxxx
bit_clear (xxxxCONL,0); //xxxxCONLbits.EDG1STAT = 0; // Set Edge status bits to zero
bit_clear (xxxxCONL,1); //xxxxCONLbits.EDG2STAT = 0;
bit_set (xxxxCONH,1); //xxxxCONHbits.IDISSEN = 1; //drain charge on the circuit
delay_us (125); //wait 125us
bit_clear (xxxxCONH,1); //xxxxCONHbits.IDISSEN = 0; //end drain of circuit
bit_set (xxxxCONL,0); //xxxxCONLbits.EDG1STAT = 1; //Begin charging the circuit
//using xxxx current source
delay_us (125); //wait for 125us
bit_clear (xxxxCONL,0); //xxxxCONLbits.EDG1STAT = 0; //Stop charging circuit
//bit_clear (PIR1,6); //PIR1bits.ADIF = 0; //make sure A/D Int not set
bit_set (ADCON0,1); //ADCON0bits.GO=1; //and begin A/D conv.
while (bit_test(ADCON0,1)); //(!PIR1bits.ADIF); //Wait for A/D convert complete
Vread = ADRESH; //Vread = ADRES; //Get the value from the A/D
Vread = Vread << 8;
Vread = Vread + ADRESL;
//printf( "Vread %Lu\n\r",Vread);
//Vread = read_adc ();
if(!xxxx_once)
{
OPENSW = 0xC0;
output_high (pin_D2);
xxxx_once = 1;
//fprintf(debug, "Vread %Lu\r\n",Vread);
}
if(xxxx_once)
{
if(Vread < (OPENSW - 110))
{
switchState = PRESSED;
}
else if(Vread > OPENSW – 110 + 10)
{
switchState = UNPRESSED;
}
if (switchState)
{
unpressed_count = 0;
if (pressed_count >= 10)
{
output_low (pin_D2);
prog = 1;
TMR1_ov = 69;
}
else
pressed_count++;
}
else if (!switchState)
{
pressed_count = 0;
if (unpressed_count >= 10)
{
output_high (pin_D2);
}
else
unpressed_count++;
}
}
}
output_low (pin_D2);
disable_interrupts(INT_TIMER1);
}
`