示例#1
0
/**
  * @brief   Main program.
  * @param  None
  * @retval None
  */
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_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32l1xx.c file
     */

  uint32_t index = 0;
  
  /* Configure all GPIO pins in Analog mode for lowsest consumption */
  GPIO_Config();

  /* ADC configuration: Channel 18 or 31 (PB12 or PF10) is used, End Of Conversion (EOC) interrupt is enabled */
  ADC_Config();

#ifdef USE_STM32L152_EVAL
  /* LCD GLASS Configuration: LSI as LCD clock source */
  LCD_Glass_Config();
  /* Initialize the TFT-LCD */
  STM32L152_LCD_Init();
#elif defined USE_STM32L152D_EVAL 
  /* Initialize the TFT-LCD */
  STM32L152D_LCD_Init();
#endif 
  
  /* Clear the TFT-LCD */
  LCD_Clear(LCD_COLOR_WHITE);
  
  while(1)
  {
    if (State == STATE_OVER_THRESHOLD) /* Input voltage is over the threshold */
    {
      /* Indicator LED: MCU in RUN mode */
      STM_EVAL_LEDOff(LED1);

      /* Disable COMP IRQ */
      NVIC_InitStructure.NVIC_IRQChannel = COMP_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
      NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
      NVIC_Init(&NVIC_InitStructure);
      /* Enable ADC1 IRQ */
      NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);

      /* COMP clock disable */
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, DISABLE);

      /* Restore MCU configuration */
      RestoreConfiguration();

      /* Enable ADC1 */
      ADC_Cmd(ADC1, ENABLE);
      /* Start ADC1 Software Conversion */
      ADC_SoftwareStartConv(ADC1);
      /* Wait for ADC to be ready */
      while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS));  

      while(State == STATE_OVER_THRESHOLD)
      {
        
        /* Display measured value on Glass LCD */
        DisplayVoltage(ADCVal);
        
        /* Display measured value on LCD */
        for (index = 0; index < 20; index++)
        {
          LCD_DisplayChar(LCD_LINE_3, (319 - (16 * index)), VoltageDisplay[index]);
        }
        /* Check if the measured value is below the threshold VREFINT: 1.22 V */
        if (ADCVal <= 0x000005EA)
        {
          State = STATE_UNDER_THRESHOLD;
        }
      }
    }
    else /* Input voltage is under the threshold */
    {
      /* LED1 ON: MCU in STOP mode */
      STM_EVAL_LEDInit(LED1);
      STM_EVAL_LEDOn(LED1);

      /* Disable ADC1 IRQ */
      NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
      NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
      NVIC_Init(&NVIC_InitStructure);
      /* Disable ADC1 */
      ADC_Cmd(ADC1, DISABLE);

      /* Configure COMP2 with interrupt enabled */
      COMP_Config();

      /* Check COMP2 output level before entering STOP mode */
      if (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_Low)
      {
        /* Disable LSI oscillator before entering STOP mode */
        RCC_LSICmd(DISABLE);

        /* Enter STOP mode with regulator in low power */
        PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
      }
    }
  }
}
//*****************************************************************************
//
// The main loop for the user interface.
//
//*****************************************************************************
int
main(void)
{
    unsigned long ulPanel;

    //
    // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is
    // a workaround to allow the PLL to operate reliably.
    //
    if(REVISION_IS_A2)
    {
        SysCtlLDOSet(SYSCTL_LDO_2_75V);
    }

    //
    // Set the clocking to run at 50MHz from the PLL.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_8MHZ);

    //
    // Set the priority of the interrupts.
    //
    IntPrioritySet(INT_CAN0, 0x00);
    IntPrioritySet(FAULT_SYSTICK, 0x20);

    //
    // Configure SysTick to generate an interrupt every millisecond.
    //
    SysTickPeriodSet(SysCtlClockGet() / 1000);
    SysTickIntEnable();
    SysTickEnable();

    //
    // Initialize the push button driver.
    //
    ButtonsInit();

    //
    // Initialize the CAN communication channel.
    //
    CANCommInit();

    //
    // Initialize the UART used to perform a "firmware update".
    //
    UpdateUARTInit();

    //
    // Initialize the display.
    //
    RIT128x96x4Init(3500000);

    //
    // Add the screen-clearing widget to the widget tree.  As the first widget
    // in the tree, this will always be drawn first, resulting in a blank
    // screen before anything else is drawn.
    //
    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sBackground);

    //
    // Display the splash screen.
    //
    DisplaySplash();

    //
    // Set the CAN device ID to one.
    //
    CANSetID(1);

    //
    // The "Voltage Control Mode" panel should be displayed first.
    //
    ulPanel = PANEL_VOLTAGE;

    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Determine which panel to display.
        //
        switch(ulPanel)
        {
            //
            // The "Voltage Control Mode" panel should be displayed.
            //
            case PANEL_VOLTAGE:
            {
                ulPanel = DisplayVoltage();
                break;
            }

            //
            // The "VComp Control Mode" panel should be displayed.
            //
            case PANEL_VCOMP:
            {
                ulPanel = DisplayVComp();
                break;
            }

            //
            // The "Current Control Mode" panel should be displayed.
            //
            case PANEL_CURRENT:
            {
                ulPanel = DisplayCurrent();
                break;
            }

            //
            // The "Speed Control Mode" panel should be displayed.
            //
            case PANEL_SPEED:
            {
                ulPanel = DisplaySpeed();
                break;
            }

            //
            // The "Position Control Mode" panel should be displayed.
            //
            case PANEL_POSITION:
            {
                ulPanel = DisplayPosition();
                break;
            }

            //
            // The "Configuration" panel should be displayed.
            //
            case PANEL_CONFIGURATION:
            {
                ulPanel = DisplayConfig();
                break;
            }

            //
            // The "Device List" panel should be displayed.
            //
            case PANEL_DEV_LIST:
            {
                ulPanel = DisplayDevList();
                break;
            }

            //
            // The "Firmware Update" panel should be displayed.
            //
            case PANEL_UPDATE:
            {
                ulPanel = DisplayUpdate();
                break;
            }

            //
            // The "Help" panel should be displayed.
            //
            case PANEL_HELP:
            {
                ulPanel = DisplayHelp();
                break;
            }

            //
            // The "About" panel should be displayed.
            //
            case PANEL_ABOUT:
            {
                ulPanel = DisplayAbout();
                break;
            }
        }
    }
}