/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dac.h"
#include "dma.h"
#include "spi.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "LCD.h"
#include "button.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint32_t   aADCxConvertedValues[2];
uint8_t disbuff[100];

uint8_t rbuf[5];
uint16_t pwm_vol[2];
float DC_vol[2];
uint16_t DualSine12bit[100];
uint32_t Idx;        //定义变量
uint8_t flag_flash=1;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
const uint16_t Sine12bit[100]={
0x0800,0x0881,0x0901,0x0980,0x09FD,0x0A79,0x0AF2,0x0B68,0x0BDA,0x0C49,
0x0CB3,0x0D19,0x0D79,0x0DD4,0x0E29,0x0E78,0x0EC0,0x0F02,0x0F3C,0x0F6F,
0x0F9B,0x0FBF,0x0FDB,0x0FEF,0x0FFB,0x0FFF,0x0FFB,0x0FEF,0x0FDB,0x0FBF,
0x0F9B,0x0F6F,0x0F3C,0x0F02,0x0EC0,0x0E78,0x0E29,0x0DD4,0x0D79,0x0D19,
0x0CB3,0x0C49,0x0BDA,0x0B68,0x0AF2,0x0A79,0x09FD,0x0980,0x0901,0x0881,
0x0800,0x077F,0x06FF,0x0680,0x0603,0x0587,0x050E,0x0498,0x0426,0x03B7,
0x034D,0x02E7,0x0287,0x022C,0x01D7,0x0188,0x0140,0x00FE,0x00C4,0x0091,
0x0065,0x0041,0x0025,0x0011,0x0005,0x0001,0x0005,0x0011,0x0025,0x0041,
0x0065,0x0091,0x00C4,0x00FE,0x0140,0x0188,0x01D7,0x022C,0x0287,0x02E7,
0x034D,0x03B7,0x0426,0x0498,0x050E,0x0587,0x0603,0x0680,0x06FF,0x077F
};
const uint16_t PWM12bit[100]={
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,
0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,
0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,
0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,
0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x0fff
};
const uint16_t zigzag_H_L_12bit[100]={
0,	 41,  82,  124, 165, 206, 248, 289, 330, 372,
413, 455, 496, 537, 579, 620, 661, 703, 744, 786,
827, 868, 910, 951, 992,1034,1075,1117,1158,1199,
1241,1282,1323,1365,1406,1448,1489,1530,1572,1613,
1654,1696,1737,1779,1820,1861,1903,1944,1985,2027,
2068,2110,2151,2192,2234,2275,2316,2358,2399,2441,
2482,2523,2565,2606,2647,2689,2730,2772,2813,2854,
2896,2937,2978,3020,3061,3103,3144,3185,3227,3268,
3309,3351,3392,3434,3475,3516,3558,3599,3640,3682,
3723,3765,3806,3847,3889,3930,3971,4013,4054,4095
};
const uint16_t zigzag_L_H_12bit[100]={
4095,4054,4013,3971,3930,3889,3847,3806,3765,3723,
3682,3640,3599,3558,3516,3475,3434,3392,3351,3309,
3268,3227,3185,3144,3103,3061,3020,2978,2937,2896,
2854,2813,2772,2730,2689,2647,2606,2565,2523,2482,
2441,2399,2358,2316,2275,2234,2192,2151,2110,2068,
2027,1985,1944,1903,1861,1820,1779,1737,1696,1654,
1613,1572,1530,1489,1448,1406,1365,1323,1282,1241,
1199,1158,1117,1075,1034,992,951,910,868,827,
786,744,703,661,620,579,537,496,455,413,
372,330,289,248,206,165,124,82,41,0
};


const uint16_t triangle12bit[100]={
0,	 81,  163, 245, 327, 409, 491, 573, 655, 737,
819, 900, 982,1064,1146,1228,1310,1392,1474,1556,
1638,1719,1801,1883,1965,2047,2129,2211,2293,2375,
2457,2538,2620,2702,2784,2866,2948,3030,3112,3194,
3276,3357,3439,3521,3603,3685,3767,3849,3931,4013,
4095,4013,3931,3849,3767,3685,3603,3521,3439,3357,
3276,3194,3112,3030,2948,2866,2784,2702,2620,2538,
2457,2375,2293,2211,2129,2047,1965,1883,1801,1719,
1638,1556,1474,1392,1310,1228,1146,1064,982,900,
 819, 737, 655, 573, 491, 409, 327, 245, 163, 81

};

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
#define KEY_ON 1
Button_t Button_MENU1;
Button_t Button_MENU2;
Button_t Button_R;
Button_t Button_EN;
Button_t Button_L;
uint8_t mune_1=0;
uint8_t set_index=0;
int8_t Wave_type;//波类�??
uint8_t  duty=50;
uint32_t frequency1=1000;//频率
uint32_t frequency2=1000;//频率
float  amplitude=1;//幅�??
float excursion=0;//偏移
void Button_MENU1_Dowm_CallBack(void *btn)
{

if(mune_1==0){
	mune_1=1;

}else if(mune_1==1){
	mune_1=0;
}



set_index=0;
	flag_flash=1;
}


