int main(void)
{

    /*!< At this stage the microcontroller clock setting is already configured,
          this is done through SystemInit() function which is called from startup
          file (startup_stm32l1xx_md.s) before to branch to application main.
          To reconfigure the default setting of SystemInit() function, refer to
          system_stm32l1xx.c file
        */

    /* Configure Clocks for Application need */
    RCC_Configuration();

    /* Configure RTC Clocks */
    RTC_Configuration();

    /* Set internal voltage regulator to 1.8V */
    PWR_VoltageScalingConfig(PWR_VoltageScaling_Range1);

    /* Wait Until the Voltage Regulator is ready */
    while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET) ;

    /* Enable debug features in low power modes (Sleep, STOP and STANDBY) */
#ifdef  DEBUG_SWD_PIN
    DBGMCU_Config(DBGMCU_SLEEP | DBGMCU_STOP | DBGMCU_STANDBY, ENABLE);
#endif

    /* Configure SysTick IRQ and SysTick Timer to generate interrupts */
    RCC_GetClocksFreq(&RCC_Clocks);
    SysTick_Config(RCC_Clocks.HCLK_Frequency / 500);

    /* Init I/O ports */
    Init_GPIOs();

    /* Initializes the LCD glass */
    LCD_GLASS_Configure_GPIO();
    LCD_GLASS_Init();

    /* Display Welcome message */

    LCD_GLASS_ScrollSentence("   ** TEMPERATURE SENSOR EXAMPLE **    ",1,SCROLL_SPEED);

    /* Disable SysTick IRQ and SysTick Timer */
    SysTick->CTRL  &= ~ ( SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk );

    /* Test user or factory temperature sensor calibration value */
    if ( testFactoryCalibData() == SUCCESS ) getFactoryTSCalibData(&calibdata);
    else if ( testUserCalibData() == SUCCESS ) calibdata = *USER_CALIB_DATA;
    else {
        /* User calibration or factory calibration TS data are not available */
        calibdata.TS_CAL_1 = DEFAULT_COLD_VAL;
        calibdata.TS_CAL_2 = DEFAULT_HOT_VAL;
        writeCalibData(&calibdata);
        calibdata = *USER_CALIB_DATA;
    }

    /* Configure Wakeup from sleep using RTC event*/
    configureWakeup();

    /* Configure direct memory access for ADC usage*/
    configureDMA();

    /* Configure ADC for temperature sensor value conversion */
    configureADC_Temp();


    while(1) {

        /* Re-enable DMA and ADC conf and start Temperature Data acquisition */
        acquireTemperatureData();

        /* Stay in SLEEP mode untill the data are acquired by ADC */
        __WFI();

        /* for DEBUG purpose uncomment the following line and comment the __WFI call to do not enter STOP mode */
        // while (!flag_ADCDMA_TransferComplete);

        /* Disable ADC, DMA and clock*/
        powerDownADC_Temper();

        /* Process mesured Temperature data - calculate average temperature value in °C */
        processTempData();

        if (flag_UserButton == TRUE) {
            clearUserButtonFlag();
            if (CurrentlyDisplayed == Display_TemperatureDegC)
                CurrentlyDisplayed = Display_ADCval;
            else
                CurrentlyDisplayed = Display_TemperatureDegC;
        }

        if (CurrentlyDisplayed == Display_TemperatureDegC) {
            /* print average temperature value in °C  */
            sprintf(strDisp, "%d °C", temperature_C );
        } else {
            /* print result of ADC conversion  */
            sprintf(strDisp, "> %d", tempAVG );
        }

        LCD_GLASS_Clear();
        LCD_GLASS_DisplayString( (unsigned char *) strDisp );

        /* Enable RTC Wakeup */
        RTC_WakeUpCmd(ENABLE);

        /* Clear WakeUp flag */
        PWR_ClearFlag(PWR_FLAG_WU);

        /* Enter in wait for interrupt stop mode*/
        PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);

        RCC_Configuration();  // reinitialize clock

        /* After Wake up : Disable Wake up from RTC*/
        RTC_WakeUpCmd(DISABLE);
    }

}
Esempio n. 2
0
int main(void)
{
	/*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32l1xx_md.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32l1xx.c file
*/
  /* Configure Clocks for Application need */
  RCC_Configuration();
  uint8_t rxBuf[4];
  uint8_t txBuf[16];

  uint8_t chByte[2];
  uint8_t chMainStep=MAIN_STEP_IDLE;
  int16_t* iCurentAdcValue=(int16_t*)chByte; // теперь тут будет лежать последнее измеренное число
  /* Configure RTC Clocks */
  RTC_Configuration();

  /* Enable debug features in low power modes (Sleep, STOP and STANDBY) */
#ifdef  DEBUG_SWD_PIN
  DBGMCU_Config(DBGMCU_SLEEP | DBGMCU_STOP | DBGMCU_STANDBY, ENABLE);
#endif
  
  /* Configure SysTick IRQ and SysTick Timer to generate interrupts */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 500);

  /* Init I/O ports */
  conf_analog_all_GPIOS();   /* configure all GPIOs as analog input */

  InitButton();
  MesureCurInit();

  LCD_GLASS_Init();/* Initializes the LCD glass */

