Program Does Nothing, Can't Figure it Out

Discussion in 'Programmer's Corner' started by ke5nnt, Jan 9, 2015.

  1. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Hi all,

    I am writing myself a journal of knowledge for mid-range PIC microcontroller programming in C using XC8 and MPLAB X. Since I am nothing more than a hobbyist and recreational programmer at best, I can easily go stretches without writing any programs, and it's easy to forget some programming things in between. My little journal is a quick reference for me to come back to when I'm stuck.

    This entry talks about the basics of writing an Interrupt Service Routine. I am using the PIC12F629. The intent is to simply have 2 LEDs connected to the MCU. Upon power-up, the blue LED is on. A button press connected to the INT pin of the MCU should turn off the blue LED and flash the red LED on/off 20 times with a 50mS delay.

    Throwing power to the device results in nothing happening. I have tried multiple changes to the code, I have tried another MCU chip in case the first one was bad, and I have tested to ensure I have 5V to the device. I indeed have power into the device, but no voltage on the LED outputs at any point. I'm figuring something has to be wrong with the code but it looks fine to me. I need some fresh and more knowledgeable eyes on it.

    As always, thanks for the help. Code to follow:

    Code (Text):
    1.  
    2. #include <xc.h>
    3.  
    4. /**********CONFIGURATION WORD******************/
    5. #pragma config BOREN = OFF, MCLRE = OFF, PWRTE = ON, WDTE = OFF, FOSC = INTRCIO
    6. /**********************************************/
    7.  
    8. /**********DEFINE GLOBAL VARIABLES*************/
    9. unsigned char GPIOimg;  //SHADOW REGISTER FOR I/O
    10. bit request;  //A FLAG BIT NAMED "request"
    11.  
    12. /****************I/O MAP***********************/
    13. #define GPIOinit 0b00000000  //ALL GPIO INITIALIZED OFF
    14. #define TRISIOinit 0b00000100  //GPIO <7:3>, <1:0> OUTPUT, GPIO 2 IS INPUT
    15.  
    16. #define REDmask 0b00010000  //THE BIT LOCATION OF THE RED LED
    17. #define BLUEmask 0b00001000  //THE BIT LOCATION OF THE BLUE LED
    18.  
    19. /****************BIT VARIATIONS****************/
    20. #define FALSE 0
    21. #define TRUE 1
    22.  
    23. /*************OSCILLATOR***********************/
    24. #define _XTAL_FREQ 4000000  //OSCILLATOR IS 4MHz
    25.  
    26. /***********SHADOWED I/O ROUTINES**************/
    27. void doRED_LED_ON(void)
    28. {
    29.   GPIOimg |= REDmask;
    30.   GPIO = GPIOimg;
    31. }
    32.  
    33. void doRED_LED_OFF(void)
    34. {
    35.   GPIOimg &= ~REDmask;
    36.   GPIO = GPIOimg;
    37. }
    38.  
    39. void doBLUE_LED_ON(void)
    40. {
    41.   GPIOimg |= BLUEmask;
    42.   GPIO = GPIOimg;
    43. }
    44.  
    45. void doBLUE_LED_OFF(void)
    46. {
    47.   GPIOimg &= ~BLUEmask;
    48.   GPIO = GPIOimg;
    49. }
    50.  
    51. /***********INTERRUPT**************************/
    52. void interrupt isr(void)
    53. {
    54.   request = TRUE;
    55. }
    56. /**********THE PROGRAM*************************/
    57. void main(void)
    58. {
    59.   TRISIO = TRISIOinit;
    60.   GPIOimg = GPIOinit;
    61.   GPIO = GPIOinit;
    62.   request = 0;
    63.   INTF = 0;
    64.   GIE = 1;
    65.   INTE = 1;
    66.  
    67.   while(1)
    68.   {
    69.   doBLUE_LED_ON();
    70.   if (request == TRUE)
    71.   {
    72.   doBLUE_LED_OFF();
    73.   request = FALSE;
    74.   for (unsigned char count = 20; count > 0; count--)
    75.   {
    76.   doRED_LED_ON();
    77.   __delay_ms(50);
    78.   doRED_LED_OFF();
    79.   __delay_ms(50);
    80.   }
    81.   INTF = 0;
    82.   }
    83.   }
    84. }
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You need to initialize the ports as digital I/O.

    See ANSEL on page 46 and CMCON on page 36 of the datasheet.
     
  3. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    1. I don't recall ever needing to do this with previous projects I've done using this MCU
    2. I believe the Analog Inputs are specific only to the 12F675 device, i am using 12F629.

    I'm not brushing off your response, only thinking out loud in a group setting.
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Yes, ANSEL doesn't affect you, then.

    You also need to clear your interrupt source in the ISR, otherwise, it will lockup in an interrupt service loop.

    Edit: I vaguely remember someone saying that GIE must be set after all interrupt masking and clearing is done (e.g. clear INTF after setting INTE and setting GIE should be the last bit of initialization done), otherwise it may generate an interrupt.
     
    Last edited: Jan 9, 2015
  5. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Thanks tshuck. I made the changes you suggested. After powering on the device I still did not get the blue LED on GP3 to light, however the interrupt routine now works and the red LED flashes as intended. After reading through the I/O section of the datasheet again, I noticed this:

    Oops, not sure how I missed that the first go. Changing ports for that LED solved that problem. Thanks for the help!
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    ..and I assumed that the bit in your TRIS settings set to input was the input-only pin. I hadn't thought to check it as a result. Whoops! You live and learn.;)

    Glad you got it!
     
    ke5nnt likes this.
Loading...