Пример #1
0
/*-----------------------------------------------------------------------*/
interrupt 12 void TC4handler(void){
    unsigned char analog_tollerance_ = ((ANALOG_TOLLERANCE*1024)/100);
    unsigned int analog_compare =0; 
    unsigned int analog_current = 0; 
    unsigned char x=0, y=0; 
  
     //Clear the interupt flag
    TFLG1 = TFLG1_C4F_MASK;
   
   
    if(analog_delay_counter == ANALOG_DELAY){
        analog_current = Analog_Read(KEYPAD_INPUT); 
        analog_compare = abs(analog_previous-analog_current); // 
        
        
        if( analog_compare >= analog_tollerance_) {
         /// process keypress
           y = (sizeof(keypad_analog_values))/2; 
           for(x= 0;x<y;x++){
                analog_compare = abs(keypad_analog_values[x]-analog_current);
          
                if(analog_compare < (analog_tollerance_)) {
                  keypad_press = x; 
                    // trigger a button event 
                  keypad_press_trigger = 1;
                  break; 
                }
                 
           }
        
        }
        
  
        
        analog_previous = analog_current; 
      #if ANALOG_DEBUG 
        printf("\nAnalog(%d):  Cur:%d     Prev:%d        ",KEYPAD_INPUT, Analog_Read(KEYPAD_INPUT),analog_previous);
      #endif  
    // reset analog counter   
        analog_delay_counter =0;    
    } 
    
    else 
     analog_delay_counter++; 
       
    TC4 = TCNT + 60000; 
    
}
void Motor_Speed(void)
{
	ADC_Init();  /*Initializes the ADC module*/
	Valor_voltaje = Analog_Read(); /*Read the convertion of the ADC*/
	calculo_tiempo_us =((unsigned short)(Valor_voltaje)*156)+4000; /*Use the aproximation ecuation*/
	tiempo_us = (calculo_tiempo_us)/100;/*Put the result in a u16*/
	delay_us_MTIM(tiempo_us);/*set the delay */
}