void DAC_ConfigurationInit() { DAC_InitTypeDef DAC_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_DAC, ENABLE ); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOA, &GPIO_InitStruct ); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOA, &GPIO_InitStruct ); DAC_InitStruct.DAC_Trigger = DAC_Trigger_Software; DAC_InitStruct.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0; DAC_InitStruct.DAC_OutputBuffer = DAC_OutputBuffer_Disable; DAC_Init( DAC_Channel_1, &DAC_InitStruct ); DAC_Init( DAC_Channel_2, &DAC_InitStruct ); DAC_Cmd( DAC_Channel_1, ENABLE ); DAC_Cmd( DAC_Channel_2, ENABLE ); DAC_SetChannel1Data( DAC_Align_12b_R, 0X0fFF); DAC_SoftwareTriggerCmd( DAC_Channel_1, ENABLE ); DAC_SetChannel1Data( DAC_Align_12b_R, 0X03FF); DAC_SoftwareTriggerCmd( DAC_Channel_2, ENABLE ); }
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); } }
void BIAS_SetValue(uint16_t val_dac){ if (val_dac < 4096) DAC_SetChannel1Data(DAC_Align_12b_R, val_dac); else DAC_SetChannel1Data(DAC_Align_12b_R, 4095); DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE); BIAS_DAC_Value = DAC_GetDataOutputValue(DAC_Channel_1); }
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); }
void DacInit(void) { GPIO_InitTypeDef GPIO_InitStructure; DAC_InitTypeDef DAC_InitStructure; DacSinusCalculate(); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_AHBPeriphClockCmd(GENERATOR_PIN_CLK, ENABLE); RCC_APB1PeriphClockCmd(GENERATOR_TIMER_CLK, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GENERATOR_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GENERATOR_PORT, &GPIO_InitStructure); DAC_StructInit(&DAC_InitStructure); DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); //delay_us(100); DAC_Cmd(DAC_Channel_1, ENABLE); DAC_SetChannel1Data(DAC_Align_12b_R, DAC_ZERO); }
int main(void) { int32_t dac_data = 0; int8_t inc_dir = 1; char buffer[16]; RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // enable the clock to GPIOA RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // enable the clock to GPIOB RCC->AHBENR |= RCC_AHBENR_GPIOCEN; // enable the clock to GPIOC // Put PORTC.8 in output mode GPIOC->MODER |= (1 << 16); // Put PORTC.9 in output mode GPIOC->MODER |= (1 << 18); // Put PORTA.0 in input mode GPIOA->MODER &= ~(3 << 0); // This configures interrupt such that SysTick_Handler is called // at ever TIMER_TICK_HZ i.e. 1/1000 = 1ms SysTick_Config(SystemCoreClock / TIMER_TICK_HZ); // Initialize the lcd lcd_init(); dac_init(); lcd_puts(" STM32F051"); lcd_gotoxy(1, 4); lcd_puts("DAC TEST"); delay_ms(2000); lcd_clear(); lcd_puts("DAC Value (PA4): "); // Generate a step wave on DAC output whose amplitude is // proportional to the voltage on ADC input. while(1) { DAC_SetChannel1Data(DAC_Align_8b_R, dac_data); DAC_SoftwareTriggerCmd(0, ENABLE); dac_data += (10 * inc_dir); if(dac_data > 255) { dac_data = 250; inc_dir = -1; } else if(dac_data < 0) { dac_data = 0; inc_dir = 1; } int_to_str(dac_data, 5, buffer, sizeof(buffer)); lcd_gotoxy(1, 0); lcd_puts(buffer); delay_ms(1000); } }
void DacInit(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); DAC_InitTypeDef DAC_InitStructure; DAC_StructInit(&DAC_InitStructure); DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Cmd(DAC_Channel_1, ENABLE); DAC_SetChannel1Data(DAC_Align_12b_R, DAC_ZERO); }
/** * @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 stm32dac0(uint16_t t) { DAC_SetChannel1Data(DAC_Align_12b_R,(uint16_t)t); DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE); if(t == 0) led(0,0); else led(0,1); }
//设置通道1输出电压 //vol:0~3300,代表0~3.3V void Dac1_Set_Vol(u16 vol) { float temp=vol; temp/=1000; temp=temp*4096/3; DAC_SetChannel1Data(DAC_Align_12b_R,temp);//12位右对齐数据格式设置DAC值 }
void CIO::interrupt() { uint8_t control = MARK_NONE; uint16_t sample = DC_OFFSET; uint16_t rawRSSI = 0U; m_txBuffer.get(sample, control); // Send the value to the DAC DAC_SetChannel1Data(DAC_Align_12b_R, sample); // Read value from ADC1 and ADC2 if ((ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)) { // shouldn't be still in reset at this point so null the sample value? sample = 0U; } else { sample = ADC_GetConversionValue(ADC1); #if defined(SEND_RSSI_DATA) rawRSSI = ADC_GetConversionValue(ADC2); #endif } // trigger next ADC1 ADC_ClearFlag(ADC1, ADC_FLAG_EOC); ADC_SoftwareStartConv(ADC1); m_rxBuffer.put(sample, control); m_rssiBuffer.put(rawRSSI); m_watchdog++; }
void Dac1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; DAC_InitTypeDef DAC_InitType; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE ); //使能PORTA通道时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE ); //使能DAC通道时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //模拟输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //GPIO_SetBits(GPIOA,GPIO_Pin_4) ;//PA.4 输出高 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_1,&DAC_InitType); //初始化DAC通道1 DAC_Cmd(DAC_Channel_1, ENABLE); //使能DAC1 DAC_SetChannel1Data(DAC_Align_12b_R, 0); //12位右对齐数据格式设置DAC值 }
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 }
//设置通道1输出电压 //vol:0~3300,代表0~3.3V void Dac1_Set_Vol(u16 vol) { float temp=vol; temp/=1000.000; temp=temp*4095/3.3; if(temp>4095) temp=4095; DAC_SetChannel1Data(DAC_Align_12b_R,temp);//12位右对齐数据格式设置DAC值 }
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); } }
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); }
int _PUMP::Poll(void) { int e=0; if(timeout==INT_MAX) DAC_SetChannel1Data(DAC_Align_12b_R,0); else { DAC_SetChannel1Data(DAC_Align_12b_R,__ramp(Th2o(),ftl*100,fth*100,fpl*0xfff/100,fph*0xfff/100)); if(tacho && pressure && current && __time__ > timeout) { if(abs(tacho->Eval(Rpm()) - Tau()) > Tau()/10) _SET_BIT(e,pumpTacho); if(abs(pressure->Eval(Rpm()) - adf.cooler) > adf.cooler/10) _SET_BIT(e,pumpPressure); if(abs(current->Eval(Rpm()) - adf.Ipump) > adf.Ipump/10) _SET_BIT(e,pumpCurrent); } if(__time__ % (5*(Tau()/100)) == 0) _BLUE2(20); } return e; }
void dac_set_b(uint16_t value){ value = 4095 - max(0, min(4095, value)); // pinMode(DAC2, OUTPUT); #ifdef PHOTON_DAC analogWrite(DAC2, value); #else /* Set the DAC Channel2 data */ DAC_SetChannel1Data(DAC_Align_12b_R, value); #endif }
/** * @brief Generates the sine wave for the treble clef * @retval None */ void Sin_gen(void){ if(score1note->noteName==0 && (Out==1024)) //check to see if at a rest Out = 1024; else { I0 = (I0+1)&0x1F; // 0 to 31 - for 1024 point sine wave Out = wave[I0]; } DAC_SetChannel1Data(DAC_Align_12b_R, (Out+Out2)/VolDiv); }
/** * @brief Generates the sine wave for the bass clef * @retval None */ void Sin_gen2(void){ if(score2note->noteName==0 && (Out2==1024)) //check to see if at a rest Out2 = 1024; //silence else { I1 = (I1+1)&0x1F; // 0 to 31 - for 32 point sine wave Out2 = wave[I1]; } DAC_SetChannel1Data(DAC_Align_12b_R, (Out+Out2)/VolDiv); }
int main(void) { uint16_t out = 0; /* Configure ADC and DAC */ ADC_Config(); DAC_Config(); /* Loop forever */ while(1) { /* Test EOC flag */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); /* Get ADC1 converted data */ ADC1ConvertedValue = ADC_GetConversionValue(ADC1); Data = ADC1ConvertedValue; /* if( Data > 0x555 ) { if( out < 0x0FFF ) { out = out + 50; } } else { out = 0; } */ /* Output to DAC */ DAC_SetChannel1Data(DAC_Align_12b_R, Data); } //end while } //end main
/** * @brief This function handles ADC1 global interrupts requests. * @param None * @retval None */ void ADC1_COMP_IRQHandler(void) { if(ADC_GetITStatus(ADC1, ADC_IT_EOC) != RESET) { /* Get converted value */ ADCVal = ADC_GetConversionValue(ADC1); /* Output converted value on DAC_OUT1 */ DAC_SetChannel1Data(DAC_Align_12b_R, ADCVal); /* Clear EOC Flag */ ADC_ClearITPendingBit(ADC1, ADC_IT_EOC); } }
/** * @brief Nastavi vzorek na vystup * @param uint16_t - hodnota, ktera se ma nastavit (v bitech) * @retval None */ void DAC_set_sample(uint16_t sample) { /* Enable DMA2 Channel3 */ DMA_Cmd(DMA2_Channel3, DISABLE); /* Enable DMA for DAC Channel2 */ DAC_DMACmd(DAC_Channel_1, DISABLE); DAC_SetChannel1Data(DAC_Align_12b_R, sample); dacState = DAC_RUN; }
/** * @brief Configure DAC peripheral * @param None * @retval None */ static void DAC_Config(void) { /* Init DAC channel 1 */ DAC_Init(DAC_Channel_1, DAC_Trigger_None, DAC_OutputBuffer_Disable); /* Enable DAC channel 1 */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Set DAC Channel1 DHR register: * DAC1 output = VREF * DOR / 256 = 3.3 * 116 / 256 = 1.5 V */ DAC_SetChannel1Data(DAC_Align_8b_R, 116); /* Close I/O Switch 24 to select PE5 as comparator 2 non inverting input: * External signal should be connected to PE5 */ SYSCFG_RIIOSwitchConfig(RI_IOSwitch_24, ENABLE); }
/** * @brief This function handles TIM6 global interrupt request. * @param None * @retval None */ void TIM6_IRQHandler(void) { if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) { /* Clear TIM6 update interrupt */ TIM_ClearITPendingBit(TIM6, TIM_IT_Update); if ((Out_Data_Offset < In_Data_Offset) && ((uint8_t)(MUTE_DATA) == 0)) { /* Set DAC Channel1 DHR register */ DAC_SetChannel1Data(DAC_Align_8b_R, Stream_Buff[Out_Data_Offset]); Out_Data_Offset++; } } }
void TIM2_IRQHandler(void) //TIM2中断 { static u16 i=0; if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 { //PE_OUT(1); TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源 GUI_TOUCH_Exec(); // GUI_Exec(); // Sin_Data[i]=100*sin((double)(i*3.14/40))+200;if (i == 1024) i = 0; //PE_OUT(0); // DAC_SetChannel1Data(DAC_Align_12b_R,1000+2000*sin((double)(i*3.14/40))); DAC_SetChannel1Data(DAC_Align_12b_R,10); i++; if (i == 80) i = 0; } }
void DMA1_Channel1_IRQHandler(void) { /* Test DMA1 TC flag */ if((DMA_GetFlagStatus(DMA1_FLAG_TC1)) != RESET ) { /* Clear DMA TC flag */ DMA_ClearFlag(DMA1_FLAG_TC1); if(Motor_State==INIT) { ADCTemp_Init(ADC_Tab); } else { LED1_ON(); SensorlessFOCRUN(); LED1_OFF(); } SVM_Angle=smc1.Theta; //DAC_SetChannel1Data(DAC_Align_12b_R,SVM_Angle/16); DAC_SetChannel1Data(DAC_Align_12b_R,ADC_Tab[IB_Channl]); if(++T2ms_Temp>T2MSTEMP) //2ms { T2ms_Temp=0; T2ms_Flag=1; Error_OMEGA=smc1.Omega-OMEGA_Old; OMEGA_Old=smc1.Omega; if(uGF.bit.RunMotor&&(uGF.bit.OpenLoop==0)) //ÅжÏÕýÐþÕðµ´ { if(_Q15abs(Error_OMEGA)>ERROROMEGAMIN) { uGF.bit.RunMotor = 0; uGF.bit.MotorFail = 1; } } }else{} if(++T100ms_Temp>T100MSTEMP) //100ms { T100ms_Temp=0; T100ms_Flag=1; }else{} } }
void Offset_Measurement_On(void) { /*DAC DMA request disable to measure the dark detector output*/ DAC_DMACmd(DAC_Channel_1, DISABLE); /*MHP Off*/ DAC_SetChannel1Data(DAC_Align_12b_R, 0); /*DMA2 interrupt will not disable ADC3 because triggering is not needed for offset measurement*/ offset_meas = 1; /*ADC3 is ENABLED because triggering from the DAC output is not needed*/ //ADC_Cmd(ADC3, ENABLE); ADC_DMACmd(ADC3, ENABLE); old_sampl_freq = GetSamplFreq(); /*4000 samples per second: we need to wait 0.25 seconds to fill the 1000 elements buffer*/ SetSamplFreq(4000); }
// initValue - 0 - 4095 void Init_DAC_1(int initValue) { DAC_InitTypeDef DAC_InitStructure; //Set DAC clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); /* 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_DeInit(); DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Cmd(DAC_Channel_1, ENABLE); DAC_SetChannel1Data(DAC_Align_12b_R, initValue); DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE); }