PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Các ngôn ngữ lập trình khác (CCS C, HT PIC,...) (http://www.picvietnam.com/forum/forumdisplay.php?f=12)
-   -   Tai sao counter timer0 Khong dem duoc :( (http://www.picvietnam.com/forum/showthread.php?t=2230)

dtv 08-04-2008 05:18 PM

Tai sao counter timer0 Khong dem duoc :(
 
2 Attachment(s)
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);
  }
}


dtv 09-04-2008 12:31 PM

Tìm ra lỗi rồi
 
mình đã tìm ra chỗ sai trong đoạn code trên rồi:
1) chu kỳ xung PWM quá nhỏ làm cho động cơ trong mô phỏng không chạy. do đó timer0 không đếm lên. Sửa lại là: setup_timer_2(T2_DIV_BY_16, 255, 1);
2) giá trị "vantoc = vantoc/do_phan_giai/t_lay_mau;" quá nhỏ

Mong các bạn góp ý về mấy cái sai ở trên :D


Múi giờ GMT. Hiện tại là 04:11 AM.

Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam