Composite reply to multiple messages. On Mon, Jul 21, 2014 at 11:42:54PM -0500, Richard R. Pope wrote: > Let me change my wording a little bit here. Won't those two > instructions put the settings back to the proper values so that the > timer counts the proper number of cycles to give me the 100mS delay? Yes, the two lines set the timer back to a value. I can't confirm the delay, I don't recall your chosen crystal frequency, and it isn't in your code comments! On Tue, Jul 22, 2014 at 04:59:26PM +1200, IVP wrote: > [... snip ...] > There are two options - >=20 > (1) Use a pre-scaler. This will allow a higher count but resolution > is coarser. A pre-scaler of 1:8 will give you a count value of > 500,000/ 8 =3D 62,500. Which you have. Apologies if I missed you > setting the pre-scaler in any of the code you posted. >=20 > (2) Change 0x0BDC to 0x3CB0 (-50,000 decimal) and generate an > interrupt every 10ms, without using the pre-scaler I agree. On Tue, Jul 22, 2014 at 05:30:02PM +1200, IVP wrote: > [... snip ...] > What you must do is clear any relevant interrupt flags. In your case > TMR1IF =3D 0. If you don't do this then the ISR will be re-entered > as soon as GIE =3D 1, as the uncleared TMR1IF is seen as a pending > interrupt to be dealt with. I agree. TMR1IF must be cleared in the ISR. Fail to do this and the ISR will execute repeatedly. Your program, Rich, does do this. On Tue, Jul 22, 2014 at 12:03:40AM -0500, Richard R. Pope wrote: > James. Your are correct. That is a typing mistake. It should be > TMR1ON =3D 0 or 1; //for off or on. My brain slipped. Sorry about > that. So does this work? Will this reset the timer to the proper > values so that I can always know that the timer will rollover at > 100mS? No, there's still confusion. See my comments in the code below. You might also remind us of the crystal frequency, using a comment in the source code near the prescaler and timer reload values. > //Global variables > int count =3D 0; >=20 > void main(void) > { >=20 > PORTA =3D 0; // Turn porta off > TRISA =3D 255; // Set Porta as all inputs > PORTC =3D 2; // Set RC1 to RC5 high > TRISC =3D 193; // Start with DP1 on, the rest of portc is= =20 > inputs > CMCON0 =3D 7; // Turn off Comparators > ANSEL =3D 16; // Set RC0/AN4 as analog >=20 > // Define commands > int DP =3D 1; > int Reset =3D 33 + DP; > int GateOn =3D 17 + DP; > int GateOff =3D DP; >=20 > while (1 =3D=3D 1) // Loop forever "while (1)" is more conventional. The comparison is not required. > { > PORTC =3D Reset; // Clear the Display > PORTC =3D GateOn; // Start counting > delay(1); // Count the frequency for one second =3D H= Z > PORTC =3D GateOff; > delay(15); // Display the frequency for 15 seconds >=20 > } > } // End NewControlCode.c >=20 > void delay(int time) > { > int limit =3D time * 10 + 1; >=20 > TMR1ON =3D 0; > TMR1IE =3D 0; > //PIR1bits.TMR1IE =3D 0; > resettimer(); > count =3D 0; > //PIR1bits.TMR1IE =3D 0; > TMR1IE =3D 1; > TMR1ON =3D 1; >=20 > while(count < limit) > { > } > return; > } >=20 > void resettimer(void) > { > TMR1H =3D 0x0B; > TMR1L =3D 0xDC; > } > //Timer1 > //Prescaler 1:8; TMR1 Preload =3D 3036; Actual Interrupt Time : 100 ms >=20 > //Place/Copy this part in declaration section >=20 > void InitTimer1(){ Your InitTimer1 function is not called. Therefore there is no attempt to set the prescaler. Therefore the prescaler will be 1:1, and the timing won't be 100 ms. > T1CON =3D 0x31; This is where you have set the prescaler, though it is not obvious that you have done so, because it is hiding in the register. Another way to put it would be: T1GINC =3D 0; TMR1GE =3D 0; T1CKPS1 =3D 1; T1CKPS0 =3D 1; T1OSCEN =3D 0; T1SYNC =3D 0; TMR1CS =3D 0; TMR1ON =3D 1; > PIR1bits.TMR1IF =3D 0; The clearing of TMR1IF at this point is probably not needed, but since the function is not called I can't be sure. > TMR1H =3D 0x0B; > TMR1L =3D 0xDC; > TMR1IE =3D 1; > INTCON =3D 0xC0; > } >=20 > void Interrupt(){ > if (PIR1bits.TMR1IF){ > PIR1bits.TMR1IF =3D 0; I find this mixing of register access styles hard to comprehend, and I'm surprised you understand it. Is there any reason why you have to use PIR1bits.TMR1IF instead of TMR1IF? > TMR1H =3D 0x0B; > TMR1L =3D 0xDC; > count++; > } > } --=20 James Cameron http://quozl.linux.org.au/ --=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 .