INT_RB kesmesi yaptım çalışmıyor kütfen yardım!!

Başlatan wildarms, 29 Mayıs 2021, 00:37:59

wildarms

Kod etiketi kullanalım lütfen
uzun kodlarda kullanılmazsa ortalık dağılıyor
(OG)
------

Arkadaşlar encoder-dc motor kontrol etmeye çalışıyorum bir kod yazdım "angle" değerini sürekli sıfır alıyorum ama sıfır almamam lazım INR_RB kesmesinden kaynaklı olduğunu düşünüyorum yardımcı olabilir misiniz acaba?

#include <16F877A.h>
#device adc=10    //set the bit value of adc
#FUSES XT, NOWDT, NOPROTECT, NOBROWNOUT, NOLVP, NOPUT, NODEBUG, NOCPD
#use delay(crystal=4000000)
#include <lcd.c>
int8 last_read;
int8 quad;
#INT_RB  // RB port interrupt on change
void RB_IOC_ISR(void)
{
int8 encoderRead;
encoderRead = input_b() & 0x30;
if(encoderRead == last_read)
return;
if(bit_test(encoderRead, 4) == bit_test(last_read, 5))
quad -= 1;
else
quad += 1;
last_read = encoderRead;
}
signed long EncoderGet(void)
{
signed long value = 0;
while(quad >= 4){
value += 1;
quad -= 4;
}
while(quad <= -4){
value -= 1;
quad += 4;
}
return value;
}

unsigned long change = 0;
unsigned long realPosition = 0;
unsigned long angle = 0.0f;

void main()
{
  unsigned int8 temp;
  unsigned int16 pwm;
  unsigned int16 rev;
  unsigned int32 referance;  //variable for ADC reading value from AN0
  unsigned int16 kp;  //variable for ADC reading value from AN1
  lcd_init();
  delay_ms(10);
  set_tris_c(0x00); //set all portc pins as output
      set_tris_b(0xFF); //set all portc pins as output
  temp = input_b();
  enable_interrupts(INT_RB);
  clear_interrupt(INT_RB);
  enable_interrupts(GLOBAL);
  setup_ccp1(CCP_PWM); //4kHz PWM signal output at CCP1 pin 17

  setup_timer_2(T2_DIV_BY_16, 255, 1);
  setup_adc_ports(ALL_ANALOG); //A0A1 A2 A3 A5 E0 E1 E2 analog pins
  setup_adc(ADC_CLOCK_DIV_32); //enable ADC and set clock FOR ADC
  while(true)
  {
      set_adc_channel(0); // next analog reading will be from channel 0
      delay_us(10); //allow time after channel selection and reading
      referance= read_adc(); //start and read A/D
      referance= referance*250;
      referance= referance/1023;
      referance= referance+20;
      printf(LCD_PUTC,"\fRef=%lu,",referance); //print ADC value FOR A0
    
    
      set_adc_channel(1); // next analog reading will be from channel 1
      delay_us(10); //allow time after channel selection and reading
      kp = 50+(read_adc()*10/1023); //start and read A/D
      printf(LCD_PUTC,"Kp=%lu",kp); //print ADC value FOR A1
      delay_ms(200);
      change = EncoderGet();
if(change)
          {
realPosition += change;
        }
rev = realPosition/360.0f; // Here 360 represents the encoder pulses per revolution
angle = ((int16)(rev*360))%360; // Real time angle is determined in this line
printf(LCD_PUTC,"\nPos=%lu,",angle);
delay_ms(700);
      pwm= kp*(referance-angle);
      set_pwm1_duty(pwm); //set pulse-width during which signal is high
    if(pwm>1023){
      pwm=1023;
      }
      if(pwm<0){
      pwm=0;
      }
      printf(LCD_PUTC,"Pwm=%lu",pwm);
      delay_ms(700);
        }
    }