void TIM2_IRQHandler(void) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); if( state == STATE_START ) { ADCIndex = 0; DAC1Value = DAC1.start; DAC2Value = DAC2.start; DAC_SetChannel1Data(DAC_Align_12b_R, DAC1Value); DAC_SetChannel2Data(DAC_Align_12b_R, DAC2Value); state = STATE_ACTIVE; } else { DAC1Value += DAC1.step; if( DAC1Value > DAC1.stop ) { EOL = 1; DAC1Value = DAC1.start; DAC2Value += DAC2.step; if(DAC2Value > DAC2.stop) { DAC2Value = DAC2.start; EOS = 1; } DAC_SetChannel2Data(DAC_Align_12b_R, DAC2Value); } DAC_SetChannel1Data(DAC_Align_12b_R, DAC1Value); } }
/** * @brief DAC Channel2 Triangle and Channel1 Noise Configuration * @param None * @retval None */ static void DAC_Noise_TriangleConfig(void) { /* DAC channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel2 */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Set DAC channel2 DHR12RD register */ DAC_SetChannel2Data(DAC_Align_12b_R, 0x100); /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits10_0; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* Enable DAC Channel1 */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Set DAC Channel1 DHR12L register */ DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0); }
void setup_dac(){ #if isUsingDevBoard //Setup GPIO RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); DAC_InitTypeDef DAC_InitStructure; /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits10_0; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel1 */ DAC_Cmd(DAC_Channel_1, ENABLE); DAC_Cmd(DAC_Channel_2, ENABLE); /* Set DAC Channel1 DHR12L register */ DAC_SetChannel1Data(DAC_Align_8b_R, 0x0000); DAC_SetChannel2Data(DAC_Align_8b_R, 0x0000); #else #endif }
void DAC2_set(uint16_t n) { if(DAC_MIN <= n && n <= DAC_MAX) { DAC_SetChannel2Data(DAC_Align_12b_R, n); } }
void TIM2_IRQHandler(void) { int d, mv; //handle power 1 d = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_1); ict_vget_1 = d2mv(d); //printf("%d\n",ict_vget_1); mv = ict_vget_1 - ict_vexp_1; ict_vout_1 -= mv; d = mv2d(ict_vout_1); if(d < 0) d = 0; DAC_SetChannel1Data(DAC_Align_12b_R, d); //handle power 2 d = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_2); ict_vget_2 = d2mv(d); //printf("%d\n",ict_vget_2); mv = ict_vget_2 - ict_vexp_2; ict_vout_2 -= mv; d = mv2d(ict_vout_2); if(d < 0) d = 0; DAC_SetChannel2Data(DAC_Align_12b_R, d); //handle I1&I2 d = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_3); ict_iget_1 = d2mA(d); d = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_4); ict_iget_2 = d2mA(d); //printf("I1 = %d I2 = %d\n", i1, i2); ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE); TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); }
static inline void dac_write(dac_t *obj, uint16_t value) { if (obj->pin == PA_4) { DAC_SetChannel1Data(DAC_Align_12b_R, value); } if (obj->pin == PA_5) { DAC_SetChannel2Data(DAC_Align_12b_R, value); } }
/** @brief Set DAC value @param[in] n DAC channel @param[in] v Value */ void dac_set(uint8_t n, uint16_t v) { if( n == 1 ) { DAC_SetChannel1Data(DAC_Align_12b_R, v); } else { DAC_SetChannel2Data(DAC_Align_12b_R, v); } }
//设置通道2输出电压 //vol:0~3300,代表0~3.3V void Dac2_Set_Vol(u16 vol) { float temp=vol; temp/=1000.000; temp=temp*4095/3.3; if(temp>4095) temp=4095; DAC_SetChannel2Data(DAC_Align_12b_R,temp);//12位右对齐数据格式设置DAC值 }
void setAnalogValue(uint8_t channel, uint16_t value){ /* assert_param(channel == 0 || channel == 1); */ /* assert_param((value & 0xfffff000) == 0); */ value = value & 0xfff; if(channel == 0) DAC_SetChannel1Data(DAC_Align_12b_R, value); else if(channel == 1) DAC_SetChannel2Data(DAC_Align_12b_R, value); }
void audio_drain(void) { if (sample_buf_in == sample_buf_out) { // buffer is empty; disable interrupt } else { // buffer has a sample; output it byte sample = sample_buf[sample_buf_out]; DAC_SetChannel2Data(DAC_Align_8b_R, sample); sample_buf_out = (sample_buf_out + 1) % SAMPLE_BUF_SIZE; } }
void dac_set_a(uint16_t value){ value = 4095 - max(0, min(4095, value)); // pinMode(DAC1, OUTPUT); #ifdef PHOTON_DAC analogWrite(DAC1, value); #else /* Set the DAC Channel1 data */ DAC_SetChannel2Data(DAC_Align_12b_R, value); #endif }
// initValue - 0 - 4095 void Init_DAC_2(int initValue) { DAC_InitTypeDef DAC_InitStructure; //Set DAC clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); /* DAC channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_DeInit(); DAC_Init(DAC_Channel_2, &DAC_InitStructure); DAC_Cmd(DAC_Channel_2, ENABLE); DAC_SetChannel2Data(DAC_Align_12b_R, initValue); DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE); }
uint8_t Dac_Output(uint8_t channel, uint16_t value) { switch(channel) { case 1: if(!(ChannelEn & DAC_CH1_EN)) return 0; DAC_SetChannel1Data(DAC_Align_12b_R,value); DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE); while(DAC->SWTRIGR & 0x0001); return 1; case 2: if(!(ChannelEn & DAC_CH2_EN)) return 0; DAC_SetChannel2Data(DAC_Align_12b_R,value); DAC_SoftwareTriggerCmd(DAC_Channel_2,ENABLE); while(DAC->SWTRIGR & 0x0002); return 1; default: return 0; } }
/** * \fn void beep_low_ms(uint16_t ms) * \brief Generate 2Khz sine signal for x miliseconds. * \param ms Number of miliseconds. * */ void beep_low_ms(uint16_t ms) { uint32_t count = ((uint32_t)ms*1000)/50; uint16_t counter = 50; uint32_t i; timer_reset(); for(i=0; i<count; i++) { DAC_SetChannel2Data(DAC_Align_12b_R, sine_samples[sine_samples_index]); while(abs_diff(counter,timer_get_value())<50) {} counter+=50; sine_samples_index++; if(sine_samples_index==SINE_SAMPLES_COUNT) sine_samples_index = 0; } }
void SetAnalogOutput(int dacOutput, int value) { assert_param(IS_DAC_ID_VALID(dacNumber)); if(value >= 0 && value <= 4095) { switch(dacOutput) { case DAC_1: DAC_SoftwareTriggerCmd(DAC_Channel_1, DISABLE); DAC_SetChannel1Data(DAC_Align_12b_R, value); DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE); break; case DAC_2: DAC_SoftwareTriggerCmd(DAC_Channel_2, DISABLE); DAC_SetChannel2Data(DAC_Align_12b_R, value); DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE); break; } } }
//------------------------------------------------------------------------------ Dac::Error DacStm32f4xx::driverSetVoltage(const float voltage) { uint16_t dacValue = computeDacValue(voltage); switch (myChannelId) { case CHANNEL_ID_1: { DAC_SetChannel1Data(DAC_Align_8b_R, dacValue); break; } case CHANNEL_ID_2: { DAC_SetChannel2Data(DAC_Align_8b_R, dacValue); break; } } return ERROR_NONE; }
void ADC_IRQHandler(void) { ADC_ClearITPendingBit(ADC1, ADC_IT_EOC); if( state != STATE_ACTIVE ) return; ADCBuffer[ADCIndex] = ADC_GetConversionValue(ADC1); // Check if we are at the end of the V_CE sweep if( EOL ) { // Disable the timer temporary TIM_Cmd(TIM2, DISABLE); // Transmit the data uint16_t nBytes = sizeof(uint16_t)*(ADCIndex); transmit( 2, &nBytes); transmit( sizeof(uint16_t)*(ADCIndex), (uint8_t*) ADCBuffer); // Reset the index ADCIndex = 0; // Check if we are at the end of the V_BE sweep // if so, don't restart the timer if( !EOS ) { EOL = 0; TIM_Cmd(TIM2, ENABLE); } else { DAC_SetChannel1Data(DAC_Align_12b_R, DAC1.start); DAC_SetChannel2Data(DAC_Align_12b_R, DAC2.start); state = STATE_IDLE; nBytes = 0; transmit( 2, &nBytes); } } else { ADCIndex++; } }
void stm32dac_init(void) { DAC_InitTypeDef DAC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); GPIO_InitStructure.GPIO_Pin = (STM32DAC0 | STM32DAC1); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(STM32DAC_PORT, &GPIO_InitStructure); /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; //DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bits4_0; 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); DAC_SetChannel1Data(DAC_Align_12b_R,0x0);DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE); DAC_SetChannel2Data(DAC_Align_12b_R,0x0);DAC_SoftwareTriggerCmd(DAC_Channel_2,ENABLE); }
//DAC通道2输出初始化 void Dac2_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; DAC_InitTypeDef DAC_InitType; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE ); //使能PORTA通道时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE ); //使能DAC通道时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA,GPIO_Pin_5) ;//PA.5 输出高 DAC_InitType.DAC_Trigger=DAC_Trigger_None; //不使用触发功能 TEN1=0 DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;//不使用波形发生 DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;//屏蔽、幅值设置 DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ; //DAC1输出缓存关闭 BOFF1=1 DAC_Init(DAC_Channel_2,&DAC_InitType); //初始化DAC通道2 DAC_Cmd(DAC_Channel_2, ENABLE); //使能DAC2 DAC_SetChannel2Data(DAC_Align_12b_R, 0); //12位右对齐数据格式设置DAC值 }
void stm32dac1(uint16_t t) { DAC_SetChannel2Data(DAC_Align_12b_R,(uint16_t)t); DAC_SoftwareTriggerCmd(DAC_Channel_2,ENABLE); }
void BSP_SET_DAC2(uint16_t value) { DAC_SetChannel2Data(DAC_Align_12b_R, value); }
void DAC_Set_Output(int Value_1, int Value_2) { DAC_SetChannel1Data(DAC_Align_12b_R, Value_1); DAC_SetChannel2Data(DAC_Align_12b_R, Value_2); }
/** * @brief Main program. * @param None * @retval None */ void DAC_SignalsGeneration(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f30x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f30x.c file */ /* Preconfiguration before using DAC----------------------------------------*/ DAC_Config(); /* TIM2 configuration to trigger DAC */ TIM_Config(); /* Configures Key Button EXTI Line */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); /* Infinite loop */ while (1) { /* If the wave form is changed */ if (WaveChange == 1) { /* Switch the selected waves forms according the Button status */ if (SelectedWavesForm == 1) { /* The sine wave and the escalator wave has been selected */ /* Sine Wave generator ---------------------------------------------*/ /* DAC channel2 Configuration */ DAC_DeInit(); DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; /* DAC Channel2 Init */ DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel2 */ DAC_Cmd(DAC_Channel_2, ENABLE); /* DMA2 channel3 configuration */ DMA_DeInit(DMA2_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 32; 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); /* Enable DMA2 Channel3 */ DMA_Cmd(DMA2_Channel3, ENABLE); /* Enable DMA for DAC Channel2 */ DAC_DMACmd(DAC_Channel_2, ENABLE); /* Escalator Wave generator ----------------------------------------*/ /* DAC channel1 Configuration */ DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* DMA2 channel4 configuration */ DMA_DeInit(DMA2_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit; DMA_InitStructure.DMA_BufferSize = 6; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_Init(DMA2_Channel4, &DMA_InitStructure); /* Enable DMA2 Channel4 */ DMA_Cmd(DMA2_Channel4, ENABLE); /* Enable DAC1 Channel1: Once the DAC1 channel1 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Enable DMA for DAC Channel1 */ DAC_DMACmd(DAC_Channel_1, ENABLE); } /* The triangle wave and the noise wave has been selected */ else { /* Noise Wave generator --------------------------------------------*/ /* DAC channel1 Configuration */ DAC_DeInit(); DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Set DAC Channel1 DHR12L register */ DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0); /* Enable DAC channel1 wave generator */ DAC_WaveGenerationCmd(DAC_Channel_1, DAC_Wave_Noise , ENABLE); /* Triangle Wave generator -----------------------------------------*/ /* DAC channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Set DAC channel2 DHR12RD register */ DAC_SetChannel2Data(DAC_Align_12b_R, 0x100); } WaveChange = !WaveChange; } } }
/** * @brief Main program * @param None * @retval None */ int main(void) { __IO uint16_t i; __IO uint16_t *ptr; __IO uint16_t tmp1; __IO uint16_t tmp2; uint32_t scpcnt; uint32_t scpwait; uint32_t scptpos; /* RCC Configuration */ RCC_Config(); /* GPIO Configuration */ GPIO_Config(); /* TIM Configuration */ TIM_Config(); /* NVIC Configuration */ NVIC_Config(); /* DAC Configuration */ DAC_Config(); /* SPI Configuration */ SPI_Config(); /* USART Configuration */ USART_Config(115200); /* Calibrate LC Meter */ LCM_Calibrate(); // /* Update bluetooth baudrate */ // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount); // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount); // USART3_puts("AT\0"); // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount) // { // if ((USART3->SR & USART_FLAG_RXNE) != 0) // { // STM32_CMD.BTBuff[i] = USART3->DR; // i++; // } // } // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount); // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount); // USART3_puts("AT+BAUD8\0"); // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount) // { // if ((USART3->SR & USART_FLAG_RXNE) != 0) // { // STM32_CMD.BTBuff[i] = USART3->DR; // i++; // } // } // while (1) // { // } while (1) { USART3_getdata((uint8_t *)&STM32_CMD.Cmd,4); switch (STM32_CMD.Cmd) { case CMD_LCMCAL: LCM_Calibrate(); USART3_putdata((uint8_t *)&STM32_CMD.STM32_LCM.FrequencyCal0,sizeof(STM32_LCMTypeDef)); break; case CMD_LCMCAP: GPIO_ResetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); USART3_putdata((uint8_t *)&STM32_CMD.STM32_LCM.FrequencyCal0,sizeof(STM32_LCMTypeDef)); break; case CMD_LCMIND: GPIO_ResetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_6); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); USART3_putdata((uint8_t *)&STM32_CMD.STM32_LCM.FrequencyCal0,sizeof(STM32_LCMTypeDef)); break; case CMD_FRQCH1: GPIO_ResetBits(GPIOD, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_0); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); break; case CMD_FRQCH2: GPIO_ResetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_1); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); break; case CMD_FRQCH3: GPIO_ResetBits(GPIOD, GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); break; case CMD_SCPSET: USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); USART3_getdata((uint8_t *)&STM32_CMD.STM32_SCP.ADC_Prescaler,sizeof(STM32_SCPTypeDef)); /* Scope magnify */ i = (STM32_CMD.STM32_SCP.ScopeMag & 0x07) << 3; GPIO_SetBits(GPIOE,(i ^ 0x38)); GPIO_ResetBits(GPIOE,i); /* Set V-Pos */ DAC_SetChannel1Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP.ScopeVPos); /* Set Trigger level */ DAC_SetChannel2Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP.ScopeTriggerLevel); if (STM32_CMD.STM32_SCP.ADC_TripleMode) { /* DMA Configuration */ DMA_TripleConfig(STM32_CMD.STM32_SCP.ADC_SampleSize); /* ADC Configuration */ ADC_TripleConfig(STM32_CMD.STM32_SCP.ADC_Prescaler, STM32_CMD.STM32_SCP.ADC_TwoSamplingDelay); } else { /* DMA Configuration */ DMA_SingleConfig(STM32_CMD.STM32_SCP.ADC_SampleSize); /* ADC Configuration */ ADC_SingleConfig(STM32_CMD.STM32_SCP.ADC_Prescaler,STM32_CMD.STM32_SCP.ADC_SampleTime); } if (STM32_CMD.STM32_SCP.ScopeTrigger == 2) { /* Rising edge */ TIM5->CCER &= ~0x2; } else { /* Falling edge */ TIM5->CCER |= 0x2; } if (STM32_CMD.STM32_SCP.ScopeTrigger) { /* Wait for trigger */ scpcnt = TIM5->CNT; scpwait = STM32_CMD.TickCount + 2; while (scpcnt == TIM5->CNT && scpwait != STM32_CMD.TickCount); } /* Start ADC1 Software Conversion */ ADC1->CR2 |= (uint32_t)ADC_CR2_SWSTART; i = 0; while (i < 30000) { i++; } /* Since BlueTooth is slower than the lowest sampling rate there is no need to wait */ SendCompressedBuffer((uint32_t *)SCOPE_DATAPTR, STM32_CMD.STM32_SCP.ADC_SampleSize / 4); /* Done */ ADC->CCR=0; ADC1->CR2=0; ADC2->CR2=0; ADC3->CR2=0; break; case CMD_SCP2SET: STM_EVAL_LEDToggle(LED4); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); USART3_getdata((uint8_t *)&STM32_CMD.STM32_SCP2.SampleRateSet,sizeof(STM32_SCP2TypeDef)); /* Scope magnify */ i = ((uint32_t)STM32_CMD.STM32_SCP2.Mag & 0x07) << 3; GPIO_SetBits(GPIOE,(i ^ 0x38)); GPIO_ResetBits(GPIOE,i); /* Set V-Pos */ DAC_SetChannel1Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP2.VPos); /* Set Trigger level */ DAC_SetChannel2Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP2.TriggerLevel); /* Get number of samples needed */ SampleSize = GetScopeSampleSize(); if (STM32_CMD.STM32_SCP2.Triple) { /* DMA Configuration */ DMA_TripleConfig(SampleSize); /* ADC Configuration */ ADC_TripleConfig((uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet >> 4,(uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet & 0xF); } else { /* DMA Configuration */ DMA_SingleConfig(SampleSize); /* ADC Configuration */ ADC_SingleConfig(((uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet >> 3) & 0x3,(uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet & 0x7); } /* Trigger configuration */ if (STM32_CMD.STM32_SCP2.Trigger == 2) { /* Rising edge */ TIM5->CCER &= ~0x2; } else { /* Falling edge */ TIM5->CCER |= 0x2; } if (STM32_CMD.STM32_SCP2.Trigger) { /* Wait for trigger */ scpcnt = TIM5->CNT; scpwait = STM32_CMD.TickCount + 2; while (scpcnt == TIM5->CNT && scpwait != STM32_CMD.TickCount); } /* Start ADC1 Software Conversion */ ADC1->CR2 |= (uint32_t)ADC_CR2_SWSTART; /* Reset wavedata */ ScopeResetWave((uint32_t *)SCOPE_WAVEPTR,SCOPE_MAXWAVESIZE); ScopeResetWave((uint32_t *)SCOPE_COUNTPTR,SCOPE_MAXWAVESIZE); /* Wait until DMA transfer complete */ while (DMA_GetFlagStatus(DMA2_Stream0, DMA_FLAG_TCIF0) == RESET); /* Done sampling */ ADC->CCR=0; ADC1->CR2=0; ADC2->CR2=0; ADC3->CR2=0; ScopeSetWaveData((uint32_t *)SCOPE_WAVEPTR, (uint32_t *)SCOPE_COUNTPTR, (uint16_t *)SCOPE_DATAPTR); scptpos = 0; if (STM32_CMD.STM32_SCP2.Trigger) { scptpos = ScopeFindTrigger((uint16_t *)SCOPE_DATAPTR); } /* Send wave data */ SendCompressedBuffer((uint32_t *)(SCOPE_DATAPTR + scptpos * 2), (uint32_t)STM32_CMD.STM32_SCP2.PixDiv * (uint32_t)STM32_CMD.STM32_SCP2.nDiv * 2 / 4); STM_EVAL_LEDToggle(LED4); break; case CMD_HSCSET: GPIO_ResetBits(GPIOD, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_0); USART3_getdata((uint8_t *)&STM32_CMD.STM32_HSC.HSCSet,sizeof(STM32_HSCTypeDef)); TIM4->ARR = STM32_CMD.STM32_HSC.HSCSet; TIM4->CCR2 = (STM32_CMD.STM32_HSC.HSCSet+1) / 2; TIM4->PSC = STM32_CMD.STM32_HSC.HSCDiv; USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); break; case CMD_DDSSET: USART3_getdata((uint8_t *)&STM32_CMD.STM32_DDS.DDS_Cmd,sizeof(STM32_DDSTypeDef)); if (STM32_CMD.STM32_DDS.DDS_Cmd == DDS_PHASESET) { SPISendData(DDS_PHASESET); SPISendData32(STM32_CMD.STM32_DDS.DDS__PhaseAdd); } else if (STM32_CMD.STM32_DDS.DDS_Cmd == DDS_WAVESET) { SPISendData(DDS_WAVESET); SPISendData(STM32_CMD.STM32_DDS.DDS_Wave); SPISendData(STM32_CMD.STM32_DDS.DDS_Amplitude); SPISendData(STM32_CMD.STM32_DDS.DDS_DCOffset); } else if (STM32_CMD.STM32_DDS.DDS_Cmd == DDS_SWEEPSET) { SPISendData(DDS_SWEEPSET); SPISendData(STM32_CMD.STM32_DDS.SWEEP_Mode); SPISendData(STM32_CMD.STM32_DDS.SWEEP_Time); SPISendData32(STM32_CMD.STM32_DDS.SWEEP_Step); SPISendData32(STM32_CMD.STM32_DDS.SWEEP_Min); SPISendData32(STM32_CMD.STM32_DDS.SWEEP_Max); } break; case CMD_LGASET: USART3_getdata((uint8_t *)&STM32_CMD.STM32_LGA.DataBlocks,sizeof(STM32_LGATypeDef)); /* Set the Prescaler value */ TIM8->PSC = STM32_CMD.STM32_LGA.LGASampleRateDiv; /* Set the Autoreload value */ TIM8->ARR = STM32_CMD.STM32_LGA.LGASampleRate; TIM8->CNT = STM32_CMD.STM32_LGA.LGASampleRate-1; DMA_LGAConfig(); TIM_DMACmd(TIM8, TIM_DMA_Update, ENABLE); /* DMA2_Stream1 enable */ DMA_Cmd(DMA2_Stream1, ENABLE); /* Enable timer */ TIM8->CR1 |= TIM_CR1_CEN; while (DMA_GetFlagStatus(DMA2_Stream1,DMA_FLAG_HTIF1) == RESET); /* Half done */ USART3_putdata((uint8_t *)LGA_DATAPTR, STM32_CMD.STM32_LGA.DataBlocks * 1024 / 2); while (DMA_GetFlagStatus(DMA2_Stream1,DMA_FLAG_TCIF1) == RESET); /* Done */ USART3_putdata((uint8_t *)(LGA_DATAPTR + STM32_CMD.STM32_LGA.DataBlocks * 1024 / 2), STM32_CMD.STM32_LGA.DataBlocks * 1024 / 2); TIM_Cmd(TIM8, DISABLE); DMA_DeInit(DMA2_Stream1); break; case CMD_WAVEUPLOAD: USART3_getdata((uint8_t *)WAVE_DATAPTR,4096); SPISendData(DDS_WAVEUPLOAD); ptr = (uint16_t *)WAVE_DATAPTR; i = 2048; while (i--) { SPISendData(*ptr); ptr++; } STM_EVAL_LEDToggle(LED4); break; }
// direct access to DAC mp_obj_t pyb_audio_dac(mp_obj_t val) { DAC_SetChannel2Data(DAC_Align_8b_R, mp_obj_get_int(val)); return mp_const_none; }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32l1xx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* Preconfiguration before using DAC----------------------------------------*/ DAC_PreConfig(); /* TIM2 Configuration ------------------------------------------------------*/ /* TIM2 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 TRGO selection */ TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); /* Configures Button GPIO and EXTI Line */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); while (1) { /* If the wave form is changed */ if (WaveChange == 1) { /* Switch the selected waves forms according the Button status */ if (SelectedWavesForm == 1) { /* The sine wave and the escalator wave has been selected */ /* Sine Wave generator ---------------------------------------------*/ DAC_DeInit(); /* DAC channel1 and channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DMA_DeInit(DMA1_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 32; 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(DMA1_Channel3, &DMA_InitStructure); /* Enable DMA1 Channel3 */ DMA_Cmd(DMA1_Channel3, ENABLE); /* DAC Channel2 Init */ DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel2 */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Enable DMA for DAC Channel2 */ DAC_DMACmd(DAC_Channel_2, ENABLE); /* Escalator Wave generator ----------------------------------------*/ /* DAC channel1 Configuration */ DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* DMA1 channel2 configuration */ DMA_DeInit(DMA1_Channel2); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit; DMA_InitStructure.DMA_BufferSize = 6; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_Init(DMA1_Channel2, &DMA_InitStructure); /* Enable DMA1 Channel2 */ DMA_Cmd(DMA1_Channel2, ENABLE); /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Enable DMA for DAC Channel1 */ DAC_DMACmd(DAC_Channel_1, ENABLE); } /* The triangle wave and the noise wave has been selected */ else { /* Noise Wave generator --------------------------------------------*/ /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits8_0; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Set DAC Channel1 DHR12L register */ DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0); /* Enable DAC channel1 wave generator */ DAC_WaveGenerationCmd(DAC_Channel_1, DAC_Wave_Noise , ENABLE); /* Triangle Wave generator -----------------------------------------*/ DAC_DeInit(); /* DAC channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Set DAC channel2 DHR12RD register */ DAC_SetChannel2Data(DAC_Align_12b_R, 0x100); } WaveChange = !WaveChange; } } }