OK, fine then. :-) Glad that it works (as far as I understand at least). I'm not going to read the code if you don't have any issues with it. 0.5% is fairly normal for a (ceramic) resonator. You need a quartz crystal to get lower errors. I leave capturing short pulses on a 'scop to others. :-) Jan-Erik. Richard R. Pope wrote 2014-07-23 14:02: > Jan-Erik, > The info about crystal frequencies is great. I have a lot of > crystals from equipment that I have taken a part and it is hard to > figure out what division to use to get a certain frequency. This will > make it much easier. > Here is the complete code that I am using. I only call the > InitTimer1() code one time. I call this code in the main() right after I > have set up the ports and the registers. > The chip runs but I can't tell for certain if the code is > executing. RA0-3 are all high because I have 10k pullups on them. They > are set up as digital inputs. I have a 4MHz resonator connected to RA4 > and 5. I am reading 3.979930MHz +- 15Hz. The wave form looks clean and > steady. I don't care about the 0.5% error at this time. > RC0 is floating at about 800 mV. It is set up as an analog input. > The RC1 bit is high. This is DP1. RC2-5 are low and this is correct > until RC4 and 5 are pulsed high. RC2 and 3 are DP2 and 3 and they are low= .. > The pulse on RC 4 is only for one second. This is the gating pulse. > The pulse on RC5 is only for a few uSs. This is the clear display pulse. > I have never learned how to set up my scope to capture a very short > pulse. Could you please tell me how to do that? I have a Tek 465B. > Thanks, > rich! > > // Includes go here > #include > > // Prototypes go here > void main(void); > void delay(int time); > void resettimer(void); > void InitTimer1(); > //void Interrupt1(); > > /*Richard R. Pope > 07-19-14 > */ > > #pragma config FOSC =3D XT, WDTE =3D OFF, PWRTE =3D ON, MCLRE =3D OFF \ > CP =3D OFF, CPD =3D OFF, BOREN =3D OFF, IESO =3D OFF, FCMEN =3D= OFF > > // Crystal Freq =3D 4MHz > > // Global variables > int count =3D 0; // number of interrupts counter > > void main(void) > { > > PORTA =3D 0b00000000; // Turn porta off > TRISA =3D 0b11111111; // Set Porta as all inputs > PORTC =3D 0b00000010; // Set RC1 to high > TRISC =3D 0b11000001; // Start with DP1 on, the rest of > portc is inputs > CMCON0 =3D 7; // Turn off Comparators > ANSEL =3D 16; // Set RC0/AN4 as analog > InitTimer1(); // Set up the timer > > // Define commands > int DP =3D 1; // The decimal points are counted from = 1 > int Reset =3D 33 + DP; // Turn on RC5 > int GateOn =3D 1 + DP; // Turn on RC4 > int GateOff =3D DP; // Leave only the DP on > int GateTime =3D 1; // The count time =3D 1 Second > int DisplayTime =3D 15; // The display time =3D 15 seconds > > while (1 =3D=3D 1) // Loop forever > { > PORTC =3D Reset; // Clear the Display > PORTC =3D GateOn; // Start counting > delay(GateTime); // Count the frequency for one > second =3D HZ > PORTC =3D GateOff; > delay(DisplayTime); // Display the frequency for 15 > seconds > > } > } // End NewControlCode.c > > void delay(int time) > { > int limit =3D time * 10 + 1; > > TMR1ON =3D 0; // Turn the timer off > TMR1IE =3D 0; // Disable timer interrupts > resettimer(); // Reset the timer to starting values > count =3D 0; // Reset the timer interrupt counter > TMR1IE =3D 1; // Enable the timer interrupts > TMR1ON =3D 1; // Turn the timer on > > while(count < limit) > { > } > return; > } > > void resettimer(void) > { > TMR1H =3D 0x3c; // Reset the timer to roll over > TMR1L =3D 0xb0; // at 100mS > } > //Timer1 > //Prescaler 1:8; TMR1 Preload =3D 3036; Actual Interrupt Time : 100 ms > > //Place/Copy this part in declaration section > > void InitTimer1(){ > T1CON =3D 0x31; > TMR1IF =3D 0; > resettimer(); > TMR1IE =3D 1; > INTCON =3D 0xC0; > } > > void Interrupt(){ > if (TMR1IF){ > TMR1IF =3D 0; > TMR1H =3D 0x3c; > TMR1L =3D 0xb0; > count++; > } > } > > On 7/23/2014 6:11 AM, Jan-Erik Soderholm wrote: >> Yes, this is not a major issue for code that is, let's >> say as an example, in the startup coe that only is run >> one at each power on anyway. Code readability and >> clarity is far mor importent in that case. >> >> > It also sounds like the compiler will actually generate >> > better code if I use the whole register. >> >> Yes, that will always be s simple 8 bit "load". >> Three instruction maximum (bank switch, MOVLW and MOVWF). >> Four on the (old) 684, the bankswitch takes two. :-) >> >> > I prefer binary over either hexadecimal or decimal >> > for setting the bits on a port. >> >> Right, for registers that hold a "value" (such as an timer >> register for example) the format (hex/bin/dec) is of less >> importance. Many times a decimal value can make the code >> easier to read such as when setting a timer to "one hundred >> and twenty three". Decimal (123) is easier to read then >> hex (7B). >> >> For registers where the individual bits has different functions >> (such as INTCON or similar) I prefer binary. That makes it >> easier to read out the bit settings. >> >> > Why is there a setting for the starting point of the counter in >> > both the Interrupt handler function and in the InitTimer function? >> >> I do not have the code in front of me, but I'd expect anything >> with "init" in its name to only run once at startup. >> >> So InitTimer run once at power on and setup the basic timer stuff. >> The ISR then sets whatever has to be set at each interrupt, such >> as reloading the timer value (if not freerunning). >> >> B.t.w, *if* one can find a setup where you get a usable "timing" >> while letting the timer fre-run (that is, without ever re-loading >> the timer value), it is often easier to get a consistent overall >> timing in the application without having to bother about specific >> adjustments for some extra instructions here and there. This can >> sometimes need a carefull selection of the crystal frequency. And >> that is also why there are all those "odd" crystal values... :-) >> >> Here is a good overview of crystal frequencies: >> http://en.wikipedia.org/wiki/Crystal_oscillator_frequencies >> >> >> Jan-Erik. >> >> > --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .