1

我正在使用 stm32L476 发现板。我正在尝试使用 DAC 生成三角波。我正在为 DAC 使用两个查找表(每个上坡和下坡各 1 个)。我在 ADC 输入端给出正弦波,并且在三角波上坡时间和下坡时间得到一些样本。我一起启动 ADC 和 DAC 用于上坡和下坡。我正在计算 ADC 样本的 FFT。对于上坡数据,我在下坡 DAC 转换开始时开始计算 FFT,而对于下坡 ADC 采样 FFT,我在下坡 DAC 转换完成后开始计算 FFT。我已经做了所有必要的事情,以确保 DAC、ADC 和 FFT 过程的正确开始和结束时间。问题是当我以高速(约 500 KSPS)运行 DAC 时,波形会出现几秒钟,然后输出变得平坦。输出波形在波峰侧变得平坦。当我将 DAC 速度降低到 400 KSPS 左右时,问题不会发生。此外,当我在下坡 FFT 完成后使用 HAL 延迟时,问题得到解决,并且所有 DAC 速度的波形都正确显示。但我不想在那里浪费任何时间。下面是代码。任何人都知道这里的问题是什么?

#include "main.h"
#include "stm32l4xx_hal.h"
#include "arm_math.h"
#include "math.h"
#include "arm_const_structs.h"

#define buf_size 128

ADC_HandleTypeDef hadc2;
DMA_HandleTypeDef hdma_adc2;

DAC_HandleTypeDef hdac1;
DMA_HandleTypeDef hdma_dac_ch2;

TIM_HandleTypeDef htim7;
TIM_HandleTypeDef htim15;

UART_HandleTypeDef huart3;
DMA_HandleTypeDef hdma_usart3_rx;

void calculate_myfft(int32_t* buf);

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC2_Init(void);
static void MX_DAC1_Init(void);
static void MX_TIM7_Init(void);
static void MX_TIM15_Init(void);
static void MX_USART3_UART_Init(void);

int buf_size1 = buf_size;
const uint16_t val1[]={8,16,25,33,41,49,57,66,
        74,82,90,98,106,115,123,131,
        139,147,156,164,172,180,188,197,
        205,213,221,229,238,246,254,262,
        270,278,287,295,303,311,319,328,
        336,344,352,360,369,377,385,393,
        401,410,418,426,434,442,450,459,
        467,475,483,491,500,508,516,524,
        532,541,549,557,565,573,581,590,
        598,606,614,622,631,639,647,655,
        663,672,680,688,696,704,713,721,
        729,737,745,753,762,770,778,786,
        794,803,811,819,827,835,844,852,
        860,868,876,885,893,901,909,917,
        925,934,942,950,958,966,975,983,
        991,999,1007,1016,1024,1032,1040,1048,
        1057,1065,1073,1081,1089,1097,1106,1114,
        1122,1130,1138,1147,1155,1163,1171,1179,
        1188,1196,1204,1212,1220,1229,1237,1245,
        1253,1261,1269,1278,1286,1294,1302,1310,
        1319,1327,1335,1343,1351,1360,1368,1376,
        1384,1392,1400,1409,1417,1425,1433,1441,
        1450,1458,1466,1474,1482,1491,1499,1507,
        1515,1523,1532,1540,1548,1556,1564,1572,
        1581,1589,1597,1605,1613,1622,1630,1638,
        1646,1654,1663,1671,1679,1687,1695,1704,
        1712,1720,1728,1736,1744,1753,1761,1769,
        1777,1785,1794,1802,1810,1818,1826,1835,
        1843,1851,1859,1867,1876,1884,1892,1900,
        1908,1916,1925,1933,1941,1949,1957,1966,
        1974,1982,1990,1998,2007,2015,2023,2031,
        2039,2048,2056,2064,2072,2080,2088,2097,
        2105,2113,2121,2129,2138,2146,2154,2162,
        2170,2179,2187,2195,2203,2211,2219,2228,
        2236,2244,2252,2260,2269,2277,2285,2293,
        2301,2310,2318,2326,2334,2342,2351,2359,
        2367,2375,2383,2391,2400,2408,2416,2424,
        2432,2441,2449,2457,2465,2473,2482,2490,
        2498,2506,2514,2523,2531,2539,2547,2555,
        2563,2572,2580,2588,2596,2604,2613,2621,
        2629,2637,2645,2654,2662,2670,2678,2686,
        2695,2703,2711,2719,2727,2735,2744,2752,
        2760,2768,2776,2785,2793,2801,2809,2817,
        2826,2834,2842,2850,2858,2867,2875,2883,
        2891,2899,2907,2916,2924,2932,2940,2948,
        2957,2965,2973,2981,2989,2998,3006,3014,
        3022,3030,3038,3047,3055,3063,3071,3079,
        3088,3096,3104,3112,3120,3129,3137,3145,
        3153,3161,3170,3178,3186,3194,3202,3210,
        3219,3227,3235,3243,3251,3260,3268,3276,
        3284,3292,3301,3309,3317,3325,3333,3342,
        3350,3358,3366,3374,3382,3391,3399,3407,
        3415,3423,3432,3440,3448,3456,3464,3473,
        3481,3489,3497,3505,3514,3522,3530,3538,
        3546,3554,3563,3571,3579,3587,3595,3604,
        3612,3620,3628,3636,3645,3653,3661,3669,
        3677,3686,3694,3702,3710,3718,3726,3735,
        3743,3751,3759,3767,3776,3784,3792,3800,
        3808,3817,3825,3833,3841,3849,3857,3866,
        3874,3882,3890,3898,3907,3915,3923,3931,
        3939,3948,3956,3964,3972,3980,3989,3997,
        4005,4013,4021,4029,4038,4046,4054,4062,
        4070,4079,4087,4095};

