PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Cơ bản về vi điều khiển và PIC (http://www.picvietnam.com/forum/forumdisplay.php?f=8)
-   -   Cần giúp về PIC16f877a điều khiển 2 động cơ DC và 1 servo! (http://www.picvietnam.com/forum/showthread.php?t=30665)

lvdinh_tvtsth 06-10-2012 04:38 PM

Cần giúp về PIC16f877a điều khiển 2 động cơ DC và 1 servo!
 
Em đan làm 1 con MCR dùng PIC16f877a điều khiển 2 động cơ DC và 1 servo. Em dùng 2 chân CCP1 và CCP2 để phát xung PWM điều khiển 2 động cơ, còn servo em dùng ngắt timer0 để điều khiển nó.
Như vậy phải dùng tới 2 ngắt là timer2 và time0 cùng lúc.
Vấn đề là lúc chỉ cho timer0 hoặc timer2 hoạt động thì nó chạy đúng (tức là chỉ động cơ hoặc servo chạy thì ổn) nhưng nếu bật cả 2 ngắt lên thì chúng không chạy.
En đã cho chạy thực tế nhiều lần mà không được :(

Bác nào có kinh nghiệm giúp em với ạ.

Code của em đây.

///////////////////////////servo chạy độc lập- chân RE0 điều khiển(chạy ổn)//////////////////
Code:

#include <16F877A.H>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)


#define d portd
#define c portc
#define e porte
#define a porta
#define b portb

#define MAX_VALUE 200

#define Right MAX_VALUE-30  //quay phai

#define Center MAX_VALUE-17  // giua

#define Left MAX_VALUE-1    //quay trai

unsigned char pulse_max=0;
unsigned char pulse_top=0;
unsigned char top_value=0;

#INT_TIMER0
void interrupt_timer0()
 {
  set_timer0(56);
  pulse_max++;           
  pulse_top++;
  if (pulse_max >= MAX_VALUE)
    {
      pulse_max=0;
      pulse_top=0;
      RE0=0;               
    }
  if (pulse_top == top_value)
    {
      RE0=1;             
    }
 }


void main(){
  trise=0x00;
  e=0;
  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2); 
  set_timer0(156); 
  enable_interrupts(global);
  enable_interrupts(int_timer0);
 
  top_value=Center;
  while(1){
      top_value=Center;  //giua
      delay_ms(5000); 

      top_value=Left;  //quay trai
      delay_ms(5000);   
 
      top_value=Center;
      delay_ms(5000);     

      top_value=Right;  //quay phai
      delay_ms(5000);   

      top_value=Left;
      delay_ms(5000);
     
     
  }
}

(còn tiếp bài dưới)

lvdinh_tvtsth 06-10-2012 04:43 PM

1 Attachment(s)
///đoạn code này cho 2 động cơ và servo cùng chạy nhưng không chạy được???////
Code:

#include <16F877A.H>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)


#define d portd
#define c portc
#define e porte
#define a porta
#define b portb

#define MAX_VALUE 200

#define Right MAX_VALUE-30  //quay phai -23

#define Center MAX_VALUE-17  // giua -17

#define Left MAX_VALUE-1    //trai -11

unsigned char pulse_max=0;
unsigned char pulse_top=0;
unsigned char top_value=0;

#INT_TIMER0
void interrupt_timer0()
 {
  set_timer0(56);
  pulse_max++;           
  pulse_top++;
  if (pulse_max >= MAX_VALUE)
    {
      pulse_max=0;
      pulse_top=0;
      RE0=0;                // Turn off RC2
    }
  if (pulse_top == top_value)
    {
      RE0=1;                // Turn On RC2
    }
 }

int value = 1;
void moto_left_dir(int1 dir){
  if(dir==0)  {
      bit_clear(a,0);
      bit_set(a,1);
  }
  else{
      bit_clear(a,1);
      bit_set(a,0);
  }
}
void moto_right_dir(int1 dir){
  if(dir==0){
      bit_clear(a,2);
      bit_set(a,3);
  }
  else {
      bit_clear(a,3);
      bit_set(a,2);
  }
}
void left_motor_forward(int value){
  moto_left_dir(1);
  setup_timer_2(T2_DIV_BY_4,124,1); // Dieu xung 10kHz
  setup_ccp1(CCP_PWM);
  set_pwm1_duty(value);
}

void right_motor_forward(int value){
  moto_right_dir(1);
  setup_timer_2(T2_DIV_BY_4,124,1); // Dieu xung 10kHz
  setup_ccp2(CCP_PWM);
  set_pwm2_duty(value);
}

void left_motor_reverse(int value){
  moto_left_dir(0);
  setup_timer_2(T2_DIV_BY_4,124,1); // Dieu xung 10kHz
  setup_ccp1(CCP_PWM);
  set_pwm1_duty(value);
}

void right_motor_reverse(int value){
  moto_right_dir(0);
  setup_timer_2(T2_DIV_BY_4,124,1); // Dieu xung 10kHz
  setup_ccp2(CCP_PWM);
  set_pwm2_duty(value);
}

void left_motor_stop(){
  setup_ccp1(CCP_OFF);
}
void right_motor_stop(){
  setup_ccp2(CCP_OFF);
}
// Chuong trình x? lý t?c d? 2 d?ng co
// 0:Stop,100:FORWARD 100%,-100:Reverse 100%

void speed (signed int left_motor_speed, signed int right_motor_speed) {
  int left_pwm_value=0, right_pwm_value=0;
  /* Left motor */
  if( left_motor_speed >= 0 ){
      left_pwm_value = 1.25*left_motor_speed; //(125*left_motor_speed/100)
      left_motor_forward(left_pwm_value);
  }
  else{
      left_motor_speed = -left_motor_speed;
      left_pwm_value = 1.25*left_motor_speed; //(125*left_motor_speed/100)
      left_motor_reverse(left_pwm_value);
  }
  /* Right motor */
      if( right_motor_speed >= 0 ){
      right_pwm_value = 1.25*right_motor_speed; //(125*left_motor_speed/100)
      right_motor_forward(right_pwm_value);
  }
  else{
      right_motor_speed = -right_motor_speed;
      right_pwm_value = 1.25*right_motor_speed; //(125*left_motor_speed/100)
      right_motor_reverse(right_pwm_value);
  }
}
void main(){
  trisc=0x00;
  trisa=0x00;
  trise=0x00;
  trisd=0x00;
  c=a=d=e=0;
  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
  setup_timer_2(T2_DIV_BY_4,124,1);
 
  set_timer0(156);
 
  setup_ccp1(CCP_PWM);
  setup_ccp2(CCP_PWM);
 
  enable_interrupts(global);
  enable_interrupts(int_timer2);
  enable_interrupts(int_timer0);
 
  top_value=Center;
  while(1){
      speed(100,100);
      top_value=Center;
      delay_ms(5000);
     
      speed(-50,-50);
      top_value=Left;
      delay_ms(5000);
   
      speed(100,100);
      top_value=Center;
      delay_ms(5000);
     
      speed(-100,-100);
      top_value=Right;
      delay_ms(5000);
     
      speed(50,50);
      top_value=Left;
      delay_ms(5000);
     
     
  }
}

Em đính kèm luôn file Proteus.

lvdinh_tvtsth 06-10-2012 04:46 PM

Nhờ các bác giúp em ạ. Nếu không được chắc em phải đầu tư con PIC18f chuyên dụng về động cơ quá! Em sinh viên nên...hicc huhu...

handsometran 13-10-2012 04:45 PM

Trích:

Nguyên văn bởi lvdinh_tvtsth (Post 78167)
Nhờ các bác giúp em ạ. Nếu không được chắc em phải đầu tư con PIC18f chuyên dụng về động cơ quá! Em sinh viên nên...hicc huhu...

Bạn xem lại hàm interrupt của bạn xem có đúng cú pháp chưa, vì tôi nhớ trong HT PIC 16 thì nó có dạng void xxx_ISR(), xxx là bạn muốn đặt gì cũng được, còn _ISR là macro trong HT Pic, nếu viết sai nó không thực hiện ngắt như mong muốn. Tôi từng gặp tình huống này rồi, mong sẽ giúp ích cho bạn.


Múi giờ GMT. Hiện tại là 10:29 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