Helpful ReplyHot!Always Ahead By 1 (Interrupt on Change)

Page: 123 > Showing page 1 of 3
Author
Markus999
Starting Member
  • Total Posts : 57
  • Reward points : 0
  • Joined: 2018/02/12 00:31:01
  • Location: 0
  • Status: offline
2018/03/02 00:33:01 (permalink)
0

Always Ahead By 1 (Interrupt on Change)

I'm doing a project wherein I'm counting the number of pulses coming from a pulse generator and display it on the LCD. I'm using the Interrupt on Change method employed in PORTB however, I'm always 1 pulse ahead of the count (e.g. the pulse generator displays 44, my LCD displays 45).

I don't understand why, there's nothing else on the datasheet that I can find so I turned here for further help. 

Please check my code and help me point out what I'm missing. I'm using PIC16f1937 as my MCU.
 

 
int counter = 0;
char buffer[16] = 0;
 
int main(void){
OSCCON = 0x70; //8MHz Freq
OSCTUNE = 0x20; //Minimum Freq
initialize_LCD();
initialize_ports();
enable_interrupts();
while(1){
IOCBP4 = 0;
IOCBN4 = 1;
IOCBF4 = 0;
itoa(buffer, counter, 10);
LCD_string_xy(1,1,"Pulse Count:");
LCD_string_xy(2,1,buffer);
}

return 0;
}
 
void interrupt ISR(void){
if (IOCBF4 == 1){
counter ++;
IOCBF4 = 0;
}
}
 
void initialize_ports(void){
TRISB4 = 1;
TRISD = 0x00;
ANSELB = 0x00;
ANSELD = 0x00;
}
 
void enable_interrupts(void){
GIE = 1;
PEIE = 1;
IOCIE = 1;
}

post edited by Markus999 - 2018/03/02 01:11:41
#1
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 01:14:07 (permalink)
+2 (2)
Try clearing IOCBF before you enable interrupts.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#2
Markus999
Starting Member
  • Total Posts : 57
  • Reward points : 0
  • Joined: 2018/02/12 00:31:01
  • Location: 0
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 01:30:25 (permalink)
0
Hi, thank you for the reply.

I tried clearing up the IOCBF bits before enabling the interrupts yet the problem still occurs.
#3
rodims
Super Member
  • Total Posts : 1353
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 03:34:39 (permalink)
+1 (1)
I'm doing a project wherein I'm counting the number of pulses coming from a pulse generator and display it on the LCD. I'm using the Interrupt on Change method employed in PORTB however, I'm always 1 pulse ahead of the count (e.g. the pulse generator displays 44, my LCD displays 45)

 
Why do you think that this is a problem (in the code) ? Is it clearly specified, how the pulse generator counts (and generates) a pulse ?
You are counting "falling edges". May be the generator has some other logic. Anyway, I think you have to look at the very first count cycle. Apparently you have detected ONE falling edge, while the generator still displays 0 pulses.
For a test also ensure that your pulse generator does not generate pulses BEFORE your PIC is initialized.
Also, what is the "inactive" level of the pulse generator (if that's defined).
a) If you then still receive an interrupt during initialization, then it's likely related to the sequence of initialization.
b) If that's ok, then have a look at the generators sequence for a pulse and check whether your edge detection behaves as expected.
c) As a summary of the above, do you know that the pulse generator works/displays correctly ?
#4
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 03:47:39 (permalink)
+1 (1)
Backtrack a bit. Do you get "1" if there are no pulses at all?

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#5
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 04:12:03 (permalink)
+2 (2)
These three instructions should be BEFORE your superloop, not in it. (in fact, before you enable interrupts).
 IOCBP4 = 0;
 IOCBN4 = 1;
 IOCBF4 = 0;

If anything, randomly clearing IOCBF4 may possibly make you miss an edge.
(Yes, the opposite of the problem you are currently reporting).
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#6
JOKERAS
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2008/12/31 11:54:22
  • Location: 0
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 04:31:00 (permalink)
-4 (4)
.
post edited by JOKERAS - 2018/03/02 05:25:57
#7
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 04:34:20 (permalink)
0
I do not understand what your question is.
I guess the code you just posted is what you did in response to my post#2
In post#6 I added that you should NOT be clearing  IOCBF4 every time around your main loop (in fact once interrupts are enabled, you should ONLY do it inside your interrupt service).
There's also no point continually writing to IOCBP4 and IOCBN4. Just do it once before you enable interrupts.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#8
JOKERAS
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2008/12/31 11:54:22
  • Location: 0
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 04:40:15 (permalink)
-4 (4)
.
post edited by JOKERAS - 2018/03/02 05:26:27
#9
rodims
Super Member
  • Total Posts : 1353
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 04:48:56 (permalink)
0 (2)
It's not the OP speaking ...
#10
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 04:57:15 (permalink)
+2 (2)
rodims
It's not the OP speaking ...

Ahh, my bad.
I guess Jokeras was trying to help, but really just added a layer of confusion.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#11
rodims
Super Member
  • Total Posts : 1353
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 05:49:46 (permalink) ☄ Helpfulby JOKERAS 2018/03/02 06:35:02
+3 (3)
@JokerAs
That's not the way a forum works. If you delete your posts (#7 and #9), it is usually not possible to understand the flow of the thread. You might have chosen to use "strike out" for comments, if that's what you intended to express.
 
If you would like to contribute to the forum, then work on it.
 
But may be your previous post mirrors your attitude:
http://www.microchip.com/forums/m1040338.aspx
 
[edit: ok, I see, there is little hope ... ]
 
 
post edited by rodims - 2018/03/02 05:52:17
#12
JOKERAS
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2008/12/31 11:54:22
  • Location: 0
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 06:43:11 (permalink)
-3 (3)
I will not write again.
You can be comfortable @rodims
#13
JOKERAS
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2008/12/31 11:54:22
  • Location: 0
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 06:45:30 (permalink)
-4 (6)
This Forum is not a place for cooperation,
It's a place where the Tipler like you has time to have fun.
#14
rodims
Super Member
  • Total Posts : 1353
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 06:48:44 (permalink)
0
I just tried to give you feedback.  I'm comfortable.
It all depends on what you want to achieve, e.g. get help, help others, or both
Since you joined the forum already in 2008, I assumed some experience with the forum.
#15
JOKERAS
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2008/12/31 11:54:22
  • Location: 0
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 06:54:12 (permalink)
-2 (2)
rodims, I am writing from Turkey and do not speak English.
I am writing and reading with Google Translate.
But as far as I understand you dial-in, you make fun of the people on your side.
I do not want to hurt you, rodims.
#16
1and0
Access is Denied
  • Total Posts : 8396
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 07:18:50 (permalink)
+1 (1)
JOKERAS
rodims, I am writing from Turkey and do not speak English.
I am writing and reading with Google Translate.

... then be more humble: ask a question, appreciate feedback, really listen, accept setbacks, and learn.
 

But as far as I understand you dial-in, you make fun of the people on your side.
I do not want to hurt you, rodims.

I don't see anyone is making fun of anybody here.  Why you want to hurt someone who is trying to help you?
#17
rodims
Super Member
  • Total Posts : 1353
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 07:43:41 (permalink)
+2 (2)
I can only imagine that Google Translate is part of the problem.
If Google translated anything wrong to you, then I'm sorry for that.
I conclude this least not last, because "akşamcı" might want to hurt me, though I'm also not a native English speaker. 
That's the problem with misunderstandings, you never know.
I suggest to release the thread back to OP / Markus999
 
 
#18
crevicedweller
New Member
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2010/12/08 16:17:39
  • Location: 0
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 08:12:03 (permalink)
0
I had essentially the same problem on a current project.
i just subtract 1 immediately after enabling the interrupt.
i found that in my case the input is high most of the time, so when the port is initialized and the interrupt enabled, the port reports a change from its initial assumption of a low signal to a high signal.
#19
1and0
Access is Denied
  • Total Posts : 8396
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Always Ahead By 1 (Interrupt on Change) 2018/03/02 08:25:18 (permalink)
0
Sound like a wrong sequence of interrupts initialization, try this order:
  // ... snipped
  IOCBF4 = 0;
  IOCIE = 1;
  PEIE = 1;
  GIE = 1;

Always enable GIE after everything is configured.
 
#20
Page: 123 > Showing page 1 of 3
Jump to:
© 2018 APG vNext Commercial Version 4.5