# Matlab, modelling a 555.

Discussion in 'Programmer's Corner' started by THCynical, Dec 9, 2014.

1. ### THCynical Thread Starter New Member

May 5, 2014
26
0
Stuck again,

The question is quite long, I've only included the part I need help with.
I have to model a 555 and produce this graph

I don't know how to alternate between the two formula.
I have written in English

2. ### THCynical Thread Starter New Member

May 5, 2014
26
0
clc
clear

t=1:1:100; %time

R1=1000; %temp presets
R2=1000;
C1=0.000001;

Vcc=10;
Vi=0; %Vi should vary
Vf=10; %Vf should vary

f = 1.44/((R1+(2*R2)).*C1); %frequencey
d = (R1+R2)/(R1+2*R2); %duty cycle

VCcha = Vf.^(1-exp(-t./(R1+R2)*C1)); %Vc charge
VCdis = Vi.*exp(-t/R2*C1); %Vc discharge

This is triangle
%while time passes
%1 VC = VCcha, until VCcha reaches ((2/3)*Vcc) THEN 2
%2 VC = VCdis, intil VCdis reaches ((2/3)*Vcc) THEN 1
%end

this is square
%if VC =((2/3)*Vcc) then Vo=Vcc
%esleif VC=((1/3)*Vcc) then Vo=0
%end

fprintf('Frequencey = %g Hz\n',f);
fprintf('Duty Cycle = %g \n',d);

plot(t,VCcha,t,VCdis)
title('555 Astable Multivibrator')
xlabel('Voltage(V)')
ylabel('Time(s)')
grid on

May 5, 2014
26
0
4. ### WBahn Moderator

Mar 31, 2012
17,722
4,788
You can alternate between two cases with a state variable and an 'if-else' statement.

5. ### THCynical Thread Starter New Member

May 5, 2014
26
0
Cheers WBahn, there's more to this than i thought, i will spend another while at it, but the problem relates to time (t)

I believe it needs to be a while loop, switching between charging and discharging, If statements setting conditions.

But time for the dis/charge equations needs to reset to zero at each change for the equations to work.

Some of my problem also, is just figuring out the syntax needed. as i say ill try put together something functional and report back.

6. ### WBahn Moderator

Mar 31, 2012
17,722
4,788
What you are basically writing is a simulator, albeit a very simple one. So your main loop does nothing except increment time by some small amount and you keep track of the voltages at the key nodes and what state you are in. Then in each increment you calculate how much current is flowing and how much the node voltages change and then whether the state changes and you update all of those before moving on to the next time increment.