/* p7_5.c: Use DAC to generate sine waveform * * This program uses a pre-calculated lookup table to generate a * sine wave output through a DAC. * The DAC is initialized with no buffer or trigger, so every * write to the DAC data register will change the analog output. * The index of the lookup table is incremented in the loop and * the data is written to the DAC. * The output of DAC is on pin PA4. * * This program was tested with Keil uVision v5.24a with DFP v2.11.0. */ #include "stm32f4xx.h" void delayUs(int n); int main(void) { int i; const static int sineWave[] = {2048,3071,3821,4095,3821,3071,2048,1024,274,0,274,1024}; RCC->AHB1ENR |= 1; /* enable GPIOA clock */ GPIOA->MODER |= 0x00000300; /* PA4 analog */ /* setup DAC */ RCC->APB1ENR |= 1 << 29; /* enable DAC clock */ DAC->CR |= 1; /* enable DAC */ while(1) { for (i = 0; i < sizeof(sineWave)/sizeof(int); i++) { DAC->DHR12R1 = sineWave[i]; /* write value of sinewave to DAC */ delayUs(10); } } } /* 16 MHz SYSCLK */ void delayUs(int n) { int i; for (; n > 0; n--) for (i = 0; i < 3; i++) ; }