Esempio n. 1
0
//-----------------------------------------------------------------//
//	Setup ADC
//	ADC_CLK = 4 MHz
//	ADC single conversion time is ADC_StartDelay +
//		(29 to 36 ADC clock cycles depending on ADC_DelayGo)
//	ADC clock = 4MHz / 128 = 31.250kHz (T = 32uS)
//-----------------------------------------------------------------//
void HW_ADCInit(void)
{
    ADC_InitTypeDef sADC;
    ADCx_InitTypeDef sADCx;

    // ADC Configuration
    // Reset all ADC settings
    ADC_DeInit();
    ADC_StructInit(&sADC);

    sADC.ADC_SynchronousMode      = ADC_SyncMode_Independent;
    sADC.ADC_StartDelay           = 10;
    sADC.ADC_TempSensor           = ADC_TEMP_SENSOR_Enable;
    sADC.ADC_TempSensorAmplifier  = ADC_TEMP_SENSOR_AMPLIFIER_Enable;
    sADC.ADC_TempSensorConversion = ADC_TEMP_SENSOR_CONVERSION_Enable;
    sADC.ADC_IntVRefConversion    = ADC_VREF_CONVERSION_Enable;
    sADC.ADC_IntVRefTrimming      = 1;
    ADC_Init (&sADC);

    // ADC1 Configuration
    ADCx_StructInit (&sADCx);
    sADCx.ADC_ClockSource      = ADC_CLOCK_SOURCE_ADC;
    sADCx.ADC_SamplingMode     = ADC_SAMPLING_MODE_SINGLE_CONV;
    sADCx.ADC_ChannelSwitching = ADC_CH_SWITCHING_Disable;
    sADCx.ADC_ChannelNumber    = ADC_CH_TEMP_SENSOR;
    sADCx.ADC_Channels         = 0;
    sADCx.ADC_LevelControl     = ADC_LEVEL_CONTROL_Disable;
    sADCx.ADC_LowLevel         = 0;
    sADCx.ADC_HighLevel        = 0;
    sADCx.ADC_VRefSource       = ADC_VREF_SOURCE_EXTERNAL;
    sADCx.ADC_IntVRefSource    = ADC_INT_VREF_SOURCE_EXACT;
    sADCx.ADC_Prescaler        = ADC_CLK_div_128;
    sADCx.ADC_DelayGo          = 0;		// CHECKME
    ADC1_Init (&sADCx);
    ADC2_Init (&sADCx);

    // Disable ADC interupts
    ADC1_ITConfig((ADCx_IT_END_OF_CONVERSION  | ADCx_IT_OUT_OF_RANGE), DISABLE);
    ADC2_ITConfig((ADCx_IT_END_OF_CONVERSION  | ADCx_IT_OUT_OF_RANGE), DISABLE);

    // ADC1 enable
    ADC1_Cmd (ENABLE);
    ADC2_Cmd (ENABLE);


    //-------------------//
    /*	ADC1_SetChannel(ADC_CH_TEMP_SENSOR);
    	ADC1_Start();
    	while( ADC_GetFlagStatus(ADC1_FLAG_END_OF_CONVERSION)==RESET );
      temp_adc = ADC1_GetResult();

    	ADC1_Start();
    	while( ADC_GetFlagStatus(ADC1_FLAG_END_OF_CONVERSION)==RESET );
      temp_adc = ADC1_GetResult();

    	temp_adc = temp_adc;
    	*/

}
/**
 * @brief  Main program.
 * @param  None
 * @retval None
 */
