Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include "stm32f4xx_gpio.h"
  3. #include "stm32f4xx_rcc.h"
  4.  
  5. #define LED1 GPIOD, GPIO_Pin_12
  6. #define LED2 GPIOD, GPIO_Pin_13
  7. #define LED3 GPIOD, GPIO_Pin_14
  8. #define LED4 GPIOD, GPIO_Pin_15
  9. #define button GPIOA, GPIO_Pin_0
  10.  
  11. GPIO_InitTypeDef GPIO_InitStruct;
  12.  
  13. static void GPIO_setup(void)
  14. {
  15. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  16. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12; // we want to configure all LED GPIO pins
  17. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; // we want the pins to be an output
  18. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this sets the GPIO modules clock speed
  19. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain)
  20. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; // this sets the pullup / pulldown resistors to be inactive
  21. GPIO_Init(GPIOD, &GPIO_InitStruct);
  22. }
  23.  
  24. static void button_setup(void)
  25. {RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  26. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; // we want to configure PA0
  27. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; // we want it to be an input
  28. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//this sets the GPIO modules clock speed
  29. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain)
  30. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN; // this enables the pulldown resistor --> we want to detect a high level
  31. GPIO_Init(GPIOA, &GPIO_InitStruct);
  32. }
  33.  
  34. void clearBits()
  35. {
  36. GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15);
  37. }
  38.  
  39. void lightLed(int number)
  40. {
  41. clearBits();
  42. switch(number)
  43. {
  44. case 0:
  45. {
  46. clearBits();
  47. GPIO_SetBits(GPIOD, GPIO_Pin_12);
  48. }
  49.  
  50. case 1:
  51. {
  52. clearBits();
  53. GPIO_SetBits(GPIOD, GPIO_Pin_13);
  54. }
  55.  
  56. case 2:
  57. {
  58. clearBits();
  59. GPIO_SetBits(GPIOD, GPIO_Pin_14);
  60. }
  61.  
  62. case 3:
  63. {
  64. clearBits();
  65. GPIO_SetBits(GPIOD, GPIO_Pin_15);
  66. }
  67.  
  68. case 4:
  69. {
  70. }
  71. }
  72. }
  73.  
  74. void lock()
  75. {
  76. while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0 == 1))
  77. {
  78. }
  79. }
  80.  
  81. void Delay(__IO uint32_t nCount)
  82. {
  83. while(nCount--)
  84. {
  85. }
  86. }
  87.  
  88. void task_1()
  89. {
  90. if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0 == 1))
  91. {
  92. GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
  93. }
  94. else
  95. {
  96. GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15);
  97. }
  98. }
  99.  
  100. void task_2()
  101. {
  102. int a = 0;
  103.  
  104. while(1)
  105. {
  106. if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0 == 1))
  107. {
  108. a++;
  109. lock();
  110. Delay(0xFFFF);
  111. }
  112.  
  113. if(a == 0)
  114. {
  115. GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
  116. }
  117.  
  118. if(a == 1)
  119. {
  120. GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
  121. }
  122.  
  123. if(a == 2) a = 0;
  124. }
  125.  
  126. }
  127.  
  128. void task_3()
  129. {
  130. int a = 0;
  131. while(1)
  132. {
  133. if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0 == 1))
  134. {
  135. a++;
  136. lock();
  137. Delay(0xFFFF);
  138. }
  139.  
  140. if(a == 0)
  141. {
  142. GPIO_SetBits(GPIOD, GPIO_Pin_12);
  143. GPIO_ResetBits(GPIOD, GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15 );
  144. }
  145.  
  146. if(a == 1)
  147. {
  148. GPIO_SetBits(GPIOD, GPIO_Pin_13);
  149. GPIO_ResetBits(GPIOD, GPIO_Pin_12 |GPIO_Pin_14 |GPIO_Pin_15 );
  150. }
  151.  
  152. if(a == 2)
  153. {
  154. GPIO_SetBits(GPIOD, GPIO_Pin_14);
  155. GPIO_ResetBits(GPIOD, GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_15 );
  156. }
  157.  
  158. if(a == 3)
  159. {
  160. GPIO_SetBits(GPIOD, GPIO_Pin_15);
  161. GPIO_ResetBits(GPIOD, GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14 );
  162. }
  163. if(a == 4) a = 0;
  164. //Delay(0x9FFFF);
  165. }
  166. }
  167.  
  168. int main(void)
  169. {
  170. GPIO_setup();
  171. button_setup();
  172. GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15 );
  173.  
  174. task_2();
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement