#include "stm32f4xx.h" #include "stm32f4xx_rcc.c" #include "stm32f4xx_gpio.c" #include "stm32f4xx_tim.c" #include "stm32f4xx_dac.c" #include "LCD2x16.c" #include "math.h" #include "dd.h" int Table[4096], ptrTable, K = 655, dK = 0; char InPat = 0; int PortE, f_VCO; // 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); } // initialize port E, Gpio_Pin_8 to GPIO_Pin_11 as outputs void GPIOEinit_8to11 (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_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); } int main () { // Table init for analog output for (ptrTable = 0; ptrTable <= 4095; ptrTable++) Table[ptrTable] = (int)(1850.0 * sin((float)ptrTable / 2048.0 * 3.141592653)); LCD_init(); LCD_string("Fin=", 0x01); LCD_string("Hz", 13); GPIOEinit_8to11(); DACinit(); TIM5init_TimeBase_ReloadIRQ(840); // 840 == 10us == 100kHz // endless loop - display frequency while (1) { LCD_uInt16((int)(K * 100000 / 65536),0x07,1); // display frequency for (int i = 0; i < 5000000; i++) {}; // waste time }; } // IRQ function for Timer5 void TIM5_IRQHandler(void) // PLL takes approx 600 ns of CPU time! { GPIOE->BSRRL = BIT_8; // TIM_ClearITPendingBit(TIM5, TIM_IT_Update); // clear interrupt flag PortE = GPIOE->IDR & BIT_15; // read input signal f_VCO = ptrTable & 0x8000; // this is locally generated signal InPat = (InPat << 2) & 0x0c; // construct pattern for phase detector if (PortE) InPat += 2; // if (f_VCO) InPat += 1; // if (PortE == f_VCO) dK = 0; // if equal signals (frequencies) else { if (InPat == 0x02) dK = 1; // if frequency too high if (InPat == 0x0d) dK = 1; // if (InPat == 0x01) dK = -1; // if frequency too low if (InPat == 0x0e) dK = -1; // }; K += dK; // correct time interval if (K > 0x8000) K = 0x8000; // but not too much if (K < 0x0080) K = 0x0080; // ptrTable = (ptrTable + K + (dK << 8)) & 0xffff; // update pointer to table GPIOE->ODR = (GPIOE->ODR & ~BIT_10) | (f_VCO >> 5);// digital out - f_VCO DAC->DHR12R1 = (Table[ptrTable >> 4]) + 2048; // analog signal -> DAC DAC->DHR12R2 = K >> 2; // frequency -> DAC GPIOE->BSRRH = BIT_8; // }