
#include "bsp_dac.h" #include "math.h" //#include "stm32f10x_tim.h" //#include "stm32f10x_dma.h" //#include "stm32f10x_dac.h" //正弦波单个周期的点数 #define POINT_NUM 32 #define angle 3.1415/64/* 初始波形数据 ---------------------------------------------------------*/ uint16_t Sine12bit[128] = { 2048, 2148,2248,2348,2447,2545,2642,2737,2831, 2923,3013,3100,3185,3267,3347,3423,3496, 3565,3631,3692,3750,3804,3854,3899,3940, 3976,4007,4034,4056,4073,4086,4093,4095, 4093,4086,4073,4056,4034,4007,3976,3940, 3899,3854,3804,3750,3692,3631,3565,3496, 3423,3347,3267,3185,3100,3013,2923,2831, 2737,2642,2545,2447,2348,2248,2148,2048, 1948,1848,1748,1649,1551,1454,1359,1265, 1173,1083,996,911,829,749,673,600, 531,465,404,346,292,242,197,156, 120,89,62,40,23,10,3,1, 3,10,23,40,62,89,120,156, 197,242,292,346,404,465,531,600, 673,749,829,911,996,1083,1173,1265, 1359,1454,1551,1649,1748,1848,1948 };uint16_t TriangleWave12bit[128] = { 2048, 2148,2248,2348,2447,2545,2642,2737,2831, 2923,3013,3100,3185,3267,3347,3423,3496, 3565,3631,3692,3750,3804,3854,3899,3940, 3976,4007,4034,4056,4073,4086,4093,4095, 4093,4086,4073,4056,4034,4007,3976,3940, 3899,3854,3804,3750,3692,3631,3565,3496, 3423,3347,3267,3185,3100,3013,2923,2831, 2737,2642,2545,2447,2348,2248,2148,2048, 1948,1848,1748,1649,1551,1454,1359,1265, 1173,1083,996,911,829,749,673,600, 531,465,404,346,292,242,197,156, 120,89,62,40,23,10,3,1, 3,10,23,40,62,89,120,156, 197,242,292,346,404,465,531,600, 673,749,829,911,996,1083,1173,1265, 1359,1454,1551,1649,1748,1848,1948 };uint16_t PulseWave12bit[128] = { 2048, 2148,2248,2348,2447,2545,2642,2737,2831, 2923,3013,3100,3185,3267,3347,3423,3496, 3565,3631,3692,3750,3804,3854,3899,3940, 3976,4007,4034,4056,4073,4086,4093,4095, 4093,4086,4073,4056,4034,4007,3976,3940, 3899,3854,3804,3750,3692,3631,3565,3496, 3423,3347,3267,3185,3100,3013,2923,2831, 2737,2642,2545,2447,2348,2248,2148,2048, 1948,1848,1748,1649,1551,1454,1359,1265, 1173,1083,996,911,829,749,673,600, 531,465,404,346,292,242,197,156, 120,89,62,40,23,10,3,1, 3,10,23,40,62,89,120,156, 197,242,292,346,404,465,531,600, 673,749,829,911,996,1083,1173,1265, 1359,1454,1551,1649,1748,1848,1948 };void dac1_change_to_sine(void) {DMA_InitTypeDef DMA_InitStructure;DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 128; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA2_Channel3, &DMA_InitStructure); } void dac2_change_to_sine(void) {DMA_InitTypeDef DMA_InitStructure;DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 128; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA2_Channel4, &DMA_InitStructure); }void dac1_change_to_Triangle(void) {DMA_InitTypeDef DMA_InitStructure;DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1; //外设数据地址DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&TriangleWave12bit; //内存数据地址 Sine12bitDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //数据传输方向内存至外设DMA_InitStructure.DMA_BufferSize = 128; //缓存大小为POINT_NUM字节 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设数据地址固定 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存数据地址自增DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设数据以字为单位DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //内存数据以字为单位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环模式DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高DMA通道优先级DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA2_Channel3, &DMA_InitStructure); }void dac2_change_to_Triangle(void) {DMA_InitTypeDef DMA_InitStructure;DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2; //外设数据地址DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&TriangleWave12bit; //内存数据地址 Sine12bitDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //数据传输方向内存至外设DMA_InitStructure.DMA_BufferSize = 128; //缓存大小为POINT_NUM字节 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设数据地址固定 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存数据地址自增DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设数据以字为单位DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //内存数据以字为单位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环模式DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高DMA通道优先级DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA2_Channel4, &DMA_InitStructure); }void dac1_change_to_Pulse(void) {DMA_InitTypeDef DMA_InitStructure;DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&PulseWave12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 128; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA2_Channel3, &DMA_InitStructure); } void dac2_change_to_Pulse(void) {DMA_InitTypeDef DMA_InitStructure;DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&PulseWave12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 128; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA2_Channel4, &DMA_InitStructure); }void set_Triangle_duty(int duty)// 例如duty=5050,占空比为50.5% {int i; int low_num=0;int hight_num=0; hight_num = 128*duty/10000; low_num=128-hight_num; for(i=0;i<128;i++){if(i<=hight_num-1)TriangleWave12bit[i]= (uint16_t)4095/hight_num*i;elseTriangleWave12bit[i]= (uint16_t)(4095-4095/low_num*(i-hight_num)); } } void set_Pulse_duty(int duty) // 例如duty=20,占空比为20% {int i; int low_num=0;int hight_num=0; hight_num = 128*duty/10000; low_num=128-hight_num;for(i=0;i<128;i++){if(i<=low_num-1)PulseWave12bit[i]= 0;elsePulseWave12bit[i]= (uint16_t)(4095); } } void set_sine_vpp(float vpp) {int i; for(i=0;i<128;i++){Sine12bit[i]= (uint16_t)((sin(i*angle)+1)/2*vpp*4096/3.3); }}void set_Triangle_vpp(float vpp) {int i; for(i=0;i<128;i++){if(i<=63)TriangleWave12bit[i]= (uint16_t)vpp/3.3*4096/64*i;elseTriangleWave12bit[i]= (uint16_t)(vpp/3.3*4096-vpp/3.3*4096/64*(i-64)); } }void set_Pulse_vpp(float vpp) {int i; for(i=0;i<128;i++){if(i<=63)PulseWave12bit[i]= 0;elsePulseWave12bit[i]= (uint16_t)(vpp/3.3*4096); } }void set_peroid(int period) {TIM_Cmd(TIM6, DISABLE);TIM6->ARR=period;TIM_Cmd(TIM6, ENABLE); // TIM_Cmd(TIM7, DISABLE); // TIM7->ARR=period; // TIM_Cmd(TIM7, ENABLE); }void DAC1_GPIO_config(void) { GPIO_InitTypeDef GPIO_InitStructure;/* 使能GPIOA时钟 */RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* DAC的GPIO配置,模拟输入 */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;GPIO_Init(GPIOA, &GPIO_InitStructure);} void DAC2_GPIO_config(void) { GPIO_InitTypeDef GPIO_InitStructure;/* 使能GPIOA时钟 */RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* DAC的GPIO配置,模拟输入 */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //pa5GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;GPIO_Init(GPIOA, &GPIO_InitStructure);}/*** @brief 配置DAC通道1的TIM* @param 无* @retval 无*/ void DAC1_TIM_Config(void) {TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;/* 使能TIM6时钟,TIM2CLK 为72M */RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);/*定时器初始周期*/TIM_TimeBaseStructure.TIM_Period = 0X06; //562500/25000+1=23 40KHZ TIM_TimeBaseStructure.TIM_Prescaler = 0x1; //预分频,不分频 72M / (1+1) =36MTIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分频系数TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);/* 配置TIM6触发源 */TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);/* 使能TIM6 */TIM_Cmd(TIM6, ENABLE); } /*** @brief 配置DAC通道2的TIM* @param 无* @retval 无*/ void DAC2_TIM_Config(void) {TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;/* 使能TIM6时钟,TIM2CLK 为72M */RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);/*定时器初始周期*/TIM_TimeBaseStructure.TIM_Period = 0X17; TIM_TimeBaseStructure.TIM_Prescaler = 0x1; //预分频,不分频 72M / (1+1) =36MTIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分频系数TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure);/* 配置TIM7触发源 */TIM_SelectOutputTrigger(TIM7, TIM_TRGOSource_Update);/* 使能TIM7 */TIM_Cmd(TIM7, ENABLE); }/*** @brief 配置DMA* @param 无* @retval 无*/ void DAC1_DMA_Config(void) { DMA_InitTypeDef DMA_InitStructure;DAC_InitTypeDef DAC_InitStructure;/* 使能DMA2时钟 */RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);/* 使能DAC时钟 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);/* 配置DAC 通道1 */DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO; //使用TIM6作为触发源DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; //不使用波形发生器DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //不使用DAC输出缓冲 DAC_Init(DAC_Channel_1, &DAC_InitStructure);/* 配置DMA2 */DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1; //外设数据地址DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; //内存数据地址 Sine12bitDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //数据传输方向内存至外设DMA_InitStructure.DMA_BufferSize = 128; //缓存大小为POINT_NUM字节 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设数据地址固定 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存数据地址自增DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设数据以字为单位DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //内存数据以字为单位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环模式DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高DMA通道优先级DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //非内存至内存模式 DMA_Init(DMA2_Channel3, &DMA_InitStructure);/* 使能DMA2-14通道 */DMA_Cmd(DMA2_Channel3, ENABLE);/* 使能通道1 由PA4输出 */DAC_Cmd(DAC_Channel_1, ENABLE);/* 使能DAC的DMA请求 */DAC_DMACmd(DAC_Channel_1, ENABLE); }/*** @brief 配置DMA* @param 无* @retval 无*/ void DAC2_DMA_Config(void) { DMA_InitTypeDef DMA_InitStructure;DAC_InitTypeDef DAC_InitStructure;/* 使能DMA2时钟 */RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);/* 使能DAC时钟 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);/* 配置DAC 通道2 */DAC_InitStructure.DAC_Trigger = DAC_Trigger_T7_TRGO; //使用TIM6作为触发源DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; //不使用波形发生器DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //不使用DAC输出缓冲 DAC_Init(DAC_Channel_2, &DAC_InitStructure);/* 配置DMA2 */DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2; //外设数据地址DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; //内存数据地址 Sine12bitDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //数据传输方向内存至外设DMA_InitStructure.DMA_BufferSize = 128; //缓存大小为POINT_NUM字节 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设数据地址固定 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存数据地址自增DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设数据以字为单位DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //内存数据以字为单位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环模式DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高DMA通道优先级DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //非内存至内存模式 DMA_Init(DMA2_Channel4, &DMA_InitStructure);/* 使能DMA2-14通道 */DMA_Cmd(DMA2_Channel4, ENABLE);/* 使能通道1 由PA4输出 */DAC_Cmd(DAC_Channel_2, ENABLE);/* 使能DAC的DMA请求 */DAC_DMACmd(DAC_Channel_2, ENABLE); }void DAC1_Mode_Init(void) {DAC1_GPIO_config();DAC1_TIM_Config(); DAC1_DMA_Config(); }void DAC2_Mode_Init(void) {DAC2_GPIO_config();DAC2_TIM_Config(); DAC2_DMA_Config(); }

