/* * This program generate a sawtooth waveform and a sine wave * using the DAC on the Wytec EduBase board. * * The Wytec EduBase board has a dual channel 12-bit DAC (MCP4922) * connected to the Tiva LaunchPad through SSI2. PORTD 6 is used * for slave select. */ #include "TM4C123GH6PM.h" #include void DAC_init(void); void DAC_write(int value, int channel); int main(void) { int i = 0; float f; DAC_init(); while(1) { // create a sawtooth waveform DAC_write(i++, 0); // create a sine wave f = sinf(0.00314159 * i) * 0x07FF + 0x800; DAC_write((int)f, 1); } } // initialize SSI2 that connects to the DAC void DAC_init(void) { SYSCTL->RCGCGPIO |= 0x02; // enable clock to GPIOB SYSCTL->RCGCGPIO |= 0x08; // enable clock to GPIOD SYSCTL->RCGCSSI |= 0x04; // enable clock to SSI2 // PORTB 7, 4 for SSI2 GPIOB->AFSEL |= 0x90; // PORTB 7, 4 for SSI2 GPIOB->PCTL &= ~0xF00F0000; // PORTB 7, 4 for SSI2 GPIOB->PCTL |= 0x20020000; GPIOB->DEN |= 0x90; // PORTB 7, 4 as digital pins // use PD6 for DAC chip select GPIOD->DIR |= 0x40; GPIOD->DEN |= 0x40; GPIOD->AMSEL &= ~0x40; GPIOD->DATA |= 0x40; // initialize SSI2 SSI2->CR1 = 0; // make it master SSI2->CC = 0; // use system clock SSI2->CPSR = 16; // clock prescaler divide by 16 gets 1 MHz clock SSI2->CR0 = 0x7; // clock rate div by 1, phase/polarity 0 0, mode freescale, data size 8 SSI2->CR1 = 2; // enable SSI2 } // write a value to DAC through SSI2 void DAC_write(int value, int channel) { const int BUFFERED = 0x4000; const int UNITYGAIN = 0x2000; const int NOTSHUTDOWN = 0x1000; GPIOD->DATA &= ~0x40; // assert chip select value = (value & 0x0FFF) | (channel ? 0x8000 : 0) | BUFFERED | UNITYGAIN | NOTSHUTDOWN; SSI2->DR = value >> 8; // send high byte SSI2->DR = value & 0xFF; // send low byte while (SSI2->SR & 0x10) ; // wait for transmit done GPIOD->DATA |= 0x40; // deassert chip select } // 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; }