Led and button with Cortex M3, LM4F120 Microcontroller

This is simple program made on LM4f123 launch pad of texas instruments. One button and a led is interfaced with PORT E pin 0 and Port E pin 1 respectively. Led is initially on, when button is pressed, it will change its state or toggle with a delay of approximately 100ms.

Debugging:

Logic Analyzer: 


Register Window in KEIL




Following is algorithm of the program..

1) Initializes registers of ARM
2) LED is initially on
3) Check if button is on or not
    if button is on, toggle led with 100ms delay
   else nothing

Downloads


Delay function is a approximation and made by knowing the time a command takes to execute. So it is not precise.  It will take 13333 cycles of while loop for almost 1ms. This fact is used in this program.


Code:

  1. // ***** 0. Documentation Section *****
  2. // SwitchLEDInterface.c
  3. // Runs on LM4F120/TM4C123
  4. // Use simple programming structures in C to toggle an LED
  5. // while a button is pressed and turn the LED on when the
  6. //      Date:    Feb 11, 2016
  7. //      Student: Muhammad Awais 2012-EE-506
  8. //      Contact Info: Fb/awais12506
  9. //      Class:   UTAustinX: UT.6.03x Embedded Systems - Shape the World
  10. // ***** 1. Pre-processor Directives Section *****
  11. #include "tm4c123gh6pm.h"
  12. //Bit specfic adressing for PE0 and PE1
  13. //base adress of PORTE is 0x40024000
  14. #define SW_PE0   (*((volatile unsigned long *)0x40024004))
  15. #define LED_PE1   (*((volatile unsigned long *)0x40024008))
  16. // ***** 2. Global Declarations Section *****
  17. // FUNCTION PROTOTYPES: Each subroutine defined
  18. void PORTE_Init(void);
  19. void Delay1ms(unsigned long msec);
  20. // ***** 3. Subroutines Section *****
  21. //GPIO_PORTE_DATA_R
  22. // PE0, PB0, or PA2 connected to positive logic momentary switch using 10k ohm pull down resistor
  23. // PE1, PB1, or PA3 connected to positive logic LED through 470 ohm current limiting resistor
  24. int main(void){
  25. //**********************************************************************
  26. // The following version tests input on PE0 and output on PE1
  27. //**********************************************************************
  28.     PORTE_Init();      // Initlizes PORTE

Newton Ralphson Method using Matlab

Newton Ralphson method is  finding successively better approximations to a functions root. In this method one initial guess of root is given. Then we find value of the function at that root. A tangent is drawn at that value of function then value of that tangent where it crosses x-axis is taken. This value is comparatively better root. Again value of function is found on that point and then again a tangent is drawn at that point and so on... This process is repeated until error has become significantly small.
Following figure is explaining this in a simple way.



MATLAB Code:

%Author: Muhammad Awais 2012-ee-506 UET Lahore
%This Function uses Newton Ralphson Method to find root of a function
%Input: Function , initial guess, minmum tolerace
%Output: Roots of function, Number of iterations used.
%Please input function in following way...
%syms x
%f=x.^3-20
function [y,iteration]=NewtonRalphson(f,x0,error)

g=diff(f);      %Differentiation of f
%older and new is to hold values of x in each iteration and last iteration.
older=0;
new=1;
%Number of iterations that it has to complete
iteration=1;

while ((abs(new-older)>error ) )
    older=x0;
    x1=x0-subs(f,x0)/subs(g,x0);  %Newton Ralphson Formula
    new=x1;
    x0=x1;
    iteration=iteration+1;

end

y=double(x1);   %Syms tool doesnt produces values in double so...

Output:


Problems:

1) Derivation

Finding derivation is a expensive process when we talk about computers. They take a lot of time determining derivation.

2)Convergence

Some time this method simply did't converge so algorithm fails.

3)Poor initial estimate

Poor initial estimate may make this  algorithm non-convergent. One such example is...
f(x)=|x|^a,\quad 0 < a < \tfrac{1}{2}  [1]

References: 

[1] https://en.wikipedia.org/wiki/Newton%27s_method

Bisection Method Using MATLAB

Bisection method is based on Intermediate Value Theorem which states as,
 Let f (x) be a continuous function on the interval [ab]. If d $ \in$ [f (a), f (b)], then there is a c $ \in$ [ab] such that f (c) = d.

We are using a sub-form  form of this theorem, In our case we are given with two values at which function changes its value form positive to negative and we have to find a value where function is zero or in other words root of the function. We then use a binary search type algorithm that slowly decreases this interval and finally gets solution with some error. 
So if we are given with a interval [xl , xu] as lower and upper limit. Value of function with xl is negative and with  xu is positive then there must be at least one point in between these two values which is  root of the function  or in other words function becomes zero. Function should be real and continuous.

Algorithm:

  1. We check weather function changes its value from positive to negative within given interval, If it changes we proceed other wise we can't find solution.
  2. We find mid point of the interval, xm. 
  3. If function at  xm is negative, it becomes our new xl, if its positive its our new xu and if it happens to be zero, we find our root (It rarely happens). 
  4. We find error by seeing the difference between last and new xm. If error is smaller than provided one, we stops other wise step 2 and 3 repeats.
  5. If we also limit iterations by 1000. Means our program can execute more than 1000 iterations.