void sendreplay(void) {u8 i;unsigned long period=0;unsigned long period_us=0;unsigned long freq=0;unsigned long freq_temp=0;u16 duty=0;for(i=0;i<24;i++){if(myusart.rcbuf[i] != rcbuf_bak[i]) break;}if(i<24){for(i=0;i<24;i++) rcbuf_bak[i] = myusart.rcbuf[i];freq = (myusart.rcbuf[4]<<8)+ myusart.rcbuf[5];freq_temp =freq;//频率设置统一处理if(freq < _freq_min){freq = _freq_min;}else if(freq > _freq_max ){freq = _freq_max;}period = (int)(_freq_vel/freq - 1);//占空比duty = (myusart.rcbuf[6]<<8)+ myusart.rcbuf[7];period_us = 1000000 / freq;if(myusart.rcbuf[3]==0x01)//确定波形 {dac1_change_to_sine();set_peroid(period);//占空比就是50%h_period_us = period_us/2;l_period_us = period_us - h_period_us; }else if(myusart.rcbuf[3]==0x02){//PA6if(freq_temp < _freq_min){freq_temp = _freq_min;}set_pwm1(freq_temp,duty);period_us = 1000000 / freq_temp;h_period_us = period_us*duty/10000;l_period_us = period_us - h_period_us; }else if(myusart.rcbuf[3]==0x03){set_Triangle_duty(duty);//无法避免重新计算数组值 dac1_change_to_Triangle(); set_peroid(period);h_period_us = period_us*duty/10000;l_period_us = period_us - h_period_us; }}}