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);
    
}





No comments:

Post a Comment

Thank you for your response.