View previous topic :: View next topic |
Author |
Message |
isv10
Joined: 29 Aug 2010 Posts: 15
|
help for use timer1 in external mode |
Posted: Tue Sep 04, 2012 8:02 am |
|
|
hello
I want to configure timer1 in external clock mode.
And the clock is provided by the 32.768 khz crystal.But it does not work.
I want to create accurate 1 second time with this crystal. Anyone can do to help me please.
1-what is problem ?
2-If i use this function (set_timer1(value)) for initialized this timer in
beginning of the program need not to use it in another line program?
Code: | #include <18f452.h>
#fuses HS
#use delay(Clock=20000000)
#include <stdio.h>
int time[3];
#int_TIMER1
void TIMER1_isr(void)
{
time[0]=time[0]+1;
if(time[0]==60){
time[0]=0;
time[1]=time[1]+1;
}
if(time[1]==60){
time[1]=0;
time[2]=time[2]+1;
}
}
void main()
{
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_8);//start timer
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(true)
{
.
.
. |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue Sep 04, 2012 8:08 am |
|
|
Code: |
setup_timer_1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_8);//start timer
|
You are telling T1 to use an external clock input, but not telling it to feed the signal back out, to form an oscillator.....
Best Wishes |
|
|
isv10
Joined: 29 Aug 2010 Posts: 15
|
|
Posted: Tue Sep 04, 2012 11:09 pm |
|
|
hi
So thanks.
In this case, I should use _t1oso and t1osi_ for connect crystal to 18f452 ???
and not need anything. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Sep 05, 2012 1:56 am |
|
|
Don't know where your underbar characters are coming from....
The pins are T1OSO, and T1OSI.
If you look at the data sheet, the oscillator has a nice little drawing.
T1OSCEN, is the setting T1_CLK_OUT
TMR1CS, is T1_EXTERNAL
Then the DIV_BY_xxx settings control T1CKPS0:1
The earlier settings were enabling the clock input, but not enabling the oscillator.
Best Wishes |
|
|
isv10
Joined: 29 Aug 2010 Posts: 15
|
|
Posted: Wed Sep 05, 2012 1:57 pm |
|
|
Dear Ttelmah
Thanks for your guide. I realized that it should connect crystal to T1OSO and T1OSI and config timer1 like that and then i can use this timer:
Code: |
setup_timer_1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
|
And if i want to initialized the timer ,necessary use this function before main loop.
|
|
|
isv10
Joined: 29 Aug 2010 Posts: 15
|
|
Posted: Wed Sep 05, 2012 2:06 pm |
|
|
Mr.Ttelmah
This project when it working ,Sometimes restart and i cant know what i do?
or send unknown character for lcd?
and A/D is not stable? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Sep 05, 2012 2:34 pm |
|
|
This is an open forum. Generally you shouldn't address a problem to one person.
Most likely possibility - supply is not adequately regulated, or a poor ground.
Separate comment:
Quote: | And if i want to initialized the timer ,necessary use this function before main loop.
set_timer1(value); |
No. The timer needs to be re-initialised, every time it overflows.
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Sep 05, 2012 3:57 pm |
|
|
got a schematic to post?
that might point to your restart trouble
also just for fun
Code: |
unsigned int32 mytix,tix=0;
#int_TIMER1
void TIMER1_isr(void){ ++tix;}
// then only when you want the time as H:M:S
do{ mytix=tix; }while mytix!=tix;
time[0]=mytix%60; // secs
time[1]=(mytix/3600)%60; //mins
time[2]=(mytix/86400)%24; // hrs
//
|
UNLESS this is a school project and a clock display is the only product - then the way you did it is going to obviously be OK 2 , as it mostly works now.
|
|
|
isv10
Joined: 29 Aug 2010 Posts: 15
|
|
Posted: Wed Sep 05, 2012 11:15 pm |
|
|
Thanks
1- Can I use set_timer1(value) in timer1 interrupts?
2- What means poor ground??
Code for A/D:
Code: |
#include <18f452.h>
#fuses HS
#device ADC=10
#use delay(Clock=20000000)
.
.
.
setup_adc_ports(AN0_AN1_AN2_AN4_AN5_VSS_VREF);
setup_adc(ADC_CLOCK_DIV_64);
.
.
.
set_adc_channel(1);
delay_us(15);
for(j=1;j<=20;j++){
TT=TT+read_ADC();
delay_ms(1);
}
TT=TT/20;
TT=(TT*4.882);
TT=(TT-1000)/3.86;
sensor1[1]=TT;
|
|
|
|
|