• AVR Freaks

Helpful ReplyHot!PIC18F26K22 timer callback function problem

Author
ndcroos
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2018/10/02 11:14:36
  • Location: 0
  • Status: offline
2020/05/26 05:37:10 (permalink)
0

PIC18F26K22 timer callback function problem

Hello all,

I am developing firmware for a custom PIC18F26K22 and MCC. I have a problem with implementing a callback function.

I want to invoke a callback function every second, using a timer.
I can send and receive text with the EUSART1-module. I use this to get feedback. There isn't a LED on my board.
However, introducing a TMR-module gives problems.
It is as if that introducing the EUSART module causes interferences.
To exclude the EUSART, I implemented a minimal project using only a TMR-module.
I use a buzzer to get feedback if the timer is working.

I think the cause of my problem is the clock. I'm using an external clock, with frequency

Here is my code:

#include "mcc_generated_files/mcc.h"

void timeout_prog_int_handler();

void timeout_prog_int_handler(){
    BUZZ_SetHigh();
    __delay_ms(500);
    BUZZ_SetLow();
    __delay_ms(500);
}

void main(void)
{
    SYSTEM_Initialize();
    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();
    TMR1_StartTimer();
    TMR1_SetInterruptHandler(timeout_prog_int_handler);
    while (1)
    {
        __delay_ms(500);
    }
}
 
These are my settings. I have experimented with the clock sources, without success.
 
https://imgur.com/a/fg7ptex
 
EDIT: I added my project in a zip file.
 
post edited by ndcroos - 2020/05/26 05:54:35
#1
du00000001
Just Some Member
  • Total Posts : 3942
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: PIC18F26K22 timer callback function problem 2020/05/26 05:44:29 (permalink) ☄ Helpfulby ndcroos 2020/05/27 12:18:53
+2 (2)
I could think of a lot of things that might go wrong.
You'd better provide your whole project: clean, zip, add the extension .txt to the zip file and upload!

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#2
ndcroos
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2018/10/02 11:14:36
  • Location: 0
  • Status: offline
Re: PIC18F26K22 timer callback function problem 2020/05/26 05:55:32 (permalink)
0
du00000001
I could think of a lot of things that might go wrong.
You'd better provide your whole project: clean, zip, add the extension .txt to the zip file and upload!


Thanks, I added a zip file.
 
#3
du00000001
Just Some Member
  • Total Posts : 3942
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: PIC18F26K22 timer callback function problem 2020/05/26 07:05:24 (permalink) ☄ Helpfulby ndcroos 2020/05/27 12:19:28
0
I don't see anything too obvious and I do not have the controller to just download.
 
So you'd better start debugging ...
  • Build for debug and download
  • I'd suggest to set the first breakpoint to the line
        if (++CountCallBack >= TMR1_INTERRUPT_TICKER_FACTOR)

    in TMR1_ISR(), to check whether the ISR is called at all.
  • If the ISR is executed, continue with 
        if(TMR1_InterruptHandler)

    in TMR1_CallBack().
  • And so on.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#4
OscarTheGrouch
Super Member
  • Total Posts : 339
  • Reward points : 0
  • Joined: 2010/11/29 18:07:28
  • Location: 0
  • Status: offline
Re: PIC18F26K22 timer callback function problem 2020/05/26 08:52:00 (permalink)
0
Check crystal, maybe no clock? Maybe just see if you can set a I/O pin level.
Have you used this crystal before?
Other than that, as suggested, use debugger.
OtG
#5
ric
Super Member
  • Total Posts : 28324
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F26K22 timer callback function problem 2020/05/26 13:16:34 (permalink) ☄ Helpfulby ndcroos 2020/05/27 12:35:20
+3 (3)
The number one rule for writing interrupt handlers is
Do the minimum possible to service the interrupt, and get out again ASAP.
Do NOT wait around inside the interrupt service.
 
This is NOT the sort of routine you want to be calling once per second!

void timeout_prog_int_handler(){
    BUZZ_SetHigh();
    __delay_ms(500);
    BUZZ_SetLow();
    __delay_ms(500);
}

Even as a crude test that last delay should not be there, as that will make the whole service take a fraction more than a second to execute.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
OscarTheGrouch
Super Member
  • Total Posts : 339
  • Reward points : 0
  • Joined: 2010/11/29 18:07:28
  • Location: 0
  • Status: offline
Re: PIC18F26K22 timer callback function problem 2020/05/26 15:50:55 (permalink)
0
ric
The number one rule for writing interrupt handlers is
Do the minimum possible to service the interrupt, and get out again ASAP.
Do NOT wait around inside the interrupt service.

I feel so ashamed for not seeing this. ;(
#7
du00000001
Just Some Member
  • Total Posts : 3942
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: PIC18F26K22 timer callback function problem 2020/05/26 16:15:44 (permalink)
+1 (1)
Initially missed this as well (as well as the timer period).
It's even worse as the timer interrupt occurs once per ms and the counter is reset post calling the callback function. That way, once the counter has reached the value 1000, funny things will happen.
 
@ndcroos
One way might be to set a callback flag once the counter reaches 1000, and poll this flag in main() as a criterion to call the LED routine.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#8
ric
Super Member
  • Total Posts : 28324
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F26K22 timer callback function problem 2020/05/26 16:38:19 (permalink) ☄ Helpfulby ndcroos 2020/05/27 12:35:12
+2 (2)
Try just
void timeout_prog_int_handler(){
    BUZZ_Toggle();
}

Should give one second on, one second off.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#9
OscarTheGrouch
Super Member
  • Total Posts : 339
  • Reward points : 0
  • Joined: 2010/11/29 18:07:28
  • Location: 0
  • Status: offline
Re: PIC18F26K22 timer callback function problem 2020/05/27 08:15:40 (permalink)
0
du00000001
@ndcroos
One way might be to set a callback flag once the counter reaches 1000, and poll this flag in main() as a criterion to call the LED routine.

This is what I do, use a Bool like 'TMR0Done'
#10
ndcroos
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2018/10/02 11:14:36
  • Location: 0
  • Status: offline
Re: PIC18F26K22 timer callback function problem 2020/05/27 12:36:33 (permalink)
0
@ric thanks, I can hear the buzz now.
Also thanks for the others suggestions, it's very helpful.
#11
Jump to:
© 2020 APG vNext Commercial Version 4.5