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.
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....

One more thing, if I go with this code, I won't be able to increase the number of modes, can you please throw me some light on how to do this? Thanks in advance.
 

You can have more number of modes and states by editing this line...


Code C - [expand]
1
2
3
mode = (mode == 10)? 1 : mode++;
 
            state = (state == 10)? 1 : state++;



instead of the existing lines, I think you will understand....

- - - Updated - - -

at the same time you can monitor some other things in the for loop, that executes forever...........
 
You can have more number of modes and states by editing this line...


Code C - [expand]
1
2
3
mode = (mode == 10)? 1 : mode++;
 
            state = (state == 10)? 1 : state++;



instead of the existing lines, I think you will understand....

- - - Updated - - -

at the same time you can monitor some other things in the for loop, that executes forever...........

Thanks a lot, Now I can understand it. I was not much handy with this conditional loop before.
 

We are replaced the unwanted delay lines by useful lines..

The common method is using switch case in the called function, you can try that also....
 

You can have more number of modes and states by editing this line...


Code C - [expand]
1
2
3
mode = (mode == 10)? 1 : mode++;
 
            state = (state == 10)? 1 : state++;



instead of the existing lines, I think you will understand....

- - - Updated - - -

at the same time you can monitor some other things in the for loop, that executes forever...........

I got the functional logic, but when I edit the codes according to the above, it's not working. I have attached the zip file, please help :-(

- - - Updated - - -

We are replaced the unwanted delay lines by useful lines..

The common method is using switch case in the called function, you can try that also....
If the multi mode function is working in the
mode = (mode == 10)? 1 : mode++;
, then I'll update with the switch case in the called function.
 

Attachments

  • Loop_Mode.rar
    85.9 KB · Views: 73

you shd not use while and delay functions that will hold the program in some point and will start the wastage of cpu time....


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
void main(void)
{
 
    InitLCD();
    TRISB2 = 1;
    unsigned int Count = 0;
    char state = 2;
    Count_In = 0;
    WriteStringToLCD("Select Mode!!");
    
    while(1)
    {
        for (i = 0; i < 20000; i++) //delay size
        {
        if( old_count != Count_In && Count_In == 1)
            {
            Count++;
            if(Count == 3)
                Count = 1;
            }    
        old_count = CountIn;
        }
        state = (state == 2)? 1 : 2;
        mode_select(Count,state);
    }
}
 
void mode_select(char Count,char state)
{
    if(Count == 1 && state == 1)
    {
        WriteStringToLCD("Mode 1 ON");
    }
 
    else if(Count == 1 && state == 2)
    {
        WriteStringToLCD("Mode 1 OFF");
    }
 
    else if(Count == 2 && state == 1)
    {
        WriteStringToLCD("Mode 2 OFF");
    }
 
    else if(Count == 2 && state == 2)
    {
        WriteStringToLCD("Mode 2 ON");
    }
}

 

If I use the if statement to check if the button is pressed or not, it's not working proper, but if I use the while loop to check if the button is pressed or not along with a few milliseconds delay, it's working perfectly. The only problem with while loop is that it's not looping forever, though it's looping properly with if statement.
Code:
if( old_count != Count_In && Count_In == 1)

while( old_count != Count_In && Count_In == 1);
__delay_ms(10);
 

I cant get what you want....

you know what we are writing is gong to execute so plan and then write...
 

I cant get what you want....
I have flashed the hex file into the microcontroller. To detect if a switch is pressed or not an if condition if( old_count != Count_In && Count_In == 1) is used. When I pressed the button, it's not working sometimes, i.e I'm not getting a proper increment every time the button is pressed.
But if I use the while loop while( old_count != Count_In && Count_In == 1); with 10 millisecs delay, I'm able to get the proper result every time, the only problem is that by using while loop the code is not looping forever.
 

by writing the if condition i tried to capture the buttons positive edge but if there is any bounce in the key pad it will swap some many times and will change the mode many times..., If you use the while it will remain in the loop till the button is pressed or a tleast for 10 ms thats why its working for you all the time......

Code C - [expand]
1
2
3
4
5
6
7
8
9
for (i = 0; i < 20000; i++) //delay size
        {
        if( old_count != Count_In && Count_In == 1)
        {
        Count = (Count == 2)? 1 : 2;
        __delay_ms(10);
        } 
        old_count = Count_In;
        }



you can use a background timer to completely remove delay lines...
 

you can use a background timer to completely remove delay lines...
Code:
void interrupt ISR(void)
{
	if(INTF)
	{
		Count++;
		if(Count == 5)
			Count = 1;

		INTF = 0;
	}
}
Finally I used the inbuilt hardware Interrupt pin, and it's working properly. :idea: Thanks for everything.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top