How to interface external crystal oscillator for Timer

Status
Not open for further replies.

Ahmmed Razu

Advanced Member level 4
Joined
Jul 7, 2011
Messages
100
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
Bangladesh
Activity points
1,968
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.
 

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

BigDog
 

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

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: 175
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?
 

There are several issues with your code.

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

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
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…