mình đang viết một chương trình nhỏ đếm xung từ encoder dùng timer0 ở chế độ counter để đếm xung. Nhưng bị lỗi ở đâu đó, timer0 không đếm được. Các ban xem đoạn code va mach mô phỏng kèm theo.
Code:
==========================================================================
//dieu khien dong co DC, encoder 200 xung/vong
//Dung timer0 o che do counter de dem xung
#include<16F877A.h>
#fuses NOWDT, NOPROTECT, HS, NOLVP
#use delay(clock = 20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
int8 tocdo;
const int8 do_phan_giai = 200; // [xung/vong]
const int8 t_lay_mau = 100; // [us]
//==============================================================================
#INT_TIMER1 //Ngat khi dat duoc thoi gian lay mau
void van_toc()
{
int8 vantoc;
vantoc = get_rtcc()*4;
vantoc = vantoc/do_phan_giai/t_lay_mau;
putc(vantoc);
set_rtcc(0);
setup_timer_1(T1_DISABLED);
set_timer1(65036);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
clear_interrupt(INT_TIMER1);
}
//==============================================================================
void thiet_lap_ngat(){
enable_interrupts(int_timer1);
enable_interrupts(global);
}
//==============================================================================
void thiet_lap_ADC(){
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
set_tris_a(0xff);
}
//==============================================================================
void thiet_lap_timer(){
set_timer1(65036);
set_rtcc(0);
setup_counters(RTCC_EXT_L_TO_H,RTCC_DIV_4); //Timer0 la counter dem xung
setup_timer_1(T1_INTERNAL); //Timer1 de dem thoi gian
setup_timer_2(T2_DIV_BY_1, 127, 1); //Timer2 danh cho PWM
}
//==============================================================================
void thiet_lap_CCP(){
setup_ccp1(CCP_PWM); //Dung ccp1 de dieu khien dong co
setup_ccp2(CCP_OFF);
}
//==============================================================================
void main(){
thiet_lap_ADC();
thiet_lap_CCP();
thiet_lap_ngat();
thiet_lap_timer();
while(1){
tocdo = read_adc();
tocdo = tocdo/2;
set_pwm1_duty(tocdo);
}
}