interrupt confusion

General discussion on mikroC.
Post Reply
Author
Message
ascomm
Posts: 129
Joined: 30 Mar 2005 18:28
Location: Finland

interrupt confusion

#1 Post by ascomm » 28 Jul 2005 17:55

I can't get this project working correctly
Program calls only blinking() function as soon as I press the button.
Can someone clarify to me the using of interrupts with 12F675?

Code: Select all

// Microcontroller controlled flashlight
//  - PIC12F675 @ 8MHz
//  - three operating modes
//  - power off -mode
//  - single button operation

unsigned short mode;

void blinking()
{
   INTCON.GIE = 0;             // Disable global interrupts
   GPIO.F1 = 0;                //Blink the light
   Delay_ms(400);
   GPIO.F1 = 1;
   Delay_ms(100);
   INTCON.GIE = 1;             // Enable global interrupts
}

void poweroff()
{
   GPIO.F1 = 0;                // Turn off the light
   asm sleep;                  // and go to sleep
}

void powersaving()
{
   INTCON.GIE = 0;             // Disable global interrupts
   GPIO.F1 = 0;                // 33,3%
   Delay_ms(10);
   GPIO.F1 = 1;
   Delay_ms(5);
   INTCON.GIE = 1;             // Enable global interrupts
}

void bright()
{
   INTCON.GIE = 0;             // Disable global interrupts
   GPIO.F1 = 0;                // 88,8%
   Delay_ms(1);
   GPIO.F1 = 1;
   Delay_ms(8);
   INTCON.GIE = 1;             // Enable global interrupts
}

void interrupt()
{
   INTCON.GIE = 0;                  // Disable global interrupts
   GPIO.F1 = 0;                     // Turn off the light
   mode++;                          // Increment mode by one
   if (mode > 3) mode = 0;          // mode can't be greater than 3
   while (GPIO.F0 == 1) asm nop;    // Wait until user releases the button
   INTCON.GPIF = 0;                 // Clear interrupt flag
   INTCON.GIE = 1;                  // Enable global interrupts
}

void main(void)
{
  GPIO = 0;         // GPIO's all bits are low
  TRISIO = 1;       // GPIO.F0 is input
  CMCON = 7;        // Disable comparator
  ANSEL = 0;        // Disable ADC
  GPIO.F1 = 0;      // GPIO.F1 is low
  WPU = 0;          // disable weak pull-ups
  mode = 0;         // mode = 0
  IOC = 1;          // Enable GPIO<0> interrupt on change
  INTCON.GPIE = 1;  // Enable interrupt on change
  INTCON.GIE = 1;   // Enable global interrupts


  while (1)
  {
    if (mode==0) poweroff();       // Call appropriate funtion
    if (mode==1) bright();         // according to value of mode
    if (mode==2) powersaving();
    if (mode==3) blinking();
  }
}

Last edited by ascomm on 31 Jul 2005 20:18, edited 2 times in total.
char *signature;
signature = "MikroC Pro and EasyPIC 2";

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: interrupt confusion

#2 Post by pizon » 29 Jul 2005 11:50

  • 1. Add

    Code: Select all

    WPU = 0;          // disable weak pull-ups
    in main(), to disable weak pull-ups;

    2. Disable interrupt in all functions during their execution:

    Code: Select all

    function bright() {
      INTCON.GIE = 0;
      ...<function body>...
      INTCON.GIE = 1;
    }
pizon

ascomm
Posts: 129
Joined: 30 Mar 2005 18:28
Location: Finland

#3 Post by ascomm » 31 Jul 2005 14:58

Still no luck. Now the program never calls the blinking function.
Also the program does random function calls and sometimes the light might blink once, and then it canges to 80%.
Mysterious :?

I edited the first message to contain current program code.
char *signature;
signature = "MikroC Pro and EasyPIC 2";

ascomm
Posts: 129
Joined: 30 Mar 2005 18:28
Location: Finland

#4 Post by ascomm » 31 Jul 2005 16:36

Well now I got this program to work right :)
I tested for wrong logical state in interrupt function.
Thanks for help, Pizon!

Edit: Code in first message fixed and full functional.
char *signature;
signature = "MikroC Pro and EasyPIC 2";

Post Reply

Return to “mikroC General”