MATLAB Code:

%Author: Muhammad Awais
%UET Lahore, Electrical Engineering
%fb/awais12506
function [iteration,xm]=Bisection(xl,xu,f,e)
%Find Root of a equation by method of BiSection
%Input: Lower Limit, Upper Limit, Function, Error Tolerance
%Please Insert f as f=@(x)x.^2+9*x+3
%Output: Root of the equation with given precison
%Exception: Give error if Equation is not convergent or roots are dont give
%postive and negative values of the function

iteration=0;     %To see how many iterations, equation took to solve
xm=xl;
error=1;
if  (f(xl)*f(xu)>0)
    disp('Interval have some error')

else
     while ( abs(error) > abs(e) ||iteration<=1000 )
        iteration=iteration+1;
        xmOld=xm;
        xm=(xl+xu)/2;   %Mid point
        xmNew=xm;
        if f(xl)*f(xm)<0   %if f(xm) is neg, equate xu with xm
            xu=xm;
        else if f(xl)*f(xm)>0 %if f(xm) is pos, equate xl with xm
            xl=xm;
            else            %it means xm is our root
            break
            end
        error=(xmNew-xmOld)/xmNew;   %Error
        end

    end

end
end

YBus building algorithm of Power System using MATLAB

Nodal admittance matrix or Ybus matrix is a matrix that contains admittances of the power system. This matrix can be used to find voltages of buses.
This Ybus matrix can be found easily and then inverted and multiplied with currents to find voltages of buses. Although taking inverse of a large system is computationally not efficient and there are ways to find Zbus directly instead of taking inverse of Ybus.
Ybus is sparse matrix with a lot of zeroes in it. So its a large matrix with a lot of zeroes.

Algorithm used in our code is  of 3 steps.

1.     Circuit is converted into matrix from called data. First column of data contains from and second contains to means nodes to which a admittance is connected. Third and forth contains value of resistance and reactance respectively.
2.     Off-diagonal entries are just net admittances connected to node i and j.
3.     Diagonal entries contains sum of all entries connected to that node. For example Y 3,3 is net of all the entries connected to bus 3.
Code:
%Author: Muhammad Awais
%Electrical Engineering, UET Lahore
%Bus admittance matrix
%Write buss impedence in Z matrix and it will solve rest of the thing
%data matrix

   data=[0   1   0   1j
   0   2   0 0.8j
   1   2   0 0.4j
   1   3   0 0.2j
   2   3   0 0.2j
   3   4   0 0.08j];

 % from  to R   X
%Z=[0   1   0   1j
 %  0   2   0 0.8j
 %  1   2   0 0.4j
  % 1   3   0 0.2j
   %2   3   0 0.2j
   %3   4   0 0.08j];

%finding order of matrix in C language way
%o1=max(Z(:,1));
%o2=max(Z(:,2));
%order=(max(o1,o2))
%find number of rows of Z= Toatl number of nodes
%row=length(Z(:,1))

[row,col]=size(data);
order=col

%Change last column into admittance, now last column also inculdes R
for m=1:row
    data(m,4)=1/(data(m,3)+data(m,4));
end

Z2adm=data;

%Yadmittance as a matrixo of zeros first
Y=zeros(order,order);

%finding ybus matrix

%1-for off-digonal vlaues
for i=1:row
    for j=1:order
        %discard source node
        if data(i,1)==0||data(i,2)==0    
           a=0;
         %for off digonal entries
        elseif data(i,1)~=0||data(i,2)~=0
            Y(data(i,1),data(i,2))=-data(i,4);
            Y(data(i,2),data(i,1))=-data(i,4);
        end
        
    end
end
%2-digonal values 
for a=1:order     %for k
    for b=1:row
        if data(b,1)==a ||data(b,2)==a
           
            Y(a,a)=(Y(a,a)+data(b,4));
        end
    end
   
end
Ybus=Y

%To find Z bus
Zbus=inv(Y)

%As Ibus=Ybus*Vbus so we can find too if we know Ibus. As here two currnet
%sources so suppose
Ibus=[1;1;0;0];
Vbus=Ybus\Ibus


Examples:
Example 1: To solve following system using this method

Impedance Matrix:

data=[0   1   0   1j
   0   2   0 0.8j
   1   2   0 0.4j
   1   3   0 0.2j
   2   3   0 0.2j
   3   4   0 0.08j];
Results:
Ybus =
        0 - 8.5000i        0 + 2.5000i        0 + 5.0000i        0         
        0 + 2.5000i        0 - 8.7500i        0 + 5.0000i        0         
        0 + 5.0000i        0 + 5.0000i        0 -22.5000i        0 +12.5000i
        0                  0                  0 +12.5000i        0 -12.5000i
