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] Interrupt Problem With PIC16F676

Status
Not open for further replies.

Manjuhb

Newbie level 6
Joined
Dec 2, 2015
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
149
Hi

I'am very new to the PIC programming. I have written a program to give 20 min of delay when an switch is pressed. I have connected an switch with active high signal to the INT pin of the PIC16F676 pin and I have connected an buzzer to RC1 port, connected a transistor to RC5 port to turn on the relay. Intention of this program is when switch is pressed relay should turn on till 20 min then it should go off by turning on the buzzer for 10SEC.

Here my problem is even though I did not press the switch the relay is turning on ie RC5 is going high. When I turn on the PCB after one min relay will turning on automatically ie External Interrupt is triggering I guess. Please help me with this problem.

Here is the code written by me...:). Plz don't mind if code has to much unnecessary instruction.


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
#include <16F676.h>
#use delay (clock=4000000)
#fuses INTRC,NOWDT,PUT,NOMCLR,PROTECT,CPD,BROWNOUT
#BIT RA2 = 0x05.2
#BIT RC1 = 0x07.1
#BIT RC5 = 0x07.5
#BIT GIE = 0x0B.7
#BIT TMR1IF = 0x0C.0
#BIT INTEDG = 0x81.6
#BIT INTE = 0x0B.4
#BIT PEIE= 0x0B.6
#BIT INTF= 0x0B.1
#BIT TMR1IE=0x8C.0
#BIT TMR1ON=0x10.0
#BYTE T1CON =0x10
long i; // 16 bit
void init(VOID);
#INT_TIMER1
void timer1_isr()
{
i++;
IF (i == 2400)
{
setup_timer_1 (T1_DISABLED);
TMR1ON = 0;
RC5 = 0;
RC1 = 1;
delay_ms (10000);
RC1 = 0;
TMR1IF = 0;
INTF = 0;
clear_interrupt (INT_EXT);
enable_interrupts(INT_EXT);
INTE = 1;
}
}
#INT_EXT
void EXT_isr()
{
i = 0;
setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);
T1CON = 0x30;
TMR1ON = 1;
INTF = 0;
clear_interrupt (INT_EXT);
INTE = 0;
Disable_interrupts(INT_EXT);
RC5 = 1;
set_timer1 (0);
}
void main(VOID)
{
delay_ms (1000); //power up delay
init ();
clear_interrupt (INT_EXT);
clear_interrupt(int_timer1);
EXT_INT_EDGE(L_TO_H); 
INTEDG = 1;
enable_interrupts(global);
enable_interrupts(INT_EXT);
enable_interrupts (INT_TIMER1);
GIE = 1;
PEIE = 1;
TMR1IE = 1;
INTE = 1;
setup_timer_1 (T1_DISABLED);
 
WHILE (1)
{
 
}
}
 
void init(VOID)
{
RC5 = 0;
RC1 = 0;
INTF = 0;
TMR1IF = 0;
set_tris_a (0x04);
set_tris_c (0x00);
}

 

Which compiler are you using?

Ignoring possible electrical problems, I'm not sure you are even defining an ISR at all. Maybe it's because I'm not familiar with that particular dialect of C but is line 19 actually declaring an ISR and if it is, do you really want a long delay in it?

Some indentation and meaningful names would make it easier to read.

Brian.
 

The data sheet for that device shows that it has a single ISR vector, but I'm guessing from the "#INT_EXT" and "#INT_TIMER1" that these are marking the following function as the ISR. Normally for these smaller chips you have a single ISR and in that you need to work out which interrupt source has triggered it.
Also I would be surprised if the compiler did not already define the various SFR names so you do not need to define your own and certainly not have them hard-coded to memory offsets.
Inside the 'timer1_isr' function you are incrementing the variable "i" but the variable is not declared as volatile. You are probably getting away with this as there is nothing in your main loop, but I would suggest that you get into the habit of declaring all variables that you modify within an ISR as volatile to save you days of debugging later on.
My guess is that you are just starting out with these devices so I would suggest that you don't start by using interrupts just yet - get to know the device and get basic things (such as the 'flash a LED once a second) going first.
I don't know what the 'setup_timer_1' function does but I would assume that it sets up the T1CON register with the configuration settings passed as a parameter. However in the 'EXT_isr' function you are then over-writing the T1CON register with your own settings (which in this case looks like you are setting it to the same as the function). That does not make sense especially when you have limited FLASH memory.
Similar comments probably apply to the control of the interrupt registers and bits.
Susan
 

Hi All,

Thanks for your valuable reply. I will try to do the coding without using ISR for external interrupt. I will post the latest status. Thank you all
 

Hi,

Here I'm using CCS compiler
 

Under Tools>Device Editor under registers segment. There is a "make include file" option. You can use it to generate the registers file, so you wont need to define SFR names.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top