/* * Display number 2014 on the seven-segment LED for TI Tiva LaunchPad * with Wytec EduBase. * The four digit seven segment LED is driven by two shift registers, * one to control the anodes (segments) and the other the common cathods * (digit select). The shift registers are daisy-chained to SSI2 * of the Tiva LaunchPad. * Each digit is enabled for 4 ms. With four digits, the cycle time is * 4 ms * 4 = 16 ms that translates to 62.5 Hz. */ #include "TM4C123GH6PM.h" void delayMs(int n); void delayUs(int n); void sevenseg_init(void); void SSI2_write(unsigned char data); int main(void) { // initialize SSI2 that connects to the shift registers sevenseg_init(); while(1) { SSI2_write(0x5B); // write pattern 2 to the seven segments SSI2_write(~(1 << 3)); // select digit 3 delayMs(4); SSI2_write(0x3F); // write pattern 0 to the seven segments SSI2_write(~(1 << 2)); // select digit 2 delayMs(4); SSI2_write(0x06); // write pattern 1 to the seven segments SSI2_write(~(1 << 1)); // select digit 1 delayMs(4); SSI2_write(0x66); // write pattern 4 to the seven segments SSI2_write(~(1 << 0)); // select digit 0 delayMs(4); } } // enable SSI2 and associated GPIO pins void sevenseg_init(void) { SYSCTL->RCGCGPIO |= 0x02; // enable clock to GPIOB SYSCTL->RCGCGPIO |= 0x04; // enable clock to GPIOC SYSCTL->RCGCSSI |= 0x04; // enable clock to SSI2 // PORTB 7, 4 for SSI2 TX and SCLK GPIOB->AMSEL &= ~0x90; // turn off analog of PORTB 7, 4 GPIOB->AFSEL |= 0x90; // PORTB 7, 4 for alternate function GPIOB->PCTL &= ~0xF00F0000; // clear functions for PORTB 7, 4 GPIOB->PCTL |= 0x20020000; // PORTB 7, 4 for SSI2 function GPIOB->DEN |= 0x90; // PORTB 7, 4 as digital pins // PORTC 7 for SSI2 slave select GPIOC->AMSEL &= ~0x80; // disable analog of PORTC 7 GPIOC->DATA |= 0x80; // set PORTC 7 idle high GPIOC->DIR |= 0x80; // set PORTC 7 as output for SS GPIOC->DEN |= 0x80; // set PORTC 7 as digital pin SSI2->CR1 = 0; // turn off SSI2 during configuration SSI2->CC = 0; // use system clock SSI2->CPSR = 16; // clock prescaler divide by 16 gets 1 MHz clock SSI2->CR0 = 0x0007; // clock rate div by 1, phase/polarity 0 0, mode freescale, data size 8 SSI2->CR1 = 2; // enable SSI2 as master } // This function enables slave select, writes one byte to SSI2, // wait for transmit complete and deassert slave select. void SSI2_write(unsigned char data) { GPIOC->DATA &= ~0x80; // assert slave select SSI2->DR = data; // write data while (SSI2->SR & 0x10) {} // wait for transmit done GPIOC->DATA |= 0x80; // deassert slave select } // 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 } // delay n microseconds (16 MHz CPU clock) void delayUs(int n) { int i, j; for(i = 0 ; i < n; i++) for(j = 0; j < 3; j++) {} // do nothing for 1 us } // This function is called by the startup assembly // code to perform system specific initialization tasks. void SystemInit(void) { // Grant coprocessor access // This is required since TM4C123G has // a floating point coprocessor SCB->CPACR |= 0x00f00000; }