Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Need help for pic12f675

Status
Not open for further replies.

uzzalpic

Junior Member level 2
Joined
Mar 24, 2014
Messages
24
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Activity points
189
Hi, I am new in pic12f675. I want to make simple circuit but I cant understand it. It will be when I press switch yellow led will blinking for 10 sec then green will glow and when switch unpressed yellow led blinking for 3min after that green le Untitled-1.jpgd will glow. please any one help me.
 

Hi,

Your description is not clear. Especially what happens when you press/release the key within the 10s/3min.

Show us your timing diagram.
Show us your flow chart.

Klaus
 

If switch on yellow led blinking for 10 sec then yellow led will of and green led will glow. Green led glow continue when I restart circuit. If switch is release yellow led blinking 3 min after 3 min yellow led will of and green led will glow
 

The circuit should work. But you need to write a program.
 

What is the problem ? You don't have a Compiler ? Use MPLAB X and XC8 Free version. Circuit is correct.
 

Try this code. The code is developed using mikroC pro compiler.

Code:
sbit sw at GP3_bit;
sbit led_g at GP4_bit;
sbit led_y at GP5_bit;


void main() {
GPIO = 0x0;
TRISIO = 0x8;
do     {
if (sw == 0)  {
led_g = 0;
led_y = 1;
delay_ms(10000);  //  10 second delay
led_y = 0;
led_g = 1;
             }
while (sw == 0);
led_y = 0;
led_g = 1;
delay_ms(3000);  //  3 second delay
led_g = 0;
led_y = 1;
while(sw==1);
    } while (1);

}
 

I used mikroc compiler but here is the code problem with me. i dont know which code use for this circuit.
 

It will be when I press switch yellow led will blinking for 10 sec

Yes, but at what frequency the LED blinks ? 1 Hz, 2 Hz ?
 

I used mikroc compiler but here is the code problem with me. i dont know which code use for this circuit.

It is not clear what is your problem and what do you want. As per your schematic I have offered a piece of code. You try it and intimate what happens. Since you have used mikroC compiler, there should not be any confusion.
 

Here is my code. Everything is ok here but yellow led not blinking. Which code i can add with my code for blinking yellow led.

Code:
#define Delay_SWITCH GP3_bit
#define LED_Green GP4_bit
#define LED_Yellow GP5_bit
#define PRESSED 0
#define UNPRESSED 1


void main() 
{
CMCON = 0x07;
ANSEL = 0b000000;
GPIO = 0b001001;
TRISIO = 0b001001;
  if (Delay_SWITCH == PRESSED)
  {
   LED_Yellow = 1;
   LED_Green = 0;
   delay_ms(10000);   // 10 sec delay
   LED_Yellow = 0;
   LED_Green = 1;
  }
  else if (Delay_SWITCH == UNPRESSED)
  {
   LED_Yellow = 1;
   LED_Green = 0;
   delay_ms(180000); // 3 min delay
   LED_Yellow = 0;
   LED_Green = 1;
  }
  while(0);
 



}
 

It seems your code will not run indefinitely. Why have you put "while(0)" at the end? Where is the infinite loop? However it will not deter the yellow LED to glow. Does Green LED glow ? Have you tried my code? What is the result?
 

Instead of a 3 min delay, have a short blink sequence and repeat it in a for loop to get 3 min.
 

Hi,

can you give me that code .
It surely is no challenge for you:
Psuedocode: run 300 times in a loop: LED ON - wait 0.5s - LED OFF - wait 0.5s

Klaus
 

can you give me that code .

Why are you sticking to get ready made code? Had the above code been done by you, it would not be difficult for you to do the code for blinking the LED yourself. Your first problem is not glowing the yellow LED. Sort out this problem and proceed further to blink.
 

The problem with your code is you have not configured the oscillator. As you are using Internal Oscillator you have to configure OSCCON and OSCTUNE regsiters.

It seems the device doesn't have internal oscillator. So, you have to change the LED pins and use external crystal.

Edit 2:

Yes, It has Internal Oscillator 4 MHz.


Here is the code and project which I made.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define ON  1
#define OFF 0
 
sbit SW at GP3_bit;
 
sbit Yellow_Led at GP5_bit;
sbit Green_Led at GP4_bit;
 
unsigned char ten_seconds_delay_count = 20;
unsigned int three_minutes_delay_count = 360;
unsigned int counter = 0, blink_counter = 0;
char myFlags = 0;
 
sbit led_flag at myFlags.B0;
sbit button_state at myFlags.B1;
 
//Timer1
//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms
//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x31;
    TMR1IF_bit = 0;
    TMR1H = 0x0B;
    TMR1L = 0xDC;
    TMR1IE_bit = 1;    
}
 
void Interrupt() {
    if((TMR1IE_bit) && (TMR1IF_bit)) { 
        //Enter your code here        
        if(led_flag == 0) {
           if(++blink_counter == 2) {
                blink_counter = 0;
                Yellow_Led = ~Yellow_Led;
           }
        
           if(++counter == ten_seconds_delay_count) {
                counter = 0;
                Green_Led = ON;
                TMR1ON_bit = 0;                
           }
        }
        else if(led_flag == 1) {
           if(++blink_counter == 2) {
                blink_counter = 0;
                Yellow_Led = ~Yellow_Led;
           }
           
           if(++counter == three_minutes_delay_count) {
                counter = 0;
                Green_Led = ON;
                TMR1ON_bit = 0;                
           }
        }
        
        TMR1IF_bit = 0;
        TMR1H = 0x0B;
        TMR1L = 0xDC;        
    }
}
 
void Turn_Off_Leds_And_Reset_Counters() {
    Yellow_Led = OFF;
    Green_Led = OFF;
    counter = 0;
    blink_counter = 0;
}
 
