Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Interrupt problem with pic16f690

Status
Not open for further replies.

trsa.foto

Newbie level 1
Joined
Feb 23, 2016
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
12
Hello. I have a problem.
On pins RB4 and RB5 i have connected an encoder. And when encoder is moved to left i want to turn on LED. But is seems to me that interupt does not work at all..
Please help. Thanks
Code:
#include "16F690.h"
#include <xc.h>

#include "lcd.h"
#include "delays.h"


#pragma config FOSC=INTRCIO
#pragma config WDTE=OFF
#pragma config PWRTE=OFF
#pragma config MCLRE=ON
#pragma config CPD=OFF
#pragma config CP=OFF
#pragma config BOREN=OFF
#pragma config IESO=OFF
#pragma config FCMEN=OFF

//#define _XTAL_FREQ 4000000
#define LEFT        1
#define RIGHT      2
#define TRUE        1
#define FALSE       0

#define T1              PORTAbits.RA0
#define T2              PORTAbits.RA1
#define LED             PORTAbits.RA4
#define BUZ             PORTBbits.RB7
#define ENCODER_A       PORTBbits.RB4
#define ENCODER_B       PORTBbits.RB5





unsigned char staro_stanje_enkodera, novo_stanje_enkodera, enkoder_A_novo_stanje, enkoder_A_staro_stanje, enkoder_B_novo_stanje, enkoder_B_staro_stanje, brojac_citanja_enkodera, smer,enkoder_pomeren;
unsigned char trenutni_meni;

#INT_RB
void encoder_interrupt_isr()
{
   if(RABIF == 1){ 
  //    GIE=0;
      RABIF =0;
       
    brojac_citanja_enkodera++;
    if(brojac_citanja_enkodera==2)
    {
        brojac_citanja_enkodera=0;
        //delay_cycles(100);
        enkoder_A_novo_stanje = ENCODER_A;
        enkoder_B_novo_stanje = ENCODER_B;
        
        novo_stanje_enkodera = enkoder_A_novo_stanje & enkoder_B_novo_stanje;
        
        if(novo_stanje_enkodera != staro_stanje_enkodera)
        {
            if (enkoder_A_novo_stanje == 0 && enkoder_A_staro_stanje == 1 && enkoder_B_novo_stanje == 0 && enkoder_B_staro_stanje == 1)
            {
                smer = DESNO;
                enkoder_pomeren = TRUE;
            }
            else if(enkoder_A_novo_stanje == 1 && enkoder_A_staro_stanje == 0 && enkoder_B_novo_stanje == 0 && enkoder_B_staro_stanje == 1)
                 {
                     smer = LEVO;
                     enkoder_pomeren = TRUE;
                 }
            
            enkoder_B_staro_stanje = enkoder_B_novo_stanje;
            enkoder_A_staro_stanje = enkoder_A_novo_stanje;
            staro_stanje_enkodera = novo_stanje_enkodera;
        }
        else
        {
            smer = 0;
            enkoder_pomeren = FALSE;
        }
    
     
    } 
   } 
   
}//encoder_interrupt


int main()
{
    
    OSCCON = 0x70;
    
    GIE=0;
    PEIE=1;
    RABIE=1;
    RABIF =0;
    GIE=1;
    INTCON=0b11001000;
    //MCU init
   OPTION_REG=0xC0; //AB pull-ups disabled
   
    
    ANSEL=0x00;
    ANSELH=0x00;

    
    CM1CON0=0x00;
    CM2CON0=0x00;
    
    T1CON=0x00;
    
    TRISA=0b00000011;
    PORTA=0x00;
    
    
    TRISB=0b00110000;
    PORTB=0b10000000;
    
    TRISC=0x00;
    PORTC=0x40;
    
    

       
    lcdInit();
    lcdInit();
    
    lcdWriteString(0x02, "GALVO_v2.0");
    lcdWriteString(0x42, "www.jena.rs");
    delay_ms(60000000);
    
     lcdClear();

    enkoder_A_staro_stanje = ENCODER_A;
    enkoder_B_staro_stanje = ENCODER_B;
    staro_stanje_enkodera =  enkoder_A_staro_stanje & enkoder_B_staro_stanje;
    
     
     
     

     
    while(1)
    
    { 
      
     
     if (smer == RIGHT){
         enkoder_pomeren = FALSE;
         smer=0;
         LED=0;
     }
     else if( smer == LEFT)
     {
        enkoder_pomeren = FALSE;
        smer=0;
        LED=1;
                    }
                 
            }
         
        return 0;        
        }
 

If you are using the XC8 compiler (I'm guessing so as you include "xc.h") the you should not include the processor specific file ("16f690.h" in this case) as the "xc.h" file will do this for you based on the processor selected in the IDE.
Also, you are testing the value of the 'smer' variable in the main loop and updating this in the ISR. Therefore the variable will need to be declared as volatile. Otherwise the compiler is well within its rights to make a copy of the variable within a register (as it is used a lot so fast access is a good thing) and therefore it will never detect the fact the ISR has changed the variable itself.
Susan
 
try to find interrupt handler for pic16f..it may b
void interrupt ISR(void).try to use it
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top