Actions

EmSys

TM4C123G LaunchPad - 7 Segment LED Source Code

From EdWiki

Revision as of 05:04, 24 November 2018 by Jshankar (Talk | contribs) (Created page with "<syntaxhighlight lang="c" line start="1"> * Display minutes and seconds on the 4-digit 7-segment LED. *: #include <stdint.h> #include <stdlib.h> #include "inc/tm4c123g...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
  1. /*
  2.  * Display minutes and seconds on the 4-digit 7-segment LED.
  3.  *
  4. */
  5.  
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include "inc/tm4c123gh6pm.h"
  9.  
  10. #define NO_OF_DIGITS    4
  11.  
  12. /* 0 - 9 pattern for the common cathode configuration */
  13. unsigned char digitPattern[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
  14. unsigned char digit_no[] = {0x10, 0x20, 0x40, 0x80};
  15.  
  16. /* for the common anode configuration */
  17. // unsigned char digitPattern[] = {0xCF, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
  18. // unsigned char digit_no[] = {0x1F, 0x2F, 0x4F, 0x8F};
  19.  
  20. void One_Second_Delay(void);
  21. void delayMs(int n);
  22.  
  23. int seconds, minutes, minutesec;
  24.  
  25. int main(void)
  26. {
  27.     volatile unsigned long delay;
  28.     int digits;
  29.  
  30.  
  31.     seconds = 0;
  32.     minutes = 0;
  33.     minutesec = 0;
  34.  
  35.     SYSCTL_RCGC2_R |= 0x01;     /* enable clock to GPIOA */
  36.     SYSCTL_RCGC2_R |= 0x02;     /* enable clock to GPIOB */
  37.     delay = SYSCTL_RCGC2_R;     /* allow time for clock to start */
  38.     delay = delay;
  39.  
  40.     GPIO_PORTB_DIR_R = 0xFF;    /* set all PORTB pins as output to drive segments */
  41.     GPIO_PORTB_DEN_R = 0xFF;    /* set all PORTB pins as digital pins */
  42.     GPIO_PORTB_DR8R_R = 0xFF;
  43.  
  44.     GPIO_PORTA_DIR_R = 0xF0;    /* set PORTA pin 7-4 as output to select digit */
  45.     GPIO_PORTA_DEN_R = 0xF0;    /* set PORTA pin 7-4 as digital pins */
  46.     GPIO_PORTA_DR8R_R = 0xF0;
  47.  
  48.     One_Second_Delay();
  49.  
  50.     for(;;) {
  51.         for(digits = 0; digits < NO_OF_DIGITS; digits++ ) {
  52.             GPIO_PORTA_DATA_R = digit_no[digits];           /* select digit */
  53.  
  54.             switch(digits) {
  55.             case 0:
  56.                 if( seconds >= 10)
  57.                     minutesec = digitPattern[seconds%10];
  58.                 else
  59.                     minutesec = digitPattern[seconds];
  60.                 break;
  61.             case 1:
  62.                 if( seconds >= 10)
  63.                     minutesec = digitPattern[seconds/10];
  64.                 else
  65.                     minutesec = digitPattern[0];
  66.                 break;
  67.             case 2:
  68.                 if( minutes >= 10)
  69.                     minutesec = digitPattern[minutes%10];
  70.                 else
  71.                     minutesec = digitPattern[minutes];
  72.                 break;
  73.             case 3:
  74.                 if( minutes >= 10)
  75.                     minutesec = digitPattern[minutes/10];
  76.                 else
  77.                     minutesec = digitPattern[0];
  78.                 break;
  79.             default:
  80.                 break;
  81.             }
  82.  
  83.             /* drive pattern of that number on the segments */
  84.             GPIO_PORTB_DATA_R = minutesec;
  85.  
  86.             delayMs(1);
  87.  
  88.             if( (NVIC_ST_CTRL_R & (1<<16) ) != 0) {
  89.                 seconds += 1;
  90.                 if(seconds >= 59 ) {
  91.                     minutes++;
  92.                     seconds = 0;
  93.                 }
  94.                 if(minutes >= 59 ) {
  95.                     minutes = 0;
  96.                     seconds = 0;
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103. void One_Second_Delay(void)
  104. {
  105.     NVIC_ST_RELOAD_R = 15999999;    /* Reload Value goes here */
  106.     NVIC_ST_CTRL_R |= 0x5;          /* Triggering with external clock */
  107. }
  108.  
  109. /* delay n milliseconds (16 MHz CPU clock) */
  110. void delayMs(int n)
  111. {
  112.     int i, j;
  113.  
  114.     for(i = 0 ; i < n; i++)
  115.         for(j = 0; j < 3180; j++)
  116.             {} /* do nothing for 1 ms */
  117. }