CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Rotary Encoder

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
ze.vana



Joined: 11 Jun 2011
Posts: 15

View user's profile Send private message Send e-mail

Rotary Encoder
PostPosted: Sun Jun 12, 2011 4:08 am     Reply with quote

Hello
It is my first time participating at forum.
I'm Jose from Brazil and I'd like to share discoveries and doubts too with all you.
Well I have been reading some posts about Rotary Encoders, I've been interested in it. So it's a long
time ago that I've been trying to develop an sin/cos and TTL Encoder Tester that works
fast and does not lose pulses. Then I got to implement this. Sure for not to get it more complicated
I added an external hardware (just a dual flip-flop D and four Nand gates) to detecting direction
A and B encoder pulses. It really works very well, Encoder can be rotated in high speed and it maintain
the zero point value. But it still needs some improvement. Looking at the code comments you can see
some difficulties that I have (like showing value on a LCD). Although I've been working for years on CNC/PLC
programming, I am beginner in CCS programming. If someone has an idea of improvement the code I would appreciate 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 instead INT16
void print_lcd(){ //because of LCD did not work well with 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-clockwise 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){
    }
  }
Jerry I



Joined: 14 Sep 2003
Posts: 96
Location: Toronto, Ontario, Canada

View user's profile Send private message

PostPosted: Tue Jun 14, 2011 3:50 am     Reply with quote

Quote:

I added an external hardware (just a dual flip-flop D and four Nand gates) to detecting direction A and B encoder pulses.


Do you have a schematic of how you added the external hardware.

Thanks
ze.vana



Joined: 11 Jun 2011
Posts: 15

View user's profile Send private message Send e-mail

Hardware scheme
PostPosted: Thu Jun 16, 2011 2:59 pm     Reply with quote

I don't know how to put image here maybe is not allowed, I can send it by email: [email protected].
Jose
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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