/*
* Display minutes and seconds on the 4-digit 7-segment LED.
*
*/
#include <stdint.h>
#include <stdlib.h>
#include "inc/tm4c123gh6pm.h"
#define NO_OF_DIGITS 4
/* 0 - 9 pattern for the common cathode configuration */
unsigned char digitPattern[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
unsigned char digit_no[] = {0x10, 0x20, 0x40, 0x80};
/* for the common anode configuration */
// unsigned char digitPattern[] = {0xCF, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
// unsigned char digit_no[] = {0x1F, 0x2F, 0x4F, 0x8F};
void One_Second_Delay(void);
void delayMs(int n);
int seconds, minutes, minutesec;
int main(void)
{
volatile unsigned long delay;
int digits;
seconds = 0;
minutes = 0;
minutesec = 0;
SYSCTL_RCGC2_R |= 0x01; /* enable clock to GPIOA */
SYSCTL_RCGC2_R |= 0x02; /* enable clock to GPIOB */
delay = SYSCTL_RCGC2_R; /* allow time for clock to start */
delay = delay;
GPIO_PORTB_DIR_R = 0xFF; /* set all PORTB pins as output to drive segments */
GPIO_PORTB_DEN_R = 0xFF; /* set all PORTB pins as digital pins */
GPIO_PORTB_DR8R_R = 0xFF;
GPIO_PORTA_DIR_R = 0xF0; /* set PORTA pin 7-4 as output to select digit */
GPIO_PORTA_DEN_R = 0xF0; /* set PORTA pin 7-4 as digital pins */
GPIO_PORTA_DR8R_R = 0xF0;
One_Second_Delay();
for(;;) {
for(digits = 0; digits < NO_OF_DIGITS; digits++ ) {
GPIO_PORTA_DATA_R = digit_no[digits]; /* select digit */
switch(digits) {
case 0:
if( seconds >= 10)
minutesec = digitPattern[seconds%10];
else
minutesec = digitPattern[seconds];
break;
case 1:
if( seconds >= 10)
minutesec = digitPattern[seconds/10];
else
minutesec = digitPattern[0];
break;
case 2:
if( minutes >= 10)
minutesec = digitPattern[minutes%10];
else
minutesec = digitPattern[minutes];
break;
case 3:
if( minutes >= 10)
minutesec = digitPattern[minutes/10];
else
minutesec = digitPattern[0];
break;
default:
break;
}
/* drive pattern of that number on the segments */
GPIO_PORTB_DATA_R = minutesec;
delayMs(1);
if( (NVIC_ST_CTRL_R & (1<<16) ) != 0) {
seconds += 1;
if(seconds >= 59 ) {
minutes++;
seconds = 0;
}
if(minutes >= 59 ) {
minutes = 0;
seconds = 0;
}
}
}
}
}
void One_Second_Delay(void)
{
NVIC_ST_RELOAD_R = 15999999; /* Reload Value goes here */
NVIC_ST_CTRL_R |= 0x5; /* Triggering with external clock */
}
/* delay n milliseconds (16 MHz CPU clock) */
void delayMs(int n)
{
int i, j;
for(i = 0 ; i < n; i++)
for(j = 0; j < 3180; j++)
{} /* do nothing for 1 ms */
}