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.

[PIC18F4550] Why Timer0 is not giving correct value

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
I am attaching my mikroC PRO PIC project files. I have to get 1 us timer0 interrupt. RD0 should toggle once every 1 us but it is toggling once every 0.5 us.

The oscillator configuration is like this.

Datasheet page no. 24

Primary oscillator PPLDIV/5 = 20 MHz / 5 = 4 MHz

USB Clock = 96 MHz / 2 = 48 MHz

CPUDIV/2 HSPLL = 96 MHz / 2 = 48 MHz

Primary Oscillator

Code:
OSCCON.SCS0 = 0;
CSCCON.SCS1 = 0;

I have used mikroE Timer calculator to generate Timer0 Interrupt code.
 

Attachments

  • Timer Interrupt rev1.rar
    47 KB · Views: 78

In 1us the uC can do the most 12 instructions, you have more than 12 in your loop, the 0.5us you get is an illusion.
 

This is the asm code of the ISR. The instructions are less than 12.


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Interrupt:
 
;timer_interrupt.c,13 ::                 void Interrupt() {
;timer_interrupt.c,14 ::                 if (TMR0IF_bit){
        BTFSS       TMR0IF_bit+0, BitPos(TMR0IF_bit+0) 
        GOTO        L_Interrupt0
;timer_interrupt.c,15 ::                 TMR0IF_bit = 0;
        BCF         TMR0IF_bit+0, BitPos(TMR0IF_bit+0) 
;timer_interrupt.c,16 ::                 TMR0H = 0xFF;
        MOVLW       255
        MOVWF       TMR0H+0 
;timer_interrupt.c,17 ::                 TMR0L = 0xF4;
        MOVLW       244
        MOVWF       TMR0L+0 
;timer_interrupt.c,19 ::                 LATD.F0 = ~LATD.F0;
        BTG         LATD+0, 0 
;timer_interrupt.c,20 ::                 }
L_Interrupt0:
;timer_interrupt.c,21 ::                 }
L_end_Interrupt:
L__Interrupt5:
        RETFIE      1
; end of _Interrupt

 

Hello milan.rajik, maybe your asm listing (I guess is provided by MikroE) does not include context saving code associated with the interrupt. I'm not sure how this compiler works but mostly all C compilers do context saving prior to executing user's interrupt code.
 

I am measuring output freq using an Oscilloscope. Also I am testing in Proteus. Both giving same result. RD0 is toggling once every 0.5 us.
 

Are you counting BTFSS as 3 and GOTO as 2 cycles. What about all the code in the main function, why dont you count that?
If the output toggles it doesn't mean that the uC is doing what you want it to do. Try 10us.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top