Autor Tema: problema con timers (solucionado)  (Leído 3365 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado pirraca

  • PIC10
  • *
  • Mensajes: 11
Re: problema con timers
« Respuesta #15 en: 31 de Marzo de 2009, 18:21:59 »
Bueno, despues de pensar varias soluciones, he optado por la siguiente:

Código: [Seleccionar]
#include <16F886.h>
#include <i2c_config.h>

#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT, NOLVP
#use delay(clock=20000000)

#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa2, FORCE_HW)

#byte INTCON = 0x0B

#bit  T0IF = INTCON.2

BYTE array[4];

static BYTE pwm1;
static BYTE pwm2;

#INT_SSP
void ssp_interupt ()
{
BYTE state;

   state = i2c_isr_state();

   if(state < 0x80)                     //Master is sending data
   {
  array[state] = i2c_read();

  if(state == 2){                     //Second received byte is data
         if (array[0]==SPEED){

if( bit_test(array[1],7)){
      pwm1 = 0;
    pwm2 = array[state];
             }
else{
pwm2 = 0;
    pwm1 = array[state];
}

  }
}

}
   if(state >= 0x80)                     //Master is requesting data
  {
      i2c_write(0x01);
   }
 

}

void main ()
{
   long time=1,flancos=0;
   static long value1,value2;

   setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM
   setup_ccp2(CCP_PWM);   // Configure CCP1 as a PWM

   set_timer1(0);

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32);

   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DIV_BY_1, 255, 1);

   pwm1 = 0;
   pwm2 = 0;
   
  enable_interrupts(INT_SSP);
  enable_interrupts(GLOBAL);

   while (TRUE){

if (T0IF==1){
                       if(time==1)
{
value1=get_timer1();
time=2;
}
else
  {
value2=get_timer1();
flancos=value2-value1;
time=1;
}

T0IF=0;
}

set_pwm1_duty(pwm1);
set_pwm2_duty(pwm2);

}
}


Como podeis apreciar, he olvidado la interrupcion de TMR0, y he pasado a mirar si se desborda mediante la tecnica de polling.

Todo funciona correctamente, y obtengo los resultados esperados, asi que prefiero olvidarme del gran problema que he tenido estos dias, aunque la solución no sea todo lo "decorosa" que podía haber sido.

En fin, muchisimas gracias a todos por vuestra desinteresada ayuda

seguiré por el foro!


 

anything