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.

PIC12629/675 Sleep Mode wake and blink led

Status
Not open for further replies.

ysyogeshshende

New Member
Hi sir
I am trying write c code for PIC12f629/PIC12f675 Microcontroller as per below points but i am failed ,please help me write code
I write code i MPLAB X XC8 complier
Points -
1 -
When PIC12f629 Power Connect (power on ) and switch 1(GP3 pin ) is high ( Switch pressed ) Led1 Blink ( gp1) 5 sec and sleep mode on ( power stand by mode on )
2-
after that if switch(gp3) is release low ( switch 0 ) led1 blink (gp1) 5 sec and sleep mode on ( power stand by mode on )
3-
again pressed switch (GP3 ) is high led2 ( gp2) continue on until switch is release ( switch 0 )
4
switch(gp3) Release Switch 0 - again led1 (gp1) blink 5 sec and sleep

please help me
 
thanks for reply sir
Yes sir but i want use pic12f629 or 12f675
its very simple i cant gate it logic
i attached my code but i think iam doing wrong , done its complicated
.configration
led1 - gp1
led2 - gp2
switch - sw1 - gp3
my logic is simple
as below
when we connect power then gp1 led1 is blink 5sec and of and PIC go to sleep
again when we presses switch same condition done led1 is blink 5sec and off and pic sleep mode
and last if again 2nd time pressed led2 gp2 blink continue until we release switch
and then go to sleep
 

Attachments

  • led_sleep_pic12f675.c
    3.2 KB · Views: 271
Last edited:
I'm not sure what your code is doing. The function sleepm is never used - nor are the EEPROM routines. You're using EEDATA as a variable - why? Your ISR is incrementing count and produces 0,1,2,3,0,0 followed by another 250 zeros - is this what you intended? And, why not just use count?. The line IOCB|=1<<sw; is actually doing IOCB|=1<<GP3; which makes no sense. You need to debounce the switch or lot's of interrupts will be generated.

Mike.
 
thanks for reply
yes sir i am not expert in these , i know i am doing wrong that's why i need help
please can you give logic to help me for run below code
please just need c code for as per below condition
.configuration
led1 - gp1
led2 - gp2
switch - sw1 - gp3
my logic is simple
as below
when we connect power then gp1 led1 is blink 5sec and of and PIC go to sleep
again when we presses switch same condition done led1 is blink 5sec and off and pic sleep mode
and last if again 2nd time pressed led2 gp2 blink continue until we release switch
and then go to sleep
 
Also with WDT enabled, it will wake the PIC from sleep when it times out, preventing any long "sleep". I don't see any WDT timer reset anywhere in the code...
 
thanks for
reply now iam off WDT

but i want wakeup when iam pressed sw=1 in GP3 pin
but cant gate it logic sorry for that
 

Attachments

  • led_sleep_pic12f675.c
    3.1 KB · Views: 251
Last edited:
Also with WDT enabled, it will wake the PIC from sleep when it times out, preventing any long "sleep". I don't see any WDT timer reset anywhere in the code...

Thanks sir
Iam done as below but for case 3 not run code not wakeup pic from sleep
using sw
/*
* File: led_sleep_pic12f675.c
* Author: Yogesh Shende
* Created on April 11, 2021, 11:46 AM
*/


// PIC12F675 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdint.h>
#include<stdlib.h>


#define l1 GP0 // LED1
#define l2 GP1 // LED2
//#define sw GP2 // sw1
#define l3 GP4 // LED4
#define l4 GP5 // LED5
#define sw GP3 // switch 1
#define _XTAL_FREQ 4000000
unsigned char count=0;
volatile unsigned int flag=0,i;
void ledloop1(void);
void ledloop2(void);
void switchcase ();

void main(void)
{
ANSEL = 0x00; // Set ports as digital I/O, not analog input
ADCON0 = 0x00; // Shut off the A/D Converter
CMCON = 0x07; // Shut off the Comparator
VRCON = 0x00; // Shut off the Voltage Reference
TRISIO = 0b11001000; // GP3 input, rest all output
GPIO = 0x00; // Make all pins 0
WPU=0; //WEAK PULL-UP REGISTER 0
//INTCONbits.INTF=0; /* Clear INT0 interrupt flag */
//INTCONbits.INTE=0; /* Enable INTOIF interrupt */
INTCONbits.GIE=1; /* Enable global interrupt */
INTCONbits.GPIE=1;
IOC3=1; //INTERRUPT-ON-CHANGE GPIO REGISTER on GP3 pin
while(1)
{

if(sw==1)
{
__delay_ms(5);
while(sw==1)
{
if(flag<3)
{
flag=flag++;
}

}
}

switch (flag)
{
case 0:
{
// if (sw != 1&&flag==0) // 1st step if switch sw=0 or sw=1 led blink and sleep
// {
__delay_ms(10);
ledloop1();
__delay_ms(100);
SLEEP();
//}
}
break;
case 1:
{
// if (sw == 1&&flag==1) //2nd step if switch sw=0 or sw=1 led blink and sleep
// while (flag)
{__delay_ms(10);
ledloop1();
__delay_ms(40);
SLEEP();
// }
}
break;
case 2:
//if (sw == 1&&flag==2) //3rd step if sw pressed 2nd time the operation run continue until sw=0
//{
// __delay_ms(10);
while(sw)
{
l3=1; // Led 3 Continue on until we release sw=0;
for (count=0; count<10; count++)
{
ledloop2();
__delay_ms(50); // delay for flash

}
}
//}
break;
}
}
}
}




