Let the future tell the truth and evaluate each one according to his work and accomplishments. The present is theirs; the future, for which I really worked, is mine. (Nikola Tesla)

17/06/2011

Serial Communication in MATLAB - 1

In these series of article about serial communication in MATLAB, I explain how we can use MATLAB to interact with hardware which are connected to a PC through a serial port. Notebooks mostly don't have serial port, but USB ports. So, you can use a USB to Serial Converter as well.

We firstly learn some basic concepts like terminator, buffer, parameters, etc.


-Terminator-

We may roughly say that a terminator is used to specify the end of a message. For instance, the format in asynchronous serial communication for a 10-bit character frame is just like a start bit, followed by 8 data bits and one stop bit in the end. The stop bit here defines a stop period. Again, we may roughly say that the same logic is reasonable for terminator.

So, what are we going to do to configure terminator? We can use any integer value ranging between 0-127 for defining an ASCII character or we can directly configure terminator to the ASCII character. We may configure its value as "CR" or "13" which is carriage return. We may also configure its value as "LF" or "10" which is line feed. We may also configure terminator to CR/LF which is a carriage return followed by a line feed or vice versa. We can also set it as 1-by-2 cell array, first cell is the read terminator and the second one is the write terminator.

While we are writing string data to the device connected to the serial port object, we use 'fprintf' command. 'fprintf' also adds the terminator character at the end of the string that you send. It is new line (\n) by default or it is specified by the terminator property which you set while you are defining serial port object at first. So, you have a chance to change it as how it's defined in the documentation of external device. In your works, you can use "HHD Serial Port Monitor" to see what MATLAB sends as terminator.

While we are getting string data from external device, we use 'fscanf' command. As in the 'fprintf' command, 'fscanf' command also uses terminator. Any reading operation is completed when a terminator, which is specified by terminator property, is read. For instance if your device use "LF" (\n) as terminator, you can set your terminator property of serial port object as "LF" in MATLAB and so the reading process is completed when MATLAB gets terminator value from device.



12/06/2011

● Finding Maximum and Minimum by Golden-Section Search using MATLAB

This program implements the golden-section search algorithm. It locates the minimum or maximum of the function depending on the user’s preference, iterates until the relative error falls below a stooping criteria or exceeds an maximum number of iterations, return both the optimal x and f(x), minimizes the number of function evaluations.

r=(sqrt(5)-1)/2; %Golden Rate
disp('Number of max iteration')
maxiter=input('maxiter= ');
disp('Value of tolerance')
tol=input('Tol= ');
disp('For max enter "1", for min enter "0"')
x=input('minmax= ');
a=input('Lower bound= '); %Lower limit
b=input('Upper bound= '); %Upper limit
disp(' ')
disp('k x1 x2')