void Enable_Interrupts() {
    INTCON = 0xC0;
}
 
void main() {
    
    OSCCAL = 0x00;
    
    CMCON = 0x07;
    ANSEL = 0x00;
    TRISIO = 0x08;
    GPIO = 0x00;
    
    InitTimer1();
    
    button_state = 1;
    
    while(1) {
    
           if((!SW) && (!button_state)){
                Turn_Off_Leds_And_Reset_Counters();
                led_flag = 0;
                Enable_Interrupts();
                button_state = 1;
           }
           else if((SW) && (button_state)) {
                Turn_Off_Leds_And_Reset_Counters();
                led_flag = 1;
                Enable_Interrupts();
                button_state = 0;
           }
    }
}

 

Attachments

  • PIC12F675 Based LED and Switch Project.rar
    19.3 KB · Views: 111
Last edited:

Here is my project simulation and code. plez check it.
 

Attachments

  • 12f675.rar
    24.3 KB · Views: 99

The problem with your code is you have not configured the oscillator. As you are using Internal Oscillator you have to configure OSCCON and OSCTUNE regsiters.

It seems the device doesn't have internal oscillator. So, you have to change the LED pins and use external crystal.
8-pin processor without internal oscillator, good joke.

RC oscillator is selected in CONFIG fuses, OSCON default values should at least work.
 

Try this code. The code is developed using mikroC pro compiler.

Code:
sbit sw at GP3_bit;
sbit led_g at GP4_bit;
sbit led_y at GP5_bit;


void main() {
GPIO = 0x0;
TRISIO = 0x8;
do     {
if (sw == 0)  {
led_g = 0;
led_y = 1;
delay_ms(10000);  //  10 second delay
led_y = 0;
led_g = 1;
             }
while (sw == 0);
led_y = 0;
led_g = 1;
delay_ms(3000);  //  3 second delay
led_g = 0;
led_y = 1;
while(sw==1);
    } while (1);

}

Code:
sbit sw at GP3_bit;
sbit led_g at GP4_bit;
sbit led_y at GP5_bit;


void main() {
GPIO = 0x0;
TRISIO = 0x8;
do     {
if (sw == 0)  {
led_g = 0;
led_y = 1;
delay_ms(10000);  //  10 second delay
led_y = 0;
led_g = 1;
             }
while (sw == 0);
led_y = 1;
led_g = 0;
delay_ms(180000);  //  3 min delay
led_y = 0;
led_g = 1;

while(sw==1);
    } while (1);
}

Now I am using your code and some modified. but yellow led still not blinking. which code can i add for blinking led.
 

Yes, I searched for OSCON but didn't find it in the Compiler and hence thought there is no Internal Oscillator. Later found that INTRC Oscillator is set in config fuses and it has just 4 MHz INTRC Oscillator.

There was a hidden bug. Fixed it. In the project settings.

New code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#define ON  1
#define OFF 0
 
sbit SW at GP3_bit;
 
sbit Yellow_Led at GP5_bit;
sbit Green_Led at GP4_bit;
 
unsigned char ten_seconds_delay_count = 20;
unsigned int three_minutes_delay_count = 360;
unsigned int counter = 0, blink_counter = 0;
unsigned char myFlags = 0;
 
sbit led_flag at myFlags.B0;
sbit button_state at myFlags.B1;
 
//Timer1
//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms
//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x31;
    TMR1IF_bit = 0;
    TMR1H = 0x0B;
    TMR1L = 0xDC;    
    INTCON = 0xC0;    
}
 
void Interrupt() {
    if((TMR1IE_bit) && (TMR1IF_bit)) { 
        //Enter your code here        
        if(led_flag == 0) {
           if(++blink_counter == 2) {
                blink_counter = 0;
                Yellow_Led = ~Yellow_Led;
           }
        
           if(++counter == ten_seconds_delay_count) {
                counter = 0;
                Green_Led = ON;
                TMR1ON_bit = 0;                
           }
        }
        else if(led_flag == 1) {
           if(++blink_counter == 2) {
                blink_counter = 0;
                Yellow_Led = ~Yellow_Led;
           }
           
           if(++counter == three_minutes_delay_count) {
                counter = 0;
                Green_Led = ON;
                TMR1ON_bit = 0;                
           }
        }
        
        TMR1IF_bit = 0;
        TMR1H = 0x0B;
        TMR1L = 0xDC;        
    }
}
 
void Turn_Off_Leds_And_Reset_Counters() {
    Yellow_Led = OFF;
    Green_Led = OFF;
    counter = 0;
    blink_counter = 0;
}
 
void Enable_Timer1_Interrupts() {
    TMR1IE_bit = 1;
}
 
void main() {
       
    CMCON = 0x07;
    ANSEL = 0x00;
    TRISIO = 0x08;
    GPIO = 0x00;
    
    InitTimer1();
    
    button_state = 1;
    
    while(1) {
    
           if((!SW) && (!button_state)){
               Delay_ms(50);
               if((!SW) && (!button_state)){
                  Turn_Off_Leds_And_Reset_Counters();
                  led_flag = 0;
                  Enable_Timer1_Interrupts();
                  button_state = 1;
               }
           }
           else if((SW) && (button_state)) {
                  Delay_ms(50);
                  if((SW) && (button_state)){
                      Turn_Off_Leds_And_Reset_Counters();
                      led_flag = 1;
                      Enable_Timer1_Interrupts();
                      button_state = 0;
                  }
           }
    }
}

 

Attachments

  • PIC12F675 Based LED and Switch Project RevA.rar
    19.6 KB · Views: 107
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top