View previous topic :: View next topic |
Author |
Message |
Linxroot
Joined: 16 May 2011 Posts: 17
|
PIC18F4620 slow clock problem |
Posted: Wed Jan 24, 2018 6:24 am |
|
|
In my project the PIC18F4620, with 10MHZ crystal result in 312KHz in
while loop. Should not it be: 10MHz crystal div/4 (in the PIC internal divider)= 2.5Mhz?
CCS PCH C Compiler, Version 5.015, xxxx
I did a test code to show the problem:
Code: |
#include <18F4620.h>
#device ADC=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP
#FUSES NOXINST
#FUSES NOFCMEN
#FUSES NOIESO
#FUSES HS
#use delay(crystal=10000000)
void main()
{
while(true)
{
output_toggle(PIN_D5);
}
}
|
The LST file:
Code: |
.................... #device PIC18F4620
....................
.................... #list
....................
.................... #device ADC=16
....................
.................... #FUSES NOWDT //No Watch Dog Timer
.................... #FUSES NOBROWNOUT //No brownout reset
.................... #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
.................... #FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
.................... #FUSES NOFCMEN
.................... #FUSES NOIESO
.................... #FUSES HS
....................
....................
.................... #use delay(crystal=10000000)
....................
....................
.................... void main()
00004: CLRF FF8
00006: BCF FD0.7
00008: MOVF FC1,W
0000A: ANDLW C0
0000C: IORLW 0F
0000E: MOVWF FC1
00010: MOVLW 07
00012: MOVWF FB4
.................... {
....................
.................... while(true)
.................... {
.................... output_toggle(PIN_D5);
00014: BCF F95.5
00016: BTG F8C.5
00018: BRA 0014
.................... }
....................
.................... }
0001A: SLEEP
Configuration Fuses:
Word 1: 0200 HS NOFCMEN NOIESO
Word 2: 1E18 PUT NOBROWNOUT BORV21 NOWDT WDT32768
Word 3: 8700 CCP2C1 PBADEN LPT1OSC MCLR
Word 4: 0081 STVREN NOLVP NOXINST NODEBUG
Word 5: C00F NOPROTECT NOCPB NOCPD
Word 6: E00F NOWRT NOWRTC NOWRTB NOWRTD
Word 7: 400F NOEBTR NOEBTRB
|
The result in scope:
[img]https://drive.google.com/file/d/18mpdubaDCJvmaJp9wP1ky3049EDvTaiY/view?usp=sharing[/img]
Would be an oscillator configuration problem? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9255 Location: Greensville,Ontario
|
|
Posted: Wed Jan 24, 2018 6:48 am |
|
|
what caps are you using for the xtal?
does another xtal work
can you scope the xtal pin to confirm it's oscillating ?
does the PIC run on internal ? |
|
|
Linxroot
Joined: 16 May 2011 Posts: 17
|
|
Posted: Wed Jan 24, 2018 7:06 am |
|
|
temtronic wrote: | what caps are you using for the xtal?
does another xtal work
can you scope the xtal pin to confirm it's oscillating ?
does the PIC run on internal ? |
I'm using 15 pF caps
I used 10Mhz resonator too and the result is the same
I tested the xtal pin in the scope and it run at 10Mhz (very stable)
Quote: | does the PIC run on internal ? |
with these settings, it should be external clock, but really, the resulting frequency seems to be of the internal oscillator. |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 304
|
|
Posted: Wed Jan 24, 2018 7:29 am |
|
|
10MHz crystal
/4 for internal instruction
/4 for number of instructions in loop
/2 for complete cycle (one loop high & one loop low)
10M /4 /4 /2 = 312.5k |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: PIC18F4620 slow clock problem |
Posted: Wed Jan 24, 2018 7:35 am |
|
|
Linxroot wrote: | with 10MHZ crystal result in 312KHz in
while loop. Should not it be: 10MHz crystal div/4 (in the PIC internal divider)= 2.5Mhz? |
In a word, no. While there is only one C statement in the loop, there is more than one instruction: two to do the output and one (braches are two cycle instructions I beleive) to do the loop. I'd expect the output to be 10MHz/4/4 (instructions)/2 (as the loop toggles and one cycle of the output is two loops) = 312.5KHz, which is what you report. Therefore it's working perfectly. Granted its not working as you expect, but it is working as is should.
Quote: | Would be an oscillator configuration problem? |
No, actually its an unrealistic expectation problem.
PS: Cross-posted with Gaugeguy who says the same thing. |
|
|
Linxroot
Joined: 16 May 2011 Posts: 17
|
Re: PIC18F4620 slow clock problem |
Posted: Wed Jan 24, 2018 9:29 am |
|
|
RF_Developer wrote: | Linxroot wrote: | with 10MHZ crystal result in 312KHz in
while loop. Should not it be: 10MHz crystal div/4 (in the PIC internal divider)= 2.5Mhz? |
In a word, no. While there is only one C statement in the loop, there is more than one instruction: two to do the output and one (braches are two cycle instructions I beleive) to do the loop. I'd expect the output to be 10MHz/4/4 (instructions)/2 (as the loop toggles and one cycle of the output is two loops) = 312.5KHz, which is what you report. Therefore it's working perfectly. Granted its not working as you expect, but it is working as is should.
Quote: | Would be an oscillator configuration problem? |
No, actually its an unrealistic expectation problem.
PS: Cross-posted with Gaugeguy who says the same thing. |
Hmmm, it's true! Thank you very much for the quick response. |
|
|
|