A counter with Cortex M3, LM4F120 Launch Pad

It uses a seven segment and a button to make a simple counter. It contains a button and seven segment display. When button is pressed, seven segment will show number one more than previous. After 9 it will again show 0.
It uses a common cathode type Seven Segment in which common point is supply and all the pins are provided with  positive voltage to glow.
Button is with positive logic, means it will be considered on when it will provide input pin a high value.
A function SevenSegment is used to display numbers on this seven Segment. Port B from 1 to 7 is used for seven segment. Following is the output code for seven segment. 

Code:

  1. // ***** 0. Documentation Section *****
  2. // SevenSegment.c
  3. // Runs on LM4F120/TM4C123
  4. // Use  programming structures in C Run on a  Seven Segment and Button
  5. //      Name:       Counter
  6. //      Descrption: As you press button, it will count and show numbers 0-9
  7. //      Date:       Feb 11, 2016
  8. //      Student:    Muhammad Awais 2012-EE-506
  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. // ***** 2. Global Declarations Section *****
  16. // FUNCTION PROTOTYPES: Each subroutine defined
  17. void PORTE_Init(void);
  18. void SevenSeg_Init(void);
  19. void clear7Seg(void);
  20. void SevenSegment(int data);
  21. void Delay1ms(unsigned long msec);
  22. // ***** 3. Subroutines Section *****
  23. //GPIO_PORTE_DATA_R
  24. //GPIO_PORTB_DATA_R
  25. // PE0 connected to positive logic momentary switch using 10k ohm pull down resistor
  26. // PB1-PB7 connected to Common Anode Seven Segment
  27. int main(void){
  28. //**********************************************************************
  29. // The following version tests input on PE0 and output on PE1
  30. //**********************************************************************
  31.   int count=0;
  32.     PORTE_Init();      // Initlizes PORTE for switch
  33.     SevenSeg_Init();   // Initlizes PORTB for Seven Segment
  34.   SevenSegment(0);   // Seven Segment is showing 0 on reset
  35.   while(1)           //Infinte loop
  36.         {
  37.        
  38.             /*
  39.             For Simple Counter Without Button
  40.             for(count;count<=9;count++)
  41.             {
  42.             SevenSegment(count);
  43.                 Delay1ms(1000);
  44.                 clear7Seg();
  45.             }
  46.             */
  47.         if (count>9)                            //When count=9 it makes it zero
  48.         {
  49.             count=0;
  50.             SevenSegment(count);
  51.         }
  52.        
  53.         if(SW_PE0==0x01)                        //If switch is presses
  54.         {
  55.             count=count+1;                          //incremetn counter
  56.             clear7Seg();                                //Clears seven segment to write new data
  57.             SevenSegment(count);                //Writes number on Seven Segment
  58.             Delay1ms(100);              //Dealy for button debouncing and other factors
  59.         }
  60.        
  61.   }
  62. }
  63. /*
  64. Descrption: Initilizes PE0==>Input
  65. Input: None
  66. Output: None
  67. */
  68. void PORTE_Init(void)
  69. {
  70.   volatile unsigned long delay;
  71.   SYSCTL_RCGC2_R |= 0x00000010;      // 1) E clock
  72.   delay = SYSCTL_RCGC2_R;            // delay to allow clock to stabilize    
  73.   GPIO_PORTE_AMSEL_R &= 0x00;        // 2) disable analog function
  74.   GPIO_PORTE_PCTL_R &= 0x00000000;   // 3) GPIO clear bit PCTL  
  75.   GPIO_PORTE_DIR_R &= ~0x01;         // 4.1) PE0 input,
  76.   GPIO_PORTE_AFSEL_R &= 0x00;        // 5) no alternate function      
  77.   GPIO_PORTE_DEN_R |= 0x01;          // 6) enable digital pins PE4-PE1
  78. }
  79. /*
  80. Descrption: Initilizes PB1 to PB7 as output
  81. Input: None
  82. Output: None
  83. */
  84. void SevenSeg_Init(void)
  85. {
  86.   volatile unsigned long delay;
  87.   SYSCTL_RCGC2_R |= 0x00000002;      // 1) B clock
  88.   delay = SYSCTL_RCGC2_R;            // delay to allow clock to stabilize    
  89.   GPIO_PORTB_AMSEL_R &= 0x00;        // 2) disable analog function
  90.   GPIO_PORTB_PCTL_R &= 0x00000000;   // 3) GPIO clear bit PCTL  
  91.   GPIO_PORTB_DIR_R |= 0xFF;          // 4) PB output  
  92.   GPIO_PORTB_AFSEL_R &= 0x00;        // 5) no alternate function      
  93.   GPIO_PORTB_DEN_R |= 0xFF;          // 6) enable digital AT PB
  94. }
  95. /*
  96. Descrption: Clears all 1-7 pins of PortB
  97. Input: None
  98. Output: None
  99. */
  100. void clear7Seg()
  101. {
  102.     GPIO_PORTB_DATA_R=0xff;
  103. }
  104. /*
  105. Descrption: All the pins who will show number on Seven Segment will be zero
  106. It is for common anode type, to make it for common cathode add ~ before all cases
  107. Input: Number to be printed on Seven Segment
  108. Output: None
  109. */
  110. void SevenSegment(int data){
  111.     switch(data)
  112.     {
  113.         case 0: GPIO_PORTB_DATA_R=0x11  ;
  114.         break;
  115.       case 1: GPIO_PORTB_DATA_R=0x7D;
  116.         break;
  117.         case 2: GPIO_PORTB_DATA_R=0x89;
  118.         break;
  119.         case 3: GPIO_PORTB_DATA_R=0x29;
  120.         break;
  121.         case 4: GPIO_PORTB_DATA_R=0x65;
  122.         break;
  123.         case 5: GPIO_PORTB_DATA_R=0x23;
  124.         break;
  125.         case 6: GPIO_PORTB_DATA_R=0x03;
  126.         break;
  127.         case 7: GPIO_PORTB_DATA_R=0x79;
  128.         break;
  129.         case 8: GPIO_PORTB_DATA_R=0x01;
  130.         break;
  131.         case 9: GPIO_PORTB_DATA_R=0x21;
  132.         break;
  133.     }
  134. }
  135. /*
  136. Descrption: Uses time of a instruction to delay time
  137. Almost 1ms for each cycle
  138. Input: Number of ms to delay
  139. Output: None
  140. */
  141. void Delay1ms(unsigned long msec){
  142. // write this function
  143.     unsigned long i;
  144.    
  145.   while(msec > 0){
  146.     i = 13333;  // this number means 1ms
  147.     while(> 0){
  148.       i = i - 1;
  149.     }
  150.     msec = msec - 1; // decrements every 1 ms
  151.   }
  152. }

Practical Implementation:

A counter without button


A seven Segment code written in Energia:

Energia is a software that is used for lauch pad to write arduino like code. Here is a code for Seven Segment in Energia.\

/*
 Seven segment display
  Muhammad Awais fb/awais12506
  
  Hardware Required:
  * LaunchPad with an LED
  *Seven segment
  *Connecting wires
  
 .
*/

// most launchpads have a red LED
int A=A2;
int B=A3;
int C=A4;
int D=A1;
int E=A0;
int FI=A5;
int G=7;

//see pins_energia.h for more LED definitions

  
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(FI,OUTPUT);
  pinMode(G, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  
    setOff();
    sendData(9);
 for(int i=0;i<10;i++)
 {
sendData(i);
delay(1000);
setOff();
 }
}
void sendData(int num)
{
  switch (num)
  {
    case 1: 
    digitalWrite(FI,LOW);
    digitalWrite(E, LOW);
    break;
    case 2:
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(G, LOW);
    digitalWrite(E, LOW);
    digitalWrite(D, LOW);
    break;
    case 3:
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(G, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    break;
    case 4:
    digitalWrite(FI, LOW);
    digitalWrite(G, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    
    break;
    case 5:
    digitalWrite(A, LOW);
    digitalWrite(FI, LOW);
    digitalWrite(G, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    break;
    case 6:
    digitalWrite(A, LOW);
    digitalWrite(FI, LOW);
    digitalWrite(G, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    break;
     case 7:
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    break;
    case 8:
    digitalWrite(A, 0);
    digitalWrite(B, 0);
    digitalWrite(C, 0);
    digitalWrite(D, 0);
    digitalWrite(E, 0);
    digitalWrite(FI,0);
    digitalWrite(G, 0);
    case 9:
    digitalWrite(A, LOW);
   // digitalWrite(B, 0);
    //digitalWrite(C, 0);
    //digitalWrite(FI,0);
    //digitalWrite(G, 0);
    case 0:
    digitalWrite(A, 0);
    digitalWrite(B, 0);
    digitalWrite(C, 0);
    digitalWrite(D, 0);
    digitalWrite(E, 0);
    digitalWrite(FI,0);

  }
}
void setOff()
{
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(FI,HIGH);
    digitalWrite(G, HIGH);
    
}

void setOn()
{
    digitalWrite(A, 0);
    digitalWrite(B, 0);
    digitalWrite(C, 0);
    digitalWrite(D, 0);
    digitalWrite(E, 0);
    digitalWrite(FI,0);
    digitalWrite(G, 0);
    
}





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