![]() |
![]() |
#11 |
Đệ tử 4 túi
|
em cũng đang theo dõi câu trả lời cho bài dk động cơ của bạn popeyee mà chưa thấy sư huynh nào trả lời.nên em hỏi vấn đề của em luôn.
em cũng làm 1 bài tập tương tự.nhưng làm mạch thật.em dùng chính tần số PWM để kiểm tra chương trình đếm xung từ động cơ.nhưng khi percent PWM là 100 hay 0% thì LCD luôn hiện số 175 còn khi có xung thì sai lệch rất lớn 40-50%.đây là đoạn code của em.các huynh chỉ giáo!!! Code:
#include <16f877a.h> //#include <def_877a.h> #fuses NOWDT,NOPROTECT,XT,NOLVP #use delay(clock=4000000) #define use_portd_lcd True #define portb=0x06 #define portd=0x08 #include <lcd.c> #define interr_per_second 20//khi set_timer1(15535)timer 1 16 bit=>65535-15535=50.000us #define interr_per_100ms 2//khi set_timer1(15535) int8 timer1over,value,percent; int16 so_vong,count,tram,chuc,donvi,ngan; #int_rtcc void interrupt_timer0()//dem so vong dong co { ++count; } #int_timer1 // ngat timer 1 khi timer 1 tran void interrupt_timer1() { if (--timer1over==0) { timer1over=interr_per_second; so_vong=(count*255+get_timer0());//100; //voi encoder la 100 xung tính trong 1 s count=0; set_timer0(15535); } } #int_ext void ngatngoai() // tang he so duty cho DC { if(percent==0)percent=0; else { percent=percent-5; value = percent*2; } } void tao_ma_so_vong() { ngan=so_vong/1000; tram=(so_vong-ngan*1000)/100; chuc=(so_vong-ngan*1000-tram*100)/10; donvi=so_vong%10; } void hienthi_lcd() { lcd_gotoxy(1,2); lcd_putc(ngan+0x30); lcd_putc(tram+0x30); lcd_putc(chuc+0x30); lcd_putc(donvi+0x30); } //ham main chinh main() { delay_ms(100); //tao thoi gian tre de khoi tao LCD lcd_init(); // khoi tao LCd // cac cong I O set_tris_a(0xff); //cho port a la port thu, su dung chan RA4 set_tris_b(0x01); output_b(0x01); //tao muc cao cho ngat ngoai set_tris_c(0); // enable cac timer setup_timer_0(rtcc_div_1|rtcc_ext_h_to_l); // timer 0 lam couter set_timer0(0); // gia tri dau timer 0 setup_timer_1(t1_internal|t1_div_by_1); // dung inter clock,dinh thoi gian 1 s set_timer1(15535); // gia tri de ngat xay ra 20lan/ 1 s // configue cac ngat enable_interrupts(global); //cho phep ngat enable_interrupts(int_rtcc); // ngat timer 0 enable_interrupts(int_timer1); // ngat timer 1 enable_interrupts(int_ext); // cho ngat ngoai ext_int_edge(h_to_l); // ngat ngoai canh xuong // khoi tao che do PWm setup_timer_2(t2_div_by_1,199,1); // t2div=1=>f=5khz,period=200 // setup_timer_2(t2_div_by_4,199,1); //t2div=4=>f=1250hz,period=200 // setup_timer_2(t2_div_by_16,199,1); //t2div=16=>f=312hz,period=200 setup_ccp1(CCP_PWM); // xung PWm tren chan CCP1 RC2 // khoi tao cac gia tri bien ban dau count=0; so_vong=0; timer1over=interr_per_second; value=200; // do phan cung dieu xung muc thap percent=100; //phan cung dung xung muc thap // tao mau cho lcd lcd_gotoxy(1,1); lcd_putc('s'); lcd_putc('o'); lcd_putc('-'); lcd_putc('v'); lcd_putc('o'); lcd_putc('n'); lcd_putc('g'); lcd_putc('-'); lcd_putc('1'); lcd_putc('s'); lcd_putc(':'); //bat dau vong lap while(1) { set_pwm1_duty(value); tao_ma_so_vong(); hienthi_lcd(); } } thay đổi nội dung bởi: cong1810, 02-09-2008 lúc 12:43 AM. |
![]() |
![]() |
|
|