Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.44 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    Demo/src/main.c
  4.   * @author  MCD Team
  5.   * @version V1.0.0
  6.   * @date    15/09/2010
  7.   * @brief   Main program body
  8.   ******************************************************************************
  9.   * @copy
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  19.   */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32F10x.h"
  23. #include "STM32vldiscovery.h"
  24.  
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* Private define ------------------------------------------------------------*/
  27. #define  LSE_FAIL_FLAG  0x80
  28. #define  LSE_PASS_FLAG  0x100
  29. /* Private macro -------------------------------------------------------------*/
  30. /* Private consts ------------------------------------------------------------*/
  31.  
  32. /* Private variables ---------------------------------------------------------*/
  33. u32 LSE_Delay = 0;
  34. u32 count = 0;
  35. u32 BlinkSpeed = 0;
  36. u32 KeyState = 0;
  37. static __IO uint32_t TimingDelay;
  38. /* Private function prototypes -----------------------------------------------*/
  39. void Delay(uint32_t nTime);
  40. void TimingDelay_Decrement(void);
  41.  
  42. /* Private functions ---------------------------------------------------------*/
  43.  
  44. /**
  45.   * @brief  Main program.
  46.   * @param  None
  47.   * @retval None
  48.   */
  49.  
  50. int main(void)
  51. {
  52.   /* Enable GPIOx Clock */
  53.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  54.  
  55.   /* Initialise LEDs LD3&LD4, both off */
  56.   STM32vldiscovery_LEDInit(LED3);
  57.   STM32vldiscovery_LEDInit(LED4);
  58.  
  59.   STM32vldiscovery_LEDOff(LED3);
  60.   STM32vldiscovery_LEDOff(LED4);
  61.  
  62.   /* Initialise USER Button */
  63.   STM32vldiscovery_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);
  64.  
  65.   /* Setup SysTick Timer for 1 msec interrupts  */
  66.   if (SysTick_Config(SystemCoreClock / 1000))
  67.   {
  68.     /* Capture error */
  69.     while (1);
  70.   }
  71.  
  72.   /* Enable access to the backup register => LSE can be enabled */
  73.   PWR_BackupAccessCmd(ENABLE);
  74.  
  75.   /* Enable LSE (Low Speed External Oscillation) */
  76.   RCC_LSEConfig(RCC_LSE_ON);
  77.  
  78.   /* Check the LSE Status */
  79.   while(1)
  80.   {
  81.     if(LSE_Delay < LSE_FAIL_FLAG)
  82.     {
  83.       /* check whether LSE is ready, with 4 seconds timeout */
  84.      Delay (120);
  85.       LSE_Delay += 0x10;
  86.       if(RCC_GetFlagStatus(RCC_FLAG_LSERDY) != RESET)
  87.       {
  88.         /* Set flag: LSE PASS */
  89.         LSE_Delay |= LSE_PASS_FLAG;
  90.         /* Turn Off Led4 */
  91.         STM32vldiscovery_LEDOff(LED4);
  92.         /* Disable LSE */
  93.         RCC_LSEConfig(RCC_LSE_OFF);
  94.         break;
  95.       }        
  96.     }
  97.    
  98.     /* LSE_FAIL_FLAG = 0x80 */  
  99.     else if(LSE_Delay >= LSE_FAIL_FLAG)
  100.     {          
  101.       if(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  102.       {
  103.         /* Set flag: LSE FAIL */
  104.         LSE_Delay |= LSE_FAIL_FLAG;
  105.         /* Turn On Led4 */
  106.         STM32vldiscovery_LEDOn(LED4);
  107.       }        
  108.       /* Disable LSE */
  109.       RCC_LSEConfig(RCC_LSE_OFF);
  110.       break;
  111.     }
  112.   }
  113.  
  114.   /* main while */
  115.   while(1)
  116.   {
  117.     if(0 == STM32vldiscovery_PBGetState(BUTTON_USER))
  118.       {
  119.         if(KeyState == 1)
  120.         {
  121.            if(0 == STM32vldiscovery_PBGetState(BUTTON_USER))
  122.           {
  123.             /* USER Button released */
  124.               KeyState = 0;
  125.             /* Turn Off LED4 */
  126.               STM32vldiscovery_LEDOff(LED4);
  127.           }      
  128.         }
  129.       }
  130.     else if(STM32vldiscovery_PBGetState(BUTTON_USER))
  131.       {
  132.         if(KeyState == 0)
  133.         {
  134.            if(STM32vldiscovery_PBGetState(BUTTON_USER))
  135.           {
  136.             /* USER Button released */
  137.               KeyState = 1;
  138.             /* Turn ON LED4 */
  139.             STM32vldiscovery_LEDOn(LED4);
  140.             Delay(1000);
  141.             /* Turn OFF LED4 */
  142.             STM32vldiscovery_LEDOff(LED4);
  143.             /* BlinkSpeed: 0 -> 1 -> 2, then re-cycle */    
  144.               BlinkSpeed ++ ;
  145.           }
  146.         }
  147.       }
  148.         count++;
  149.         Delay(100);
  150.       /* BlinkSpeed: 0 */
  151.       if(BlinkSpeed == 0)
  152.           {
  153.             STM32vldiscovery_LEDOn(LED4);
  154.             STM32vldiscovery_LEDOn(LED3);
  155.             Delay(100);
  156.             STM32vldiscovery_LEDOff(LED4);
  157.             STM32vldiscovery_LEDOff(LED3);
  158.         Delay(100);
  159.  
  160.          }
  161.            /* BlinkSpeed: 1 */
  162.            if(BlinkSpeed == 1)
  163.           {
  164.             STM32vldiscovery_LEDOn(LED4);
  165.             STM32vldiscovery_LEDOff(LED3);
  166.             Delay(100);
  167.             STM32vldiscovery_LEDOff(LED4);
  168.             STM32vldiscovery_LEDOn(LED3);
  169.         Delay(100);
  170.            
  171.           }  
  172.           /* BlinkSpeed: 2 */        
  173.           if(BlinkSpeed == 2)
  174.           {
  175.            
  176.             STM32vldiscovery_LEDOn(LED3);
  177.             STM32vldiscovery_LEDOn(LED4);    
  178.           }    
  179.           /* BlinkSpeed: 3 */
  180.           else if(BlinkSpeed == 3)
  181.         BlinkSpeed = 0;
  182.   }
  183. }
  184.  
  185. /**
  186.   * @brief  Inserts a delay time.
  187.   * @param  nTime: specifies the delay time length, in milliseconds.
  188.   * @retval None
  189.   */
  190. void Delay(uint32_t nTime)
  191. {
  192.   TimingDelay = nTime;
  193.  
  194.   while(TimingDelay != 0);
  195. }
  196.  
  197. /**
  198.   * @brief  Decrements the TimingDelay variable.
  199.   * @param  None
  200.   * @retval None
  201.   */
  202. void TimingDelay_Decrement(void)
  203. {
  204.   if (TimingDelay != 0x00)
  205.   {
  206.     TimingDelay--;
  207.   }
  208. }
  209.  
  210. #ifdef  USE_FULL_ASSERT
  211. /**
  212.   * @brief  Reports the name of the source file and the source line number
  213.   *         where the assert_param error has occurred.
  214.   * @param  file: pointer to the source file name
  215.   * @param  line: assert_param error line source number
  216.   * @retval None
  217.   */
  218. void assert_failed(uint8_t* file, uint32_t line)
  219. {
  220.   /* User can add his own implementation to report the file name and line number,
  221.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  222.  
  223.   /* Infinite loop */
  224.   while (1)
  225.   {
  226.   }
  227. }
  228. #endif
  229.  
  230. /**
  231.   * @}
  232.   */
  233.  
  234. /**
  235.   * @}
  236.   */
  237.  
  238. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement