s_m4rt

Untitled

Nov 30th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #include "stm32f0xx.h"
  2. #include "stm32f0xx_gpio.h"
  3. #include "stm32f0xx_rcc.h"
  4. #include "stm32f0xx_tim.h"
  5.  
  6. void Init(void) {
  7. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //Wlaczamy GPIOA dla USER buttona
  8. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); //Wlaczamy GPIOC dla LEDow
  9.  
  10. GPIO_InitTypeDef GP; //Tworzymy strukture GPIO
  11.  
  12. GP.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  13. GP.GPIO_Mode = GPIO_Mode_OUT; //LEDy jako wyjscie
  14. GP.GPIO_OType = GPIO_OType_PP;
  15. GP.GPIO_PuPd = GPIO_PuPd_NOPULL;
  16. GP.GPIO_Speed = GPIO_Speed_50MHz;
  17. GPIO_Init(GPIOC, &GP); //dodajemy do struktury
  18.  
  19. GP.GPIO_Pin = GPIO_Pin_0;
  20. GP.GPIO_Mode = GPIO_Mode_IN; //Button jako wejscie
  21. GP.GPIO_OType = GPIO_OType_PP;
  22. GP.GPIO_PuPd = GPIO_PuPd_NOPULL;
  23. GP.GPIO_Speed = GPIO_Speed_50MHz;
  24. GPIO_Init(GPIOA, &GP); //dodajemy do struktury
  25. }
  26.  
  27. int counter = 0;
  28. int mode = 0;
  29. int zmienna = 0;
  30. int zmienna2 = 0;
  31. int odstep = 0;
  32. void func() {
  33. counter++;
  34. if (counter >= 2000) {
  35. counter = 0;
  36. }
  37. if (mode == 0) {
  38. if (counter > 1000) {
  39. GPIO_SetBits(GPIOC, GPIO_Pin_6);
  40. } else {
  41. GPIO_ResetBits(GPIOC, GPIO_Pin_6);
  42. }
  43. } else {
  44. GPIO_ResetBits(GPIOC, GPIO_Pin_6);
  45. }
  46. if (mode == 1) {
  47. if (counter > 1000) {
  48. GPIO_SetBits(GPIOC, GPIO_Pin_7);
  49. } else {
  50. GPIO_ResetBits(GPIOC, GPIO_Pin_7);
  51. }
  52. } else {
  53. GPIO_ResetBits(GPIOC, GPIO_Pin_7);
  54. }
  55. if (mode == 2) {
  56. if (counter > 1000) {
  57. GPIO_SetBits(GPIOC, GPIO_Pin_8);
  58. } else {
  59. GPIO_ResetBits(GPIOC, GPIO_Pin_8);
  60. }
  61. } else {
  62. GPIO_ResetBits(GPIOC, GPIO_Pin_8);
  63. }
  64. if (mode == 3) {
  65. if (counter > 1000) {
  66. GPIO_SetBits(GPIOC, GPIO_Pin_9);
  67. } else {
  68. GPIO_ResetBits(GPIOC, GPIO_Pin_9);
  69. }
  70. } else {
  71. GPIO_ResetBits(GPIOC, GPIO_Pin_9);
  72. }
  73. if (mode == 4) {
  74. if (counter > 1000) {
  75. GPIO_SetBits(GPIOC, GPIO_Pin_9|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);
  76. } else {
  77. GPIO_ResetBits(GPIOC, GPIO_Pin_9|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);
  78. }
  79. }/* else {
  80. GPIO_ResetBits(GPIOC, GPIO_Pin_9|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);
  81. }*/
  82. }
  83. void button() {
  84. if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1 && zmienna < 2000) {
  85. zmienna++;
  86. odstep=0;
  87. }
  88. else {
  89. if (zmienna > 1 && zmienna < 500) {
  90. mode++;
  91. zmienna=0;
  92. zmienna2=0;
  93. }
  94. if (zmienna >= 1000) {
  95. if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1 && odstep == 0)
  96. ;
  97. if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0)
  98. odstep++;
  99. if (odstep >= 1000){
  100. zmienna=0;
  101. zmienna2=0;
  102. odstep=0;
  103. }
  104. if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1 && odstep >= 1) {
  105. zmienna2++;
  106. } else {
  107. if (zmienna2 > 50 && zmienna2 < 500) {
  108. mode=2;
  109. zmienna=0;
  110. zmienna2=0;
  111. odstep=0;
  112. }
  113. if (zmienna2 > 1000) {
  114. mode=0;
  115. zmienna=0;
  116. zmienna2=0;
  117. odstep=0;
  118. }}
  119. } else {
  120. zmienna=0;
  121. odstep=0;
  122. }
  123. }
  124. if (mode > 4) {
  125. mode = 0;
  126. }
  127. }
  128. int main(void) {
  129. Init();
  130. SysTick_Config(SystemCoreClock / 10000);
  131. while (1) {
  132.  
  133. }
  134. }
  135. void SysTick_Handler(void) //wektor przerwania
  136. {
  137. func();
  138. button();
  139. }
Add Comment
Please, Sign In to add comment