Zbus =
        0 + 0.5000i        0 + 0.4000i        0 + 0.4500i        0 + 0.4500i
        0 + 0.4000i        0 + 0.4800i        0 + 0.4400i        0 + 0.4400i
        0 + 0.4500i        0 + 0.4400i        0 + 0.5450i        0 + 0.5450i
        0 + 0.4500i        0 + 0.4400i        0 + 0.5450i        0 + 0.6250i
Vbus =
        0 + 0.9000i
        0 + 0.8800i
        0 + 0.8900i
        0 + 0.8900i

Example 2:

Impedance Matrix:
data=[0   1   0  0.25j
   0   1   0  -4j
   1   4   0   0.4j
   1   3   0   .1j
   0   2   0    .2j
   0   2   0    -4j
   0   3   0    -4j
   0   3   0.9412  .2353j
   3   4   0      0.2j
   0   4   0     -4j
   0   4   .4706  0.11765j
    ];

Results:
Ybus =
        0 -20.2500i        0 + 4.0000i        0 +10.0000i        0 + 2.5000i
        0 + 4.0000i        0 -15.0000i        0                  0 + 6.2500i
        0 +10.0000i        0             1.0000 -15.0000i        0 + 5.0000i
        0 + 2.5000i        0 + 6.2500i        0 + 5.0000i   2.0000 -13.9998i
Zbus =
   0.0359 + 0.1305i   0.0303 + 0.0718i   0.0481 + 0.1134i   0.0498 + 0.0887i
   0.0303 + 0.0718i   0.0264 + 0.1224i   0.0398 + 0.0744i   0.0439 + 0.0878i
   0.0481 + 0.1134i   0.0398 + 0.0744i   0.0652 + 0.1733i   0.0648 + 0.1061i
   0.0498 + 0.0887i   0.0439 + 0.0878i   0.0648 + 0.1061i   0.0736 + 0.1538i
Vbus =
   0.0662 + 0.2023i
   0.0567 + 0.1941i
   0.0879 + 0.1878i
   0.0937 + 0.1765i




Reference:

Examples were taken from Power System Analysis - Hadi Saadat

Earth quake


  • Just felt a Earthquake of 6.5 intensity. Some days ago I read that Pakistan is in the most active quake zone. 

          http://www.dawn.com/news/1215636
  • A website for monitoring earthquake is as follows....


  • And here are some precautionary measures....

Kron Reduction using Matlab Code

Kron Reduction
In power system anylasis, kron reduction is a way of reducing buses in Y bus matrix of the power system. It usually used for two reasons
  1.             To remove a bus where current injection is zero to reduce size of matrix thereby effectively making computation easy. Buses with no load or source don't inject currents.
  2.            To use only some buses specific buses instead of whole system to know required voltages only. This way we can get voltages of our interest with less computational power of our computer.

If Y j ,k  is our matrix to be kron reduced and p is number of row and coloum to be reduced then this formula is used to find kron reduced elements.

Matlab Code:
%Author: Muhammad Awais fb/awais12506
%Kron Reduction
%p is row and colum to be removed
%Y is array to be kron reduced

clear all
clc
p=2;
Y=[-16.75i 11.75i 2.5i 2.5i
    11.75i -19.25i 2.5i 5i
    2.5i 2.5i -5.8i 0
    2.5i 5i 0 8.3i];
[row,col]=size(Y);   
Y_new=zeros(row,col);  %Produces 4 by 4 matrixes of zeros

%In this loop all p colum and row is replaced by 0 and other by respective
%value
for k=1 :row
    for l=1 : col
        if k==p || l==p
            Y_new(k,l)=0;
        else 
           Y_new(k,l)=Y(k,l)-((Y(k,p)*Y(p,l))/(Y(p,p)));
           
        end
    end
end
% To remove p row and colum
Y_new(:, p) = [];
Y_new(p, :) = [];
Y_new




Example:

Kron Reduction by calculation

Kron Reduction by our Program:
Y_new =
        0 - 9.5779i        0 + 4.0260i        0 + 5.5519i
        0 + 4.0260i        0 - 5.4753i        0 + 0.6494i
        0 + 5.5519i        0 + 0.6494i        0 + 9.5987i

There was a mistake while entering data so answer was a bit wrong. Mistake is corrected and now also I have written the code in function form ....

MATLAB Code:
%Author: Muhammad Awais fb/awais12506
%Descrption: Function uses Kron Reduction to reomove a column
%Input:Y==> Matrix to be reduced, p==> Row and Colum to be removed
%OutPut: Reduced Matrix
function Y_Reduced=KronReduction(Y,p)
[row,col]=size(Y);
Y_new=zeros(row,col);  %Produces 4 by 4 matrixes of zeros

%In this loop all p colum and row is replaced by 0 and other by respective
%value
for k=1 :row
    for l=1 : col
        if k==p || l==p
            Y_new(k,l)=0;
        else
           Y_new(k,l)=Y(k,l)-((Y(k,p)*Y(p,l))/(Y(p,p)));

        end
    end
end
% To remove p row and colum
Y_new(:, p) = [];
Y_new(p, :) = [];
Y_Reduced=Y_new;
end

Output: