PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Các ngôn ngữ lập trình khác (CCS C, HT PIC,...)

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 08-04-2008, 05:18 PM   #1
dtv
Nhập môn đệ tử
 
dtv's Avatar
 
Tham gia ngày: Apr 2008
Bài gửi: 4
:
Unhappy Tai sao counter timer0 Khong dem duoc :(

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);
   }
}
Hình Kèm Theo
File Type: jpg demxung.JPG (158.8 KB, 24 lần tải)
File Kèm Theo
File Type: rar demxung.rar (18.4 KB, 56 lần tải)

thay đổi nội dung bởi: namqn, 08-04-2008 lúc 06:37 PM.
dtv vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 01:59 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam