Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

[PIC18F4550] Why Timer0 is not giving correct value

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
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


Vbase

Full Member level 6
Joined
Apr 7, 2015
Messages
367
Helped
74
Reputation
148
Reaction score
72
Trophy points
28
Activity points
1,997
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.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
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

 

Yosmany325

Member level 3
Joined
Oct 31, 2012
Messages
62
Helped
8
Reputation
16
Reaction score
8
Trophy points
1,298
Activity points
1,758
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.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
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.
 

Vbase

Full Member level 6
Joined
Apr 7, 2015
Messages
367
Helped
74
Reputation
148
Reaction score
72
Trophy points
28
Activity points
1,997
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top