CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Timer1 Oscillator using as System Clock in PIC18 problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
a.g.electronic96@gmail.co



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

Timer1 Oscillator using as System Clock in PIC18 problem
PostPosted: Thu Mar 21, 2013 9:25 am     Reply with quote

Hi every one, I have been searching about how the timer1 oscillator could be be utilized as the system clock in PIC18 series?
When the timer1 oscillator is being used as system clock, is there any need to configure the OSC select Fuse bit like internal external ... (like this #fuses LP or INTRC_IO).
Please post an example just for using the timer1 osc with external crystal (32.768) as system clock.
I will be expecting for your kindly replies.
Thanks in advance
Embarassed Sad
Code:

#include <18F24K20.h>
#fuses NOWDT,NOPROTECT
#use delay(clock=32768)
#byte OSCCON=0xFD3
#bit SCS0=0xFD3.0
#bit SCS1=0xFD3.1

#byte T1CON=0xFCD
#bit T1RUN=0xFCD.6
#bit T1CKPS0=0xFCD.4
#bit T1CKPS1=0xFCD.5
#bit T1OSCEN=0xFCD.3
#bit TMR1CS=0xFCD.1
#bit TMR1ON=0XFCD.0

void main (void)
{
SCS0=0;
SCS1=1;

T1CKPS0=0;
T1CKPS1=0;
T1OSCEN=1;

  while(true)
   {
      if (T1RUN==1)
      {
         output_high(PIN_C6);
         delay_ms(1);
         output_low(PIN_C6);
         delay_ms(1);
    }

  }
}

but, the above setting is not working because when I remove the OSC 32.768Khz from pins T1OSI and T2OSO, the PIC is still working with an unknown frequency. I really need your help. Sad
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 21, 2013 9:47 am     Reply with quote

most of the newer 18F series PICs have a secondary 'fall back' oscillator.
The datasheet for your PIC will say if it is so..
..if so the processor header will have details about the 'fuses'.

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Thu Mar 21, 2013 12:24 pm     Reply with quote

Key is to understand that the chip always wakes up using the primary oscillator.
This you would select as the internal RC, so you don't use a crystal. Once it's awake, you then switch to the secondary (Timer1) oscillator.
You need INTRC_IO to setup/start the internal oscillator, and not waste pins.

Then set the clock statement to your secondary oscillator frequency.
As soon as your code starts, call setup_oscillator(OSC_TIMER1), which will switch the chip from the primary oscillator to the timer1 oscillator.

Best Wishes
jeremiah



Joined: 20 Jul 2010
Posts: 1349

View user's profile Send private message

PostPosted: Thu Mar 21, 2013 1:48 pm     Reply with quote

Also be aware that due to that situation Ttelmah mentioned above, be wary of any timing based commands (like delays, uart, spi, etc...not that you would be using some of those with 32kHz anyways...but as an FYI) being called prior to switching to your secondary oscillator. The compiler thinks your timing should be based off of the 32kHz (which is right), but until you call setup_oscillator() to switch to the secondary as the source the chip will be running at the internal frequency (I didn't check the data sheet to see if that was the same or different frequency). The call to setup_oscillator() should happen as early as you can make it.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Thu Mar 21, 2013 3:29 pm     Reply with quote

Oh, and as another general comment, it is _very_ rare indeed that you ever need to use direct access to registers in CCS. As a comment for instance, just getting towards the end of a current CCS project for me - over 19500 lines of PIC code, and a similar amount of Intel code, and not one direct register access in CCS, despite using two PWM's, two serials, USB, SPI, and I2C, analogs, and a huge mix of digital I/O. A total of five PIC's, and an Atom processor
The exception is the off chip/compiler bug, where programming round may be necessary, but for most other things, if you find yourself fiddling with registers, ask what you are missing in the CCS manual....

Best Wishes
a.g.electronic96@gmail.co



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

PostPosted: Thu Mar 21, 2013 7:44 pm     Reply with quote

Hi, and thank you so much for your replies, I found something from the data sheet. Your reply helped me very much... let me check it and then I put my experience in this post...
Confused
a.g.electronic96@gmail.co



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

PostPosted: Fri Mar 22, 2013 12:45 am     Reply with quote

Hi, Ttelmah

I have tried to config the timer1 oscillator as the processor clock (secondary Osc), I read again the data sheet and found a lot of information that helped me to understand what is going on when the secondary oscillator is desired to set, I did the following manipulation in my setting...,
Code:
Code:
#include <18F24K20.h>
#fuses INTRC_IO,NOWDT,NOPROTECT// setting the primary OSC ( internal 8MHz)
#use delay(clock=8000000)
//set the time1  according to the below:
T1CKPS1=0;  // set the  Prescaler of  the input clock source of timer1 to 1:1
T1CKPS2=0;
T1OSCEN=1;   //Timer1 Oscillator is enabled
T1SYNC=1;   //The external pulses are NOT synchronized with the internal phase clock
TMR1CS=1;    //The clock source of the Timer1 module is the pin RB6/T1CKI (Rising Edge)
TMR1ON=1;   // The Timer1 module is enabled

SCS0=1;active the secondary Oscillator as the clock source of processor
SCS1=0;



It has been written in the pic18f24K20's data sheet while the pic is working in secondary Oscillator mode the bit T1RUN should be set to 1 from zero and stay 1 till the mode of oscillation is changed.
but the problem is that I think that the pic never goes in secondary OSC. mode... because the T1RUN bit is being checked . Please halp me.... (these settings were extracted from pages 27-28-29-37-43-44 of data sheet of pic18f24k20 ).
a.g.electronic96@gmail.co



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

PostPosted: Fri Mar 22, 2013 1:49 am     Reply with quote

Hey guys, in my previous post I said that the timer1 oscillator is not working as expected.
actually I did some tests that prove my expectation about processor clock which is being supplied by another clock source. because when I checked T1RUN bit as it was mentioned in the last post, it was zero that means the timer 1 oscillator doesn't perform.
and the I checked two other status bits of clock source OSTS and IOFS located at third and second bits in OSCCON register respectively.
when I remove the line related to switching from INTRC to Timer1_OSC mode, I realize that the status bit for selecting oscillator is (OSTS) which is zero and correct, it means that the processor is using internal clock source as it was set. but when the oscillator mode is changed it changed instead the T1RUN become 1 that depicts the processor is operating by another unselected oscillator mode....
I don't know why .... but I really need your kindly answers


Last edited by a.g.electronic96@gmail.co on Fri Mar 22, 2013 1:52 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Fri Mar 22, 2013 1:50 am     Reply with quote

Your clock statement, needs to match the final frequency you want the chip to run at. As I said before, no register fiddling is needed.
You just need:
Code:

#include <18F24K20.h>
#fuses INTRC_IO,NOWDT,NOPROTECT
#use delay(clock=32768)

void main(void)
{
    //declare variables
    setup_timer1(T1_EXTERNAL); //Start the external oscillator
    setup_oscillator(OSC_TIMER1); //And switch to it
    //Code
}

The only 'caveat' as shown, is this does an instant start, and does not wait for timer1 to stabilise. You can change the oscillator line to:

Code:

    while (setup_oscillator(OSC_TIMER1) != OSC_STATE_EXT_RUNNING) ; //Wait for secondary to start

This takes longer, but means timings will be right, as opposed to a bit 'random' for the first few operations with the direct start.

Best Wishes
a.g.electronic96@gmail.co



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

PostPosted: Fri Mar 22, 2013 2:09 am     Reply with quote

Hi, Ttelmah
I wrote exact program that you posted, but it didn't work Sad Crying or Very sad
the version of the softwre that I am working is PCWHD Compiler ver. 4.104
So if there is any thing that I am doing wrongly please guide me ...
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Fri Mar 22, 2013 3:12 am     Reply with quote

That is very early for that chip.
Put this instead of the oscillator line I posted:
Code:

#define T1_OSC_EN 0x10

   setup_timer_1(T1_EXTERNAL | T1_OSC_EN); //start the secondary oscillator

CCS changed the default on later compilers to enable the oscillator, on your compiler it doesn't.

Best Wishes
a.g.electronic96@gmail.co



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

PostPosted: Fri Mar 22, 2013 3:19 am     Reply with quote

Thank you so much Sir, l am gonna check it and I will post the result back... what version do you suggest to work???
thanks
a.g.electronic96@gmail.co



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

PostPosted: Sat Mar 23, 2013 11:22 pm     Reply with quote

It was suggested to use the code below for switching from primary oscillator to secondary oscillator (timer1). I tried it but I could not what the problem is. It should be very simple and easy but when I compile and program on chip. It is not working, because I am checking the T1RUN that indicates the situation of clock source which is being used for system clock. I even check the SCS0 and SCS1, which they had 1 and 0 respectively, that are correct as expected to be changed when switching oscillator.
I am using a clock crystal (32768Hz) and with a couple of 18PF as driver capacitors.
The version of PCW that I am using is 4.130,
the chip as it has been written in the code is PIC18F24K20.

Please what is the problem in the code....?
because I have read all the secondary oscillator part of the chip's data sheet, and I think that all things are right, but still noting happens.

Code:

#include <18F24K20.h>
#fuses INTRC_IO,NOWDT,NOPROTECT
#use delay(clock=32768)

void main(void)
{
    //declare variables
    setup_timer1(T1_EXTERNAL); //Start the external oscillator
    setup_oscillator(OSC_TIMER1); //And switch to it
    //Code
}


Ttelmah wrote:
The only 'caveat' as shown, is this does an instant start, and does not wait for timer1 to stabilize. You can change the oscillator line to:

Code:

while (setup_oscillator(OSC_TIMER1) != OSC_STATE_EXT_RUNNING) ; //Wait for secondary to start
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Sun Mar 24, 2013 1:31 am     Reply with quote

Have you added the T1_OSC_EN bit as I showed.

For some reason, the compiler define file has not got this bit set. It appears in some versions of the compiler, almost at 'random'. Without this bit, the chip will only work if you attach an external _oscillator_, not a crystal. The bit needs to be set to turn the oscillator module on.

It is safer to assume the compiler does not set this bit, unless you have checked the assembler for your version, and are sure it does.

Best Wishes
a.g.electronic96@gmail.co



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

Ttelmah
PostPosted: Sun Mar 24, 2013 2:44 am     Reply with quote

Hi Sir,
Yes, I did exactly you had guided in last post, but it did not work. Indeed, I am sure after changing the source of processor's clock, if switching is not completely accomplished (might be caused by whether crystal or any other problem), then PIC will go to the default situation and will be connected to an internal clock source that exactly happened for my PIC18F after switching.
Actually I checked my crystal, it seems that is not working, because I put the Fuses on LP and connected the 32.768 KHz crystal on OSC1 and OSC2 Pins (for external oscillator) but I got no answer again... the only problem is just with 32.768 kHz because when I change the Fuses to XT or HS or INTRC or _INTRC_IO, it works fine and whenever I remove the crystal the operation will be stopped that depicts in these terms it is working well, but when the crystal was substituted and changed program it stopped.
Actually I also checked 4 or 5 32768 crystals ... all results were the same. If the problem had been derived by crystal, I should have got result.
Just the thing is that I was testing these crystals with 22pf load capacitors , or 47 capacitors...
What do you think about this problem,
Best regards.
Thank you so much for considering.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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