I am a Spanish student and I'm involved in a project with ARduino UNO and Joystick Radio control.
I'm trying to measure the duty cycle of a radio control and not the values are updated, ie not responding to the orders of the levers, turning both as acceleration. The algorithm is the folowing:
/////////// DEFICIION DE PINES //////////
const int Pin_in_ch1 = 2; const int Pin_in_ch2 = 3;
////////////////////////////////////////
////////////// VARIABLES TIPO "VOLATILE" INTERRUPCIION CH1 (PIN 2) ////////// volatile boolean primero_ch1; volatile boolean disparo_ch1; volatile unsigned long tiempo_inicial_ch1; volatile unsigned long tiempo_final_ch1; //////////////////////////////////////////////////////////////////// volatile unsigned long overflowCount; ////////////// VARIABLES TIPO "VOLATILE" INTERRUPCIION CH2 (PIN 3) ////////// volatile boolean primero_ch2; volatile boolean disparo_ch2; volatile unsigned long tiempo_inicial_ch2; volatile unsigned long tiempo_final_ch2; ////////////////////////////////////////////////////////////////////
unsigned int desborde = 65536; ///////// VARIABLES PARA LEER CH1 /////////////////
char dato_ch1 = 0; char dato_ant_ch1 = 0; char Ddato_ch1 = 0;
unsigned int Tini = 0; unsigned int Tact = 0; unsigned int nuevoTact = 0;
unsigned int DT = 0; unsigned int Duty_ch1 = 0;
const float Tclk = 0.0625; // microsegundos
boolean flancosubida = 0; boolean flancobajada = 1; int pulso = 0;
unsigned long T = 0; unsigned long T1 = 0; unsigned long T2 = 0;
unsigned long tini_loop = 0; unsigned long tact_loop = 0; unsigned long totalT_loop = 0;
unsigned int vueltas = 0; boolean Ok = 0;
ISR(TIMER1_OVF_vect){
overflowCount++;
}
void setup() {
Serial.begin(9600);
pinMode(Pin_in_ch1,INPUT); pinMode(Pin_in_ch2,INPUT); //digitalWrite(Pin_in_ch1,HIGH); //pull-up
TCCR1A = 0; TCCR1B = 0; TIMSK1 = _BV(TOIE1); // interrupcion desbordamiento TCNT1 = 0; TCCR1B = _BV(CS10); //No pre-escaler
PrepararInterrupcionPD2(); PrepararInterrupcionPD3();
}
void loop() {
tini_loop = micros();
flancosubida = 0; flancobajada = 1; dato_ant_ch1 = dato_ch1;
///////////// CALCULO DE FRECUENCIA Y PERIODO EN CH1 ///////////// if(!disparo_ch1) return;
unsigned long tiempo_transcurrido_ch1 = tiempo_final_ch1 - tiempo_inicial_ch1; float freq_ch1 = F_CPU / float(tiempo_transcurrido_ch1); float T_ch1 = float(tiempo_transcurrido_ch1*62.5e-6); // expresado en ms
///////////// CALCULO DE FRECUENCIA Y PERIODO EN CH1 /////////////
// ###############################################################
///////////// CALCULO DE FRECUENCIA Y PERIODO EN CH2 ///////////// if(!disparo_ch2) return;
unsigned long tiempo_transcurrido_ch2 = tiempo_final_ch2 - tiempo_inicial_ch2; float freq_ch2 = F_CPU / float(tiempo_transcurrido_ch2); float T_ch2 = float(tiempo_transcurrido_ch2*62.5e-6); // expresado en ms
///////////// CALCULO DE FRECUENCIA Y PERIODO EN CH2 /////////////
Leer_Canales();
PrepararInterrupcionPD2(); PrepararInterrupcionPD3();
tact_loop = micros(); totalT_loop = tact_loop - tini_loop;
}
enter code here
void PrepararInterrupcionPD2(){
EIFR = _BV(INTF0); primero_ch1 = true; disparo_ch1 = false; attachInterrupt(0,InterrupcionPD2,RISING); }
enter code here
void PrepararInterrupcionPD3(){
EIFR = _BV(INTF1); primero_ch2 = true; disparo_ch2 = false; attachInterrupt(1,InterrupcionPD3,RISING); }
enter code here
void InterrupcionPD2(){
unsigned int contador_ch1 = TCNT1;
if(disparo_ch1) return;
if(primero_ch1) { tiempo_inicial_ch1 = (overflowCount << 16) + contador_ch1; primero_ch1 = false; return; }
tiempo_final_ch1 = (overflowCount << 16) + contador_ch1;
disparo_ch1 = true;
detachInterrupt(0);
}
enter code here
void InterrupcionPD3(){
unsigned int contador_ch2 = TCNT1;
if(disparo_ch2) return;
if(primero_ch2) { tiempo_inicial_ch2 = (overflowCount << 16) + contador_ch2; primero_ch2 = false; return; }
tiempo_final_ch2 = (overflowCount << 16) + contador_ch2;
disparo_ch2 = true;
detachInterrupt(1);
}
void Leer_Canales(){
nuevoTact = 0;
dato_ch1 = digitalRead(Pin_in_ch1); // Leer pin
Ddato_ch1 = dato_ch1 - dato_ant_ch1; // diferencia de valores leidos
if(Ddato_ch1 == 1) // diferecia = 1 , flanco de subida
{
Tini = TCNT1; // empiezo a contar
flancosubida = 1;
}
if(Ddato_ch1 == -1) // diferencia = -1 , flanco de bajada
{
Tact = TCNT1; // detengo contador
flancobajada = 0;
if(Tact < Tini ) // en caso de desborde del contador
{
nuevoTact = Tact + desborde; // a las cuentas actuales le sumo el desborde = 65536
DT = nuevoTact - Tini;
// Duty_ch1 = DT * Tclk;
}
else
{
DT = Tact - Tini; // diferencias de cuentas
//Duty_ch1 = DT * Tclk;
}
}
Duty_ch1 = DT * Tclk; // calculo duty-cycle, Tclk = periodo micro = 62.5e-9
}
TCNT1
is always the same value? It would be helpful if you included the code that sets up Timer 1. – Gerben Nov 9 '15 at 19:26