Exemplo n.º 1
0
/**
 * @brief  初始化DAC模块
 * @code
 *  DAC_InitTypeDef DAC_InitStruct = {0};
 *  DAC_InitStruct.bufferMode = kDAC_Buffer_Swing; 
 *  DAC_InitStruct.instance = HW_DAC0;
 *  DAC_InitStruct.referenceMode = kDAC_Reference_2; 
 *  DAC_InitStruct.triggerMode = kDAC_TriggerSoftware; 
 *  DAC_Init(&DAC_InitStruct);
 * @endcode
 * @param  DAC_InitTypeDef: 
 *         instance      :模块号
 *         @arg HW_DAC0 :芯片的DAC0 模块
 *         @arg HW_DAC1 :芯片的DAC1 模块
 * @retval None
 */
void DAC_Init(DAC_InitTypeDef* DAC_InitStruct)
{
    SIM->SCGC2 |= SIM_SCGC2_DAC0_MASK;
    /* trigger select */
    switch(DAC_InitStruct->triggerMode)
    {
        case kDAC_TriggerSoftware:
            DAC_InstanceTable[DAC_InitStruct->instance]->C0 |= DAC_C0_DACTRGSEL_MASK;
            break;
        case kDAC_TriggerHardware:
            DAC_InstanceTable[DAC_InitStruct->instance]->C0 &= ~DAC_C0_DACTRGSEL_MASK;
            break;
        default:
            break;
    }
    /* buffer mode */
    switch(DAC_InitStruct->bufferMode)
    {
        case kDAC_Buffer_Disable:
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 &= ~DAC_C1_DACBFEN_MASK;
            break;
        case kDAC_Buffer_Normal:
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 |= DAC_C1_DACBFEN_MASK;
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 &= ~DAC_C1_DACBFMD_MASK;
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 |= DAC_C1_DACBFMD(0);
            break;
        case kDAC_Buffer_Swing:
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 |= DAC_C1_DACBFEN_MASK;
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 &= ~DAC_C1_DACBFMD_MASK;
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 |= DAC_C1_DACBFMD(1);
            break;
        case kDAC_Buffer_OneTimeScan:
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 |= DAC_C1_DACBFEN_MASK;
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 &= ~DAC_C1_DACBFMD_MASK;
            DAC_InstanceTable[DAC_InitStruct->instance]->C1 |= DAC_C1_DACBFMD(2);
            break;
        default:
            break;
    }
    /* reference */
    switch(DAC_InitStruct->referenceMode)
    {
        case kDAC_Reference_1:
            DAC_InstanceTable[DAC_InitStruct->instance]->C0 &= ~DAC_C0_DACRFS_MASK;
            break;
        case kDAC_Reference_2:
            DAC_InstanceTable[DAC_InitStruct->instance]->C0 |= DAC_C0_DACRFS_MASK;
            break;
        default:
            break;
    }
    /* clear flags */
    DAC_InstanceTable[DAC_InitStruct->instance]->SR &= ~DAC_SR_DACBFRPBF_MASK;
    DAC_InstanceTable[DAC_InitStruct->instance]->SR &= ~DAC_SR_DACBFRPTF_MASK;
    DAC_InstanceTable[DAC_InitStruct->instance]->SR &= ~DAC_SR_DACBFWMF_MASK;
    /* enable module */
    DAC_InstanceTable[DAC_InitStruct->instance]->C0 |= DAC_C0_DACEN_MASK;
}
Exemplo n.º 2
0
void DAC_SetBufferConfig(DAC_Type *base, const dac_buffer_config_t *config)
{
    assert(NULL != config);

    uint8_t tmp8;

    /* DACx_C0. */
    tmp8 = base->C0 & ~(DAC_C0_DACTRGSEL_MASK);
    if (kDAC_BufferTriggerBySoftwareMode == config->triggerMode)
    {
        tmp8 |= DAC_C0_DACTRGSEL_MASK;
    }
    base->C0 = tmp8;

    /* DACx_C1. */
    tmp8 = base->C1 &
           ~(
#if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
               DAC_C1_DACBFWM_MASK |
#endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
               DAC_C1_DACBFMD_MASK);
#if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
    tmp8 |= DAC_C1_DACBFWM(config->watermark);
#endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
    tmp8 |= DAC_C1_DACBFMD(config->workMode);
    base->C1 = tmp8;

    /* DACx_C2. */
    tmp8 = base->C2 & ~DAC_C2_DACBFUP_MASK;
    tmp8 |= DAC_C2_DACBFUP(config->upperLimit);
    base->C2 = tmp8;
}
void analogout_init(dac_t *obj, PinName pin) {
    obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
    if (obj->dac == (DACName)NC) {
        error("DAC pin mapping failed");
    }

    SIM->SCGC2 |= SIM_SCGC2_DAC0_MASK;

    DAC0->DAT[obj->dac].DATH = 0;
    DAC0->DAT[obj->dac].DATL = 0;

    DAC0->C1 = DAC_C1_DACBFMD(2);     // One-Time Scan Mode

    DAC0->C0 = DAC_C0_DACEN_MASK      // Enable
             | DAC_C0_DACSWTRG_MASK   // Software Trigger
             | DAC_C0_DACRFS_MASK;    // VDDA selected

    analogout_write_u16(obj, 0);
}
Exemplo n.º 4
0
/***********************************************************************************************
 功能:DAC初始化
 形参:DAC_InitStruct: DAC初始化结构
 返回:0
 详解:0
************************************************************************************************/
void DAC_Init(DAC_InitTypeDef* DAC_InitStruct)
{
	//参数检查
	assert_param(IS_DAC_TRIGGER_MODE(DAC_InitStruct->DAC_TrigerMode));
	assert_param(IS_DAC_BUFFER_MODE(DAC_InitStruct->DAC_BufferMode));
	assert_param(IS_DAC_WATERMARK_MODE(DAC_InitStruct->DAC_WaterMarkMode));
	
	//开启DAC模块时钟
	SIM->SCGC2|=SIM_SCGC2_DAC0_MASK;  
	//配置BUFFER模式
	switch(DAC_InitStruct->DAC_BufferMode)
	{
		case DAC_TRIGER_MODE_NONE:
			DAC0->C0 &= ~DAC_C0_DACTRGSEL_MASK;
			DAC0->C0 |= DAC_C0_DACSWTRG_MASK;
			break;
		case DAC_TRIGER_MODE_SOFTWARE:
			DAC0->C0 |= DAC_C0_DACTRGSEL_MASK;
			DAC0->C0 |= DAC_C0_DACSWTRG_MASK;
			break;
		case DAC_TRIGER_MODE_HARDWARE:
			DAC0->C0 &= ~DAC_C0_DACTRGSEL_MASK;
			DAC0->C0 &= ~DAC_C0_DACSWTRG_MASK;
			break;
		default:break;
	}
	//选择参考源2
	DAC0->C0 |= DAC_C0_DACRFS_MASK;  
	//开启DAC模块
	DAC0->C0 |= DAC_C0_DACEN_MASK ;
	
	//配置DAC_C1寄存器 配置BUFFER模式
	switch(DAC_InitStruct->DAC_BufferMode)
	{
		case BUFFER_MODE_NORMAL:
			DAC0->C1 |= DAC_C1_DACBFEN_MASK;
			DAC0->C1 |= DAC_C1_DACBFMD(0);
			break;
		case BUFFER_MODE_SWING:
			DAC0->C1 |= DAC_C1_DACBFEN_MASK;
			DAC0->C1 |= DAC_C1_DACBFMD(1);  
			break;
		case BUFFER_MODE_ONETIMESCAN:
			DAC0->C1 |= DAC_C1_DACBFEN_MASK;
			DAC0->C1 |= DAC_C1_DACBFMD(2);
			break;
		case BUFFER_MODE_DISABLE:
			DAC0->C1 &= ~DAC_C1_DACBFEN_MASK;
			break;
	}
	//设置水位
	switch(DAC_InitStruct->DAC_WaterMarkMode)
	{
		case WATER_MODE_1WORD:
			DAC0->C1 |= DAC_C1_DACBFWM(0);
			break;
		case WATER_MODE_2WORD:
			DAC0->C1 |= DAC_C1_DACBFWM(1);
			break;
		case WATER_MODE_3WORD:
			DAC0->C1 |= DAC_C1_DACBFWM(2);
			break;
		case WATER_MODE_4WORD:
			DAC0->C1 |= DAC_C1_DACBFWM(3);
			break;
		default:break;
	}

  //配置C2寄存器 设置上限和下线
	DAC0->C2 =  DAC_C2_DACBFUP(DAC_InitStruct->DAC_BufferUpperLimit);
	DAC0->C2 |= DAC_C2_DACBFRP(DAC_InitStruct->DAC_BufferStartPostion);
}
Exemplo n.º 5
0
/*
 * dac_int
 * DAC初始化函数,使能DAC各项功能
 * 
 * 参数:
 *    dac_init_struct--DAC初始化结构体,
 *                        具体定义见DAC_InitTypeDef
 *
 * 输出:
 *    0--配置错误
 *    1--配置成功
 */
