else // If the change is relatively small, then creep, track 0-point automatically
{
if(++TouchZeroCnt[i] >= 20) // Continuously detect small changes 20 times/4 = 5 seconds.
{
TouchZeroCnt[i] = 0;
TouchZero[i] = adc_prev[i]; // Use slowly changing values as 0 points }
}
adc_prev[i] = j; // Save this time's sample value
}
}
u8 check_adc(u8 index) // Get touch information function, called every 50ms
// Judge key is pressed or released with hysteresis control
{
u16 delta;
adc[index] = 1023 - Get_ADC10bitResult(index); // Get ADC value, convert to press the key, ADC value increases
if(adc[index] < TouchZero[index]) return 0; // A value smaller than 0-point is considered a key release
delta = adc[index] - TouchZero[index];
if(delta >= 40) return 1; //Key pressed
if(delta <= 20) return 0; //Key released
return 2; // Keep the original state
}
void ShowLED(void)
{
u8 i;
i = check_adc(0);
if(i == 0) P_LED0 = 1; //Light off
if(i == 1) P_LED0 = 0; //Light on
i = check_adc(1);
if(i == 0) P_LED1 = 1; //Light off
if(i == 1) P_LED1 = 0; //Light on
i = check_adc(2);
if(i == 0) P_LED2 = 1; //Light off
if(i == 1) P_LED2 = 0; //Light on
i = check_adc(3);
if(i == 0) P_LED3 = 1; //Light off
if(i == 1) P_LED3 = 0; //Light on
i = check_adc(4);
if(i == 0) P_LED4 = 1; //Light off
if(i == 1) P_LED4 = 0; //Light on
i = check_adc(5);
if(i == 0) P_LED5 = 1; //Light off
if(i == 1) P_LED5 = 0; //Light on
i = check_adc(6);
if(i == 0) P_LED6 = 1; //Light off
if(i == 1) P_LED6 = 0; //Light on
i = check_adc(7);
if(i == 0) P_LED7 = 1; //Light off
if(i == 1) P_LED7 = 0; //Light on
}
Assembly code
;Operating frequency for testing is 24MHz