|
|
View previous topic :: View next topic |
Author |
Message |
mahdifahime
Joined: 05 Jan 2013 Posts: 22
|
problem by timer2 |
Posted: Sat Jan 05, 2013 10:38 am |
|
|
Hi Everybody
I working with timer2, but the exact timing can not be obtained.
No problem with timer 1 and exact time, but in timer2 and timer0 I can not fix the exact time.
The program is:
Code: |
#include <16f72.h>
#device adc=10
#use delay (crystal=20Mhz)
#fuses hs,NOWDT,noput,noBROWNOUT,PROTECT
#define led pin_c5
#use i2c(master,SDA=PIN_C4,SCL=PIN_C3)
#include <2408.c>
#byte t2con=0x12
#byte pr2=0x92
#byte pie1=0x8c
#byte INTCON = 0x0B
#byte pir1=0x0c
#bit TMR2ON = T2CON.2
int8 ll;
//--------------------------------------------------------------
#int_timer0
void Timer0_isr(){shomaresh+=1;feshari+=1;set_timer0(0); return;}
//------------------------------------------------------------------------------
#int_timer2
void timer2_isr() { ll+=1; if(ll>=100){ll=0;output_toggle(led);}
set_timer2(0);
setup_timer_2 ( T2_DIV_BY_16,250,16); }
//-------------------------------------------------------------------
void main(){
setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_256);
interrupt_active(INT_TIMER0);
setup_timer_2 ( T2_DIV_BY_16,250,16);
enable_interrupts(INT_timer2);
enable_interrupts(GLOBAL);
set_tris_b(0b00000101);set_tris_a(0b0000011);
output_high(led);delay_ms(3000);
while(true){
tmr2on=1;
continuation program.........} |
Version: 4.128
Please help me to get the exact time of the timer2 and timer0.
Thanks
Last edited by mahdifahime on Sat Jan 05, 2013 12:32 pm; edited 1 time in total |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Jan 05, 2013 11:52 am |
|
|
please post a complete program that
actually SHOWS the timing error and magnitude that troubles you.
BTW: you show no INT handlers either ...???
i assume there is some external circuit - so show that please as well |
|
|
mahdifahime
Joined: 05 Jan 2013 Posts: 22
|
|
Posted: Sat Jan 05, 2013 12:31 pm |
|
|
I use internal Oscillator for timer2
i want the led blink alternately but not blink alternately |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Jan 05, 2013 12:57 pm |
|
|
Code: |
set_timer2(0);
setup_timer_2 ( T2_DIV_BY_16,250,16);
|
the lines are redundant
if you get RID of these two lines above in your timer2 ISR -
then the ISR will be called every 13.36 msecs
and the rest is math |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Jan 05, 2013 1:24 pm |
|
|
Don't set the timers to zero in the ISR. The ISR is _called_ when the timer resets to zero.
Give you variables proper names. 'll' doesn't exactly tell you anything about the variable. Names cost _nothing_.]
Get rid of all your register declarations. You generally don't need these in CCS.
Are you aware of the RMW problem in PIC's. Are you sure that your load resistor on the LED is large enough?.
Don't change timer settings inside the interrupt. Once these are set, they remain set.
Your TRIS settings basically do nothing, since you are not using fast_io.
Timer2, is already turned on.
Stuffing code on one line does not help anyone.
'interrupt_active', tests an interrupt. Returns it's value. As you have it, it does nothing....
You are aware the 16F72, only has an 8bit ADC. ADC=10, just wastes time.
Code: |
#include <16f72.h>
#device adc=8
#use delay (clock=20Mhz) //you are selecting 'crystal' with HS
#fuses HS,NOWDT,NOPUT,NOBROWNOUT,PROTECT
//keep case consistent
#define led pin_c5
#use i2c(master,SDA=PIN_C4,SCL=PIN_C3)
#include <2408.c>
int8 tick;
#int_timer2
void timer2_isr(void) {
if(++tick>=100) {
tick=0;
output_toggle(led);
}
}
//-------------------------------------------------------------------
void main(void){
setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_2 ( T2_DIV_BY_16,250,16);
enable_interrupts(INT_timer2);
enable_interrupts(GLOBAL);
output_high(led);
//delay is pointless
while(TRUE) {
}
}
|
With your clock, the LED will toggle every (4*251*16*16*100)/20000000 seconds. 1.28512 seconds. Make your tick at a sensible value. Say:
setup_timer_2 (T2_DIV_BY_16,249,10);
Then gives 125 ticks per second.
Makes timing calculations much simpler:
Code: |
int8 tick=125;
#int_timer2
void timer2_isr(void) {
if(--tick==0) {
tick=125;
output_toggle(led);
}
}
|
Best Wishes |
|
|
mahdifahime
Joined: 05 Jan 2013 Posts: 22
|
|
Posted: Sat Jan 05, 2013 1:50 pm |
|
|
Very good, thanks.
The problem was solved. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|