for k=1:maxiter
if (x==1) %Part that will find max
x1=a+r*(b-a); %First one of the points inside
x2=b-r*(b-a); %Second one of the points inside
fx1=-x1^4-2*x1^3-8*x1^2-5*x1; %Value of fx at first inside point
fx2=-x2^4-2*x2^3-8*x2^2-5*x2; %Value of fx at second inside point
if fx1>fx2 %Changing interval as [x2,b]
a=x2;
x2=x1;
x1=a+r*(b-a);
else
b=x1; %Changing interval as [a,x1]
x1=x2;
x2=b-r*(b-a);
end
fprintf ('%d %7.5f %7.5f\n', k,x1,x2)
%Stop if the error is below tolerance
if(abs((x1-x2))
end
else %Part that will find min
x1=a+r*(b-a);
x2=b-r*(b-a);
fx1=3+6*x1+5*x1^2+3*x1^3+4*x1^4;
fx2=3+6*x2+5*x2^2+3*x2^3+4*x2^4;
if fx1
a=x2; %Changing interval as [x2,b]
x2=x1;
x1=a+r*(b-a);
else
b=x1; %Changing interval as [a,x1]
x1=x2;
x2=b-r*(b-a);
end
fprintf ('%d %7.5f %7.5f\n', k,x1,x2)
if(abs((x1-x2))
end
end
end
disp(' ')
disp('Results')
disp('x1 x2 f(x1) f(x2)')
fprintf ('%7.5f %7.5f %7.5f %7.5f\n',x1,x2,fx1,fx2)



● Calculating cos(x) Using Taylor Series Expansion on MATLAB

This program evaluates cosine function using "Taylor Series Expansion".
x=input('x= '); %Number that we will calculate its cosine
N=input('N= '); %Number of iteraritons
cosx=1; %Initial value cos(0)
truecos=cos(x) %True value of cos(x) calculated by Matlab command

for k=2:N
fact=1; %Initial value to calculate factorial
for i=1:(2*(k-1)) %for loop to calculate required factorial
fact=fact*i;
end
cosx=cosx+(-1)^(k+1)*x^(2*(k-1))/fact %calculation of cos(x)
error=(truecos-cosx)/truecos*100 %calculation of relative error
end


15/05/2011

●Gauss Elimination Method using Matlab for Numerical Analysis

Here is the code to implement Gauss Elimination Method. The program asks for order of the system, coefficient matrix and right hand side vector. Then, it prints out the the augmented matrix after each forward elimination step and prints out the solutions after back substitution.


n=input('Order of the system= ');

A=input('Coefficient matrix= ');

B=input('Right hand vector= ');

X=input('Initial Estimate= ');

tol=input('Tolerance= ');

N=X;

for a=1:100

for i=1:n

z=0;

for t=1:i-1

z=z+(A(i,t)*X(t));

end

for t=i+1:n

z=z+(A(i,t)*X(t));

end

N(i)=X(i);

X(i)=(B(i)-z)/A(i,i);

end

X

if(abs((X(i)-N(i))/X(i))

end

end

20/02/2011

●Sending Data by Using PIC16F628A with Serial Communication

Here is the program written by using CCS C.


#include <16f628.h>
#fuses NOPROTECT, NOWDT, NOLVP, NOMCLR, INTRC_IO, XT
#use delay(clock=4M) //Be aware you set the same clock frequency for PIC
#use rs232(baud=9600 , xmit=PIN_B2) //Be aware you have the same baud rate for virtual terminal and use the B2 pin for transmitting the data

void main(){
while(1){
printf("Sezgin"); //Sending data as a string
delay_ms(250); //Setting the time for the delay
}
}



27/01/2011

●Serial Communication Using PIC16F628A

Serial communication refers to sending one bit data at a time on a line or a computer bus. It is used for long distance communications, especially computer networks where difficulties on cabling and synchronization makes parallel communication inconvenient.

While we're using PIC16F628A for serial communication, we use RS-232 standard. So, we need to know some terms as baud rate and some instructions used in CCS C for RS-232.

We define instructions for RS-232 in CCS C as below:
#use rs232 (...)

One of the definitions for RS-232 is the baud rate (baud=x). The baud rate is used to denote the number of bits transferred per second. We may define baud rate as different values like 9600.
Here is an table for RS232 cable length according to Texas Instruments:

Baud rateMaximum cable length (ft)
1920050
9600500
48001000
24003000


In our examples, we use 9600 as our baud rate. So we can define;
#use rs232 (baud=9600)

The other thing is the transmitting the data. We should specify the pin that we choose for transmitting the data (xmit=x). We are free to choose a pin. So we can define;
#use rs232 (xmit=PIN_B0)

The other thing is the receiving the data. We should specify the pin that we choose for receiving the data (rcv=x). We are free to choose a pin. So we can define;
#use rs232 (rcv=PIN_B2)

Parity is the another thing in RS232 communication (parity=x). We can define the parity as even(E), odd(O) or not(N). So we can define;
#use rs232 (parity=N)

Bits definition is another step in our rs232 definition (bits=x). While using software UART, CCS supports from 5 bits up to 9 bits. If we want to use two stop bits, we should define 9 bits and make the 9th bit as high. So we can define;
#use rs232 (bits=8)

For RS485 communication, we may use a intstruction (enable=x). So we can define;
#use rs232 (enable=PIN_B4)

Finally, we should regulate the virtual terminal operating properties compatible with the arranged values on our program. For instance on Proteus:





11/09/2010


●Displaying a Scrolling Script from Right to Left on 2x16 LCD

Here is the program written by using CCS C.


#include <16f628.h>
#fuses NOPROTECT, NOWDT, NOLVP, NOMCLR, INTRC_IO
#use delay(clock=4M)
#include

void main()
{

int a=32,x=16;

lcd_init(); //Must be called before any other function

for( ; ;x--,a--){

lcd_gotoxy(x,1);
lcd_putc("Sezgin Secil");
delay_ms(200);
lcd_putc("\f"); //To clear the display
if(a==0){ a=32,x=16; }

}
}


Note: lcd_gotoxy and lcd_putc are the functions that are presented by lcd.c file.

ShareThis