View Single Post
Old 11-05-2011, 10:00 PM   #2
ganbaregenki
Nhập môn đệ tử
 
Tham gia ngày: Sep 2009
Bài gửi: 8
:
Chào mọi người.Lâu lắm rồi mình chưa cập nhật cho thread,mà hình như không có ai thích tìm hiểu về vấn đề này với mình nhỉ.
Hiện tại thì đồ án của mình cũng đã bước vào giai đoạn cuối và đang tiến hành typing với lấy đồ thị thời gian thực nữa là okie.
Mình gửi code VXL và code MATLAB cho mọi người cùng thảo luận nhé.
Dưới đây là code VXL.string mình gửi từ MATLAB có dạng 'axb'.Trong đó x là giá trị duty cho pwm có dải từ 0 đến 255.
Code:
/*test uart cho pic 16f877a su dung phan mem mirko & MATLAB & Rs232
*/
char uart_rd;
unsigned int temp,duty_cycle;
volatile long int pwm_data;
char temp_text0[7],temp_text1[7], *temp_text_all,*res; //ADD A CHAR ARRAY TO HOLD THE TEXT
void main() 
 {
     ADCON1 = 0;              // All PORTA pins as analog, VDD as Vref
     trisa = 0xff;
     TRISB=0;
     PORTB=0;
     PWM1_Init(5000);                    // Initialize PWM1 module at 5KHz
     PWM1_Start();                       // start PWM1
     UART1_Init(9600);
     Delay_ms(100);
     temp=100;
     while(1){


                   if (UART1_Data_Ready())   // if data is received
                   { 
                   

                   uart_rd=UART1_Read();
                   
                   if(uart_rd=='a') pwm_data=0;
                   else
                   {
                   if(uart_rd!='b')
                       {

                         pwm_data=pwm_data*10+uart_rd-48;
                       }
                       else
                       {
                           duty_cycle=pwm_data;
                           if(duty_cycle>255) duty_cycle=255;
                           PWM1_Set_Duty(duty_cycle);
                           temp = Adc_Read(0)*0.243902439;
                           IntToStr (temp, temp_text0); //CONVERT ADC VALUE TO TEXt
                           uart1_write_text(temp_text0); //SEND TEXT VALUE TO UARTT
                           //temp = Adc_Read(2)*0.243902439;
                            //IntToStr (temp, temp_text); //CONVERT ADC VALUE TO TEXt
                            //uart1_write_text(temp_text); //SEND TEXT VALUE TO UART
                    }
                    
                   }
                   
              }
                     //temp = Adc_Read(1)*0.243902439;
                     //IntToStr (temp, temp_text1); //CONVERT ADC VALUE TO TEXt
                     //uart1_write_text(temp_text1); //SEND TEXT VALUE TO UART
                     //temp_text_all=strcat(temp_text0,";");
                     //temp_text_all=strcat(temp_text_all,temp_text1);
                     //uart1_write_text(temp_text_all); //SEND TEXT VALUE TO UART
                     //delay_ms(200);
      }
}
Trên đây mình mới chỉ cần gửi 1 tín hiệu để MATLAB xử lý nên code vẫn chưa có đoạn đọc adc từ chân AN1.AN2,AN3 và AN5.Và lệnh gộp string+lệnh gửi string all.
Còn đây là code MATLAB.Mọi người thấy có gì sai thì cứ thảo luận nhé
Code:
% Reading of a  sensor temperature
% using a microcontroller PIC16F887 
% Starting: April 15 , 2011
% Author's Data: hoancaubk@gmail.com

% HaNoi university of Technology and Science

clear all;
close all;
 
s = serial('COM1'); %assigns the object s to serial port
 
set(s, 'InputBufferSize', 256); %number of bytes in inout buffer
set(s, 'BaudRate', 9600);       %set baud rate
set(s, 'Parity', 'none');       %
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',0.5);          %Set time out for COM1,it is very important because of
                                % real time control 
fopen(s);
fprintf(s,'%s','a102b'); % to active PIC16F887 by sending a value to PIC from PC
i=0;
while(i<20)
    data=Str2num(fscanf(s))
    
    % code controller and actuator system here
    
    fprintf(s,'%s','axb') %send control signal value to PIC:axb
    i=i+1
end
fclose(s);                                   %close serial port
à.Do mình sử dụng vòng while nên vấn đề plot data vẫn còn đang có lỗi.
Chắc tuần sau mình sẽ up final code và MATLAB gui nhé.
Thân
ganbaregenki vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn