จะใช้ Code นี้ได้ต้อง Copy QEI Lib เข้าไปไว้ใน 18F2431, 18F4431, 18F2331, 18F4331 แล้วแต่ว่าใช้เบอร์ไหน
#include <18F2431.h>
#use delay(clock=20000000)
#fuses HS,PUT,BROWNOUT,MCLR,NOWDT,NOPROTECT,NOLVP
#define TxD PIN_C6
#define RxD PIN_C7
#use rs232(baud=115200, xmit=TxD,rcv=RxD)
void main()
{
POSCNT = 0;
MAXCNT = 0xFFFF;
QEICON = 24; // quad in x4 mode, resettable by maxcount
enable_interrupts(GLOBAL);
enable_interrupts(INT_IC2QEI);
while(true)
{
printf("%lu\r\n", POSCNT);
}
}
Lab14: การวัด Velocity Encoder
#include <18F2431.h>
#use delay(clock=20000000)
#fuses HS,PUT,BROWNOUT,MCLR,NOWDT,NOPROTECT,NOLVP
#define TxD PIN_C6
#define RxD PIN_C7
#use rs232(baud=115200, xmit=TxD,rcv=RxD)
void main()
{
int16 pos1,pos2;
float time;
float Vel;
POSCNT = 0;
MAXCNT = 0xFFFF;
QEICON = 24;
enable_interrupts(GLOBAL);
enable_interrupts(INT_IC2QEI);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
while(true)
{
set_timer1(0);
pos1 = POSCNT;
printf("%.3f\r\n", Vel);
pos2 = POSCNT;
time = get_timer1();
time = time*8*0.0000002;
if(pos2>pos1){
Vel = (float)(pos2 - pos1)/time;
}
else{
Vel = (float)(pos1 - pos2)/time;
}
}
}