Tuesday, August 27, 2013

Clock Pulses Counter

Based in my last post, I decided to modified a little bit the code and create a 'Clock Pulses Counter'.
It is very easy following the steps explained in 'Increment/Decrement Variable (counter)' and adding some additional features.

Since the example is based in my last post, the main block will be the 'Matlab Function' block.

The schematic in this example can be seen in the next figure:


Clock pulses counter Schematic
As it can be seen, the schematic is almost identical, but a global variable called flag has been added.

The code I used in this example is the following:

 function y = fcn(u)
%clock flag counter

global A;
global flag;

if u
    if flag
        A = A+1;
        flag = false;
    end
else
flag = true;

end

y = A;

Global variable A is an int counter and global variable flag a boolean, which can be true or false.

Global variable A properties
Global variable flag properties

After initialized the global variable flag in the fuction Matlab block like it was explained in the last post, the example is ready to be run.

The output in this case would be a little bit different:

Output when clock period is 0.1 seconds

Output when clock period is 0.05 seconds
Notice that the counter would be faster or lower depending on the frequency of the clock.

Note: In order to see the ouput signal like the image above the simulation must be run step by step.





Licencia Creative Commons

Wednesday, August 21, 2013

Increment/Decrement Variable (counter)


One problem I had to face in simulink was to implement a incrementacion or decrementation of one variable, and the variable remained updated. (The common expression counter = counter + 1 or counter++ in C).
After some test I found the block 'Matlab Function' the most appropiate to solve this problem.

Fucntion Matlab block
First of all, in order to use this block, a C compiler must be installed in your computer. If you want to know if a C compiler is installed write this expression in Matlab window:
>>mex -setup
The response would be the list of compilers installed in your computer and if Matlab accept them or not.
Furthermore, it gives you this webside: http://www.mathworks.com/support/compilers/R2013a/win64.html.
where a free compiler can be downloaded and installed. This compiler was the one I used and the block works fine.

Data Store Memory block
After that, since I wanted to increment/decrement a variable and this variable must remain updates I found the best solution in global variable block ('Data Store Memory'):
In this block, I had to set the initial value and it is required to set data type and signal type different to auto or complex.

Then, the global variable must be initialized in the fuction block.
Inside the block (double click): Simulink/ Edit Data -> Ports and Data Manager
Then, Add/Data
Name: Name of the global variable
Scope: Data Store Memory

After all that, I was ready to write the code. The code I used was:

function y = fcn(u)
%one input, one output. A increment/decrement depending on u

global A;
if u
A = A+1;
else
A = A-1;
end


y = A;

Note: Output variable must be assigned in all possible cases (recommendation: if is a conditional instruction, always use 'else')

Finally my schematic was like next figure:
Increment/Decrement Variable schematic

The ouput I obtained was:

Increment/Decrement variable output

Note: In order to see the ouput signal like the image above the simulation must be run step by step.


Licencia Creative Commons

Wednesday, August 14, 2013

Variable PWM Generator


I had to solve the problem of generating a PWM signal with a duty cycle variable. The most useful block I found for this issue was PWM Generator (DC-DC) where the PWM frequency is fixed but you can modify the duty cycle from an input.
It seems easy, but when I firts tried, I didn't get the result I expected.
Finally I realized that the problem was in the characteristics of the display. So here you can find an example of the block running as it's expected.
PWM signal generator circuit. Variable duty Cycle

Stair Generator Parameters
 In order to have a good representation of the different duty cycles the scope must be configured in continuous mode (sample time = 0) and the appropiate time range (In this case t = 0.001)
Display parameters
 Here you can find the simulation result:
Simulation result
Notice that the duty cycle follows the input signal.



Licencia Creative Commons