|
|
View previous topic :: View next topic |
Author |
Message |
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
My second perfect is imperfect !! SOLVED !! |
Posted: Fri Sep 07, 2012 8:44 pm |
|
|
I need to make precise timing of a second, with millisecond accuracy.
For this purpose, a PIC18F26K80, I use the internal oscillator, placed at 16 Mhz and with PLL enabled, and I was really close to doing.
This is the configuration I use:
Code: |
#include <18F26K80.h>
#device adc=12
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES PLLEN //PLL habilitado
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES WDT_NOSLEEP //Watch Dog Timer, disabled during SLEEP
#use delay(int=64000000, RESTART_WDT)
#use rs232(baud=460800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1,restart_wdt)
|
It happens every 10 or 12 seconds (measured on an oscilloscope) TIMING delayed or advanced to 8 microseconds, and that is complicating my life.
Try now to try to explain why this happens to me, put a 16 MHz external crystal with PLL enabled should give me the same time as the internal oscillator, and if all were walking right, correct the defect in the timing explain before. ..
What happens to me is not working as it should, times with or without PLL enabled are 12 seconds or eight seconds in any case, instead of my precious second you need to have ...
This is one of the settings I use:
Code: |
#include <18F26K80.h>
#device adc=12
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
//#FUSES HSH //High speed Osc, high power 16MHz-25MHz
#FUSES HSM //High speed Osc, medium power 4MHz-16MHz
//#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES PLLEN //PLL habilitado
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES WDT_NOSLEEP //Watch Dog Timer, disabled during SLEEP
//#use delay(crystal=64Mhz, RESTART_WDT) //int=64000000
#use delay(oscillator=16Mhz, clock=64Mhz, RESTART_WDT)
#use rs232(baud=460800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1,restart_wdt)
|
The other problem is that the transmitted serial 4-fold speed, even with PLL activated, whereas the internal oscillator option, perfectly transmits at the correct speed.
Can anyone help me fix it??
I read several threads and found none who would pass a similar problem.
Also in other PIC18 and use the PLL with external crystal, and never had a problem like this.
This PIC has many more configuration options, and quite confused me.
Thanks in advance.
PD: Sorry for my bad english. _________________ MGLSOFT
Last edited by mglsoft on Thu Sep 13, 2012 8:49 pm; edited 1 time in total |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Sep 08, 2012 3:52 am |
|
|
You don't show how you generate timing (at output pin?) in your application. A well considered usage of timer with output capture feature can be assumed to achieve clock cycle accurate timing. |
|
|
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
|
Posted: Sat Sep 08, 2012 5:27 am |
|
|
The precision of time is given by interruption of Timer4 that sets a flag running code inside the main () function.
Here the code of the interruption:
Code: |
////////////////////////////////////////////////////////////////////////////////////
//
// InterrupciĆ³n por overflow del Timer 4
//
////////////////////////////////////////////////////////////////////////////////////
#INT_TIMER4
void isr_timer4(void)
{
//ms++; //este timer va corriendo y se incrementa en cada milisegundo
if(++ms == 1000) // Pulso cada 1000 ms en led
{
ms=0;
Flag_Timer_actual = TRUE;
}
}
|
And inside the main () function is the use of this flag and its replacement:
Code: |
void main()
{
<<<< PIC initialization code >>>>
do
{
restart_wdt(); // reset del watchdog
if(flagHayDatos==1)
{ // Detecto que ya hay datos de flancos ...
<<<< Fixed code >>>>
flagHayDatos=0; // Indico que ya han sido procesados los datos.
}
// Lee el valor a convertir en entrada AN0
value = read_adc(ADC_START_AND_READ);
// Escribe el valor del conversor A/D en salida PWM
set_pwm1_duty(500);
if (Flag_Timer_actual)// Pulso cada 1000 ms para hacer los calculos
{
Flag_Timer_actual = FALSE;
Output_Low(LED); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<para avisar que inicio cuentas
<<<<here code calculations >>>>
Output_High(LED); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< aqui termino cuentas
}
}
while (TRUE);
}
|
Time measurements from the oscilloscope is done by measuring the LED pin is handled in main ().
It measures the time between the rising edges to determine the frequency of execution and timing accuracy.
Although I do not show the code in main (), that is always executed the same number of instructions, on the other hand, I think it was refined enough to be sure it's not my problem here.
I repeat that my intention is to run the PIC through external crystal and not get running at the same time as the internal crystal, at least to verify that this is not my problem. _________________ MGLSOFT |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Sep 08, 2012 6:23 am |
|
|
In a short, interrupt flag will be set cycle accurate, interrupt function will be executed with some jitter due to varying interrupt latency, LED output will be set in main with considerable more jitter. So what you observe is just normal operation.
There are some options to resync the event in main() by reading the actual timer value and perform a variable delay. But it's not so easy to do it exactly. IMHO you should have understand the timing problems involved with interrupt processing before to expect succes. |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Sat Sep 08, 2012 8:10 am |
|
|
You mean the internal oscillator without a crystal? "Precise timing of a second, with millisecond accuracy" implies precision to one part in a thousand. I believe Microchip promises accuracy to 2% at 25C, and much worse over the full temperature range. See Table 31-8.
So be sure what you need, and whether you'll always get it. |
|
|
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
Crystal 16Mhz and Time accuracy ... |
Posted: Sat Sep 08, 2012 9:01 am |
|
|
Thank you. I understand that there may be such differences.
Also I want to check, putting the external crystal and noting whether the difference persists or disappears.
My problem is that I can not run the external crystal as it should, and that is where I need your help.
Can you help with that? _________________ MGLSOFT |
|
|
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
|
Posted: Mon Sep 10, 2012 10:26 am |
|
|
Any Help Me ?? _________________ MGLSOFT |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Sep 11, 2012 8:03 am |
|
|
Are you having problems getting your PIC to run from an external crystal? If so you need to look closely at the PIC data sheet Oscillator section and at the specs of your crystal. Also make sure your crystal wiring is short and direct. What sort of circuit board are you using? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
|
Posted: Tue Sep 11, 2012 12:08 pm |
|
|
The external oscillator section of the PIC18F26K80 data sheet is only 2 pages, and I read it about 20 times, without finding this answer.
Use long Mhz crystal 10 with the PLL PIC18F activated, making the work internally PIC to 40 Mhz.
This PIC start in reverse, first using the internal oscillator (which is excellent!) And now I want to use the external oscillator, like the inner set, but does not work for nothing.
Just finished measuring with the oscilloscope and a 10 MHz crystal type in another circuit breadboard is indicating a frequency of 10 Mhz correctly, but in my case the 16 Mhz I have at the PIC indicates no oscillation.
You change it twice (I have only 4 glasses, and I keep one without touching it) and to no avail, still the same.
In my poor conclusion in PIC accepts no settings that I do in the program, and does not enable the OSC2 pin and will not even start to oscillate.
Why I asked to review whether this well as I set it up to see if I'm doing something wrong, because at this point I have no objectivity to it alone. _________________ MGLSOFT |
|
|
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
|
Posted: Thu Sep 13, 2012 8:44 pm |
|
|
For now I have half result ...
I tried engrave configuration bytes using this option:
Code: | # byte _CONFIG1L = 0x300000
# byte _CONFIG1H = 0x300001
|
and in the main:
Code: | _CONFIG1H = 0b00011101;
_CONFIG1H = 0b01010010; |
And nothing! .. was the same ...
Finally I tried changing this subtlety, and walked! (at least in the pcb circuit, because the breadboard does not know who should start ...)
Above code:
Code: |
# use delay (oscillator = 16Mhz, clock = 64MHz, RESTART_WDT)
|
Current Code:
Code: |
# use delay (xtal = 16Mhz, clock = 64MHz, RESTART_WDT)
|
Apparently the call to # use_delay () makes changes to the configuration bits, no matter what you put first ...
At least I can not find documentation to explain why these changes occur in bits of configuracioĆ³n.
Someone knew that this works well??
Topic solved, at least for me, May it be someone else solve a similar problem.
Thank you very much to all who tried to help me solve. _________________ MGLSOFT |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Sep 13, 2012 11:21 pm |
|
|
Your reports aren't very clear.
Do you say, 10 MHz crystal works and 16 MHz doesn't?
Or clock switching from internal RC to HSPLL doesn't work? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Sep 14, 2012 4:53 am |
|
|
#use delay, makes no changes to configuration bits, if you use it's original syntax. So:
#use delay(CLOCK=40MHz)
Changes nothing. It assumes _you_ are correctly setting the fuses/PLL to generate this clock.
Where it makes changes, is if you tell it you want it to. So:
#use delay(INT=8MHz)
will automatically try to set the fuses for an internal oscillator at 8MHz.
#use delay(crystal=10MHz, clock=40MHz)
will try to enable a crystal oscillator, and a 4* PLL
This _is_ in the manual:
'type=speed', with 'type' equal to oscillator, osc, crystal, xtal, internal, int, or rc "The compiler will automatically set the oscillator configuration bits based upon your defined type".
They couldn't be much clearer.....
Unfortunately, with the sheer complexity of the possible settings on a lot of chips, this often doesn't work right. Also people habitually set confligting values in the fuses. Though the clock statement will _add_ the fuses needed for the specified option, it won't turn off other fuses that conflict....
In general, I feel (especially with recent chips), it is much safer to sit down and work out the fuse pattern needed yourself, set these, and use the 'clock=' option, rather than one that specifies an oscillator type.
Best Wishes |
|
|
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
|
Posted: Fri Sep 14, 2012 5:26 am |
|
|
Thanks for answering.
Equally confusing clarification.
# use_delay () changes or does not change the configuration bits?
You start saying it does not change if you use only the option clock = xx
But then goes on to say that if you change if we use other options.
In my case, I came to this state because I started using the internal oscillator and just now trying to xtal.
As I have been programming in assembler, I usually set starting configuration bits in a single block.
That also do at CCS. I never had this problem before.
So I try to let my best explained what happens when driving the speed change in this way.
I'm a big advocate of this compiler, I have solved many things since I use, but it also makes me critical when something does not work as I understand it does.
I think we lack information about these functions and scope. Nothing more. _________________ MGLSOFT |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Fri Sep 14, 2012 5:51 am |
|
|
Ok...I've been using the compiler since 1997 and though retired(haha) from the realworld,like to keep reasonably current..so..
One of the 'problems' with these newer PICs is the huge range of options they have, so documenting them in perfect detail is a challenge itself.I often have to read,reread and read again the datasheet to understand what's going on ,though nowadays I've stuck with only 2 versions of PICs to better get familiar and comfortable with them.
To better understand what Ttelmah has said about the
#use delay(options...)
you have to think like the CCS compiler.'Play computer'.
From what I gather, when the compiler 'parses' the #USE delay(.....) line of code, it will set the appropriate bits in various 'option' registers of the PIC as well as embed in the 'delay' function code the loop counter based on the 'clock' option. It's smart enough to only cut code/set bits for the options that you supply.Considering the number of options(clock..internal,external,HS,PLL,etc., speed...10MHz,40,2,etc.) it'd take a few pages to explain all of the combinations and not all PICS have the same features.
There is a learning curve to all programming and this compiler is no different EXCEPT CCS does supply a wealth of examples that can show how to use their product.Yes, you have to scan them ,or read the help files or just cut code and see what happens.The bottom line...the more you experiment the faster you'll see why so any of us are still using CCS.
hth
jay |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|