#include "stm32f4xx.h" #include "stm32f4xx_rcc.c" #include "stm32f4xx_dac.c" #include "stm32f4xx_gpio.c" #include "stm32f4xx_tim.c" #include "dd.h" #include "LCD2x16.c" #include "math.h" int Table[4096], ptrTable, Am = 255, k = 655; void SWITCHinit (void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOE, &GPIO_InitStructure); } // initialize port E, Gpio_Pin_8 to GPIO_Pin_15 as outputs void GPIOEinit (void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOE, &GPIO_InitStructure); } // DAC init function void DACinit (void) { DAC_InitTypeDef DAC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Init(DAC_Channel_2, &DAC_InitStructure); DAC_Cmd(DAC_Channel_1, ENABLE); DAC_Cmd(DAC_Channel_2, ENABLE); } // Timer 5 init function - time base void TIM5init_TimeBase_ReloadIRQ (int interval) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = interval; TIM_TimeBaseInitStructure.TIM_Prescaler = 0; TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStructure); NVIC_EnableIRQ(TIM5_IRQn); // Enable IRQ for TIM5 in NVIC TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE); // Enable IRQ on update for Timer5 TIM_Cmd(TIM5, ENABLE); } int main () { int sw, Fp, j; for (ptrTable = 0; ptrTable <= 4095; ptrTable++) Table[ptrTable] = (int)(1850.0 * sin((float)ptrTable / 2048.0 * 3.14159265)); SWITCHinit (); GPIOEinit (); LCD_init (); LCD_string("Frq=", 0x01); LCD_string("Hz", 0x0d); LCD_string("Amp=", 0x41); DACinit (); TIM5init_TimeBase_ReloadIRQ(840); // 840 == 10us while (1) { // endless loop sw = GPIOE->IDR; if ((sw & S370) && (k < 32768)) k++; if ((sw & S371) && (k > 2)) k--; if ((sw & S372) && (Am < 255)) Am++; if ((sw & S373) && (Am > 1)) Am--; Fp = (int)(1.0e5 * (float)k / 65536.9); LCD_uInt16(Fp, 0x08, 1); LCD_uInt16(Am, 0x48, 1); for (j = 0; j<200000; j++){}; // waste some time }; } // IRQ function for Timer5 void TIM5_IRQHandler(void) { GPIOE->BSRRL = BIT_8; TIM_ClearITPendingBit(TIM5, TIM_IT_Update); // clear interrupt flag ptrTable = (ptrTable + k) & 0xffff; DAC->DHR12R1 = (Am * Table[ ptrTable >> 4 ]) / 256 + 2048; DAC->DHR12R2 = (Am * Table[((ptrTable >> 4) + 1024) & 4095]) / 256 + 2048; GPIOE->BSRRH = BIT_8; }