const uint16_t val2[] = {
        4087,4079,4070,4062,
                4054,4046,4038,4029,4021,4013,4005,3997,
                3989,3980,3972,3964,3956,3948,3939,3931,
                3923,3915,3907,3898,3890,3882,3874,3866,
                3857,3849,3841,3833,3825,3817,3808,3800,
                3792,3784,3776,3767,3759,3751,3743,3735,
                3726,3718,3710,3702,3694,3686,3677,3669,
                3661,3653,3645,3636,3628,3620,3612,3604,
                3595,3587,3579,3571,3563,3554,3546,3538,
                3530,3522,3514,3505,3497,3489,3481,3473,
                3464,3456,3448,3440,3432,3423,3415,3407,
                3399,3391,3382,3374,3366,3358,3350,3342,
                3333,3325,3317,3309,3301,3292,3284,3276,
                3268,3260,3251,3243,3235,3227,3219,3210,
                3202,3194,3186,3178,3170,3161,3153,3145,
                3137,3129,3120,3112,3104,3096,3088,3079,
                3071,3063,3055,3047,3038,3030,3022,3014,
                3006,2998,2989,2981,2973,2965,2957,2948,
                2940,2932,2924,2916,2907,2899,2891,2883,
                2875,2867,2858,2850,2842,2834,2826,2817,
                2809,2801,2793,2785,2776,2768,2760,2752,
                2744,2735,2727,2719,2711,2703,2695,2686,
                2678,2670,2662,2654,2645,2637,2629,2621,
                2613,2604,2596,2588,2580,2572,2563,2555,
                2547,2539,2531,2523,2514,2506,2498,2490,
                2482,2473,2465,2457,2449,2441,2432,2424,
                2416,2408,2400,2391,2383,2375,2367,2359,
                2351,2342,2334,2326,2318,2310,2301,2293,
                2285,2277,2269,2260,2252,2244,2236,2228,
                2219,2211,2203,2195,2187,2179,2170,2162,
                2154,2146,2138,2129,2121,2113,2105,2097,
                2088,2080,2072,2064,2056,2048,2039,2031,
                2023,2015,2007,1998,1990,1982,1974,1966,
                1957,1949,1941,1933,1925,1916,1908,1900,
                1892,1884,1876,1867,1859,1851,1843,1835,
                1826,1818,1810,1802,1794,1785,1777,1769,
                1761,1753,1744,1736,1728,1720,1712,1704,
                1695,1687,1679,1671,1663,1654,1646,1638,
                1630,1622,1613,1605,1597,1589,1581,1572,
                1564,1556,1548,1540,1532,1523,1515,1507,
                1499,1491,1482,1474,1466,1458,1450,1441,
                1433,1425,1417,1409,1400,1392,1384,1376,
                1368,1360,1351,1343,1335,1327,1319,1310,
                1302,1294,1286,1278,1269,1261,1253,1245,
                1237,1229,1220,1212,1204,1196,1188,1179,
                1171,1163,1155,1147,1138,1130,1122,1114,
                1106,1097,1089,1081,1073,1065,1057,1048,
                1040,1032,1024,1016,1007,999,991,983,
                975,966,958,950,942,934,925,917,
                909,901,893,885,876,868,860,852,
                844,835,827,819,811,803,794,786,
                778,770,762,753,745,737,729,721,
                713,704,696,688,680,672,663,655,
                647,639,631,622,614,606,598,590,
                581,573,565,557,549,541,532,524,
                516,508,500,491,483,475,467,459,
                450,442,434,426,418,410,401,393,
                385,377,369,360,352,344,336,328,
                319,311,303,295,287,278,270,262,
                254,246,238,229,221,213,205,197,
                188,180,172,164,156,147,139,131,
                123,115,106,98,90,82,74,66,
                57,49,41,33,25,16,8,0};

int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int run_buf = 0;

uint16_t ADC1ConvertedValues[buf_size];
uint16_t ADC1ConvertedValues2[buf_size];
const int lengthofa = sizeof( ADC1ConvertedValues ) / sizeof( ADC1ConvertedValues[0] );
int32_t ADC1ConvertedValues91[buf_size];
int32_t ADC1ConvertedValues92[buf_size];
int32_t data_output1_temp1[buf_size];
int32_t data_output1_temp2[buf_size];
int16_t data_arrayh1[buf_size];
int16_t data_output[2*buf_size];
float hann[buf_size];

int run_adc = 0;
int dac_half_conv_cmpt = 0;
int adc_conv_cmpt = 0; 
int fft_flag = 0;;

int slope = 1;
int restart_dac = 0;
int fft_2_goingon = 0;
int timer15_start = 0;
int l1 = 0;
int l2 = 0;
int some_delay = 0;
int start_next_chirp = 0;
int start_fft = 0;
int dac_conv_cmpt = 0;
int timer15_started = 0;
int run_buf_was_6 = 0;

uint8_t data[2];
uint8_t var1;
uint8_t var3;
uint8_t var2;
uint8_t var4;
uint8_t var5;
uint8_t var6;

int i = 0;
int16_t i_buf[buf_size];
int send_to_usart = 0;

int fft1 = 0;
int fft2 = 0;

uint8_t *g;
int fft1_count = 0;
int fft2_count = 0;
int ready_for_next_chirp = 0;

int main(void)
{
    int p = 0;
    int n1=sizeof(val1);
    l1=n1/sizeof(val1[0]);
    int n2=sizeof(val2);
    l2=n2/sizeof(val2[0]);
  HAL_Init();
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC2_Init();
  MX_DAC1_Init();
  MX_TIM7_Init();
  MX_TIM15_Init();
  MX_USART3_UART_Init();

    g = &data[0];
    HAL_UART_Receive_DMA(&huart3, (uint8_t*)g, 1);
    HAL_TIM_Base_Start(&htim7);
    HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);
    HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_2, (uint32_t*)val1, l1, DAC_ALIGN_12B_R);
    HAL_GPIO_WritePin(GPIOE, GPIO_PIN_11, GPIO_PIN_SET);
    HAL_ADC_Start_DMA(&hadc2, (uint32_t*)&ADC1ConvertedValues, buf_size);
    for (p = 0; p < buf_size; p++)
          {
              hann[p] = 0.54 - 0.46 * cosf((2*PI*p)/(buf_size-1));
          }

    while (1)
    {
      if((run_buf == 1) && (ready_for_next_chirp == 1))
          {
              //HAL_TIM_Base_Stop_IT(&htim15);
              send_to_usart = 0;
              //HAL_TIM_Base_DeInit(&htim15);
              MX_TIM15_Init();
              ready_for_next_chirp = 0;
              //HAL_TIM_Base_Init(&htim15);
              count2 += 1;
              run_buf_was_6 = 0;
              run_buf = 3;
              HAL_GPIO_WritePin(GPIOE, GPIO_PIN_11, GPIO_PIN_SET);
              HAL_TIM_Base_Init(&htim7);
              HAL_TIM_Base_Start(&htim7);
              HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_2);
              HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_2, (uint32_t*)val1, l1, DAC_ALIGN_12B_R);
              HAL_ADC_Start_DMA(&hadc2, (uint32_t*)&ADC1ConvertedValues, buf_size);
              //slope = 1;
              //start_fft = 1;
          }

          else if(run_buf == 2)
          {
              run_buf = 4;
              count3 += 1;
              //dac_conv_cmpt = 0;
              HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_2);
              HAL_TIM_Base_Init(&htim7);
              HAL_TIM_Base_Start(&htim7);
              HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_2, (uint32_t*)val2, l2, DAC_ALIGN_12B_R);
              fft_flag = 0;
              slope = 1;
              start_fft = 1;
          }

          if(some_delay == 1)
          {
              some_delay = 0;
              count7 += 1;
              //HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_2);
              HAL_Delay(1);
              timer15_started = 1;
              fft_flag = 0;
              run_buf = 1;
              //HAL_TIM_Base_Start_IT(&htim15);
          }

          if((start_fft == 1) && (slope == 1))
                {
              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, SET);
              start_fft = 0;
              fft1 = 1;
              count5 += 1;
                      //count += 1;
                      //adc_conv_cmpt = 0;
                      //fft_flag = 0;
                      for(int j=0; j<buf_size; j++)
                          {
                                ADC1ConvertedValues91[j] = (int32_t)((int32_t)(ADC1ConvertedValues[j]) - 2048);
                          }
                      calculate_myfft(ADC1ConvertedValues91);
                      fft_flag = 1;
                      fft1 = 0;
                    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, RESET);
                  }
            else if((start_fft == 1) && (slope == 2))
                  {
                      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, SET);
                      start_fft = 0;
                      count6 += 1;
                      fft2 = 1;
                      timer15_started = 0;
                      //count1 += 1;
                      //adc_conv_cmpt = 0;
                      //fft_flag = 3;
                      for(int j=0; j<buf_size; j++)
                          {
                                ADC1ConvertedValues92[j] = (int32_t)((int32_t)(ADC1ConvertedValues2[j]) - 2048);
                          }
                      fft_2_goingon = 1;
                      //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, SET);
                      calculate_myfft(ADC1ConvertedValues92);
                      fft_2_goingon =0;
                      fft_flag = 2;
                      fft2 = 0;
                      send_to_usart = 1;
                      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, RESET);
                      some_delay = 1;
                      ready_for_next_chirp = 1;
                  }

        if((data[0] == 0x43) && (send_to_usart == 1))
                  {
                      send_to_usart = 0;
                      data[0] = 0x00;
                      data_output1_temp1[0] = 0;
                      data_output1_temp1[1] = 0;
                      for(i=0; i<buf_size; i++)
                                                 {
                                                     var3 = (data_output1_temp1[i] & 0x000000FF);
                                                     //while(adc_go != 0);
                                                     HAL_UART_Transmit(&huart3, &var3, 1, 5);


                                                     var1 = ((data_output1_temp1[i] & 0x0000FF00) >> 8);                                                 
                                             HAL_UART_Transmit(&huart3, &var1, 1, 5);                                           
                                                     var2 = ((data_output1_temp1[i] & 0x00FF0000) >> 16);
                                                    HAL_UART_Transmit(&huart3, &var2, 1, 5);

                                                    var4 = ((data_output1_temp1[i] & 0xFF000000) >> 24);
                                                    HAL_UART_Transmit(&huart3, &var4, 1, 5);

                                                 }

                  }

        if((data[0] == 0x41) && (send_to_usart == 1))
                  {
                      send_to_usart = 0;
                      data[0] = 0x00;
                      for(i=0; i<buf_size; i++)
                                                 {

                                                     var5 = (ADC1ConvertedValues91[i] & 0x00FF);

                                                     HAL_UART_Transmit(&huart3, &var5, 1, 5);                                                        
                                                     var6 = ((ADC1ConvertedValues91[i] & 0xFF00) >> 8);                                                          HAL_UART_Transmit(&huart3, &var6, 1, 5);                                                       
                                                 }  
                  }

        if((data[0] == 0x44) && (send_to_usart == 1))
                          {
                              send_to_usart = 0;
                              data[0] = 0x00;
                              data_output1_temp2[0] = 0;
                              data_output1_temp2[1] = 0;
                              for(i=0; i<buf_size; i++)
                                                         {

                                                             var3 = (data_output1_temp2[i] & 0x000000FF);

                                                             HAL_UART_Transmit(&huart3, &var3, 1, 5);


                                                             var1 = ((data_output1_temp2[i] & 0x0000FF00) >> 8);

                                                             HAL_UART_Transmit(&huart3, &var1, 1, 5);


                                                             var2 = ((data_output1_temp2[i] & 0x00FF0000) >> 16);
                                                            HAL_UART_Transmit(&huart3, &var2, 1, 5);

                                                            var4 = ((data_output1_temp2[i] & 0xFF000000) >> 24);
HAL_UART_Transmit(&huart3, &var4, 1, 5);

                                                         }
                              //data[0] = 0x00;
                          }

                if((data[0] == 0x42) && (send_to_usart == 1))
                          {
                              send_to_usart = 0;
                              data[0] = 0x00;
                              for(i=0; i<buf_size; i++)
                                                         {

                                                             var5 = (ADC1ConvertedValues92[i] & 0x00FF);
                                                             //while(adc_go != 0);
                                                             HAL_UART_Transmit(&huart3, &var5, 1, 5);


                                                             var6 = ((ADC1ConvertedValues92[i] & 0xFF00) >> 8);
                                                             //while(adc_go != 1);
                                                             HAL_UART_Transmit(&huart3, &var6, 1, 5);
                                                             //HAL_Delay(5);

                                                         }
                              //data[0] = 0x00;
                          }

    }

}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    adc_conv_cmpt = 1;
    count += 1;
    HAL_GPIO_WritePin(GPIOE, GPIO_PIN_11, GPIO_PIN_RESET);
}