uint8_t Dac::dac_int(DAC_InitTypeDef dac_init_struct)
{
  uint8_t x;
  DAC_Type *dacx = dac_init_struct.DAC_Dacx;
  uint8_t work_mode = dac_init_struct.DAC_BufferWorkMode;
  uint8_t watermark = dac_init_struct.DAC_BufferWatermarkSel;
  uint8_t upper = dac_init_struct.DAC_BufferUpperLimit;
  DAC_ISR_CALLBACK rdptbot_isr = dac_init_struct.DAC_ReadPointerBottomIsr;
  DAC_ISR_CALLBACK rdpttop_isr = dac_init_struct.DAC_ReadPointerTopIsr;
  DAC_ISR_CALLBACK watermk_isr = dac_init_struct.DAC_BufferWatermarkIsr;
  
  upper = (upper==NULL?1:upper);
    
  //参数检查
  ASSERT( work_mode <= BUFFER_MODE_ONETIMESCAN );  //判断模式选择
  ASSERT( watermark <= WATERMARK_4WORDS );      //判断缓冲区水印选择
  ASSERT( upper <= 16);         //判断缓冲区上限数值
    
  //配置DAC时钟
  if(dacx == DAC0)
  {
    x = 0;
    SIM->SCGC2 |= SIM_SCGC2_DAC0_MASK;   //开启DAC0时钟
  }
  else if(dacx == DAC1)
  {
    x = 1;
    SIM->SCGC2 |= SIM_SCGC2_DAC1_MASK;   //开启DAC1时钟
  }
  else 
  {
    return 0;
  }
  
  //配置C2寄存器
  dacx->C2 = DAC_C2_DACBFUP(upper - 1);     //配置缓冲区上限数值
  
  //配置C1寄存器
  dacx->C1 = 0;
  if(dac_init_struct.DAC_BufferEnable == TRUE)
  {
    dacx->C1 |= DAC_C1_DACBFEN_MASK;    //使能缓冲区 
  }
  dacx->C1 |= DAC_C1_DACBFMD(work_mode);        //缓冲区工作模式配置
  dacx->C1 |= DAC_C1_DACBFWM(watermark);        //缓冲区水印选择配置
  if(dac_init_struct.DAC_DmaEnable == TRUE)
  {
    dacx->C1 |= DAC_C1_DMAEN_MASK;      //使能DMA
  }
  
  //配置C0寄存器
  dacx->C0 = 0;
  if(dac_init_struct.DAC_ReadPointerBottomIntEnable == TRUE && rdptbot_isr != NULL)
  {
    dacx->C0 |= DAC_C0_DACBBIEN_MASK;   //使能缓冲区底部中断
    DAC_RDPTBOT_ISR[x] = rdptbot_isr;
  }
  if(dac_init_struct.DAC_ReadPointerTopIntEnable == TRUE && rdpttop_isr != NULL)
  {
    dacx->C0 |= DAC_C0_DACBTIEN_MASK;   //使能缓冲区顶部中断
    DAC_RDPTTOP_ISR[x] = rdpttop_isr;
  }
  if(dac_init_struct.DAC_BufferWatermarkIntEnable == TRUE && watermk_isr != NULL)
  {
    dacx->C0 |= DAC_C0_DACBWIEN_MASK;   //使能缓冲区水印中断
    DAC_WATERMK_ISR[x] = watermk_isr;
  }
  if(dac_init_struct.DAC_SoftTrgEnable == TRUE)
  {
    dacx->C0 |= DAC_C0_DACTRGSEL_MASK;  //使能软件触发缓冲区
  }
  dacx->C0 |= DAC_C0_DACRFS_MASK;       //使能DACREF_2(VDDA)为参考电压
  
  //使能DAC模块,可编程参考发生器开始工作
  dacx->C0 |= DAC_C0_DACEN_MASK;
  
  return 1;
}