PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

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

Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 06-10-2012, 04:43 PM   #1
lvdinh_tvtsth
Nhập môn đệ tử
 
Tham gia ngày: Apr 2011
Bài gửi: 7
:
///đ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.
File Kèm Theo
File Type: rar MCR.rar (19.1 KB, 39 lần tải)
lvdinh_tvtsth vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời

Ðiều Chỉnh
Xếp Bài

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:29 AM.


Đượ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