void calculate_myfft(int32_t* buf)
{
    //fft_flag = 2;
    int n = 0;
    int i = 0;
    //int SAMPLE_LENGTH = 32;

    uint32_t fftSize = buf_size;


    /*for (n = 0; n < buf_size; n++)
        {
            hann[n] = 0.54 - 0.46 * cosf((2*PI*n)/(buf_size-1));
        }*/

    for(n = 0; n < buf_size; n++)
    {
        data_arrayh1[n] = (int16_t)(hann[n]*buf[n]);
    }

    uint32_t ifftFlag = 0;  //to be set when inverse FFT is to be performed
    uint32_t doBitReverse = 1; // Set bit reversal for DIF and DIT selection
    volatile arm_status status;
    arm_rfft_instance_q15 instance;
    arm_rfft_init_q15(&instance, fftSize, ifftFlag, doBitReverse);
    arm_rfft_q15(&instance, data_arrayh1, data_output);

    if(fft1 == 1)
    {
        fft1_count += 1;
        for(i=0; i<(2*buf_size); i=i+2)
        {
            data_output1_temp1[i/2] = (int32_t)((data_output[i] * data_output[i]) + (data_output[i+1] * data_output[i+1]));
            //data_output1_plot[i/2] = data_output1_temp[i/2];
            //fft_flag = 1;
        }
    }
    else if(fft2 == 1)
    {
        fft2_count += 1;
        for(i=0; i<(2*buf_size); i=i+2)
            {
                data_output1_temp2[i/2] = (int32_t)((data_output[i] * data_output[i]) + (data_output[i+1] * data_output[i+1]));
            }
    }
}

