PIC Vietnam

Go Back   PIC Vietnam > Robotics > Thực hành

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

Thực hành Bắt đầu làm một robot như thế nào? Mẹo vặt? Kỹ thuật? Công nghệ?... Hãy bắt tay vào việc...

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 16-05-2007, 03:07 AM   #11
gaumisa
Nhập môn đệ tử
 
Tham gia ngày: May 2007
Bài gửi: 10
:
Help Me !

minh co tham khao doan code cua bluepine va lam nhu sau : minh da tao 4 nut nhan tren Vb : opencom , closecom , start , stop . Nut start se gui gia tri "a" , stop goi gia tri "b" .
Phan pic minh viet voi muc dich nhu sau :
dung ngat RDA de khi co 1 bye du lieu truyen tu may tinh ( ki tu "a" hay "b" ) , thi con pic dung hoat dong , nhay den gan gia tri do ( khong biet gia tri luc do la ma hay la ki tu nhi ) vao bien tam va kiem tra !
Dung ngat timer0 lam chu ki lay mau , va doc gia tri timer1 ( doc encoder ; chan ccp2 : gan voi tin hieu encoder )
chuong trinh nhu sau :

Code:
#include <16f877a.h>
#device *=16 ADC=10 
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP,NOCPD,NOWRT,NODEBUG
#use delay (clock = 20000000)
// Giao tiep vi dieu khien
#use rs232(baud=9600, parity=N,xmit=PIN_C6, rcv=PIN_C7 ) // Giao tiep RS232
#use i2c(master,fast,sda=PIN_C4,scl=PIN_C3,FORCE_HW)
#use fast_io(A)
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include<ctype.h>

#byte PR2 = 0x92

int16 e2;
int16 e1;
///////////////////////////////////////////////////////

int16 v_set = 200;

/////////////////////////////////////////////////////////
int16 v_cur;
int16 e_sum;
int16 e_del;
int16 flag_timer1;
int16 i=1 ;

///////////////////////////////////////////////////////////
unsigned long kp =5;
unsigned long ki = 0.62;
unsigned long kd = 10;
////////////////////////////////////////////////////////////


int16 pw_duty;

int16 temp_timer1;

char tam  ;
#byte TMR0 = 0x01 //timer0 REGISTER
#byte TMR1_L = 0X0E
#byte TMR1_H = 0X0F

#define START_VALUE_TIMER0 5 //tri khoi tao ban dau cua timer0 55, so xung dem duoc moi khi ngat la 255 - 5
#define START_VALUE_TIMER1 15535 //tri khoi tao ban dau cua timer1 65536-15536
#define dir PIN_B3 //or #bit dir = 0x6.3
#define brk PIN_B4
#define pwm PIN_C2
#priority RDA , timer0 
#INT_RDA// ngat khi nhan 1 bye 
RDA_isr()
{

tam=getc(); // lay gia tri tu cong  
}

#INT_TIMER0 // ngat timer0 tang bien len 1
void timer0_int()
{

   set_timer0(START_VALUE_TIMER0);
   i++ ;


   if ( i==50 )//neu nhu sau 50 lan tran cua timer0 (40ms:chu ki lay mau ) thi :

      temp_timer1 = get_timer1();
      set_timer1(START_VALUE_TIMER1);//lan tran thu 50 cua timer0 se set timer1
      v_cur =  8*(temp_timer1 - START_VALUE_TIMER1);
      i = 0;
      printf("%ld\t",pw_duty);
      flag_timer1 = 1;
}


void init_timer0()
{

setup_timer_0(RTCC_INTERNAL | RTCC_DIV_16); // timer0 dung xung ngoai va chia xung vao


setup_timer_1(T1_EXTERNAL | T1_DIV_BY_8); // 

set_timer0(START_VALUE_TIMER0);
set_timer1(START_VALUE_TIMER1);
enable_interrupts(INT_RDA);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
}

void init_PWM(int16 frequency){

setup_ccp1(CCP_PWM); // initiate PWM
PR2 = 20000000/4/frequency - 1; // set PWM period

setup_timer_2(T2_DIV_BY_1,255,1); // initiate time 2 The cycle time will be (1/clock)*4*t2div*(period+1)
// (1/20000000)*4*1*(255+1) = 51.2 us( will over flow every 51.2 us, will intrup every 51.2 uS or 19.5 khz;

}

void cal_pid(){

long temp_kp;
long temp_ki;
long temp_kd;
e2 = v_set - v_cur;
e_sum += e2;
e_del = e2 - e1;
e1 = e2;

temp_kp = kp*e2;
temp_ki = ki*e_sum;
temp_kd = kd*e_del;
pw_duty +=temp_kp;

if (pw_duty <1000)
pw_duty += temp_ki;
if (pw_duty <1000)
pw_duty += temp_kd;
if (pw_duty <1000) // vi khi pw_duty <256 thi PWM chi dieu rong xung 8 bit / //chu khong phai 10 bit,
pw_duty = 256;
if (pw_duty >1000) // bao hoa
pw_duty = 1000;

printf(" \t%ld", v_cur); // truyen toc do xung ve may tinh hien thi tren Hyperterminal
}

void main()
{
int8 timer0_value;

init_timer0();
init_PWM(19500);

while(tam=='a') //vd 'a' ung voi nut quay thuan // khac a , khac (b) la stop
 {


      if (flag_timer1 ==1)
      {
            flag_timer1 = 0;
            cal_pid();
            output_low(pwm);// tat 4 con fet tuc thoi
            delay_us(20);
            output_low(brk);
            output_low(dir);
            delay_us(50);
            set_pwm1_duty(pw_duty);

     }
 }
}
Mong cac ban giup minh ! minh khong biet tai sao khi nhan nut tren VB , mach lai khong chay ! chuong trinh VB da kiem tra dung roi ! thanks nhieu !

thay đổi nội dung bởi: falleaf, 16-05-2007 lúc 05:12 PM.
gaumisa vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


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à 05:34 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