daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest May 29th, 2018 70 in 38 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <util/delay.h>
  2.  
  3. volatile uint32_t   ul_count;
  4. volatile uint32_t   tiempo;
  5. volatile uint32_t   periodo;
  6. volatile uint32_t   primero=0;
  7.  
  8. void InitTimer1(){
  9.     // initialize Timer1
  10.     TCCR1A = 0;
  11.     TCCR1B = 0;
  12.     TCCR1B |= (1 << WGM12);//modo CTC
  13.     TCCR1B |= (1 << CS11);//prescaler 8 010
  14.     OCR1A = 1999; //interrumpo cada 1 mili seh
  15. }
  16.  
  17. void InitAnalogComp(){
  18.   // Configurar
  19.   // Entrada no inversora: AIN0
  20.   // Entrada inversora: ADC0
  21.   ACSR|=(1<<ACBG);//band gap
  22.   ADCSRB|=(1<<ACME);//a AIN1 le aplico una tension invertida
  23.   ACSR|=(1<<ACIS1)|(1<<ACIS0);//flanco ascendente
  24.   ACSR|=(1<<ACIE);//activo int
  25.   //ACSR|=(1<<ACI);//activo bandera de int
  26.   ACSR|=(1<<ACIC);//activo capturas de tiempo
  27. }
  28.  
  29. ISR(TIMER1_COMPA_vect){
  30.     ul_count++;
  31. }
  32.  
  33. ISR(ANALOG_COMP_vect){
  34. if(primero==1){
  35.   periodo=ICR1-tiempo;
  36.   primero=0;
  37.   }else{
  38.        tiempo=ICR1;      //tiempo incial
  39.        primero=1;
  40.     }
  41.               Serial.print("periodo ");
  42.              Serial.println( periodo);
  43.   }
  44.  
  45.  
  46. int main(void){
  47.   cli(); // disable global interrupts
  48.   Serial.begin(115200); // USART a 115200 bps
  49.   InitAnalogComp();
  50.   InitTimer1();
  51.  
  52.   ul_count = 0;
  53.   sei(); // Enable global interrupts
  54.      TCNT1 = 0;
  55.     TIMSK1 |= (1 << OCIE1A);     // Turn on the timer  
  56.    
  57.     while(1){
  58.                  _delay_ms(1000);
  59.       }
  60.     TIMSK1 &= ~(1 << OCIE1A);    // Turn off the timer
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top