Advertisement
Guest User

Lab4Ej3

a guest
Apr 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <util/delay.h>
  2.  
  3. volatile uint32_t ul_count;
  4.  
  5. void InitTimer1(){
  6. // initialize Timer1
  7. TCCR1A = 0;
  8. TCCR1B = 0;
  9. TCCR1B |= (1 << WGM12);
  10. TCCR1B |= (1 << CS11);
  11. OCR1A = 1999;
  12. }
  13.  
  14. void InitAnalogComp(){
  15. // Configurar
  16. // Entrada no inversora: AIN0
  17. // Entrada inversora: ADC0
  18. ADCSRB|=(1<<ACME);
  19.  
  20. }
  21.  
  22. ISR(TIMER1_COMPA_vect){
  23. ul_count++;
  24. }
  25. float calcular_C(uint32_t ul_count){
  26. return (ul_count/100.0);
  27. }
  28. int main(void){
  29. cli(); // disable global interrupts
  30. Serial.begin(115200); // USART a 115200 bps
  31. DDRB |= (1 << DDB4); //PORT B4 como salida
  32.  
  33. InitAnalogComp();
  34. InitTimer1();
  35.  
  36. ul_count = 0;
  37. sei(); // Enable global interrupts
  38.  
  39. while(1){
  40. // Enable timer1 interrupt mask for compare match A
  41. TCNT1 = 0;
  42. TIMSK1 |= (1 << OCIE1A); // Turn on the timer
  43. PORTB |= (1 << PB4); // (pin 12 = HIGH)
  44.  
  45. while((ACSR & (1 << ACO))); // Wait to 63% of charge.
  46.  
  47. TIMSK1 &= ~(1 << OCIE1A); // Turn off the timer
  48. PORTB &= ~(1 << PB4); // (pin 12 = LOW)
  49.  
  50. // Enviar el valor de tau en (unidad de tiempo??)
  51. Serial.print("Tau = ");
  52. Serial.println(ul_count);
  53. //Implementar una funcion que calcule C: float CalcularC(ul_count, R)
  54. Serial.print("C=");
  55. Serial.println((float)calcular_C(ul_count));
  56. ul_count = 0;
  57.  
  58. // Esperar 5 segundos para la próxima medición
  59. _delay_ms(5000);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement