//-----------------------------------------------------------------//
//	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;
    	*/

}
Example #2
0
//collection of ADC data and also real precise values from external voltmeter
//external data is entered with joystick movement as 0.000 number
static unsigned int ADCCollectDataCalib(unsigned int * ADCBuffer, double * ADCRealValues)
{
  unsigned int i;
  unsigned int multiplier;
  signed int Voltage;
  unsigned char cursorPos;
  unsigned char ArrowChar = '^';
  unsigned char JoyUp,JoyDown,JoyLeft,JoyRight,JoySel;

  //disable ADC interrupts
  ADC2_ITConfig(DISABLE);
  //enable ADC
  ADC2_Init(ADC2_CONVERSIONMODE_SINGLE, ADC2_CHANNEL_12, ADC2_PRESSEL_FCPU_D6, ADC2_EXTTRIG_TIM, DISABLE, ADC2_ALIGN_RIGHT, ADC2_SCHMITTTRIG_CHANNEL12, DISABLE);
  //clear end of conversion bit
  ADC2_ClearFlag();

  //for joystick control (init)
  //pull-ups on on all used buttons
  GPIOB->CR1 |= 0xF0;
  GPIOD->CR1 |= 0x80;
  Voltage = 0;
  multiplier = 1000;
  cursorPos = 2;
  //print values on LCD display
  LCD_Clear();
  LCD_SetCursorPos(LCD_LINE1, 1);
  LCD_PrintDec4(Voltage);
  LCD_PrintString(LCD_LINE1, DISABLE, ENABLE, "  =Real U");
  LCD_SetCursorPos(LCD_LINE2, cursorPos/2);
  LCD_PrintChar(ArrowChar); 
  
  //set external voltage - measure it with external voltmeter - enter voltmeter
  //value into LCD - measure ADC data - store real and ADC data to buffers
  //finish data collecting by KEY button on PCB board
  for(i=0; i<ADC_BUFFER_SIZE; i++)
  {
    LCD_SetCursorPos(LCD_LINE2, 0);
    LCD_PrintDec2(i);
    while(JOY_SEL);
    while(!JOY_SEL)
    {
      if(BUTTON_LOW)
        break;
      JoyUp   = JOY_UP;
      JoyDown = JOY_DOWN;
      JoyLeft = JOY_LEFT;
      JoyRight= JOY_RIGHT;
      JoySel  = JOY_SEL;
      if(JoyLeft)//increase multiplier
      {
        if (cursorPos!=2) 
        {
          multiplier *= 10;
          cursorPos  -= 1;
        }        
      }
      if(JoyRight)//decrease multiplier
      {
        if (cursorPos!=5) 
        {
          multiplier /= 10;
          cursorPos  += 1;
        }
      }
      if(JoyRight || JoyLeft)//repaint cursor position
      {
        LCD_PrintString(LCD_LINE2, DISABLE, DISABLE, "      ");//clear cursor
        LCD_SetCursorPos(LCD_LINE2, cursorPos/2);//set cursor position
        if(cursorPos & 1)
          LCD_PrintChar(' ');//print space
        LCD_PrintChar(ArrowChar);//print cursor
        while (JOY_RIGHT || JOY_LEFT);
        delay(1000l);
      }
      if(JoyUp)//increase real value
      {
        Voltage += multiplier;
      }
      if(JoyDown)//decrease real value
      {
        Voltage -= multiplier;
      }
      if(JoyUp || JoyDown)//repaint real value
      {
        LCD_PrintString(LCD_LINE1, DISABLE, DISABLE, "      ");//clear voltage
        if (Voltage<0)
        {
          LCD_SetCursorPos(LCD_LINE1, 1);
          LCD_PrintDec4(-Voltage);
          LCD_SetCursorPos(LCD_LINE1, 0);
          LCD_PrintChar('-');//print sign
        }
        else
        {
          LCD_SetCursorPos(LCD_LINE1, 1);
          LCD_PrintDec4(Voltage);
          LCD_SetCursorPos(LCD_LINE1, 0);
          LCD_PrintChar('+');//print sign
        }
        while (JOY_UP || JOY_DOWN);
        delay(1000l);
      }
      
      //start single conversion
      ADC2_StartConversion();
      while (!ADC2_GetFlagStatus());
      //clear end of conversion bit
      ADC2_ClearFlag();
      //collect ADC value and display it
      LCD_SetCursorPos(LCD_LINE2, 5);//set cursor position
      LCD_PrintDec4(ADC2_GetConversionValue());
    }
    
    //stop collecting if KEY buttom pressed
    if(BUTTON_LOW)
    {
      break;
    }
    //otherwise collect ADC data (+ real data) and store them
    else
    {
      //start single conversion
      ADC2_StartConversion();
      while (!ADC2_GetFlagStatus());
      //clear end of conversion bit
      ADC2_ClearFlag();
      //collect ADC value
      ADCBuffer[i] = ADC2_GetConversionValue();
      //collect real value
      ADCRealValues[i] = (double)Voltage/1000;
    }
  }
  return i;
}//ADCCollectDataCalib