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();
}
}