//  RCC_AHBPeriphClockCmd(LD_GPIO_PORT_CLK , ENABLE);
  //RCC_AHBPeriphClockCmd(LD_GPIO_PORT_CLK 		| P_GATE1_GPIO_PORT_CLK |
///		  	  	  	  P_GATE2_GPIO_PORT_CLK 	| N_GATE1_GPIO_PORT_CLK |
//		  	  	  	  N_GATE2_GPIO_PORT_CLK		, ENABLE);

  Delay.Init();
  DbgUART.UART_Init(USART3);
  i2cMgr.SetDbgUART(&DbgUART);
  i2cMgr.Init();
  calipers.Init();
  calipers.Callback=CallBackCalipers;



  // Setup i2cCmd  to write config data to ADC
  txBuf[0]=0x88; //Bits 3 and 2 control the ADS1100Тs data rate "1 0"= 16SPS
  I2C_command.Address=0x48;
  I2C_command.DataToRead.Length = 0;
  I2C_command.DataToRead.Buf=rxBuf;
  I2C_command.DataToWrite.Buf = txBuf;
  I2C_command.DataToWrite.Length = 1;
  I2C_command.Callback=CallBackI2C;
  i2cMgr.AddCmd(I2C_command);

  // Setup i2cCmd  to read data from ADC
  I2C_command.Address=0x48;
  I2C_command.DataToRead.Length = 4;
  I2C_command.DataToRead.Buf=rxBuf;
  I2C_command.DataToWrite.Buf = txBuf;
  I2C_command.DataToWrite.Length = 0;
  I2C_command.Callback=CallBackI2C;

  /* Display Welcome message */ 
 // LCD_GLASS_ScrollSentence((uint8_t*)"      CELESTIA ONLINE ",1,SCROLL_SPEED);

  Delay.Reset(&TimeDelay);
  Delay.Reset(&DbgDelay);
  MesureCurStop();
  char chI2cCounter=0;
  MesureCurUpward();
  chflagI2C=1;
  while(1){
	  i2cMgr.Task();
	  calipers.Task();
	  switch (chMainStep)
	  {
	  case MAIN_STEP_IDLE:
		  if (calipers.GetState()==SPI_END_RX)  //при выходе из холостго режима пропускаем первый отсчет со штангена, чтобы ток в датчике
		  {
			  chMainStep=MAIN_STEP_WAIT_CALIPERS_START;
			  DbgUART.SendPrintF("OUT IDLE  \n");
			  MesureCurUpward();					// включаем ток
			  chflagI2C=0;
		  }
		  break;
	  case MAIN_STEP_WAIT_CALIPERS_START:
		  if (calipers.GetState()==SPI_IDLE) // давно небыло посылок с штангена,
		  {
			  DbgUART.SendPrintF("IN IDLE  \n");
			  chMainStep=MAIN_STEP_IDLE;   // переходим в холостой режим
			  MesureCurStop();				//отключаем ток в датчике.
		  }
		  if (calipers.GetState()==SPI_START_RX)  // начало приема данных со штангена
		  {
			  //DbgUART.SendPrintF("IN I2C  \n");
			  chMainStep=MAIN_STEP_WAIT_I2C;
			  i2cMgr.AddCmd(I2C_command);
		  }
		  break;
	  case MAIN_STEP_WAIT_I2C:
		  if (chflagI2C==1) // закончилась работа с I2C
		  {
			    chMainStep=MAIN_STEP_WAIT_CALIPERS_END;
			    MesureCurToggle();				// переключаем направление тока
		  }
		  break;
	  case MAIN_STEP_WAIT_CALIPERS_END:
		  if (calipers.GetState()==SPI_END_RX) // закончилcz прием данных о штангена
		  {
			  	chByte[0]=rxBuf[1];
			  	chByte[1]=rxBuf[0];
			  	DbgUART.SendPrintF("ACD_VAL=%d  \n",*iCurentAdcValue);
			    LCD_GLASS_Clear();
			    tiny_sprintf(strDisp, " %d ", calipers.iSpiDataRx );
			    LCD_GLASS_DisplayString( (unsigned char *) strDisp );
			    DbgUART.SendPrintF("CALIPERS_VAL=%d  \n",calipers.iSpiDataRx);
			    DbgUART.SendPrintF("OUT I2CE  \n");
			  chMainStep=MAIN_STEP_WAIT_CALIPERS_START;
		  }
		  break;
	  } //switch

	 // if (Delay.Elapsed(&DbgDelay,100))  DbgUART.SendPrintF("i2c flag=%d  main_state=%d \n ",chflagI2C, chMainStep) ;
	/*  if (chflagI2C==1) // закончилась работа с I2C
	  {
		  MesureCurToggle();
		  chflagI2C=0;
	  }
	  if (Delay.Elapsed(&DbgDelay,250))
	  {
		  if (chI2cCounter<=10)
		  {
			  //MesureCurToggle();
			  chByte[0]=rxBuf[1];
		  	  chByte[1]=rxBuf[0];
			  DbgUART.SendPrintF("ACD_VAL=%d  \n",*iCurentAdcValue);
			  rxBuf[0]=0;
			  rxBuf[1]=0;
			  chI2cCounter++;
			  //chflagI2C=0;
			  i2cMgr.AddCmd(I2C_command);
		  }
	  }
*/

    if (flag_UserButton == TRUE)
    {
       clearUserButtonFlag();

       chI2cCounter=0;

    }
  }

}