|
Tài trợ cho PIC Vietnam |
Cơ cấu chấp hành (Actuator) Động cơ DC, AC, động cơ không chổi than, động cơ không cảm biến... |
|
Ðiều Chỉnh | Xếp Bài |
11-08-2011, 01:39 PM | #1 |
Đệ tử 1 túi
Tham gia ngày: Jun 2011
Bài gửi: 14
: |
Nhờ mọi người giúp em phần phím bấm chọn chế độ...
hix,chả là em đang thử điều khiển cái động cơ DC bằng PWM pic 16f877a.
em đã thứ nghiêm nếu em chỉ viết code kiểu như sau: #include <16F877A.h> #include <def_877a.h> #device *=16 ADC=8 #FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT #use delay(clock=20000000) #use rs232(baud=38400,parity=N,xmit=PIN_C6,rcv=PIN_C7) #include <lcd_lib_4bit.c> #byte TRISB=0x86 #byte PORTB=0x06 #byte TRISD=0x88 #byte PORTD=0x08 #byte PORTC=0x07 #byte TRISC=0x87 #bit RC0=0x06.6 #bit RC1=0x06.7 void motor_forward(int16 value) { RC0=1; RC1=0; setup_timer_2(T2_DIV_BY_4,124,1); setup_ccp1(CCP_PWM); set_pwm1_duty(value); } void motor_reverse(int16 value) { RC0=0; RC1=1; setup_timer_2(T2_DIV_BY_4,124,1); setup_ccp1(CCP_PWM); set_pwm1_duty(value); } void motor_stop() {RC0=0; RC1=0; } void main() {TRISD=0x00; TRISB=0x00; int16 j,i; while(1) { motor_forward(5000);// thay bang motor_reverse hay la motor_stop deu ok cả } } nhưng khi em kết hợp với code quét bàn phím thì nó chạy khùng khùng. phần scan key: int scankey() { int keys; if (key1==0) { keys=1;key1==1;delay_ms(100);goto done;} if (key2==0) { keys=2;key2==1;delay_ms(100);goto done;} if (key3==0) { keys=3;key3==1;delay_ms(100);goto done;} done: return keys; } phần main: key=scankey(); switch(key) { case 1:motor_forward(1000); break; case 2:motor_stop(); break; case 3:motor_reverse(1000); break; mấy anh cao thue,có cái code quet bàn phim diều khiển PWM thì up lên cho em tham khảo với.em cảm ơn mọi người nhiều!!! ps:em up kèm file mo phỏng và code.Các anh xem dùm em với!!! |
|
|