|
Tài trợ cho PIC Vietnam |
Lỗi lập trình ngôn ngữ cấp cao Những lỗi trường gặp trong lập trình. Khóa luồng khi bug được lỗi. |
|
Ðiều Chỉnh | Xếp Bài |
21-08-2008, 02:01 AM | #3 |
Nhập môn đệ tử
Tham gia ngày: Apr 2008
Bài gửi: 6
: |
Ct của mình dùng đk 1 tay máy 3 trục dùng 3 đcơ bước nên nó dài dòng quá, ban đầu post lên sợ ko ai đọc.
Mình tóm tắt là có 3 motor bước, mỗi cái quay 1 trục, trên hành trình có gắn 3 ctắc để set vị trí home, ctắc nối vào chân ngắt của RB. Giao tiếp RS232 báo ngắt khi bộ đệm nhận dữ liệu đầy. Hoạt động: gửi 1 kí tự xuống PIC thì ngắt nối tiếp RDA gọi hàm quay động cơ tương ứng. Gửi kí tự "O" thì ngắt RDA đk 3 trục về vtrí home, bằng cách là khi mỗi trục gặp ctắc, hàm ngắt RB đặt mức ưu tiên cao hơn sẽ ngắt hàm RDA, cho dừng và quay 3 trục về home. Vấn đề ở đây là ngắt RB ko ngắt đc hàm RDA đang thực thi, nên 3 động cơ không dừng như mong muốn. Nếu ngắt RDA ko chạy thì ngắt RB vẫn hoạt động tốt. Mình cũng thử là cho RB ngắt khi chạy phát xung ngoài hàm main, thì vẫn ko ngắt đc. Trình dịch cảnh báo Warning 216 như mình nói. đây là code, mọi ngừoi chịu khó xem giúp mình: Code:
#include <16F877A.h> #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) #priority rb,rda #byte porta=0x05 #byte portb=0x06 #byte portd=0x08 #bit catch=porta.3 // ngo ra nam cham #bit clock1=porta.2 // Step motor 1 #bit en_step1=porta.1 #bit dir1=porta.0 #bit clock2=portd.7 // step motor 2 #bit en_step2=portd.6 #bit dir2=portd.5 #bit en_step3=portd.3 // step motor 3 #bit dir3=portd.2 #bit clock3=portd.1 int count1; // bien dem so xung motor1 int16 count2,count3; // motor2 va 3 char ss; void rotate_high1() // ham phat xung cho motor 1 { // quay toc do cao clock1=1; delay_us(30000); clock1=0; delay_us(500); } void rotate_low1() // motor 1 toc do thap { clock1=1; delay_us(50000); clock1=0; delay_us(500); } void rotate_high2() { clock2=1; delay_us(1000); clock2=0; delay_us(100); } void rotate_low2() { clock2=1; delay_us(2000); clock2=0; delay_us(100); } void rotate3() { clock3=1; delay_us(700); clock3=0; delay_us(100); } //************************************************** void left_right() // ham quay motor 1 { int tam; tam=count1; en_step1=1; while (count1>0) { count1--; if ((count1>30)&&(count1<(tam-30))) { rotate_high1(); } else { rotate_low1(); } } count1=0; delay_ms(500); } //**************************************************** void go_up_down() // ham quay motor 2 { en_step2=1; while (count2>0) { count2--; if (dir2==0) { rotate_low2(); } else { rotate_high2(); } } count2=0; delay_ms(500); en_step2=0; } //***************************************************** void in_out() // ham quay motor 3 { en_step3=1; while (count3>0) { count3--; rotate3(); } delay_ms(1000); en_step3=0; count3=0; } //******************************************************* #int_RB void rb_isr() // ham phuc vu ngat RB { int current; int last; set_tris_b(0xff); current=input_b(); last=current; delay_ms(50); set_tris_b(0xff); current=input_b(); if (current==last) { clear_interrupt(int_RDA); if (!bit_test(current,4)) // ctac tren mam xoay bao ngat { if (home) // chi thuc hien khi o che do home: { // quay mam ben phai 60 do en_step1=0; count1=70; dir1=1; en_step1=1; while (count1>0) { count1--; if ((count1>30)&&(count1<(70-30))) { rotate_high1(); } else { rotate_low1(); } } count1=0; } } if (!bit_test(current,5)) // ctac trong bao ngat { en_step3=0; delay_ms(500); count3=2000; dir3=0; in_out(); } if (!bit_test(current,6)) //ctac tren bao ngat { en_step2=0; delay_ms(500); count2=500; dir2=1; go_up_down(); count2=0; } if (!bit_test(current,7)) // cam bien phat hien co vat den { putc('y'); delay_ms(10); } } clear_interrupt(int_RB); } //**************************************************************** #int_RDA // ham phuc vu ngat noi tiep void RDA_isr() { delay_us(50); ss=getc(); if (ss=='u') // di len { count2=2000; dir2=0; go_up_down(); printf("done"); } if (ss=='d') // di xuong { count2=2000; dir2=1; go_up_down(); printf("done"); } if (ss=='l') // quay trai { count1=70; //1 lan quay 60 do dir1=0; left_right(); printf("done"); } if (ss=='r') // quay phai { count1=70; dir1=1; left_right(); printf("done"); } if (ss=='i') //di vao { count3=4000; //1 lan di chuyen 30mm dir3=1; in_out(); printf("done"); } if (ss=='o') // di ra { count3=4000; dir3=0; in_out(); printf("done"); } if (ss=='#') // test comm { putc('!'); delay_us(100); } if (ss=='O') // go home: dieu khien tay may { // ve vi tri home home=1; count1=100; dir1=0; left_right(); count2=2000; dir2=0; go_up_down(); count3=2666; dir3=1; in_out(); home=0; } if (ss=='1') // chuong trinh di chuyen cho vat 1 { count2=10000; // chay xuong dir2=1; go_up_down(); catch=1; // gap vat count2=10000; // di len dir2=0; go_up_down(); count1=102; // xoay 90 do dir1=1; left_right(); count3=13333; // di ra dir3=0; in_out(); count2=10000; // ha xuong dir2=1; go_up_down(); catch=0; // nha vat count2=10000; // di len dir2=0; go_up_down(); count1=102; // xoay 90 do dir1=0; left_right(); count3=13333; // di vao dir3=1; in_out(); } clear_interrupt(int_rda); } void main() { enable_interrupts(INT_RB|H_TO_L); enable_interrupts(GLOBAL); ext_int_edge(H_TO_L); enable_interrupts(INT_RDA); set_tris_d(0x00); set_tris_b(0xff); set_tris_a(0x00); printf("reset"); while (1) { } } thay đổi nội dung bởi: bazooka2006, 21-08-2008 lúc 02:07 AM. |
|
|