void  Init_GPIOs (void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    conf_analog_all_GPIOS();   /* configure all GPIOs as analog input */

    /* Enable GPIOs clock */
    RCC_AHBPeriphClockCmd(LD_GPIO_PORT_CLK | USERBUTTON_GPIO_CLK, ENABLE);

    /* USER button and WakeUP button init: GPIO set in input interrupt active mode */

    /* Configure User Button pin as input */
    GPIO_InitStructure.GPIO_Pin = USERBUTTON_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_Init(USERBUTTON_GPIO_PORT, &GPIO_InitStructure);

    /* Connect Button EXTI Line to Button GPIO Pin */
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);

    /* Configure User Button and IDD_WakeUP EXTI line */
    EXTI_InitStructure.EXTI_Line = EXTI_Line0 ;  // PA0 for User button AND IDD_WakeUP
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    /* Enable and set User Button and IDD_WakeUP EXTI Interrupt to the lowest priority */
    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn ;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    /* Configure the GPIO_LED pins  LD3 & LD4*/
    GPIO_InitStructure.GPIO_Pin = LD_GREEN_GPIO_PIN | LD_BLUE_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(LD_GPIO_PORT, &GPIO_InitStructure);
    GPIO_LOW(LD_GPIO_PORT, LD_GREEN_GPIO_PIN);
    GPIO_LOW(LD_GPIO_PORT, LD_BLUE_GPIO_PIN);

    /* Disable all GPIOs clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB |
                          RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD |
                          RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOH, DISABLE);

}
예제 #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;

    }
  }

}