Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

increasing a number into a PIC16f690 via interrupt

Status
Not open for further replies.

picozero

New Member
Hi
I have a simple problem is driving me crazy:
Let's start "for dummies", just increasing one value of +1 every time I send an input High to RA1:
What 's missing ? Do I need something particular like a timer ? The datasheet is very unclear to me and the example are in assembry (that I don't know).

Code:
 #include <htc.h>

 /***** CONFIGURATION *****/
 __CONFIG(MCLREN & UNPROTECT & WDTDIS  & INTIO);
 
 /***** PROTOTYPES *****/
 void set7segC(char digit);           // display digit on 7-segment display
 void set7segAB(char digit);           // display digit on 7-segment display
 
 
 //Global variable, dealt by interrupts
     unsigned int    adc_dec;        // scaled ADC output (0-99)
 
 /***** MAIN PROGRAM *****/
 void main()
 {
     adc_dec = 0; // initialize global value
     
     // configure ports
     TRISA  = 0b11101011;
     ANSEL  = 0b00000001;
     ANSELH = 0b00000001; 
     TRISB  = 0b00000000;                      // configure PORTB and PORTC as all outputs
     TRISC  = 0b00000000;
     CM1CON0 = 0;                    // disable comparator 1 (RB0, RB1, RB2 usable)
     CM2CON0 = 0;                    // disable comparator 2 (RC0, RC1, RC4 usable)
     VRCON   = 0;                      // disable CVref (RC2 usable)
     // configure ADC     
     ADCON0 = 0b10000001;       
              //1------             Right justified
              //-0------            VDD
              //--0000--            Set AN0 as the input !!!!! AN0 = RA0
              //-------00             turn ADC off  
     ADCON1 = 0b00010000; //Before turning on the ADC
 
 //Interrupts setting
 GIE=1;
 INTEDG=1;
 PEIE=1;
 RABIE=1;
 IOCA = 0b00000010; //Enable only RA1
         
     ADON=1;           // turn on the A2D conversion module
     // Main loop
     for (;;)
     {
         // sample input
         GODONE = 1;             // start conversion
         while (GODONE);          // wait until done
         
 /*
         // scale result to 0-99
         //adc_dec =(((ADRESL + (ADRESH << 8)) * 3) / 31);
          if(RA1) // Read the volt value on the input while the redled is on
     {
       red_led =  (((ADRESL + (ADRESH << 8)) * 3) / 31);
     }
         if(RA5)// Read the volt value on the input while the irled is on and update the output
     {
       ir_led =  (((ADRESL + (ADRESH << 8)) * 3) / 31);
       adc_dec = ir_led;//red_led / ir_led;
     }
 */
  
     set7segC(adc_dec%10);    // output ones digit of result on segment bus 
     set7segAB(adc_dec/10);   // output tens digit of result on segment bus 
     }
 
       
 }
 
 //Interrupt Handler
 void interrupt ISR( void ) 
 {
     /* Is this a port change interrupt and is it enabled? */
     if( RABIF && RABIE ) 
     {
         RABIF = 0;
         adc_dec= adc_dec +1;
      }
 }


For some strange reason the number on the segment is not updated, but if I put straightforward
Code:
 void interrupt ISR( void ) 
 {
     /* Is this a port change interrupt and is it enabled? */
     if( RABIF && RABIE ) 
     {
         RABIF = 0;
         adc_dec= 5;
      }
 }


THe display will show me 5 .... strange question
 
Hi!
Thank you for addressing me to the right direction.
I have done the debug using the MPlab simulator.
Something weird happens.
Without stimulus (therefore no interrupt), the code stuck at
set7segC(adc_dec%10); // output ones digit of result on segment bus
set7segAB(adc_dec/10); // output tens digit of result on segment bus
and this is ok!

if I "fire" the stimulus, the code jumps on
void interrupt ISR( void )
and does not go out anymore even if I reset RABIF = 0;
Any suggestions ? Is there anything else I should reset to go out the interrupt and back to the main function ?


Or the problem is the fact the timer is not set ?
 
Last edited:
I solved in this way but it doesn't make any sense:

I put
RABIE= 0;
inside the interrupt function and at this point the function was closing and going back to the main.

Inside the main I reactivate RABIE =1 otherwise the interrupts are not enable.
There must be a more sensible way !!!!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top