Untitled


SUBMITTED BY: Guest

DATE: Feb. 11, 2014, 1:15 p.m.

FORMAT: Text only

SIZE: 5.4 kB

HITS: 1383

  1. #include "stm32f10x.h"
  2. //#include <stdio.h>
  3. /*
  4. * Standalone STM32F1 led blink sample.
  5. *
  6. * This program demonstrates how to blink a led with 1Hz, using a
  7. * continuous loop and SysTick delays.
  8. *
  9. * The external clock frequency is specified as a preprocessor definition
  10. * passed to the compiler via a command line option (see the 'C/C++ General' ->
  11. * 'Paths and Symbols' -> the 'Symbols' tab, if you want to change it).
  12. * The value selected during project creation was HSE_VALUE=8000000.
  13. *
  14. * Note: The default clock settings take the user defined HSE_VALUE and try
  15. * to reach the maximum possible system clock. For the default 8MHz input
  16. * the result is guaranteed, but for other values it might not be possible,
  17. * so please adjust the PLL settings in libs/CMSIS/src/system_stm32f10x.c
  18. *
  19. * The build does not use startup files, and it does not even use
  20. * any standard library function.
  21. *
  22. * If the application requires special initialisation code present
  23. * in some other libraries (for example librdimon.a, for semihosting),
  24. * define USE_STARTUP_FILES and uncheck the corresponding option in the
  25. * linker configuration.
  26. *
  27. */
  28. // ----------------------------------------------------------------------------
  29. static void
  30. Delay(__IO uint32_t nTime);
  31. static void
  32. TimingDelay_Decrement(void);
  33. void
  34. SysTick_Handler(void);
  35. /* ----- SysTick definitions ----------------------------------------------- */
  36. #define SYSTICK_FREQUENCY_HZ 1000
  37. /* ----- LED definitions --------------------------------------------------- */
  38. /* Olimex STM32-H103 LED definitions */
  39. /* Adjust them for your own board. */
  40. #define BLINK_PORT GPIOC
  41. #define BLINK_PIN 12
  42. #define BLINK_RCC_BIT RCC_APB2Periph_GPIOC
  43. #define BLINK_TICKS SYSTICK_FREQUENCY_HZ/2
  44. // +++
  45. #define DEBUG_LED_PIN_D10 GPIO_Pin_4
  46. #define DEBUG_LED_PIN_D11 GPIO_Pin_5
  47. #define DEBUG_LED_PIN_D12 GPIO_Pin_6
  48. #define BLINK_PORT GPIOA // REDEF
  49. #define LEFT_STEP 1
  50. typedef enum {
  51. RIGHT = 1, LEFT = -1
  52. } WAVE_DIRECTION;
  53. // ----------------------------------------------------------------------------
  54. void move_wave(int* wdix, WAVE_DIRECTION* wstep, int num_steps);
  55. int main() {
  56. /*
  57. * At this stage the microcontroller clock setting is already configured,
  58. * this is done through SystemInit() function which is called from startup
  59. * file (startup_cm.c) before to branch to application main.
  60. * To reconfigure the default setting of SystemInit() function, refer to
  61. * system_stm32f10x.c file
  62. */
  63. /* Use SysTick as reference for the timer */
  64. SysTick_Config(SystemCoreClock / SYSTICK_FREQUENCY_HZ);
  65. /* GPIO Periph clock enable */
  66. RCC_APB2PeriphClockCmd(BLINK_RCC_BIT, ENABLE);
  67. GPIO_InitTypeDef GPIO_InitStructure;
  68. /* Configure pin in output push/pull mode */
  69. // GPIO_InitStructure.GPIO_Pin = (1 << BLINK_PIN);
  70. GPIO_InitStructure.GPIO_Pin = DEBUG_LED_PIN_D10 | DEBUG_LED_PIN_D11
  71. | DEBUG_LED_PIN_D12;
  72. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  73. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  74. GPIO_Init(BLINK_PORT, &GPIO_InitStructure);
  75. uint16_t wave_codes[] = {
  76. DEBUG_LED_PIN_D12,
  77. DEBUG_LED_PIN_D11,
  78. DEBUG_LED_PIN_D10,
  79. };
  80. const int NUM_WSTEPS = sizeof(wave_codes) / sizeof(wave_codes[0]);
  81. int seconds = 0;
  82. int widx = 0;
  83. WAVE_DIRECTION wstep = RIGHT;
  84. /* Infinite loop */
  85. while (1) {
  86. /* Assume the LED is active low */
  87. /* Turn on led by setting the pin low */
  88. GPIO_ResetBits(BLINK_PORT, wave_codes[widx]);
  89. widx += 1;
  90. widx %= NUM_WSTEPS;
  91. //move_wave(&widx, &wstep, NUM_WSTEPS);
  92. Delay(BLINK_TICKS);
  93. /* Turn off led by setting the pin high */
  94. GPIO_SetBits(BLINK_PORT, wave_codes[widx]);
  95. //move_wave(&widx, &wstep, NUM_WSTEPS);
  96. Delay(BLINK_TICKS);
  97. widx += 1;
  98. widx %= NUM_WSTEPS;
  99. ++seconds;
  100. }
  101. }
  102. void move_wave(int* widx, WAVE_DIRECTION* wstep, int num_steps) {
  103. *widx += *wstep;
  104. if (*widx >= num_steps) {
  105. *widx = 0;
  106. *wstep = RIGHT;
  107. }
  108. if (*widx < 0) {
  109. *widx = 0;
  110. *wstep = RIGHT;
  111. }
  112. }
  113. // ----------------------------------------------------------------------------
  114. static __IO uint32_t uwTimingDelay;
  115. /**
  116. * @brief Inserts a delay time.
  117. * @param nTime: specifies the delay time length, in SysTick ticks.
  118. * @retval None
  119. */
  120. void Delay(__IO uint32_t nTime) {
  121. uwTimingDelay = nTime;
  122. while (uwTimingDelay != 0)
  123. ;
  124. }
  125. /**
  126. * @brief Decrements the TimingDelay variable.
  127. * @param None
  128. * @retval None
  129. */
  130. void TimingDelay_Decrement(void) {
  131. if (uwTimingDelay != 0x00) {
  132. uwTimingDelay--;
  133. }
  134. }
  135. // ----------------------------------------------------------------------------
  136. /**
  137. * @brief This function is the SysTick Handler.
  138. * @param None
  139. * @retval None
  140. */
  141. void SysTick_Handler(void) {
  142. TimingDelay_Decrement();
  143. }
  144. // ----------------------------------------------------------------------------

comments powered by Disqus