/** * @brief Configures the DAC. * @param None * @retval None */ void DAC_Configuration(void) { DAC_InitPara DAC_InitStructure; RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_DAC, ENABLE); /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_TRIGGER_T2_TRIG; DAC_InitStructure.DAC_WaveType = DAC_WAVEGENE_NOISE; DAC_InitStructure.DAC_LFSRNoise_AmplitudeTriangle = DAC_LFSR_BITS9_0; DAC_InitStructure.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; DAC_Init(DAC_CHANNEL_1, &DAC_InitStructure); /* DAC channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_TRIGGER_T3_TRIG; DAC_InitStructure.DAC_LFSRNoise_AmplitudeTriangle = DAC_LFSR_BITS10_0; DAC_Init(DAC_CHANNEL_2, &DAC_InitStructure); /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is automatically connected to the DAC converter. */ DAC_Enable(DAC_CHANNEL_1, ENABLE); /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Enable(DAC_CHANNEL_2, ENABLE); /* Set DAC dual channel DHR12RD register */ DAC_SetDualChannelData(DAC_ALIGN_12B_R, 0x7F0, 0x7F0); }
/**************************************************************************//** * @brief DAC_setup * Configures the DAC *****************************************************************************/ void DAC_setup(void) { /* Enable necessary clocks */ CMU_ClockEnable(cmuClock_DAC0, true); /* Use default settings */ DAC_Init_TypeDef init = DAC_INIT_DEFAULT; DAC_InitChannel_TypeDef initChannel = DAC_INITCHANNEL_DEFAULT; /* Calculate the DAC clock prescaler value that will result in a DAC clock * close to 500kHz. Second parameter is zero, if the HFPERCLK value is 0, the * function will check what the current value actually is. */ init.prescale = DAC_PrescaleCalc(500000, 0); /* Set reference voltage to Vdd */ init.reference = dacRefVDD; /* Enable PRS triggered conversion (channel 5) */ initChannel.prsEnable = true; initChannel.prsSel = dacPRSSELCh5; /* Initialize DAC and DAC channel 0 */ DAC_Init(DAC0, &init); DAC_InitChannel(DAC0, &initChannel, 0); /* Enable DAC channel 0, located on pin PB11 */ DAC_Enable(DAC0, 0, true); }
void DAC_Deinit(DAC_Type *base) { DAC_Enable(base, false); /* Disable the clock. */ CLOCK_DisableClock(s_dacClocks[DAC_GetInstance(base)]); }
/** * @brief DAC configuration. * @param None * @retval None */ void DAC_Configuration(void) { DAC_InitPara DAC_InitStruct; DAC_Init(DAC1,&DAC_InitStruct); DAC_InitStruct.DAC_Trigger= DAC_TRIGGER_SOFTWARE; DAC_InitStruct.DAC_OutputBuffer=DAC_OUTPUTBUFFER_ENABLE ; DAC_Enable(DAC1,ENABLE); }
/*! * @brief Main function */ int main(void) { dac_config_t dacConfigStruct; uint32_t dacValue; BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_InitDebugConsole(); PRINTF("\r\nDAC basic Example.\r\n"); /* Configure the DAC. */ /* * dacConfigStruct.referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2; * dacConfigStruct.enableLowPowerMode = false; */ DAC_GetDefaultConfig(&dacConfigStruct); DAC_Init(DEMO_DAC_INSTANCE, &dacConfigStruct); DAC_Enable(DEMO_DAC_INSTANCE, true); DAC_SetBufferReadPointer(DEMO_DAC_INSTANCE, 0U); /* Make sure the read pointer to the start. */ /* * The buffer is not enabled, so the read pointer can not move automatically. However, the buffer's read pointer * and itemss can be written manually by user. */ while (1) { PRINTF("\r\nPlease input a value (0 - 4095) to output with DAC: "); SCANF("%d", &dacValue); PRINTF("\r\nInput value is %d\r\n", dacValue); if (dacValue > 0xFFFU) { PRINTF("Your value is output of range.\r\n"); continue; } DAC_SetBufferValue(DEMO_DAC_INSTANCE, 0U, dacValue); PRINTF("DAC out: %d\r\n", dacValue); /* * The value in the first item would be converted. User can measure the output voltage from DAC_OUTx pin. */ } }
int main(void) { CHIP_Init(); /*Turn on the DAC clock.*/ CMU_ClockEnable(cmuClock_DAC0, true); /*configure and enable the DAC.*/ DAC_setup(); DAC_Enable(DAC0, 0, true); /*Write data to registers. V1 = 1.0*/ DAC0->CH0DATA = (uint32_t)((1.0 * 4096) / 3.3); /*configure OPA0, OPA1 and OPA2.*/ OPAMP_Init_TypeDef configuration0 = OPA_INIT_DIFF_RECEIVER_OPA0 ; OPAMP_Init_TypeDef configuration2 = OPA_INIT_DIFF_RECEIVER_OPA2 ; /*Redefine the resistances. Want to divide the difference by 3.*/ configuration2.resSel = opaResSelDefault; configuration0.resSel = opaResSelR2eq3R1; /*OPA2 positive input = VSS*/ configuration2.resInMux = opaResInMuxVss; /*Enable OPA0 and OPA2. All the configurations are set.*/ OPAMP_Enable(DAC0, OPA0, &configuration0); OPAMP_Enable(DAC0, OPA2, &configuration2); /*Disable OPA0. This is done because we want to use OPA0 as a part of the DAC. The configurations set above are still there.*/ DAC0->OPACTRL &= ~DAC_OPACTRL_OPA0EN; /*Never end.*/ while(1); }
void analogout_enable(dac_t *obj, uint8_t enable) { DAC_Enable(obj->dac, obj->channel, enable); }
/**************************************************************************//** * @brief Main function * The DAC produces an output signal which the ADC measures. The measured * result is printed on the LCD screen. * * The code shows two different examples of ADC input: * * One version uses DAC output channel 0 as input. * * The other version uses ADC input channel 3 (PD3 on the STK). This pin must * then be connected with a wire to the DAC output channel 0 (PB11 on the STK). * *****************************************************************************/ int main(void) { /* Variable declarations */ uint32_t DAC_Value; uint32_t sample; double voltage; /* Initialize chip */ CHIP_Init(); /* Initialize LCD */ SegmentLCD_Init(false); /* Enable clocks required */ CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(cmuClock_ADC0, true); /* Configure ADC */ ADCConfig(); /* Initialize the DAC */ DAC_setup(); /* Enable DAC channel 0, located on pin PB11 */ DAC_Enable(DAC0, 0, true); /* This code is only necessary when using ADC input channel 3 which * is connected to pin PD3 on the STK. Enable it by uncommenting the * definition of EXTERNAL above */ #ifdef EXTERNAL CMU_ClockEnable(cmuClock_GPIO, true); GPIO_PinModeSet(gpioPortD, 3, gpioModeInputPull, 1); GPIO_PinOutClear(gpioPortD, 3); #endif /* Stay in this loop forever */ while (1) { /* Calculate DAC output to 0.5 V. */ DAC_Value = (uint32_t)((0.5 * 4096) / 1.25); /* Write the new value to DAC register */ DAC_WriteData(DAC0, DAC_Value, 0); /* Start single conversion for ADC */ ADC_Start(ADC0, adcStartSingle); /* Wait while conversion is active */ while (ADC0->STATUS & ADC_STATUS_SINGLEACT) ; /* Get ADC result */ sample = ADC_DataSingleGet(ADC0); /* Calculate output voltage produced by the DAC */ voltage = (sample * 1.25) / 4096; /* Write the result to LCD */ char buffer[10]; snprintf(buffer, 8, "%1.2f", voltage); SegmentLCD_Write(buffer); /* wait 100ms in EM2 before next conversion */ RTCDRV_Trigger(100, NULL); EMU_EnterEM2(true); } }