Timer 1 oscillator

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Timer 1 oscillator

Post by blackcattech » Mon Mar 26, 2012 3:43 pm

Has anyone used the Timer 1 oscillator on the PIC18F14K22?

I can't seem to get it oscillating...

I've scoured the datasheet and can't see anything you need to do beyond setting T1OSCEN to actually make it run, plus setting TMR1CS to get it feeding in to Timer 1 itself. (And of course turning the timer on!)

However, having done this I can't even get a signal on the pins using a scope never mind getting the timer to increment.

Is there anything odd about the K series that you need to account for?

Also, the xtal I'm using says it needs a 12.5pf load so I've used two 12.5pf capacitors. The PIC datasheets talk about 27pf typical - am I misunderstanding the capacitor requirement and could this stop it oscillating completely?

RKP
Registered User
Registered User
Posts: 82
Joined: Mon Oct 22, 2007 3:14 pm
Location: Maryland

Post by RKP » Mon Mar 26, 2012 5:07 pm

Never used this part but have you tried with these settings.
Configure portC for the crystal input.
TRISC = %xxxxxx10

T1CON = %10xx1xx1

RKP

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Mon Mar 26, 2012 6:33 pm

RKP wrote: Configure portC for the crystal input.
TRISC = %xxxxxx10
Port A.... No, not tried that but the datasheet says that TRIS etc are over-ridden if the oscillator is enabled. I will give it a try though.

T1CON = %10xx1xx1
Any reason for using 16-bit mode? I have it set at %00101010 and enable the timer separately. That should be /4 prescale, oscillator enabled, external clock as source.

RKP
Registered User
Registered User
Posts: 82
Joined: Mon Oct 22, 2007 3:14 pm
Location: Maryland

Post by RKP » Mon Mar 26, 2012 7:21 pm

I assumed that when you were referring to your crystal caps that you were using a (secondary) external crystal for your timer1 source.
Ignore my TRISC statement, now I know you are using the main clock for your source.
Any reason for using 16-bit mode? I have it set at %00101010 and enable the timer separately. That should be /4 prescale, oscillator enabled, external clock as source.
According to my data sheet bit1 of T1CON set 8/16 bit mode and bit0 enables the timer. Your choice enables 16 bit operation but you never enable the timer.

Change T1CON = %00101010 to %00101001
Also bit3 can be 0 as well since there is no oscillator to enable.

Now if you are not sure if the PIC is really runnig at all and the timer stuff above will not contribute to that one way or tthe other. You can always write a short program to toggle a port pin an this will let you if the PIC is running.

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Mon Mar 26, 2012 7:32 pm

I suspect you haven't realised that the 18F14K22 is a 20-pin device so the timer 1 oscillator is on RA4 and RA5. I am using a 32kHz watch xtal on these two pins with 12pF caps. The xtal is:

http://www.rapidonline.com/Electronic-C ... pf-90-3052

The datasheet lists bit 7 of T1CON as 8/16 bit mode. As I say, I enable the timer separately - in other words, I configure it then start it rather than start and configure in one go. I remember reading somewhere that is good practice, maybe that was only on the PIC16s but I can't see it harming.

A set bits 1, 3 and 5 which according to the datasheet I've just accessed via a google search are TMR1CS (1 = use clock from the T13CKI pin), T1OSCEN (1 = enabled) and 1:4 prescale.

RKP
Registered User
Registered User
Posts: 82
Joined: Mon Oct 22, 2007 3:14 pm
Location: Maryland

Post by RKP » Mon Mar 26, 2012 8:01 pm

Doh!!

Disregard my previous posts I was looking at the 18F24K22 data sheet and apoligize to you and anyone lookig at this thread.

Now looking at the corrcet data sheet 14K22
did you set TRISA = %xx10xxxx
There is also an analog input on RA4(AN3) it needs to be disabled.
ANSEL = %00000000 ' this disables the analog inputs
T1CON = %00101010 ' then set bit0 when you want to turn it on.

RKP
Registered User
Registered User
Posts: 82
Joined: Mon Oct 22, 2007 3:14 pm
Location: Maryland

Post by RKP » Tue Mar 27, 2012 2:58 am

Blackcattech,

What FOSC setting are you using in your config statemnent.
Also what is your OSCCON setting.

Can you post you config settings including the OSCCON reg.

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Mar 27, 2012 7:24 am

The settings are:

Module IntOSC
Config FOSC = IRC
OSCCON = %01000000

I'm wondering whether the fact the main oscillator is on the same pins is somehow affecting it, although I'd have thought it would be automatically over-ridden. I've had the oscillator working on the equivalent PIC16 in the past without problem. There is nothing in the errata that I can see so I'm starting to run out of things to try!

I've tried the TRIS setting without success. The rest of my settings are:

Device = 18F14K22
Clock = 2

Config
DEBUG = OFF,
FCMEN = OFF,
IESO = OFF,
PWRTEN = ON,
BBSIZ = OFF,
BOREN = OFF,
WDTEN = OFF,
WDTPS = 4096,
MCLRE = OFF,
STVREN = OFF,
XINST = ON,
LVP = OFF


I'm starting to wonder if I can get away with the accuracy of the internal oscillator for this project and not bother with the xtal at all...

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Mar 27, 2012 8:31 am

Curioser and curioser... I've changed the code to display the TMR1 register on the seven-segment display and commented out a lot of stuff that wasn't needed for this to try and cut down to basics.

I found if I showed TMR1H it would increment very slowly. I've just changed that to TMR1L and it incremented (visibly, i.e. maybe at 2Hz rather than 8kHz) up to 66 then has stopped...

Looks like the oscillator is working sometimes but not all the time.

I've tried adding a second capacitor in parallel to the first so there is now 24pf on either side of the xtal but the improvement is marginal if it even exists. Next step I guess is try changing the xtal itself...

I've checked the PCB and there is definitely 0V to the capacitors and no shorts that I can find. Most odd... It is looking like a hardware problem rather than a software one.

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Mar 27, 2012 10:07 am

This is going to be one of those.....

Tinkered a bit, resoldered some connections and I've got some life from the oscillator.

It counts up to 100 then stops...

Far too slow - takes maybe 20 seconds to do so when in reality 100 counts at 8KHz wouldn't be visible to the naked eye.

No limiting, rounding, anything. I just use utils.digit to put the three digits of the byte in to the three elements of the array used to set the display. The display keeps working so the processor and ISR is still running.

I think the next step is to just have a program that turns the oscillator on and nothing else and see if it runs.

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Mar 27, 2012 10:14 am

Updating again....

Everything off apart from the timer 1 oscillator, I get a 32kHz sine wave with an amplitude of about 0.4V on T13CKI.

Is that what I should expect?

If so then it maybe is a software issue...

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Mar 27, 2012 10:29 am

Got it!

But I now have another problem...

If you don't sync the timer with the internal clock then the oscillator runs. For some reason syncing it stops the oscillator. Not just the timer, the oscillator as well.

However, I'm using the capture function of the CCP module to 'timestamp' events. The datasheet says that capture may not work if the timer is in async mode....

Any ideas for ways around this?

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Mar 27, 2012 12:51 pm

Actually.... I haven't got it.

Brain is being scrambled here and I got my T1CON bits mixed up...

I'd routed Fosc/4 in to timer 1 rather than disabling sync and as I was seeing movement on the timer assumed I'd fixed it.

So, the situation now is:

With the oscillator enabled I get a sine wave of approx 0.4V amplitude on T13CKI. As soon as I configure timer 1 to use T13CKI as the clock source, the oscillator stops oscillating.

So, the questions are:

Should I be seeing this sine wave or should I not be seeing a square wave somewhere?

Is what I see on the pin buffered before going in to the timer or is the load of the timer stopping oscillation?

Is there something else the datasheet is hiding that needs clearing or setting to enable this oscillator to work?

I've tried slew rate limiting on and off and forcing TRISA with no joy.

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Mar 27, 2012 1:05 pm

I'll keep talking to myself as this is probably a good reference for anyone else in the future who hits this snag....

I have got it now.

You need to disable the pullups on RA4 and RA5. Actually, possibly only on RA5 but I've disabled all but the two pins I need pullups on just to be sure.

Nothing about this in the datasheet, although I've noticed over the years that Microchip don't seem to re-write sections when a module is updated, they just document the new bits and I think a lot of 'interaction' like this isn't detailed properly.

By the way, on the off-chance David is reading this, it would be useful to either add a function or modify the Input() function to enable or disable pullups for a particular pin. Unless I'm missing it, you can declare an alias for a pin and use it with Input(), Output() etc, but if you want to turn the pullups off you have to either declare another alias or write WPUx direct. Not a deal-breaker but it would be nice for completeness.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Mar 27, 2012 2:08 pm

Thanks for keeping us updated.

The pullup thing is pretty chip-dependent. Not all devices and pins have them, and the ones that do have different methods of controlling them. Might be hard to do a generic control.

Post Reply