View previous topic :: View next topic |
Author |
Message |
54kemal5461
Joined: 26 May 2021 Posts: 5
|
PIC16F1829 timer1 with external clock not working help |
Posted: Wed May 26, 2021 8:06 am |
|
|
Hello everyone, I want to toggle the output of pin_c7 at 1 second intervals using timer1 interrupt. I know when using internal clock signal, but when I add external clock signal, it does not work within the time I want. I use the same method in two calculations, what is the reason for this?
This code also gives an output of 0.8 seconds, but is not the same as the calculation (I am using 20MHZ crystal).
Code: | #include <16F1829.h>
//#FUSES NOWDT,ECH,CLKOUT, NOPROTECT, NOMCLR, NOBROWNOUT
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD,ECH,CLKOUT
#use delay(clock=20000000)
#use fast_io(a) //Port yönlendirme komutları A portu için geçerli
#use fast_io(b) //Port yönlendirme komutları B portu için geçerli
int16 SAYAC_YNLME=0,SAYAC_YNLME_1=0,Saniye_1=0;
unsigned int A=0,Saniye=0;
#INT_TIMER1
void interrupt1()
{
if(INT_TIMER1)
{
clear_interrupt(INT_TIMER1);
set_timer1(55550);
SAYAC_YNLME++;
SAYAC_YNLME_1++;
if(SAYAC_YNLME>=10)
{
Saniye++;
output_toggle(pin_c7);
SAYAC_YNLME=0;
Saniye_1++;
}
}
}
void main()
{
set_tris_a(0b00100100);
set_tris_b(0b00000000);
set_tris_c(0b00000000);
setup_wdt(WDT_OFF);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_4(T4_DISABLED,0,1);
setup_timer_6(T6_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_spi(SPI_DISABLED);
setup_comparator(NC_NC_NC_NC);
setup_dac(DAC_OFF);
setup_adc(ADC_OFF);
setup_adc_ports(NO_ANALOGS);
output_a(0x00);
output_b(0x00);
output_c(0x00);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_ENABLE_T1OSC);
set_timer1(55550);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(TRUE)
{
if(input(pin_a2) ==1 && A==0)
{
A =1;
output_toggle(pin_c7);
}
if(input(pin_a2) ==0 && A==1)
{
A =0;
}
}
}
|
thank you in advance for your help |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Wed May 26, 2021 8:12 am |
|
|
What do you have connected to the T1OSI and T1OSO pins? |
|
|
54kemal5461
Joined: 26 May 2021 Posts: 5
|
|
Posted: Wed May 26, 2021 8:20 am |
|
|
gaugeguy wrote: | What do you have connected to the T1OSI and T1OSO pins? |
ECS-200-20-4X-DU crystal and 20 pf capacities. The datasheet shows the 2nd and 3rd legs and I tied them there. |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Wed May 26, 2021 9:38 am |
|
|
The data sheets shows the T1 oscillator circuit is only designed for a 32kHz crystal, not a 20MHz one. The T1 oscillator is not the same as the primary chip clock oscillator. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 26, 2021 10:06 am |
|
|
In addition to that, the lines shown in bold below are not needed
and should be removed.
Quote: | #INT_TIMER1
void interrupt1()
{
if(INT_TIMER1)
{
clear_interrupt(INT_TIMER1);
set_timer1(55550);
SAYAC_YNLME++;
SAYAC_YNLME_1++;
if(SAYAC_YNLME>=10)
{
Saniye++;
output_toggle(pin_c7);
SAYAC_YNLME=0;
Saniye_1++;
}
} // Remove this brace
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed May 26, 2021 10:52 am |
|
|
I see others have pointed out the problem.
I was going to quote from the data sheet:
Quote: |
A dedicated low-power 32.768 kHz oscillator circuit is
built-in between pins T1OSI (input) and T1OSO
(amplifier output). This internal circuit is to be used in
conjunction with an external 32.768 kHz crystal.
|
Not going to work with a 12MHz crystal..... |
|
|
54kemal5461
Joined: 26 May 2021 Posts: 5
|
|
Posted: Wed May 26, 2021 10:56 am |
|
|
gaugeguy wrote: | The data sheets shows the T1 oscillator circuit is only designed for a 32kHz crystal, not a 20MHz one. The T1 oscillator is not the same as the primary chip clock oscillator. |
Thanks, now I understand what you mean, I will try and reach the result as soon as possible. |
|
|
54kemal5461
Joined: 26 May 2021 Posts: 5
|
|
Posted: Wed May 26, 2021 10:58 am |
|
|
PCM programmer wrote: | In addition to that, the lines shown in bold below are not needed
and should be removed.
Quote: | #INT_TIMER1
void interrupt1()
{
if(INT_TIMER1)
{
clear_interrupt(INT_TIMER1);
set_timer1(55550);
SAYAC_YNLME++;
SAYAC_YNLME_1++;
if(SAYAC_YNLME>=10)
{
Saniye++;
output_toggle(pin_c7);
SAYAC_YNLME=0;
Saniye_1++;
}
} // Remove this brace
} |
|
thanks i will fix them too |
|
|
54kemal5461
Joined: 26 May 2021 Posts: 5
|
|
Posted: Wed May 26, 2021 11:01 am |
|
|
Ttelmah wrote: | I see others have pointed out the problem.
I was going to quote from the data sheet:
Quote: |
A dedicated low-power 32.768 kHz oscillator circuit is
built-in between pins T1OSI (input) and T1OSO
(amplifier output). This internal circuit is to be used in
conjunction with an external 32.768 kHz crystal.
|
Not going to work with a 12MHz crystal..... |
As far as I understand, it seems like a problem to be solved when I use only 32 MHZ crystal. Is there any code related to t1 I need to add in the other software? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed May 26, 2021 11:28 pm |
|
|
If you are using a single external crystal, then just clock T1 from this.
The 'point' about T1 having it's own oscillator, is a series of things. First
you can use a 32KHz 'watch' crystal on this, and not use any crystal at
all on the CPU (use it's internal oscillator), and still have an accurate
'timing' source. Then this oscillator is very low power, allowing this to
remain running while the CPU is largely shut down for low power applications.
This is why the datasheet points out that this oscillator will continue to run
during sleep. Allows an accurate RTC with very low power.
32K crystals are amongst the most stable ones you can get. At this low
frequency they can be 'tuning fork' crystals, which are amongst the most
temperature stable type. So for only a few pence, you can get units with
5PPM 'accuracy'. Only a couple of minutes a year drift, over normal operating
temperature ranges. |
|
|
Woody
Joined: 11 Sep 2003 Posts: 83 Location: Warmenhuizen - NL
|
|
Posted: Thu May 27, 2021 4:35 am |
|
|
54kemal5461 wrote: |
As far as I understand, it seems like a problem to be solved when I use only 32 MHZ crystal. Is there any code related to t1 I need to add in the other software? |
To make sure you get the numbers right: 32 Kilohertz, not 32 Megahertz. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Thu May 27, 2021 5:23 am |
|
|
There is another thing.
The approach being used will potentially waste a large amount of
processor time. If timer1 was clocked off an external 20Mhz crystal, it'd
be counting in 1/20000000th second 'ticks'. Now he uses /1 for the
prescaler, and sets the timer to 55500 inside the interrupt. So it'd only
be counting 10036 cycles of this clock. Just under 2000*/second. Each
time the interrupt code triggers the processor will spend perhaps 80
instruction cycles in the interrupt code, so the processor will be spending
about 3% of it's resources just handling this task.
Much better if he just wants a 1KHz signal, to just use a PWM.
Zero processor usage. |
|
|
|