/* * Display number 2014 on the seven-segment LED for FRDM-KL25Z * 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 cathodes * (digit select). The shift registers are daisy-chained to SPI0 * of the FRDM-KL25Z. * 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 "MKL25Z4.h" void delayMs(int n); void SPI0_init(void); void SPI0_write(unsigned char data); int main(void) { // initialize SPI0 that connects to the shift registers SPI0_init(); while(1) { SPI0_write(0x5B); // write pattern 2 to the seven segments SPI0_write(~(1 << 3)); // select digit 3 delayMs(4); SPI0_write(0x3F); // write pattern 0 to the seven segments SPI0_write(~(1 << 2)); // select digit 2 delayMs(4); SPI0_write(0x06); // write pattern 1 to the seven segments SPI0_write(~(1 << 1)); // select digit 1 delayMs(4); SPI0_write(0x66); // write pattern 4 to the seven segments SPI0_write(~(1 << 0)); // select digit 0 delayMs(4); } } void SPI0_init(void) { SIM->SCGC5 |= 0x1000; /* enable clock to Port D */ PORTD->PCR[1] = 0x200; /* make PTD1 pin as SPI SCK */ PORTD->PCR[2] = 0x200; /* make PTD2 pin as SPI MOSI */ SIM->SCGC4 |= 0x400000; /* enable clock to SPI0 */ SPI0->C1 = 0x10; /* disable SPI and make SPI0 master */ SPI0->BR = 0x60; /* set Baud rate to 1 MHz */ SPI0->C1 |= 0x40; /* Enable SPI module */ SIM->SCGC5 |= 0x0800; /* enable clock to Port C */ PORTC->PCR[9] = 0x100; /* make PTC9 pin as GPIO */ PTC->PDDR |= 1 << 9; /* make PTC9 as output pin for /SS */ PTC->PSOR = 1 << 9; /* make PTC9 idle high */ } void SPI0_write(unsigned char data) { volatile char dummy; PTC->PCOR = 1 << 9; /* assert /SS */ while(!(SPI0->S & 0x20)) { } /* wait until tx ready */ SPI0->D = data; /* send data byte */ while(!(SPI0->S & 0x80)) { } /* wait until tx complete */ dummy = SPI0->D; /* clear SPRF */ PTC->PSOR = 1 << 9; /* deasssert /SS */ } // delay n milliseconds (41.94MHz CPU clock) void delayMs(int n) { int i, j; for(i = 0 ; i < n; i++) for(j = 0; j < 7000; j++) {} /* do nothing */ }