Hola les consulto, estoy tratando de medir frecuencia y duty de una señal sin usar la función pulsein, estoy usando el arduino uno y no logro progresar con mi proyecto, subo simulación , gracias
Código:
#define F_CPU 16000000l
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(6, 9, 10, 11, 12, 13);
unsigned int a,b,c,high,period;
int freq,duty_cycle;
void setup ( )
{
lcd.begin(16,2);
lcd.clear();
PORTD = 0xFF; /* Turn ON Pull-UP resistor */
}
void loop()
{
TCCR1B |= _BV(ICES1); // enable input capture
TIMSK1 = _BV(ICIE1); // enable input capture interrupt for timer 1
TIMSK1 |= _BV(TOIE1); // enable overflow interrupt to detect missing input pulses
TCCR1A = 0;
TCNT1=0;
TIFR1 = (1<<ICF1); /* clear ICP flag (Input Capture flag) */
TCCR1B = 0x41; /* rising edge, No prescaler */
while ((TIFR1&(1<<ICF1)) == 0);
a = ICR1; /* take value of capture register */
TIFR1 = (1<<ICF1); /* clear ICP flag (Input Capture flag) */
TCCR1B = 0x01; /* falling edge, No prescaler */
while ((TIFR1&(1<<ICF1)) == 0);
b = ICR1; /* take value of capture register */
TIFR1 = (1<<ICF1); /* clear ICP flag (Input Capture flag) */
TCCR1B = 0x41; /* rising edge, No prescaler */
while ((TIFR1&(1<<ICF1)) == 0);
c = ICR1; /* take value of capture register */
TIFR1 = (1<<ICF1); /* clear ICP flag (Input Capture flag) */
TCCR1B = 0; /* stop the timer */
if(a<b && b<c) { /* check for valid condition, to avoide timer overflow reading */
high=b-a;
period=c-a;
freq= F_CPU/period; /* calculate frequency */
duty_cycle = ((int) high/ (int) period)*100; /*calculate duty cycle */
char buffer[20];
lcd.setCursor(3,0);
sprintf(buffer, "F = %4d", freq);
lcd.print(buffer);
lcd.setCursor(3,1);
sprintf(buffer, "pwm = %4d", duty_cycle);
lcd.print(buffer);
delay(50);
}
}