void main ( void )
{
	ClockConfigure();

#if defined (_USE_DEBUG_UART_)
	DebugUARTInit();
#endif /* #if defined (_USE_DEBUG_UART_) */

	/* ADC Configuration */
	/* Reset all ADC settings */
	ADC_DeInit();
	DEBUG_PRINTF("ADC Init...");
	// Inti clock ADC
	RST_CLK_ADCclkSelection(RST_CLK_ADCclkCPU_C1);
	RST_CLK_ADCclkPrescaler(RST_CLK_ADCclkDIV2);
	// Enable clock ADC
	RST_CLK_ADCclkEnable(ENABLE);
	RST_CLK_PCLKcmd(RST_CLK_PCLK_ADC, ENABLE);
	ADC_StructInit(&sADC);

	sADC.ADC_StartDelay = 0;
	sADC.ADC_TempSensor = ADC_TEMP_SENSOR_Enable;
	sADC.ADC_TempSensorAmplifier = ADC_TEMP_SENSOR_AMPLIFIER_Enable;
	sADC.ADC_TempSensorConversion = ADC_TEMP_SENSOR_CONVERSION_Enable;
	sADC.ADC_IntVRefConversion = ADC_VREF_CONVERSION_Disable;
	sADC.ADC_IntVRefTrimming = 1;
	ADC_Init(&sADC);

	/* ADC1 Configuration */
	ADCx_StructInit(&sADCx);
	sADCx.ADC_ClockSource = ADC_CLOCK_SOURCE_CPU;
	sADCx.ADC_SamplingMode = ADC_SAMPLING_MODE_CICLIC_CONV;
	sADCx.ADC_ChannelSwitching = ADC_CH_SWITCHING_Disable;
	sADCx.ADC_ChannelNumber = ADC_CH_TEMP_SENSOR;
	sADCx.ADC_Channels = 0;
	sADCx.ADC_LevelControl = ADC_LEVEL_CONTROL_Disable;
	sADCx.ADC_LowLevel = 0;
	sADCx.ADC_HighLevel = 0;
	sADCx.ADC_VRefSource = ADC_VREF_SOURCE_INTERNAL;
	sADCx.ADC_IntVRefSource = ADC_INT_VREF_SOURCE_INEXACT;
	sADCx.ADC_Prescaler = ADC_CLK_div_512;
	sADCx.ADC_DelayGo = 7;
	ADC1_Init(&sADCx);

	/* Enable ADC1 EOCIF and AWOIFEN interupts */
	ADC1_ITConfig(ADCx_IT_END_OF_CONVERSION, ENABLE);
	/* Enable ADC IRQ */
	NVIC_EnableIRQ(ADC_IRQn);

	DEBUG_PRINTF("Ok\r\n");

	/* ADC1 enable */
	ADC1_Cmd(ENABLE);

	while (1);
}
void main(void)
#endif
{
  RST_CLK_DeInit();
  RST_CLK_CPU_PLLconfig (RST_CLK_CPU_PLLsrcHSIdiv2,0);
  /* Enable peripheral clocks --------------------------------------------------*/
  RST_CLK_PCLKcmd((RST_CLK_PCLK_RST_CLK | RST_CLK_PCLK_DMA | RST_CLK_PCLK_ADC),ENABLE);
  RST_CLK_PCLKcmd((RST_CLK_PCLK_SSP1 | RST_CLK_PCLK_SSP2),ENABLE);

  /* Disable all interrupt */
  NVIC->ICPR[0] = 0xFFFFFFFF;
  NVIC->ICER[0] = 0xFFFFFFFF;

  /* DMA Configuration */
  /* Reset all settings */
  DMA_DeInit();
  DMA_StructInit(&DMA_InitStr);
  /* Set Primary Control Data */
  DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)(&(MDR_ADC->ADC1_RESULT));
  DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)ADCConvertedValue;
  DMA_PriCtrlStr.DMA_SourceIncSize = DMA_SourceIncNo;
  DMA_PriCtrlStr.DMA_DestIncSize = DMA_DestIncHalfword;
  DMA_PriCtrlStr.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_PriCtrlStr.DMA_Mode = DMA_Mode_PingPong;
  DMA_PriCtrlStr.DMA_CycleSize = 10;
  DMA_PriCtrlStr.DMA_NumContinuous = DMA_Transfers_1;
  DMA_PriCtrlStr.DMA_SourceProtCtrl = DMA_SourcePrivileged;
  DMA_PriCtrlStr.DMA_DestProtCtrl = DMA_DestPrivileged;

  /* Set Alternate Control Data */
  DMA_AltCtrlStr.DMA_SourceBaseAddr = (uint32_t)(&(MDR_ADC->ADC1_RESULT));
  DMA_AltCtrlStr.DMA_DestBaseAddr   = (uint32_t)ADCConvertedValue;
  DMA_AltCtrlStr.DMA_SourceIncSize = DMA_SourceIncNo;
  DMA_AltCtrlStr.DMA_DestIncSize = DMA_DestIncHalfword;
  DMA_AltCtrlStr.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_AltCtrlStr.DMA_Mode = DMA_Mode_PingPong;
  DMA_AltCtrlStr.DMA_CycleSize = 10;
  DMA_AltCtrlStr.DMA_NumContinuous = DMA_Transfers_1;
  DMA_AltCtrlStr.DMA_SourceProtCtrl = DMA_SourcePrivileged;
  DMA_AltCtrlStr.DMA_DestProtCtrl = DMA_DestPrivileged;

  /* Set Channel Structure */
  DMA_InitStr.DMA_PriCtrlData = &DMA_PriCtrlStr;
  DMA_InitStr.DMA_AltCtrlData = &DMA_AltCtrlStr;
  DMA_InitStr.DMA_Priority = DMA_Priority_Default;
  DMA_InitStr.DMA_UseBurst = DMA_BurstClear;
  DMA_InitStr.DMA_SelectDataStructure = DMA_CTRL_DATA_PRIMARY;

  /* Init DMA channel ADC1 */
  DMA_Init(DMA_Channel_ADC1, &DMA_InitStr);

  /* Enable dma_req or dma_sreq to generate DMA request */
  MDR_DMA->CHNL_REQ_MASK_CLR = (1<<DMA_Channel_ADC1);
  MDR_DMA->CHNL_USEBURST_CLR = (1<<DMA_Channel_ADC1);

  /* Enable DMA channel ADC1 */
  DMA_Cmd(DMA_Channel_ADC1, ENABLE);

  /* ADC Configuration */
  /* Reset all ADC settings */
  ADC_DeInit();
  ADC_StructInit(&sADC);

  sADC.ADC_SynchronousMode      = ADC_SyncMode_Independent;
  sADC.ADC_StartDelay           = 0;
  sADC.ADC_TempSensor           = ADC_TEMP_SENSOR_Enable;
  sADC.ADC_TempSensorAmplifier  = ADC_TEMP_SENSOR_AMPLIFIER_Enable;
  sADC.ADC_TempSensorConversion = ADC_TEMP_SENSOR_CONVERSION_Enable;
  sADC.ADC_IntVRefConversion    = ADC_VREF_CONVERSION_Disable;
  sADC.ADC_IntVRefTrimming      = 1;
  ADC_Init (&sADC);

  /* ADC1 Configuration */
  ADCx_StructInit (&sADCx);
  sADCx.ADC_ClockSource      = ADC_CLOCK_SOURCE_CPU;
  sADCx.ADC_SamplingMode     = ADC_SAMPLING_MODE_CICLIC_CONV;
  sADCx.ADC_ChannelSwitching = ADC_CH_SWITCHING_Disable;
  sADCx.ADC_ChannelNumber    = ADC_CH_TEMP_SENSOR;
  sADCx.ADC_Channels         = 0;
  sADCx.ADC_LevelControl     = ADC_LEVEL_CONTROL_Disable;
  sADCx.ADC_LowLevel         = 0;
  sADCx.ADC_HighLevel        = 0;
  sADCx.ADC_VRefSource       = ADC_VREF_SOURCE_INTERNAL;
  sADCx.ADC_IntVRefSource    = ADC_INT_VREF_SOURCE_INEXACT;
  sADCx.ADC_Prescaler        = ADC_CLK_div_512;
  sADCx.ADC_DelayGo          = 7;
  ADC1_Init (&sADCx);

  /* Enable ADC1 EOCIF and AWOIFEN interupts */
  ADC1_ITConfig((ADCx_IT_END_OF_CONVERSION  | ADCx_IT_OUT_OF_RANGE), DISABLE);

  /* ADC1 enable */
  ADC1_Cmd (ENABLE);

  /* Enable DMA IRQ */
  NVIC_EnableIRQ(DMA_IRQn);

  while(1);
}
void main(void)
#endif
{
	ClockConfigure();
#if defined (_USE_DEBUG_UART_)
	DebugUARTInit();
#endif /* #if defined (_USE_DEBUG_UART_) */
	/* Enable peripheral clocks ----------------------------------------------*/
	RST_CLK_PCLKcmd((RST_CLK_PCLK_RST_CLK |
			         RST_CLK_PCLK_ADC     |
			         RST_CLK_PCLK_PORTD	  |
			         RST_CLK_PCLK_PORTB),
			         ENABLE);

	/* Reset PORTD settings */
	PORT_DeInit(MDR_PORTD );

	/* Configure ADC pin: ADC2 */
	/* Configure PORTD pin 9 */
	PORT_InitStructure.PORT_Pin = PORT_Pin_9;
	PORT_InitStructure.PORT_OE = PORT_OE_IN;
	PORT_InitStructure.PORT_MODE = PORT_MODE_ANALOG;
	PORT_Init(MDR_PORTD, &PORT_InitStructure);

	/* Configure PORTB pins 0, 1 for output to switch LED1,2 on/off */
	/* Configure PORTB pins 0, 1 */
	PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT;
	PORT_InitStructure.PORT_Pin = PORT_Pin_0 | PORT_Pin_1;
	PORT_InitStructure.PORT_OE = PORT_OE_OUT;
	PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL;
	PORT_InitStructure.PORT_SPEED = PORT_SPEED_MAXFAST;
	PORT_Init(MDR_PORTB, &PORT_InitStructure);

	/* ADC Configuration */
	/* Reset all ADC settings */
	ADC_DeInit();
	DEBUG_PRINTF("ADC Init...");
	// Inti clock ADC
	RST_CLK_ADCclkSelection(RST_CLK_ADCclkCPU_C1);
	RST_CLK_ADCclkPrescaler(RST_CLK_ADCclkDIV2);
	// Enable clock ADC
	RST_CLK_ADCclkEnable(ENABLE);
	RST_CLK_PCLKcmd(RST_CLK_PCLK_ADC, ENABLE);
	ADC_StructInit(&sADC);
	sADC.ADC_StartDelay 			= 0;
	sADC.ADC_TempSensor 			= ADC_TEMP_SENSOR_Enable;
	sADC.ADC_TempSensorAmplifier 	= ADC_TEMP_SENSOR_AMPLIFIER_Enable;
	sADC.ADC_TempSensorConversion	= ADC_TEMP_SENSOR_CONVERSION_Disable;
	sADC.ADC_IntVRefConversion 		= ADC_VREF_CONVERSION_Disable;
	sADC.ADC_IntVRefTrimming 		= 0;
	sADC.ADC_IntVRefAmplifier 		= ADC_INT_VREF_AMPLIFIER_Enable;
	ADC_Init(&sADC);

	ADCx_StructInit(&sADCx);
	sADCx.ADC_ClockSource 		= ADC_CLOCK_SOURCE_CPU;
	sADCx.ADC_SamplingMode 		= ADC_SAMPLING_MODE_CICLIC_CONV;
	sADCx.ADC_ChannelSwitching 	= ADC_CH_SWITCHING_Disable;
	sADCx.ADC_ChannelNumber 	= ADC_CH_ADC2;
	sADCx.ADC_Channels 			= 0;
	sADCx.ADC_LevelControl 		= ADC_LEVEL_CONTROL_Enable;
	sADCx.ADC_LowLevel 			= L_Level;
	sADCx.ADC_HighLevel 		= H_Level;
	sADCx.ADC_VRefSource 		= ADC_VREF_SOURCE_INTERNAL;
	sADCx.ADC_IntVRefSource 	= ADC_INT_VREF_SOURCE_INEXACT;
	sADCx.ADC_Prescaler 		= ADC_CLK_div_16;
	sADCx.ADC_DelayGo 			= 0xF;
	ADC1_Init(&sADCx);

	/* Enable ADC1 EOCIF and AWOIFEN interupts */
	ADC1_ITConfig((ADCx_IT_END_OF_CONVERSION | ADCx_IT_OUT_OF_RANGE), ENABLE);
	NVIC_EnableIRQ(ADC_IRQn);

	/* ADC1 enable */
	ADC1_Cmd(ENABLE);

	while (1);
}