void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef* hdac)
{
    count1 += 1;
    //dac_conv_cmpt = 1;
    HAL_TIM_Base_Stop(&htim7);
    HAL_TIM_Base_DeInit(&htim7);

    if(run_buf == 3)
    {
        run_buf = 2;
        //run_buf = 4;
        HAL_GPIO_WritePin(GPIOE, GPIO_PIN_11, GPIO_PIN_SET);
        HAL_ADC_Start_DMA(&hadc2, (uint32_t*)&ADC1ConvertedValues2, buf_size);
        //HAL_Delay(3);
        //some_delay = 1;
    }
    else if(run_buf == 4)
    {
        run_buf = 6;
        //HAL_Delay(3);
        run_buf_was_6 = 1;
        slope = 2;
        start_fft = 1;
        //some_delay = 1;
    }
    else if(run_buf == 0)
    {
        run_buf = 2;
        //run_buf = 4;
        HAL_GPIO_WritePin(GPIOE, GPIO_PIN_11, GPIO_PIN_SET);
        HAL_ADC_Start_DMA(&hadc2, (uint32_t*)&ADC1ConvertedValues2, buf_size);
    }
}


void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;


  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 40;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }


  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_ADC;
  PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_SYSCLK;
  PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
  PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
  PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
  PeriphClkInit.PLLSAI1.PLLSAI1N = 34;
  PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
  PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_ADC1CLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }


  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }


  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);


  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);


  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}


static void MX_ADC2_Init(void)
{

  ADC_ChannelConfTypeDef sConfig;

  hadc2.Instance = ADC2;
  hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc2.Init.Resolution = ADC_RESOLUTION_12B;
  hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc2.Init.LowPowerAutoWait = DISABLE;
  hadc2.Init.ContinuousConvMode = ENABLE;
  hadc2.Init.NbrOfConversion = 1;
  hadc2.Init.DiscontinuousConvMode = DISABLE;
  hadc2.Init.NbrOfDiscConversion = 1;
  hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc2.Init.DMAContinuousRequests = DISABLE;
  hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc2.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }


  sConfig.Channel = ADC_CHANNEL_8;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

static void MX_DAC1_Init(void)
{

  DAC_ChannelConfTypeDef sConfig;

  hdac1.Instance = DAC1;
  if (HAL_DAC_Init(&hdac1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }


  sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
  sConfig.DAC_Trigger = DAC_TRIGGER_T7_TRGO;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
  sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

static void MX_TIM7_Init(void)
{

  TIM_MasterConfigTypeDef sMasterConfig;

  htim7.Instance = TIM7;
  htim7.Init.Prescaler = 0;
  htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim7.Init.Period = 79;
  if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

static void MX_TIM15_Init(void)
{

  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  htim15.Instance = TIM15;
  htim15.Init.Prescaler = 39;
  htim15.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim15.Init.Period = 2000;
  htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim15.Init.RepetitionCounter = 1;
  if (HAL_TIM_Base_Init(&htim15) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim15, &sClockSourceConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim15, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

static void MX_USART3_UART_Init(void)
{

  huart3.Instance = USART3;
  huart3.Init.BaudRate = 115200;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}


static void MX_DMA_Init(void) 
{

  __HAL_RCC_DMA1_CLK_ENABLE();

  HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);

  HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);

  HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);

}

static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;


  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();

  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOE, GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);

  /*Configure GPIO pin : PA4 */
  GPIO_InitStruct.Pin = GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_6|GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_PB6);
  HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_PB7);

}
void _Error_Handler(char * file, int line)
{
  while(1) 
  {
  }

}
#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{

}

#endif
4

0 回答 0