PDA

View Full Version : TMR1 & Button - long delay (1 min) before starting



flotulopex
- 12th March 2017, 16:07
Hi There,

I'm trying different little things to get familiar with the use of TMR1 as a timer (I want to use it as a time base).

Starting with "easy" little things, I'm trying to have my LED toggeling when the button is pressed.

It does work (immediatley) as long as I have not defined the T1CON and PIE1 registers like hereunder. If I uncomment both T1CON and PIE1 registers, the led will not toggle before (about) one minute has past.

Why is this please?


' ====== DEFINES ================================================== ================================
DEFINE OSC 4

' ====== FUSES ================================================== ==================================
' PIC 16F690
@ __Config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_INTRC_OSC_NOCLKOUT &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF

' ====== REGISTERS ================================================== ==============================
' 76543210
OPTION_REG = %10000000 ' PORT A&B Pull-Ups disabled (look WPUA & WPUB) INTEDG rising edge on A2
OSCCON = %01100000 ' Internal RC set to 4Mhz - not to be used with XTal
ANSEL = %00000000 ' Analog inputs Channels 0 to 7
ANSELH = %00000000 ' Analog inputs Channels 8 to 11
ADCON0 = %00000000 ' A/D Module is OFF
CM1CON0 = %00000000 ' Comparator1 Module is OFF
CM2CON0 = %00000000 ' Comparator2 Module is OFF
INTCON = %11000000 ' INTerrupts CONtrol: GIE is ON, PEIE is ON
'T1CON = %00111001 ' Timer1 OSC enabled, Timer1 enabled, presc.1:8 (RA4/RA5!!)
'PIE1 = %00000001 ' Enable TMR1IF overflow flag
PORTA = %00000000 ' Ports High/Low (0 to 5)
TRISA = %00000100 ' Set Input/Output (0 to 5)
PORTB = %00000000 ' Ports High/Low (4 to 7)
TRISB = %00000000 ' Set Input/Output (4 to 7)
PORTC = %00000000 ' Ports High/Low (0 to 7)
TRISC = %00000000 ' Set Input/Output (0 to 7)

' ====== VARIABLES ================================================== ==============================
LED var PORTB.6
Btn var PORTA.2
' ====== INITIALIZE ================================================== =============================
PAUSE 1000 'circuit settle time

' ====== PROGRAM ================================================== ================================
TEST:
IF Btn = 0 THEN
TOGGLE LED
PAUSE 200
ENDIF
GOTO TEST

Roger

HenrikOlsson
- 12th March 2017, 17:42
Are you running TMR1 from an external Watch x-tal?

Anyway, you don't want to enable the interrupt without actually having any code in place to service the interrupt. I suspect that's what's causing your problem.

Try uncommenting ONLY the T1CON line and I think you'll see that the code continues to work.

/Henrik.

flotulopex
- 13th March 2017, 16:01
Hi Henrik,

I must be tired.....

You're right, I've forgotten the most important part when using INTerrupts: the ISR.

It's corrected and, of course, it works.

Thanks a lot ;-)

Br

Roger

flotulopex
- 13th March 2017, 16:15
BTW Henrik,

If you have already seen this, what is best (more accurate): internal 32kHz oscillator or external crystal?

HenrikOlsson
- 13th March 2017, 18:16
An external x-tal will "always" be more accurate than the internal oscillator on a PIC.

On the 16F690 the internal 8MHz oscillator is specified as +/-1% under the best conditions (that doesn't mean it can't be better than that though) and the low frequency, 31kHz oscillator is labeled "uncallibrated" and no furhter specification can be found in the datasheet - as far as I can see.

Your typical €0.20 watch crystal is around 20ppm witch is 500 better than the internal 1% oscillator and if you pony up €0.60 you can get a 5ppm one which would then be 2000 times "better".

So the answer is that running TMR1 off of an exernal 32.768kHz Watch crystal is "better" (for timekeeping purposes) than running it off the internal oscillator. But if you're ALREADY running the PIC on a 4MHz crystal you might not gain much by using a separate crystal for the timer - except that the timer will keep on running when you put the device to sleep.

/Henrik.

flotulopex
- 13th March 2017, 20:42
Thank you Henrik.

I'm always in trouble when it gets to make time sensitive projects only using the PIC's features and thier own possiblities.

In some of my projects, I just use an RTC to use its 1Hz time base and have it read by an external interrupt in the PIC.

So, what is best?

HenrikOlsson
- 13th March 2017, 21:47
Define "best".
Accuracy, long time stability, resolution, power consumption, ease of use, what?

You can either run the PIC off of an external 4MHz (or whatever you need) x-tal and derive the TMR timebase from the main oscillator. You won't get as slow as 1Hz but you can get, lets say, 100Hz and keep counting interrupts. Or you can use the TMR1 oscillator with a separate (32768Hz ) x-tal. Since it's a 16bit timer it will overflow every 2 seconds if you let it freerun.

/Henrik.

flotulopex
- 14th March 2017, 10:39
When timing is in the game, for me "best" is "accurate".

Does a higher crystal frequency mean more accuracy?

Is it best to choose 4'194'304MHz (32'768kHz*128) crystal and lots of interrupts (if used) or a 32'768kHz one (less interrupts - plenty of time to do "things")?

HenrikOlsson
- 14th March 2017, 10:59
Doesn't really matter as far accuracy goes. Look at the frequency tolerance of the crystals you're considering, the one with the lowest value will be the most accurate one. But keep in mind that the TMR1 oscillator is designed specifically for low frequency crystals, typically a 32.768kHz. The datasheet for the 16F690 says DC to 200kHz.

/Henrik.

flotulopex
- 14th March 2017, 13:03
Thanks a lot, Henrik.