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 Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
[email protected]



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

PostPosted: Wed Mar 27, 2013 11:42 am     Reply with quote

Hi guys. Eventually, the external oscillator worked in my pic. I did this because I wanted to be sure that my crystal is working fine. But , when I wrote a blinking program and execute it, the frequency of blinking was not accurate, so, I measured the frequency of oscillator (32.768 kHz) on pin osc1 and oc2 by oscilloscope, it was around 35.7. The question is how it could be modified to make accurate.
Thanks to all of you for your kindly replies.
Best wishes.
temtronic



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

View user's profile Send private message

PostPosted: Wed Mar 27, 2013 12:09 pm     Reply with quote

If you mean your external xtal is not accurate, then try a higher quality xtal and adjust the caps accordingly. You get what you pay for! A high precision 'watch crystal' could easily cost MORE than the PIC!

I have to ask WHY you want/need to use a 32KHz timing source?

cheers
Jay
[email protected]



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

temtronic
PostPosted: Sun Mar 31, 2013 12:13 am     Reply with quote

Hi guys,
as I said in previous post, eventually, the problem of LP crystal was solved just for external osc... whereas I need it to work as timer1 OSC clock, for being used as the main clock source of the PIC..., but, despite of working the 32.768 crystal as the external clock (LP), it is still not working for timer 1...
I did exactly the program that has been posted in the forum and also the penultimate post ... please guide me ....
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

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

If it is running using LP, then it is exactly the same as when it runs using Timer1. It's the same accuracy everything. The only difference is the pins used.
The only reason to run it as Timer1, rather than as LP, is when you want to have another faster crystal on the primary oscillator, and 'switch down' to the Timer1 oscillator. LP is the 'main clock source for the PIC'. The other difference is that using LP, you can wake up using this crystal, using Timer1 you can't.
So what advantage does Timer1 have for you?. Do you want to use another faster crystal at the same time?. Timer1, can never be the primary oscillator. LP can.

Best Wishes
[email protected]



Joined: 10 Nov 2012
Posts: 19

View user's profile Send private message

Ttelmah
PostPosted: Sun Mar 31, 2013 7:30 pm     Reply with quote

Thank you so much for your great and complete answer. yes, exactly, I want to use another crystal as the main clock source when the PIC is working, and whenever the PIC is set to sleep mode the Timer1 oscillator supplies the clock of timer1 for timing after 1 sec and waking up... but I do not know that why, when I set PIC to LP mode it is working well with 32.768khz,whereas, when I set PIC18F24K20 to Timer1 oscillator after switching it is set to the internal default oscillator. I did program as it was suggested in last few posts. the version of compiler as it was mentioned is PCW 4.130,
best regards.
danvica



Joined: 22 Nov 2013
Posts: 4

View user's profile Send private message

PostPosted: Fri Nov 22, 2013 3:35 am     Reply with quote

@Ttelmah
I thought using Timer1 as oscillator can reduce current consumption.
I'm using PIC18LF1220 and I'm trying to keep its consume low using a 31.768KHz cristal on Timer1 pins.

But I'm confuse on the different power managed modes.

The datasheet reports a supply current of max 12uA @3V if the pic is in SEC_RUN mode.
This can be ok for my purpose but I'm not sure how to enter that mode.

The datasheet reports (3.4.2):

"The SEC_RUN mode is the compatible mode to the “clock switching” feature offered in other PIC18
devices. In this mode, the CPU and peripherals are clocked from the Timer1 oscillator. This gives users the
option of lower power consumption while still using a high accuracy clock source.
SEC_RUN mode is entered by clearing the IDLEN bit, setting SCS1:SCS0 = 01 and executing a SLEEP instruction."

So, the CPU is in SLEEP (program execution is halt) or not ?


Thanks for any help.
Daniele
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Nov 22, 2013 4:44 am     Reply with quote

Key is to look at the 'setup_oscillator' command. This has an extra control called 'OSC_IDLE_MODE'. If you set this, _before_ calling the sleep instruction, you go into 'idle', rather than 'full sleep'.

So:
Code:

setup_oscillator(OSC_TIMER1|OSC_IDLE_MODE);
sleep();
delay_cycles(1);
setup_oscillator(OSC_NORMAL);


Switches to the low power oscillator, and puts the processor into the 'idle' rather than full sleep mode. Then when the 'wake up' event triggers, the oscillator restarts.

Best Wishes
danvica



Joined: 22 Nov 2013
Posts: 4

