View Single Post
Old 18-04-2010, 12:33 PM   #2
betabeta
Đệ tử 1 túi
 
Tham gia ngày: Nov 2008
Bài gửi: 15
:
Post

Mình tìm được cái này trên mạng, thấy dùng tốt.
Trong file .h thêm vào các define
//Defines specific for 18F2431 which are not present in 18F2431.H file
//bits of QEICON Quadrature Encoder Interface control register
#define DISABLE_VELOCITY 0x80
#define QEI_OFF 0x00
#define QEI_2XINDX 0x04
#define QEI_2XPER 0x08
#define QEI_4XINDX 0x14
#define QEI_4XPER 0x18
#define VELOCITY_DIV_1 0x00
#define VELOCITY_DIV_4 0x01
#define VELOCITY_DIV_16 0x02
#define VELOCITY_DIV_64 0x03

int16 MAXCNT;
#byte MAXCNT = 0xF64
#byte MAXCNTL = 0xF64
#byte MAXCNTH = 0xF65

#byte QEICON = 0xFB6
//bits in QEICON
#define QEI_NOT_VELM 7
#define QEI_ERROR 6
#define QEI_DIRECTION 5
#bit QD=0xFB6.5
int16 POSCNT;
#byte POSCNT = 0xF66
#byte POSCNTH = 0xF67
#byte POSCNTL = 0xF66
int16 VREG;
#byte VREG = 0xF68
#byte VREGH = 0xF69
#byte VREGL = 0xF68
#byte DFLTCON = 0xF60 //Digital Filter Control Register
#byte CAP1CON = 0xF63

trong file .c phần main thêm vào các thiết lập

enable_interrupts(INT_IC1);
enable_interrupts(INT_IC2QEI);
enable_interrupts(INT_IC3DR);
enable_interrupts(int_rda);

QEICON = QEI_2XPER | VELOCITY_DIV_4;
MAXCNT=600;
QEICON &=0b01001111; //clear Velocity mode bit to enable
CAP1CON = 0b01001000; //enable Time Base reset (TMR5)
//TMR5 used for velocity measurement
setup_timer_5 (T5_INTERNAL | T5_DIV_BY_1);




/////////////////////////////////////////////////////

enable_interrupts(global);

viết thêm các chương trình ngắt cho nó
#int_IC1
// Velocity capture interrupt
void IC1_isr()
{
f=1;
}
#int_IC2QEI
/*
Position counter interrupt
Increment (decrement) variable MSB_position that provides a 32-bit position
counter along with hardware position counter
*/
void IC2QEI_isr()
{
dao=!dao;
OUTPUT_BIT(PIN_B5,dao);
}

#int_IC3DR
/*
Direction of movement interrupt
*/

void IC3DR_isr()
{
}
betabeta vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn