|
|
View previous topic :: View next topic |
Author |
Message |
Phygraff
Joined: 25 Apr 2011 Posts: 1 Location: Lima, Peru
|
Using Timer1 as external pulse counter |
Posted: Tue Apr 26, 2011 7:14 pm |
|
|
I'm trying to use the timer1 feature of PIC18F2550 as a counter of external pulses (from a rotary encoder), but I'm not familiar with C programming. Anyone can help me with the basics?
Thanks. _________________ Luis A. Torres |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 27, 2011 12:10 pm |
|
|
CCS only has one example for using Timer1 as an counter with an
external clock source. I don't like, it's very messy:
Quote: | c:\program files\picc\examples\ex_freqc.c |
I don't have time today to write a more clean example. I have always
avoided using Timer1 in that mode, because there's an errata on it.
They have a work-around in the errata, but again, it would take me
some time to incorporate the errata and make a nice clean example
and I don't have time today. |
|
|
ze.vana
Joined: 11 Jun 2011 Posts: 15
|
|
Posted: Sun Jun 12, 2011 4:47 am |
|
|
PCM programmer wrote: | CCS only has one example for using Timer1 as an counter with an
external clock source. I don't like, it's very messy:
Quote: | c:\program files\picc\examples\ex_freqc.c |
I don't have time today to write a more clean example. I have always
avoided using Timer1 in that mode, because there's an errata on it.
They have a work-around in the errata, but again, it would take me
some time to incorporate the errata and make a nice clean example
and I don't have time today. |
Hi
I've done this simple code using timer0 timer1 with external inputs T0CKI RA4/T1CKI RC0. I'm sure you it works, only problem it needs external circuit
to detect pulses A/B direction, I have Proteus photo diagram, don't know how to post it.
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <lcd.c>
int16 T0,T1;
float R; //R is the result of Timer1 - Timer0
//I had to put a FLOAT insted INT16
void print_lcd(){ //because of LCD did not work well whith INT16,
lcd_init(); //I don't know why!
lcd_gotoxy(1,1);
printf(lcd_putc,"\f%g" R);
delay_ms(200);
}
void print_serial() {
t1=GET_TIMER1(); //get counter UP value
t0=GET_TIMER0(); //get counter DOWN value
R=T1-T0; //R is the actual encoder value
printf("\f R:%g T1:%Lu T0:%Lu ",R,T1,t0 );
delay_ms(200);
if(t0 > t1){ //prevent to not anti-clokwise first
SET_TIMER0(0);
SET_TIMER1(0);
}
}
#int_timer2
void timer_t2(){
static int tmp;
tmp++;
if(tmp <=5){ //it divides the timing of showing
print_lcd(); //LCD and serial
}
if(tmp>=5){
print_serial();
}
if(tmp>=10){
tmp=0;
}
}
void main(){
set_tris_a(0xff);
set_tris_b(0xff);
set_tris_c(0xff);
set_tris_d(0xff);
//TIMER0 16 bits as counter /DOWN- ext input T0CKI RA4
SETUP_TIMER_0(RTCC_EXT_H_TO_L|RTCC_DIV_1);
SET_TIMER0(0);
ENABLE_INTERRUPTS ( INT_TIMER0 );
ENABLE_INTERRUPTS ( GLOBAL );
// TIMER1 16 bits as counter /UP- ext input T1CKI RC0
SETUP_TIMER_1(T1_EXTERNAL | T1_DIV_BY_1 );
ext_int_edge(H_TO_L);
set_timer1(0);
enable_interrupts(int_timer1);
enable_interrupts(global);
//TIMER2 int
SETUP_TIMER_2(T2_DIV_BY_1,0xc0,2);
set_timer2(0);
enable_interrupts(int_timer2);
enable_interrupts(global);
while(true){
}
}
|
|
|
|
|
|
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
|