//void __interrupt() ISR()
//{
// while(GPIF)
// {
// if(flag<3)
// {
// flag=flag++;
// }
// flag=0;
// }
//GPIO=flag;
//}
//
void ledloop1(void)
{
for(i=0;i<10;i++)
{
l1=~l1;
__delay_ms(60);
}
}
void ledloop2(void)
{
while(1)
{
for(i=0;i<10;i++)
{
l2=~l3;
__delay_ms(60);
}
}
}
 

Attachments

  • led_sleep_pic12f675.c
    3.7 KB · Views: 221
First thing you need to do is tidy up your code. I can't see half of it due to all the junk. Keep only what you're using, properly indent it and see if you can see why it wont work. What code do you think it will execute when an IOC interrupt comes along? To post formatted code use [code] before it and [/code] after it.

Mike.
 
Thanks For reply

Iam done my 3 step but after case 2 and switch =0 i need to go to 0 stage thats flag 0 and case 0 but its not work
please help me for that thanks for these

Code:
/*
 * File:   led_sleep_pic12f675.c
 * Author: Yogesh Shende
 * Created on April 11, 2021, 11:46 AM
 */


// PIC12F675 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF       // Power-Up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF      // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = OFF       // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF         // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdint.h>
#include<stdlib.h>


#define l1  GP0   // LED1
#define l2  GP1   // LED2
//#define sw  GP2   // sw1
#define l3  GP4   // LED4
#define l4  GP5   // LED5
#define sw  GP3   // switch 1 
#define _XTAL_FREQ 4000000
unsigned char count=0,i;
volatile unsigned int flag=0;
void ledloop1(void);
void ledloop2(void);
void ledloop3(void);


void main(void)
   {
    ANSEL  = 0x00;       // Set ports as digital I/O, not analog input
    ADCON0 = 0x00;         // Shut off the A/D Converter
    CMCON  = 0x07;         // Shut off the Comparator
    VRCON  = 0x00;         // Shut off the Voltage Reference
    TRISIO = 0b11001000;       // GP3 input, rest all output
    GPIO = 0x00;       // Make all pins 0
    WPU=0; //WEAK PULL-UP REGISTER 0
    //INTCONbits.INTF=0;    /* Clear INT0 interrupt flag */
    //INTCONbits.INTE=0;    /* Enable INTOIF interrupt */
    INTCONbits.GIE=1;        /* Enable global interrupt */
    INTCONbits.GPIE=1;
    IOC3=1; //INTERRUPT-ON-CHANGE GPIO REGISTER on GP3 pin 
   while(1)
   {
       {
       if(sw==0&&flag==0) //Power On and  Switch is release   SW=0 - need blink led and sleep  -  -if(sw==0&&GPIF==0)
         {
           __delay_ms(5);
       
           while(sw==0&&GPIF==0)
           {
           flag=0;
           }
       }
       else if (sw==1&&flag==0&&GPIF==1) // Power on and ( 1st )switch pressed  SW=1 - LED blink and sleep ())
           {__delay_ms(5);
               while(sw==1&&flag==0&&GPIF==1)
               {
               flag=1;
               }
           }
       else if (sw==1&&flag==1&&GPIF==1) // Switch Sw=1 (2nd time ) Led blink continue  
           {__delay_ms(5);
           while(sw==1&&flag==1&&GPIF==1)
           {
              
               flag=2;
           }
           }
       else if(sw==0&&flag==2)// 2nd switch sw=0 led blink and sleep
           {
               __delay_ms(5);
               if(flag==2)
               { 
                  flag=0;
                    }
                }
           }
       
    switch (flag)
    {
        case 0: // 1st step if switch sw=0 or sw=1 led blink and sleep 
        {    
         ledloop1();    
          __delay_ms(10);
          GPIF=0;
          SLEEP();
        }
        break;
        case 1: //2nd step if switch sw=0 or sw=1 led blink and sleep 
        {
          ledloop3();
          __delay_ms(10);
           GPIF=0;
           SLEEP();
        }
        break;
        case 2: //3rd step if sw pressed 2nd time the operation run continue until sw=0
            if(sw)
               {
                 l3=1; // Led 3 Continue on until we release sw=0;
                    for (count=0; count<10; count++)
                     {
                         ledloop2();
                         __delay_ms(50);    // delay for flash
                         
                     }
                 }
            break;
        }
    }
}   
   

    
void ledloop1(void)
{
    for(i=0;i<10;i++)
    {
    l1=~l1;
    __delay_ms(60);
    }
}
void ledloop2(void)
{
    while(1)
    {
  for(i=0;i<10;i++)
    {
    l2=~l2;
    __delay_ms(60);
    }
}
}
void ledloop3(void)
{
    for(i=0;i<10;i++)
    {
    l4=~l4;
    __delay_ms(60);
    }
}
 

Attachments

  • led_sleep_pic12f675.c
    4.1 KB · Views: 225
Yes, it will run to whatever code the interrupt vector points to. But, who knows what that even is. There is no specific code to handle it. A crash or hang is most likely...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top