View user's profile Send private message

PostPosted: Fri Nov 22, 2013 7:44 am     Reply with quote

Thanks !

But...after the sleep, which mode the CPU is in ? The one set by fuses ?

Table 3-3 of datasheet is not clear to me.

I have many questions... but actually the most simple / important is: how to reduce the average current consumption ?

I thought:

1. Use Timer1 as 32KHz clock (primary ?)
2. Enter sleep mode (and I don't need peripherals to be clocked) for most of the time
3. Wake up by Timer1 counter overflow.
4. Do the job
5. Repeat from 2

Is it the right way ? I repeat, I'm confused... thanks for any hint.

Daniele
danvica



Joined: 22 Nov 2013
Posts: 4

View user's profile Send private message

PostPosted: Sat Nov 23, 2013 12:03 am     Reply with quote

While I could solve my specific case I still have problems understanding power saving modes.

Here is a small program:

Code:

#include <18f1220.h>
#device adc=8

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <C:\TAPS\NUOVO_PROGETTO\mixing\pic18.h>

#fuses INTRC_IO,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOFCMEN,MCLR,NOIESO

#use delay(clock=32768)

#use fast_io(A)
#use fast_io(B)

#define INTS_PER_SECOND    20

#define   LEDR         LATB_RB5
#define   LEDG         LATA_RA1

#define   LED2A         LATA_RA2
#define   LED2K         LATA_RA3


#define   SENSE_VDD      LATB_RB2
#define   SENSE_MODE      LATB_RB4
#define   SENSE_OUT      PORTB_RB3

#define   WATEROFF      PORTA_RA6

#define   ELA            LATB_RB0
#define   ELB            LATB_RB1
#define EL_VDD         LATA_RA7


int32 maintick;
int16 sec1tick;

#int_timer1         // ogni 50ms                 
void clock_isr()   
{   
   clear_interrupt(INT_TIMER1);
   
   maintick++;
   
   sec1tick--;
   if (!sec1tick)
   {
      sec1tick=INTS_PER_SECOND;
   }   
      
   set_timer1(63896);
}
   
void initports(void)
{
   int i;
   
   EL_VDD=0;
   SENSE_VDD=0;
   SENSE_MODE=0;
   ELA=0;
   ELB=0;
   LED2A=0;
   LED2K=0;
   
   for (i=0;i<5;i++)
   {
      LEDR=1;
      LEDG=0;
      delay_ms(200);
      LEDR=0;
      LEDG=1;
      delay_ms(200);
   }
   
   LEDR=0;
   LEDG=0;
   
}   
void main(void)
{
   setup_oscillator(OSC_31KHZ|OSC_INTRC);
   
   set_tris_a(0b01100001);
   set_tris_b(0b11001000);
   
   delay_ms(200);
   
   port_b_pullups(FALSE);
   setup_adc_ports(NO_ANALOGS);
   setup_adc( ADC_OFF );
   
   initports();
   
   maintick=0;
   sec1tick=INTS_PER_SECOND;
      
   setup_timer_1( T1_EXTERNAL | T1_DIV_BY_1 | T1_CLK_OUT);
   set_timer1(63896);
   
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   
   //setup_oscillator(OSC_31KHZ|OSC_INTRC);   
   // 80uA running while(1);
   // 40uA with interrupt enabled
   // 30uA with interrupt disabled   
   
   //setup_oscillator(OSC_TIMER1|OSC_IDLE_MODE);
   // 80uA running while(1);
   // 24uA with interrupt enabled
   // 8uA with interrupt disabled   
   
   setup_oscillator(OSC_TIMER1);
   // 80uA running while(1);
   // 36uA with interrupt enabled
   // 24uA with interrupt disabled   
   
    while (1);      // test running
   
    /*while (1)      // test sleeping
    {
       sleep();
    }*/   
}


Compiler version: 4.078

Note: you can avoid pin-related error by not calling initports() function.

Close to the setup_oscillator function calls there are the measured current consumptions.

So:

1. In RUN the cunsumption is the same. Is it depending on the frequency only ?

2. In SLEEP, no interrupt. Why the consumption is higher setting OSC_IDLE_MODE ? Shouldn't it leave the peripherals clocked, increasing the total current (even if in this case they are turned off) ?

3. How to reach the max 12uA in SEC_RUN (ref.datasheet 22.2) ? Is it possible executing a code in this mode ?

Thanks.
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 Previous  1, 2
Page 2 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