Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC 18F4550 Timer 1 help

Status
Not open for further replies.

noname06

New Member
Hi,

I am using a PIC18F4550 microcontroller. I have a 4MHz crystal and wants to use timer1. How do calculate how many counts i needed for a millisecond interrupt. Anyone can help?
 
Assuming you're not using the PLL, a millisecond will be 1000 clock cycles.

Mike.
 
Ian,

You should divide by 4 not 5 and so it's 200nS and a count of 5000.

Mike.
 
To get a count of exactly 1000 ticks you might be better to use TMR2, and set PR2 = (250-1) so TMR2 makes an interrupt exactly every 250 cycles, then just count 4 cycles to get an exact mS. Alternatively you could do the same but use 1:4 prescaler.
 
I still cannot get millisecond interrupt...Can anyone check if my control register for timer1 is correct??

T1CONbits.RD16 = 1; // 16-Bit = Read/Write Mode Enable bit
T1CONbits.T1RUN = 1; // Device clock is derived from another source
T1CONbits.T1CKPS1 = 1; //1:4 Prescaler value
T1CONbits.T1CKPS0 = 0;
T1CONbits.T1OSCEN = 0; //Timer1 oscillator is shut down
T1CONbits.T1SYNC = 1; //Do not synchronize external clock input
T1CONbits.TMR1CS = 1; //External clock from RC0/T1OSO/T13CKI pin (on the rising edge)
T1CONbits.TMR1ON = 1; // starts Timer1
 
You need T1CONbits.TMR1CS = 0 so it uses the 4MHz clock.

Also check out the special event trigger of the PWM module which lets you set any interval for timer 1.

Mike.
 
I am using a PIC18F4550. I do not know if the calculation is the same as 16F. As i know 16F timer1 calculation has a preloaded value but not really sure if 18F needs it too.
 
My main purpose is to use a 20MHz crystal but i am trying out with 4MHz first. Mike, if i set T1CONbits.TMR1CS = 0 this means that i am using internal clock instead of my crystal?
 
The reset value for TMR1CS is zero. His issue is that he's not setting a preload value.

I know that but in his code he sets it to 1 and so it isn't working. He also doesn't need a preload value if he uses the Special Event Trigger as I suggested.

Mike.
 
PIE1bits.TMR1IE needs to be set for PIR1bits.TMR1IF to be an actual interrupt. Also an interrupt handler routine needs to be created to capture the event. Alternatively PIR1bits.TMR1IF can be monitored to see if it has tripped. Here's some code that monitors the flag to see if it has tripped. This code generates a 1 ms delay for a 20MHz crystal generating a 48MHz internal clock.

Code:
// Setting Timer 1 configuration register (T1CON) one bit at a time
  T1CONbits.RD16 = 1;  // 0 = R/W as 8 bits, 1 = R/W as 16 bits
//TMR1_OSC = T1CONbits.T1RUN = ;  // read only: 1 = TMR1 OSC in use
  T1CONbits.T1CKPS1 = 0;  // 2 bit Prescale value:  00: PS = 1,
  T1CONbits.T1CKPS0 = 0;  //  01: PS = 2, 10: PS = 4, 11: PS = 8
  T1CONbits.T1OSCEN = 0;  // Timer 1 Osc Enable: 0 = off, 1 = on
  T1CONbits.T1SYNC = 0;  // Timer 1 Osc Sync: 0 = on, 1 = off
  T1CONbits.TMR1CS = 0;  // Clock Source: 0 = Fcy, 1 = TMR1_OSC
  T1CONbits.TMR1ON = 1;  // 0 = turn timer off, 1 = turn timer on
  // Alternatively you could set the T1CON register as a single byte:
                 // RD16 CHK PS1 PS0 OSC SYNC SRC ON
  T1CON = 0x81;  //  1    0   0   0   0   0   0   1
  TMR1H =  0xD1;  // TMR1H must be written first
  TMR1L =  0x20;  // count up from D120h

  PIR1bits.TMR1IF = 0;  // reset the interrupt flag

  while(!PIR1bits.TMR1IF) {  // wait for the interrupt flag = 1 then break the loop
  // turn something on or off - or just wait
  };

  T1CONbits.TMR1ON = 0;  // 0 = turn timer off, 1 = turn timer on

A more detailed explanation can be found here:

**broken link removed**

and here:
**broken link removed**
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top