# How to interface external crystal oscillator for Timer

Status
Not open for further replies.

#### Ahmmed Razu

How to interface external crystal oscillator for Timer/Counter

I want to use external crystal oscillator as a Timer clock source but i can not understand how i will interface the external crystal oscillator for Timer with specific pin of microcontroller(i.e. Timer oscillator input pin).It will be better for me with circuit diagram.

#### bigdogguru

It would help if you specified the microcontroller to which you are referring, as specifics vary depending the particular device.

BigDog

#### Ahmmed Razu

I am using PIC18F4550 and i have 32kHz crystal oscillator as a Timer clock source.
Can you also provide me for ATMEGA32?

#### wp100

Hi,

On the Pics /4550 you connect a 32k crystal and caps as shown below, this is then used as the clock source to Timer1 as a RTC.

On a ATMEGA32 I believe its very similar, according to the datasheet pins PC6 and PC7 are the input to its Timer1 , but it only needs the crystal, caps not required.

Timer/Counter
Oscillator
For AVR microcontrollers with Timer/Counter Oscillator pins (TOSC1 and TOSC2), the crystal is
connected directly between the pins. No external capacitors are needed. The Oscillator is opti-
mized for use with a 32.768 kHz watch crystal.

#### Attachments

• xtal.jpg
26.1 KB · Views: 51
Ahmmed Razu

### Ahmmed Razu

Points: 2

#### Ahmmed Razu

I have made a code for Timer1 with external oscillator as a timer clock source but it does not working.(PIC18F4550,Compiler:MikroC).My code is following..........

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
unsigned int cnt=0;

void interrupt()
{if(TMR1IF_bit==1)
{
cnt++;
PORTB=cnt;
TMR1IF_bit=0;
}
}

void main()
{
ADCON1 = 0x0F;                         // Configure all ports with analog function as digital
CMCON  = 7;

TRISB=0x00;
PORTB=0;

GIE_bit=1;
TMR1IE_bit=1;
//TMR1IP_bit=1;
//IPEN_bit=1;

delay_ms(1000);

T1CON=0x0F;

while(1);

}

my connection diagram...
where is my fault?

#### bigdogguru

There are several issues with your code.

Reference: PIC18F2455/2550/4455/4550 Datasheet, Section: 12.3 Timer1 Oscillator, Page: 133
12.3 Timer1 Oscillator

An on-chip crystal oscillator circuit is incorporated
between pins T1OSI (input) and T1OSO (amplifier
output). It is enabled by setting the Timer1 Oscillator
Enable bit, T1OSCEN (T1CON<3>). The oscillator is a
low-power circuit rated for 32 kHz crystals. It will
continue to run during all power-managed modes. The
circuit for a typical LP oscillator is shown in Figure 12-3.
Table 12-1 shows the capacitor selection for the Timer1
oscillator.

The user must provide a software time delay to ensure
proper start-up of the Timer1 oscillator.

Therefore configure Timer1, near the top of your program, before enabling any Timer1 interrupts. In other words, move the statement:

Code:
T1CON=0x0F;

near the top of the code listing, followed by a sufficient delay to allow the Timer1 Oscillator to begin operating.

Also you have failed to enable peripheral interrupts by setting the PEIE bit, INTCON<6>, along with the GIE bit, INTCON<7>.

Therefore the Timer1 interrupt will not be triggered and the Interrupt Service Routine (ISR) will never be called.

Keep in mind the selection of caps for the 32.768 kHz crystal are dependent largely on the crystal's specs and not on any recommendation, therefore refer to the crystal's datasheet for the proper capacitor selection.

There maybe other issues, however the aforementioned came to mind.

BigDog

Ahmmed Razu

Points: 2