View previous topic :: View next topic |
Author |
Message |
boulder
Joined: 15 Mar 2008 Posts: 53
|
Setup Timer1 on PIC16F687 |
Posted: Mon Dec 08, 2008 7:55 pm |
|
|
Hi,
I want to have a increment of 1 from 0 to 9 every 500ms and display the number on a 7_segment_led, but increment is never happened. The below is my code could you help me to find out what's wrong with it?
Thanks!
Code: |
#include <16F687.h>
#use delay(clock=3686400, crystal)
#use rs232(baud=115200, xmit=PIN_B7, rcv=PIN_B5)
#fuses XT, NOWDT, PUT, MCLR, NOPROTECT, NOCPD, NOBROWNOUT, NOIESO, NOFCMEN
volatile unsigned int8 i=0;
#INT_TIMER1
void TIMER1_ISR()
{
i++;
if(i > 9)
i = 0;
}
void main()
{
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H SPI_CLK_DIV_16); //Setup SPI interface, mode 0
set_timer1(0x1F00); //1 interrupt every 500ms
setup_timer_1(T1_EXTERNAL | T1_CLK_OUT | T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(global);
while(1)
{
Display_i_to_led();
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 08, 2008 8:40 pm |
|
|
Quote: | setup_timer_1(T1_EXTERNAL | T1_CLK_OUT | T1_DIV_BY_8); |
Use T1_INTERNAL instead of the parameters in bold. |
|
|
boulder
Joined: 15 Mar 2008 Posts: 53
|
|
Posted: Mon Dec 08, 2008 9:01 pm |
|
|
Yes, it works with the internal clock, 8MHz. Why it does not work with external clock? In my PCB, this chip is connected to an external clock, 3866400 Hz, on purpose for UART baudrate 115200. Are there any methods to make timer1 work with this external clock?
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 08, 2008 9:16 pm |
|
|
Quote: | #use delay(clock=3686400, crystal)
#use rs232(baud=115200, xmit=PIN_B7, rcv=PIN_B5)
#fuses XT, NOWDT, PUT, MCLR, NOPROTECT, NOCPD, NOBROWNOUT, NOIESO, NOFCMEN |
You are not using the internal oscillator at 8 MHz. You are using the
crystal oscillator (XT) at 3.6864 MHz. |
|
|
boulder
Joined: 15 Mar 2008 Posts: 53
|
|
Posted: Mon Dec 08, 2008 9:22 pm |
|
|
Yes, I am using internal clock. I already changed clocked after your first reply from
Code: |
#use delay(clock=3686400, crystal)
|
to
Code: |
#use delay(clock=8000000)
|
I don't understand why external clock does not work. Do you have any explanation for it?
Thanks!!! |
|
|
boulder
Joined: 15 Mar 2008 Posts: 53
|
|
Posted: Mon Dec 08, 2008 9:40 pm |
|
|
I realize that if I use external clock
Code: |
#use delay(clock=3686400, crystal)
#use rs232(baud=115200, xmit=PIN_B7, rcv=PIN_B5)
#fuses XT, NOWDT, PUT, MCLR, NOPROTECT, NOCPD, NOBROWNOUT, NOIESO, NOFCMEN
|
But Timer1 setup use INTERNAL
Code: |
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
|
it works too. Why?
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 08, 2008 9:54 pm |
|
|
T1_INTERNAL uses the crystal frequency, divided by 4. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 08, 2008 9:57 pm |
|
|
You can't run the Timer1 oscillator at 3.6864 MHz. Look in the 16F687
data sheet, on page 246 (page 248 in the Acrobat reader):
http://ww1.microchip.com/downloads/en/DeviceDoc/41262E.pdf
It shows that the typical frequency for the Timer1 oscillator is only
32.768 KHz. An older revision of the data sheet lists 200 KHz as the
maximum allowable frequency. You can't run it with a 3.6864 MHz
crystal. |
|
|
boulder
Joined: 15 Mar 2008 Posts: 53
|
|
Posted: Mon Dec 08, 2008 10:30 pm |
|
|
Is it Okay for me to use both 3686400Hz and T1_INTERNAL and in my project? Because I need this external crystal for baudrate 115200, also it works fine for Timer1 with T1_INTERNAL.
Thanks!!!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 08, 2008 10:34 pm |
|
|
That is the standard way of doing it. |
|
|
|