• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

18F46K20 timing error

camerart

Active Member
Thread starter #1
Hi,
Here is a program on an 18F46K20 PIC

There is a LOOP around the START UP LEDS, which should flash ON/OFF 1/sec, the LEDs flash app 1 /4sec.

I've tried to set the timing registers, and notice to get 32MHz x4 is needed, then in T1CON BIT 1 the internal option divides by 4, which seems to defeat the object. The LEDs flash app 1 in 4sec

T1CON = %11111101 '7=16BIT, 6=TIMER1 osc 54=1:8, 3=EN OSC, 2=INT, 1=INT osc/4 0=En TMR1
T2CON = 0
T3CON = 0
OSCCON = %01101110 '7=sleep 654=Osc frequency 8MHz 3=CONFIG1 REG 2=stable 10=INT OSC
OSCTUNE.PLLEN = 1 'OSCx4

Any ideas please?
Camerart.
 

Attachments

#2
T1CON bit 1 should be 0
-> Timer1 clock source is internal oscillator.
But I think the serial and timer1 interrupts influence on Waitms delays
making them longer.

Edit: I did not notice, that only the loop is running, so timer1 and interrupts don't influence on timings.
 
Last edited:

camerart

Active Member
Thread starter #3
T1CON bit 1 should be 0
-> Timer1 clock source is internal oscillator.
But I think the serial and timer1 interrupts influence on Waitms delays
making them longer.

Edit: I did not notice, that only the loop is running, so timer1 and interrupts don't influence on timings.
Hi I,
From the program posted :

T1CON = %11111101 '7=16BIT, 6=TIMER1 osc 54=1:8, 3=EN OSC, 2=INT, 1=INT osc/4 0=En TMR1
C.
 
#4
Are you running from an ICSP ? .... The reason I ask, is that some PIC's are aware of a Reset Condition versus a Cold Start. Some ICSP will make the PIC think it started from a Reset after programming. My PicKit3 does this.

PDF Reference:
http://ww1.microchip.com/downloads/en/DeviceDoc/40001303H.pdf

From the PDF page 27 .... "On device Resets, the output frequency of the internal oscillator is set to the default frequency of 1 MHz."

Also see page 33

I'm not familiar with this particular PIC, but I have seen plenty of others... I usually do something at the very beginning of my code that forces or resets the the PIC to the desired speed.
 

Pommie

Well-Known Member
Most Helpful Member
#5
Why have you posted a file that's nearly 700 lines long? Post a file with only the bits that aren't working.

Have you solved this now?

Mike.
 

camerart

Active Member
Thread starter #6
Why have you posted a file that's nearly 700 lines long? Post a file with only the bits that aren't working.

Have you solved this now?

Mike.
Hi M,
In #1 didn't I write what you are requesting?

I posted the whole program, in case anyone interested in simulating it in Oshonsodft, could do.
C.
 

camerart

Active Member
Thread starter #7
Are you running from an ICSP ? .... The reason I ask, is that some PIC's are aware of a Reset Condition versus a Cold Start. Some ICSP will make the PIC think it started from a Reset after programming. My PicKit3 does this.

PDF Reference:
http://ww1.microchip.com/downloads/en/DeviceDoc/40001303H.pdf

From the PDF page 27 .... "On device Resets, the output frequency of the internal oscillator is set to the default frequency of 1 MHz."

Also see page 33

I'm not familiar with this particular PIC, but I have seen plenty of others... I usually do something at the very beginning of my code that forces or resets the the PIC to the desired speed.
Hi B,
Yes, ICSP.
The PIC seems to start ok, and I looked farther into this, but got a bit confused. I tried erasing with Pickit3 and MPLAB, looked to the code and it was erased, re-programmed and still the same.

I googled, and found my similar question i June.

I'm puzzled about DEFINE CLOCK_FREQUENCY and PLLEN. OSCCON is set to 8MHz then PLLEN is set x4. I changed the CLOCK_FREQUENCY from 32MHz to 8MHz and the LEDs now flash correctly. Is this the correct setting?
C.
 

camerart

Active Member
Thread starter #10
Clock_frequency should be defined as the real frequency.
Maybe the real frequency is 8Mhz, not 32Mhz you try to set.
Hi J,

Yes, I think the CLOCK_FREQUENCY should be set to the OSCCON setting. I presume that to test the PLLEN x4 is working, the OSC output PIN should be set and READ with an oscilloscope?

It seems to be working ok now, thanks to all.
C.
 

Pommie

Well-Known Member
Most Helpful Member
#12
If you are trying to debug something then write the minimum code. Posting 700 lines of code means that no one has time to go through it all. Yes, you posted a snippet but you assume that this is where your error lays - it can be anywhere in your 700 lines. Post code that is less than 50 lines that shows the error and people may be able to help.

Mike.
 

camerart

Active Member
Thread starter #13
If you are trying to debug something then write the minimum code. Posting 700 lines of code means that no one has time to go through it all. Yes, you posted a snippet but you assume that this is where your error lays - it can be anywhere in your 700 lines. Post code that is less than 50 lines that shows the error and people may be able to help.

Mike.
Hi M,
As mentioned, I typed the three offending lines in #1, so that's all you need to read.

I only posted the full program in case anyone wanted it. sorry for the confusion.

C.
 
Last edited:

camerart

Active Member
Thread starter #14
It seems that, the 4xPLL is not working.
Check with an oscilloscope
Hi J,
Instead of trying the oscilloscope, I had a closer look at OSCCON, and saw that PLLEN only works with (HFINTOSC or LFINTOSC) so I reset OSCCON as image:

Now it's working with Define CLOCK_FREQUENCY = 32, which is, as I recall is similar to what you said in June.

C.
 

Attachments

camerart

Active Member
Thread starter #15
If you are trying to debug something then write the minimum code. Posting 700 lines of code means that no one has time to go through it all. Yes, you posted a snippet but you assume that this is where your error lays - it can be anywhere in your 700 lines. Post code that is less than 50 lines that shows the error and people may be able to help.

Mike.
Hi M,
To clarify: I'm not a good enough programmer to be able to write a short test, because I would get very mixed up. In the posted program I commented out as much as I could, while keeping it working, apart from the timing issue.
C.
 

Latest threads

EE World Online Articles

Loading

 
Top