dac.c 3.11 KB
Newer Older
时昊's avatar
时昊 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include "dac.h"
#include "cgc.h"

/***********************************************************************************************************************
* Function Name: DAC_Init
* @brief  This function initializes the D/A converter.
* @param  ch - spcify the dac channel to initialized
* @return None
***********************************************************************************************************************/
void DAC_Init(DAC_InitTypeDef* DAC_InitStruct)
{
	CGC_PER1PeriphClockCmd(CGC_PER1Periph_DAC,ENABLE);
	DAC->DAM   = 0x00U;

	if(DAC_InitStruct->DAC_Channel & 0x01)
	{
		DAC->DAM |= DAC_InitStruct->DAC_Mode;
		DAC->DACS0 = DAC_InitStruct->DAC_ConValue;
	}
	if(DAC_InitStruct->DAC_Channel & 0x02)
	{
		DAC->DAM |= DAC_InitStruct->DAC_Mode;
		DAC->DACS1 = DAC_InitStruct->DAC_ConValue;
	}
}

/***********************************************************************************************************************
* Function Name: DAC_Start
* @brief  This function enables the DA converter channel [#n].
* @param  ch - spcify the dac channel to start
* @return None
***********************************************************************************************************************/
void DAC_Start(DAC_Channel_t ch)
{
    if(ch & 0x01) 
    {
        DAC->DAM  |= DAC_DAM_DACE0_Msk;     /* enables D/A conversion operation */
    }
    if(ch & 0x02) 
    {
        DAC->DAM  |= DAC_DAM_DACE1_Msk;     /* enables D/A conversion operation */
    }
}
/***********************************************************************************************************************
* Function Name: DAC_Stop
* @brief  This function stops the DA converter channel [#n].
* @param  ch - spcify the dac channel to stop
* @return None
***********************************************************************************************************************/
void DAC_Stop(DAC_Channel_t ch)
{
    if(ch & 0x01) 
    {
        DAC->DAM  &= ~DAC_DAM_DACE0_Msk;     /* stops D/A conversion operation */
    }
    if(ch & 0x02) 
    {
        DAC->DAM  &= ~DAC_DAM_DACE1_Msk;     /* stops D/A conversion operation */
    }
}

/***********************************************************************************************************************
* Function Name: DAC_Set_Value
* @brief  This function sets the DA converter channel [#n] value.
* @param  reg_value - value of conversion
* @return None
***********************************************************************************************************************/
void DAC_Set_Value(DAC_Channel_t ch, uint8_t regvalue)
{
    if(ch & 0x01) 
    {
        DAC->DACS0 = regvalue;
    }
    if(ch & 0x02) 
    {
        DAC->DACS1 = regvalue;
    }
}

/***********************************************************************************************************************
* Function Name: DAC_Set_PowerOff
* @brief  This function stops supply of input clock and reset all SFR.
* @param  None
* @return None
***********************************************************************************************************************/
void DAC_Set_PowerOff(void)
{
    CGC->PER1 &= ~CGC_PER1_DACEN_Msk;     /* stops input clock supply */
}