#include "stm32f4xx.h" #include "LCD2x16.c" #include "math.h" int Table[4096], TablePtr, Am = 255, K = 655; int main () { // Table init for (TablePtr = 0; TablePtr <= 4095; TablePtr++) Table[TablePtr] = (int)(1850.0 * sin((float)TablePtr / 2048.0 * 3.141592653)); // GPIO clock enable, digital pin definitions RCC->AHB1ENR |= 0x00000001; // Enable clock for GPIOA RCC->AHB1ENR |= 0x00000010; // Enable clock for GPIOE GPIOE->MODER |= 0x00010000; // output pin PE08: time mark GPIOE->MODER |= 0x00040000; // output pin PE09: toggle GPIOA->MODER |= 0x00001000; // output pin PA06: LED D390 // LCD init LCD_init(); LCD_string("Tp=", 0x01); LCD_string("Am=", 0x41); // DAC set-up RCC->APB1ENR |= 0x20000000; // Enable clock for DAC DAC->CR |= 0x00010001; // DAC control reg, both channels ON GPIOA->MODER |= 0x00000f00; // PA04, PA05 are analog outputs // Timer 2 set-up RCC->APB1ENR |= 0x0001; // Enable clock for Timer 2 TIM2->ARR = 840; // Auto Reload: 8400 == 100us -> 100kHz TIM2->DIER |= 0x0001; // DMA/IRQ Enable Register - enable IRQ on update TIM2->CR1 |= 0x0001; // Enable Counting // NVIC IRQ enable NVIC_EnableIRQ(TIM2_IRQn); // Enable IRQ for TIM2 in NVIC // waste time - check pushbuttons and display parameters while (1) { if (GPIOE->IDR & 0x0001) GPIOA->ODR |= 0x0040; // LED on else GPIOA->ODR &= ~0x0040; // else LED off if ((GPIOE->IDR & 0x0004) && (K < 32767)) K += 1; // S372 if ((GPIOE->IDR & 0x0008) && (K > 2)) K -= 1; // S373 if ((GPIOE->IDR & 0x0010) && (Am < 255)) Am += 1; // S374 if ((GPIOE->IDR & 0x0020) && (Am > 1)) Am -= 1; // S375 GPIOE->ODR |= 0x0200; // time mark: PE09 up GPIOE->ODR &= ~0x0200; // time mark: PE09 down float Fp = 1e5 * K / 65536.9; // calculate frequency LCD_uInt16((int)Fp,0x08,1); // display frequency LCD_uInt16(Am,0x48,1); // display amplitude }; } // IRQ function void TIM2_IRQHandler(void) // DDS takes approx 500ns of CPU time! { GPIOE->ODR |= 0x0100; // PE08 up TIM2->SR &= ~0x00000001; // clear update event flag in TIM2 TablePtr = (TablePtr + K) & 0xffff; // increment and limit to 16 bits DAC->DHR12R1 = (Am * Table[ TablePtr >> 4 ]) / 256 + 2048; // Table[TablePtr] -> DAC DAC->DHR12R2 = (Am * Table[((TablePtr >> 4) + 1024) & 4095]) / 256 + 2048; // Table[TablePtr] -> DAC GPIOE->ODR &= ~0x0100; // PE08 down }