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.

Need help with PIC16F877A Interrupt Code

Status
Not open for further replies.

richardlaishram

Member level 4
Joined
Jan 6, 2013
Messages
77
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Location
Planet Earth
Activity points
1,804
Code:
#include <htc.h>

#define Count_In RB3

__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF);

void main(void)
{

	Button = 0;
	TRISB3 = 1;
	unsigned int Count = 0;

	while(1)
	{

		if(Button == 1)	// Button is pressed
		{
		while(1)
		{
			while(Button == 1);	// Wait for de-assertion of the button
			__delay_ms(10);	
			Count++;		

			if(Count ==3)
				Count = 1;

			switch(Count)
			{
				case 1:
				{
					WriteStringToLCD("Loop 1");	// Write Message on LCD
					__delay_ms(50);
				}break;
				case 2:
				{
					WriteStringToLCD("Loop 1");	// Write Message on LCD
					__delay_ms(50);
				}break;
			}
		}
	}
}

This is the code in which I'm having problem. When the switch is pressed it goes to case 1 once and then stops. Please tell me a way to make it loop forever and at the same time I can switch to case 2 when the switch is pressed for the second time. :cry:
Thanks in advance.
 

RGB_Interrupt.jpg
This is the Proteus Simulation and here it's auto incrementing the count and looping through the various cases when I put the break; inside the loop. When I flashed it to the microcontroller it's the same and not looping as expected.
 

Button is not defined and it is set to 0 i.e., as output pin. Count_In is defined and set as input (RB3) but never used. Button is used (which is not defined) without debounce delay. There are 2 while(1) loops. If execution enters the inner while(1) loop it never comes out of it.


Edit: You have defined Count_In as input pin. So, use it instead of Button which is not defined.

Replace


Code C - [expand]
1
if(Button == 1)



by


Code C - [expand]
1
if(Count_In == 1)

 
Last edited:

View attachment 96328
This is the Proteus Simulation and here it's auto incrementing the count and looping through the various cases when I put the break; inside the loop. When I flashed it to the microcontroller it's the same and not looping as expected.

you are getting an auto increment B'coz once you get into the while loop (first while after checking the button ==1), you never go back to check it again, and you are stuck in infinite loop. Count++ does its work at every cycle and you find that system is auto incrementing.

Moreover to check the looping between the two states, Its better if you put two different strings on your LCD.r
 
Last edited:

I am leaving everything as it was for your easy understanding and making a small change....


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
#include <htc.h>
 
#define Count_In RB3
 
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF);
 
void main(void)
{
 
    Button = 0;
    TRISB3 = 1;
    unsigned int Count = 0;
 
    while(1)
    {
 
        if(Button == 1) // Button is pressed
        {
        while(1)
        {
            while(Button == 0); // extra added
            while(Button == 1); // Wait for de-assertion of the button
            __delay_ms(10); 
            Count++;        
 
            if(Count ==3)
                Count = 1;
 
            switch(Count)
            {
                case 1:
                {
                    WriteStringToLCD("Loop 1"); // Write Message on LCD
                    __delay_ms(50);
                }break;
                case 2:
                {
                    WriteStringToLCD("Loop 2"); // Write Message on LCD // you can not find the diff if  it was the same statement
                    __delay_ms(50);
                }break;
            }
        }
    }
}

 

Code:
#define Count_In RB3
Button = 0;   // Initialize RB3 as 0
TRISB3 = 1;  // RB3 as Input
unsigned int Count = 0;

Even if it's inside the inner while loop, it will still check if the button is pressed or not as far as I know. Please let me know if I have done anything wrong while initializing the pin.
 

check jayanth's 2nd post again, your answer lies there
 

Code:
#define Count_In RB3
Button = 0;   // Initialize RB3 as 0
TRISB3 = 1;  // RB3 as Input
unsigned int Count = 0;

Even if it's inside the inner while loop, it will still check if the button is pressed or not as far as I know. Please let me know if I have done anything wrong while initializing the pin.

It was wrong you shd not alter the value of a input pin

it shd like

Code C - [expand]
1
2
#define Button RB3
TRISB3 = 1;



What is exactly your problem???
 

What is exactly your problem???

The problem is that I want the case 1 or 2 to loop forever until I press the button. But it is executing the loop once and then stopping with the last statement.

Below is the updated code. Please have a look.

Code:
#include <htc.h>
#include "LCD.h"
#define Count_In RB2

__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF);

void main(void)
{

	InitLCD();
	TRISB2 = 1;
	TRISC = 0;
	unsigned int Count = 0;
	WriteStringToLCD("Initializing...");
	
	while(1)
	{

		if(Count_In == 1)		// If Count button is pressed
		{
			while(Count_In == 0)
			{
				while(Count_In == 0);
				if(Count_In == 1);	// Wait for de-assertion of the button
				__delay_ms(10);	
				Count++;			// Increment Count value

					if(Count == 3)
					Count = 1;		// Reset Count

				switch (Count)
				{
					case 1:
					{
						ClearLCDScreen();
						WriteStringToLCD("Mode 1 ON");	// Write Message on LCD
						__delay_ms(100);
						WriteStringToLCD("Mode 1 OFF");
						__delay_ms(100);
						break;
					}

					case 2:
					{
						ClearLCDScreen();
						WriteStringToLCD("Mode 2 ON");	// Write Message on LCD
						__delay_ms(100);
						WriteStringToLCD("Mode 2 OFF");
						__delay_ms(100);
						break;
					}


				}
			}
		}
	}
}

What I want is that when Mode 1 is selected, it should toggle "Mode 1 ON" and "Mode 1 OFF" repeatedly. But it's executing once.
 

I would say you need to rethink your logic.
A few suggestions : use switch statement outside your if(count_in ==1) and use this block only to update the value of count.
 

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
#include <htc.h>
#include "LCD.h"
#define Count_In RB2
 
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF);
 
bit old_count;
 
void main(void)
{
 
    InitLCD();
    TRISB2 = 1;
    TRISC = 0;
    unsigned int Count = 0;
    WriteStringToLCD("Initializing...");
    
    while(1)
    {
 
        if(Count_In == 1 && old_count != Count_In);
        Count = (count == 1)? 2 : 1;
        
        old_count = Count_In;
 
        switch (Count)
        {
            case 1:
            {
                ClearLCDScreen();
                WriteStringToLCD("Mode 1 ON");  // Write Message on LCD
                __delay_ms(1000);
                WriteStringToLCD("Mode 1 OFF");
                __delay_ms(1000);
                break;
            }
 
            case 2:
            {
                ClearLCDScreen();
                WriteStringToLCD("Mode 2 ON");  // Write Message on LCD
                __delay_ms(1000);
                WriteStringToLCD("Mode 2 OFF");
                __delay_ms(1000);
                break;
            }
 
        }
    }
}

 

I have attached the whole project along with the Proteus Simulation file. Thanks in advance.
 

Attachments

  • Loop_Switch_Mode.rar
    105.6 KB · Views: 79

you know what is the problem ??
it is the semicolon""

The code is working with the Proteus Simulation. But now the problem is when the code is flashed into the Controller. Maybe error is with
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF); :cry:
 

may be its the problem of thread handling so i have updated a completely new version with more quick response use the project file as it was....
 

Attachments

  • Loop_Switch_Mode.7z
    97.1 KB · Views: 82
may be its the problem of thread handling so i have updated a completely new version with more quick response use the project file as it was....
Thanks a ton. Now the only problem is that instead of pressing the button once to start the modes, it's starting automatically. I'll have to work on it to figure that out.
I used the following config. __CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF);
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top