void Button_MENU2_Dowm_CallBack(void *btn)
{

	set_index++;
	if(mune_1==0)
	{
		set_index%=4;
	}
	else
	{
		set_index%=2;
	}
}
void Button_R_Dowm_CallBack(void *btn)
{

	if(mune_1==0)
	{
	switch (set_index)
		{
			case 0:Wave_type++;Wave_type%=5;//0:正弦  1:方波 2:锯齿
			break;
			case 1:frequency1++;if(frequency1>=10000){frequency1=10000;};  MX_TIM3_SET_Frequency(  frequency1);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
			break;
			case 2:amplitude+=0.1;if(amplitude>=6)amplitude=6;
			break;
			case 3:excursion+=0.1;if(excursion>=3)excursion=3;
			break;

		}
	switch(Wave_type)
	{
		case 0:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(Sine12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+ amplitude*0.125*(Sine12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+ amplitude*0.125*(Sine12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 1:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(PWM12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(PWM12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(PWM12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 2:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 3:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 4:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff)  ;
						}

				}
			break;




	}
	HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1,(uint32_t *)DualSine12bit,100,DAC_ALIGN_12B_R);

	}
	else
	{
		switch (set_index)
			{
				case 0:frequency2++;  MX_TIM2_SET_Frequency(  frequency2);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
				break;
				case 1:if(duty<99){ duty ++;  }else {duty=99;}  TIM2->CCR3=duty;
				break;

			}
	}
}
void Button_R_LONG_CallBack(void *btn)
{

	if(mune_1==0)
	{
	switch (set_index)
		{
			case 0:Wave_type++;Wave_type%=5;//0:正弦  1:方波 2:锯齿
			break;
			case 1:frequency1+=10;if(frequency1>=10000){frequency1=10000;};   MX_TIM3_SET_Frequency(  frequency1);
			break;
			case 2:amplitude+=0.1;if(amplitude>=6)amplitude=6;
			break;
			case 3:excursion+=0.1;if(excursion>=3)excursion=3;
			break;

		}
	switch(Wave_type)
	{
		case 0:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(Sine12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+ amplitude*0.125*(Sine12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+ amplitude*0.125*(Sine12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 1:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(PWM12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(PWM12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(PWM12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 2:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 3:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 4:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff)  ;
						}

				}
			break;



	}
	HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1,(uint32_t *)DualSine12bit,100,DAC_ALIGN_12B_R);

	}
	else
	{
		switch (set_index)
			{
				case 0:frequency2+=10;  MX_TIM2_SET_Frequency(  frequency2);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
				break;
				case 1:if(duty<99){ duty ++;  }else {duty=99;}  TIM2->CCR3=duty;
				break;

			}
	}
}



void Button_EN_Dowm_CallBack(void *btn)
{

}
void Button_L_Dowm_CallBack(void *btn)
{

	if(mune_1==0)
	{
	switch (set_index)
		{
			case 0:Wave_type--;Wave_type%=5;//0:正弦�?? 1:方波 2:锯齿�??
			break;
			case 1:frequency1-=1;if(frequency1<1)frequency1=1;MX_TIM3_SET_Frequency(  frequency1);
			break;
			case 2:amplitude-=0.1;if(amplitude<=0)amplitude=0.1;
			break;
			case 3:excursion-=0.1;if(excursion<=-3)excursion=-3;
			break;

		}
	switch(Wave_type)
	{
		case 0:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+ amplitude*0.125*(Sine12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(Sine12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(Sine12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 1:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+ amplitude*0.125*(PWM12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+ amplitude*0.125*(PWM12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(PWM12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 2:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 3:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 4:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff)  ;
						}

				}
			break;

	}
	HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1,(uint32_t *)DualSine12bit,100,DAC_ALIGN_12B_R);

	}
	else
	{
		switch (set_index)
			{
				case 0:if(frequency2>2)frequency2--;else frequency2=1;  MX_TIM2_SET_Frequency(  frequency2);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
				break;
				case 1:if(duty>2){ duty --;  }else {duty=1;}  TIM2->CCR3=duty;
				break;

			}
}
}
void Button_L_LONG_CallBack(void *btn)
{

	if(mune_1==0)
	{
	switch (set_index)
		{
			case 0:Wave_type--;Wave_type%=5;//0:正弦�?? 1:方波 2:锯齿�??
			break;
			case 1:frequency1-=10;if(frequency1<10)frequency1=10;MX_TIM3_SET_Frequency(  frequency1);
			break;
			case 2:amplitude-=0.1;if(amplitude<=0)amplitude=0.1;
			break;
			case 3:excursion-=0.1;if(excursion<=-3)excursion=-3;
			break;

		}
	switch(Wave_type)
	{
		case 0:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(		( 2110	-excursion/2.429/3.3*4096	+ 	amplitude*0.125*(Sine12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if((2110	-excursion/2.429/3.3*4096	+	amplitude*0.125*(Sine12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] = 2110	-excursion/2.429/3.3*4096	+amplitude* 0.125*(Sine12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 1:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(		( 2110-excursion/2.429/3.3*4096		+ amplitude*0.125*(PWM12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096		+ amplitude*0.125*(PWM12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110	-excursion/2.429/3.3*4096	+amplitude* 0.125*(PWM12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 2:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_H_L_12bit[Idx]-0x7ff)  ;
						}

				}
			break;
		case 3:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(zigzag_L_H_12bit[Idx]-0x7ff)  ;
						}

				}
			break;

		case 4:
				for (Idx = 0; Idx <100; Idx++)
				{
						if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff))>=4095)
						{
							DualSine12bit[Idx] =4095;
						}
						else if(( 2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff))<=0)
						{
							DualSine12bit[Idx]=0;
						}
						else
						{
							DualSine12bit[Idx] =   2110-excursion/2.429/3.3*4096+amplitude* 0.125*(triangle12bit[Idx]-0x7ff)  ;
						}

				}
			break;




	}
	HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1,(uint32_t *)DualSine12bit,100,DAC_ALIGN_12B_R);

	}
	else
	{
		switch (set_index)
			{
				case 0:if(frequency2>20)frequency2-=10;else frequency2=1;  MX_TIM2_SET_Frequency(  frequency2);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
				break;
				case 1:if(duty>20){ duty -=10;  }else {duty=10;}  TIM2->CCR3=duty;
				break;

			}
	}
}



uint8_t Read_Button_MENU1_Level(void)
{
	return HAL_GPIO_ReadPin(KEY_MENU1_GPIO_Port,KEY_MENU1_Pin);
}
uint8_t Read_Button_MENU2_Level(void)
{
	return HAL_GPIO_ReadPin(KEY_MENU2_GPIO_Port,KEY_MENU2_Pin);
}
uint8_t Read_Button_R_Level(void)
{
	return HAL_GPIO_ReadPin(KEY_L_GPIO_Port,KEY_L_Pin);

}
uint8_t Read_Button_EN_Level(void)
{
	return HAL_GPIO_ReadPin(KEY_EN_GPIO_Port,KEY_EN_Pin);
}
uint8_t Read_Button_L_Level(void)
{
	return HAL_GPIO_ReadPin(KEY_R_GPIO_Port,KEY_R_Pin);
}



/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void SHOW_OSC(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_SPI1_Init();
  MX_TIM3_Init();
  MX_DAC_Init();
  MX_TIM2_Init();
  MX_TIM17_Init();
  /* USER CODE BEGIN 2 */

	HAL_TIM_Base_Start_IT(&htim17);
	HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, 2048);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
	HAL_DAC_Start(&hdac, DAC_CHANNEL_1);

	LCD_Init();
	LCD_direction(0);
	LCD_Clear(0xffff);
	POINT_COLOR=BLUE;
	BACK_COLOR=0xffff;



	Button_Create("Button_MENU1", &Button_MENU1, Read_Button_MENU1_Level,KEY_ON);
	Button_Attach(&Button_MENU1,BUTTON_DOWM,Button_MENU1_Dowm_CallBack);                       //单击
	Button_Attach(&Button_MENU1,BUTTON_LONG,Button_MENU1_Dowm_CallBack);                       //单击

	Get_Button_Event(&Button_MENU1);
	Button_Create("Button_MENU2", &Button_MENU2, Read_Button_MENU2_Level, KEY_ON);
	Button_Attach(&Button_MENU2,BUTTON_DOWM,Button_MENU2_Dowm_CallBack);
	Button_Attach(&Button_MENU2,BUTTON_LONG,Button_MENU2_Dowm_CallBack); //单击
	Get_Button_Event(&Button_MENU2);
	Button_Create("Button_R", &Button_R, Read_Button_R_Level, KEY_ON);
	Button_Attach(&Button_R,BUTTON_DOWM,Button_R_Dowm_CallBack);
	Button_Attach(&Button_R,BUTTON_LONG,Button_R_LONG_CallBack); //单击
	Get_Button_Event(&Button_R);
	Button_Create("Button_EN", &Button_EN, Read_Button_EN_Level, KEY_ON);
	Button_Attach(&Button_EN,BUTTON_DOWM,Button_EN_Dowm_CallBack);
	Button_Attach(&Button_EN,BUTTON_LONG,Button_EN_Dowm_CallBack); //单击
	Get_Button_Event(&Button_EN);
	Button_Create("Button_L", &Button_L, Read_Button_L_Level, KEY_ON);
	Button_Attach(&Button_L,BUTTON_DOWM,Button_L_Dowm_CallBack);
	Button_Attach(&Button_L,BUTTON_LONG,Button_L_LONG_CallBack); //单击
	Get_Button_Event(&Button_L);



TIM2->CCR3=50;
	 for (Idx = 0; Idx <100; Idx++)
	{

		DualSine12bit[Idx] = 2110-excursion/2.429/3.3*4096+ amplitude*0.125*(Sine12bit[Idx]-0x7ff);
	}
	  HAL_TIM_Base_Start(&htim3);
	  HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1,(uint32_t *)DualSine12bit,100,DAC_ALIGN_12B_R);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

 	 //

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	if(flag_flash==1)
	{
	  flag_flash=0;
	  LCD_Clear(0xffff);
	}
if(mune_1==0)//函数发生�??
{

	 show_string( 0, 10, "DDS函数发生器", strlen("DDS函数发生器 "),  RED,WHITE  ) ;
	 show_string( 0, 50, "波形  :", strlen("波形  :"),  RED,WHITE  ) ;
	 if(Wave_type==0)
	 {
		  show_string( 16*7,50,  "正弦波 ", strlen("正弦波 "), set_index==0? BLUE:RED,set_index==0? RED:WHITE ) ;
	 }
	 else if(Wave_type==1){
		  show_string( 16*7,50,  " 方波  ", strlen(" 方波  "), set_index==0? BLUE:RED,set_index==0? RED:WHITE ) ;
	 }
	 else if(Wave_type==2){
		  show_string( 16*7,50,  "锯齿波1", strlen("锯齿波1"), set_index==0? BLUE:RED,set_index==0? RED:WHITE ) ;
	 }
	 else if(Wave_type==3){
		  show_string( 16*7,50,  "锯齿波2", strlen("锯齿波2"), set_index==0? BLUE:RED,set_index==0? RED:WHITE ) ;
	 }
	 else if(Wave_type==4){
		  show_string( 16*7,50,  "三角波 ", strlen("三角波 "), set_index==0? BLUE:RED,set_index==0? RED:WHITE ) ;
	 }
	 show_string( 0, 90, "频率  :", strlen("频率  :"),  RED,WHITE  ) ;

	 sprintf(disbuff,"%5dHz",frequency1);
	 show_string( 16*7,90, disbuff, strlen(disbuff),set_index==1? BLUE:RED,set_index==1? RED:WHITE ) ;
	 show_string( 0, 130, "峰峰值:", strlen("峰峰值:"),  RED,WHITE  ) ;
	 sprintf(disbuff,"%4.1fV",amplitude);
	 show_string( 16*7,130, disbuff, strlen(disbuff),set_index==2? BLUE:RED,set_index==2? RED:WHITE ) ;
	 show_string( 0, 170, "偏置值:", strlen("偏置值:"),  RED,WHITE  ) ;
	 sprintf(disbuff,"%4.1fV ",excursion);
	 show_string( 16*7,170, disbuff, strlen(disbuff), set_index==3? BLUE:RED,set_index==3? RED:WHITE ) ;


}
else if(mune_1==1){
	show_string( 10, 10, "PWM方波发生器", strlen("PWM方波发生器"),  RED,WHITE  ) ;

	 show_string( 0, 50, "频率  :", strlen("频率  :"),  RED,WHITE  ) ;
	 sprintf(disbuff,"%5dHz",frequency2);
	 show_string( 16*7,50, disbuff, strlen(disbuff),set_index==0? BLUE:RED,set_index==0? RED:WHITE ) ;

	 show_string( 0, 90, "占空比:", strlen("占空比:"),  RED,WHITE  ) ;
	 sprintf(disbuff,"%2d  ",duty);
	 show_string( 16*7,90, disbuff, strlen(disbuff), set_index==1? BLUE:RED,set_index==1? RED:WHITE ) ;

}

  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

  void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	  if(htim->Instance==TIM17)
	  {
		  Button_Process();

	  }

}



/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
