Advertisement
Domy131097

MRS-ZAD2

Aug 20th, 2018
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None
  1. /* Includes */
  2. #include <stddef.h>
  3. #include "stm32f10x.h"
  4. #include "stm32f10x_rcc.h"
  5. #include "stm32f10x_gpio.h"
  6. #include "stm32f10x_tim.h"
  7.  
  8. volatile uint32_t millis;
  9.  
  10. void initGPIO(void);
  11. void initTimer(void);
  12. void SysTick_Handler(void);
  13. void wait(uint32_t time);
  14. void led(int32_t br);
  15.  
  16. int main(void) {
  17. //Varijabla u svrhu brojača
  18. volatile int32_t br=0;
  19. //Varijable u svrhu kontrole stanja tipkala
  20. volatile uint32_t con1=0,con2=0;
  21.  
  22. //inizijalizacija
  23. initGPIO();
  24. initTimer();
  25.  
  26. SysTick_Config(SystemCoreClock / 1000); // 1 ms
  27. //ciklicko ponavljanje
  28. led(br);
  29. while(1){
  30. con1 = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5);
  31. //Čitanje ulaza s tipkala spojena na pin B6
  32. con2 = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6);
  33. //Provjeravanje da li je tipkalo na pinu B5 stisnuto
  34. if(con2==1 && con1==0){
  35. //Povećavanje brojača
  36. br++;
  37. //Provjera da li je brojač veći od devet ako je vrati ga na nulu
  38. if(br>9) br=0;
  39. //Provjeravanje da li je tipkalo na pinu B6 stisnuto
  40. }else if(con1==1 && con2==0){
  41. //Smanjivanje brojača
  42. br--;
  43. //Provjera da li je brojač manji od 0 ako je vrati ga na devet
  44. if(br<0) br=9;
  45. }
  46. //Pozivanja funkcije led da bi se pokazalo trenutno stanje brojača
  47. led(br);
  48. wait(1000);
  49. }
  50. }
  51.  
  52. void initGPIO(void) {
  53. //Deklariranje strukture
  54. GPIO_InitTypeDef GPIO_InitStructure;
  55. //Omogućuje pinove GPIOA i GPIOB
  56. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  57. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  58. //Inicijalizacija izlaznih pinova
  59. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_12 | GPIO_Pin_11 | GPIO_Pin_10;
  60. //Postavljanje pinova od A0 do A3 te A10 do A12 da budu izlazni
  61. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  62. //Postavljanje brzine promjene logičke '0' u logičku '1'
  63. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  64. //Definiranje da se radi o pinovima s oznakom A
  65. GPIO_Init(GPIOA, &GPIO_InitStructure);
  66. //Inicijalizacija ulaznih pinova
  67. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  68. //Postavljanje pinova B5 i B6 da budu ulazni
  69. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  70. //Postavljanje brzine promjene logičke '0' u logičku '1'
  71. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  72. //Definiranje da se radi o pinovima s oznakom B
  73. GPIO_Init(GPIOB, &GPIO_InitStructure);
  74. }
  75.  
  76. void initTimer(void) {
  77. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
  78.  
  79. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  80.  
  81. //inicijalizacija timera
  82. TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
  83. TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
  84. TIM_TimeBaseInitStruct.TIM_Period = 1000 - 1;
  85. TIM_TimeBaseInitStruct.TIM_Prescaler = SystemCoreClock / 100000 - 1; //100kHz
  86. TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //null
  87. TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0; //null
  88. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
  89.  
  90. TIM_Cmd(TIM2, ENABLE);
  91. }
  92.  
  93. void wait(uint32_t time) {
  94. millis = time;
  95. while(millis > 0);
  96. }
  97.  
  98. void SysTick_Handler(void) {
  99. if(millis > 0) {
  100. millis--;
  101. }
  102. }
  103.  
  104. void led(int32_t br){
  105. switch(br){
  106. case 0:
  107. GPIOA->BRR = GPIO_Pin_0;// LED PIN_0 ON
  108. GPIOA->BRR = GPIO_Pin_1;// LED PIN_1 ON
  109. GPIOA->BRR = GPIO_Pin_2;// LED PIN_2 ON
  110. GPIOA->BRR = GPIO_Pin_3;// LED PIN_3 ON
  111. GPIOA->BSRR = GPIO_Pin_10;// LED PIN_10 OFF
  112. GPIOA->BRR = GPIO_Pin_11;// LED PIN_11 ON
  113. GPIOA->BRR = GPIO_Pin_12;// LED PIN_12 ON
  114. break;
  115. case 1:
  116. GPIOA->BRR = GPIO_Pin_0;// LED PIN_0 ON
  117. GPIOA->BSRR = GPIO_Pin_1;// LED PIN_1 ON
  118. GPIOA->BSRR = GPIO_Pin_2;// LED PIN_2 OFF
  119. GPIOA->BRR = GPIO_Pin_3;// LED PIN_3 OFF
  120. GPIOA->BSRR = GPIO_Pin_10;// LED PIN_10 OFF
  121. GPIOA->BSRR = GPIO_Pin_11;// LED PIN_11 OFF
  122. GPIOA->BSRR = GPIO_Pin_12;// LED PIN_12 OFF
  123. break;
  124. case 2:
  125. GPIOA->BRR = GPIO_Pin_0;// LED PIN_0 ON
  126. GPIOA->BRR = GPIO_Pin_1;// LED PIN_1 OFF
  127. GPIOA->BRR = GPIO_Pin_2;// LED PIN_2 ON
  128. GPIOA->BSRR = GPIO_Pin_3;// LED PIN_3 ON
  129. GPIOA->BRR = GPIO_Pin_10;// LED PIN_10 ON
  130. GPIOA->BRR = GPIO_Pin_11;// LED PIN_11 ON
  131. GPIOA->BSRR = GPIO_Pin_12;// LED PIN_12 OFF
  132. break;
  133. case 3:
  134. GPIOA->BRR = GPIO_Pin_0;// LED PIN_0 ON
  135. GPIOA->BSRR = GPIO_Pin_1;// LED PIN_1 ON
  136. GPIOA->BRR = GPIO_Pin_2;// LED PIN_2 ON
  137. GPIOA->BRR = GPIO_Pin_3;// LED PIN_3 OFF
  138. GPIOA->BRR = GPIO_Pin_10;// LED PIN_10 ON
  139. GPIOA->BRR = GPIO_Pin_11;// LED PIN_11 ON
  140. GPIOA->BSRR = GPIO_Pin_12;// LED PIN_12 OFF
  141. break;
  142. case 4:
  143. GPIOA->BRR = GPIO_Pin_0;// LED PIN_0 ON
  144. GPIOA->BSRR = GPIO_Pin_1;// LED PIN_1 ON
  145. GPIOA->BSRR = GPIO_Pin_2;// LED PIN_2 OFF
  146. GPIOA->BRR = GPIO_Pin_3;// LED PIN_3 OFF
  147. GPIOA->BRR = GPIO_Pin_10;// LED PIN_10 ON
  148. GPIOA->BSRR = GPIO_Pin_11;// LED PIN_11 OFF
  149. GPIOA->BRR = GPIO_Pin_12;// LED PIN_12 ON
  150. break;
  151. case 5:
  152. GPIOA->BSRR = GPIO_Pin_0;// LED PIN_0 OFF
  153. GPIOA->BSRR = GPIO_Pin_1;// LED PIN_1 ON
  154. GPIOA->BRR = GPIO_Pin_2;// LED PIN_2 ON
  155. GPIOA->BRR = GPIO_Pin_3;// LED PIN_3 OFF
  156. GPIOA->BRR = GPIO_Pin_10;// LED PIN_10 ON
  157. GPIOA->BRR = GPIO_Pin_11;// LED PIN_11 ON
  158. GPIOA->BRR = GPIO_Pin_12;// LED PIN_12 ON
  159. break;
  160. case 6:
  161. GPIOA->BSRR = GPIO_Pin_0;// LED PIN_0 OFF
  162. GPIOA->BRR = GPIO_Pin_1;// LED PIN_1 ON
  163. GPIOA->BRR = GPIO_Pin_2;// LED PIN_2 ON
  164. GPIOA->BRR = GPIO_Pin_3;// LED PIN_3 ON
  165. GPIOA->BRR = GPIO_Pin_10;// LED PIN_10 ON
  166. GPIOA->BRR = GPIO_Pin_11;// LED PIN_11 ON
  167. GPIOA->BRR = GPIO_Pin_12;// LED PIN_12 ON
  168. break;
  169. case 7:
  170. GPIOA->BRR = GPIO_Pin_0;// LED PIN_0 ON
  171. GPIOA->BSRR = GPIO_Pin_1;// LED PIN_1 ON
  172. GPIOA->BSRR = GPIO_Pin_2;// LED PIN_2 OFF
  173. GPIOA->BRR = GPIO_Pin_3;// LED PIN_3 OFF
  174. GPIOA->BSRR = GPIO_Pin_10;// LED PIN_10 OFF
  175. GPIOA->BRR = GPIO_Pin_11;// LED PIN_11 ON
  176. GPIOA->BRR = GPIO_Pin_12;// LED PIN_12 ON
  177. break;
  178. case 8:
  179. GPIOA->BRR = GPIO_Pin_0;// LED PIN_0 ON
  180. GPIOA->BRR = GPIO_Pin_1;// LED PIN_1 ON
  181. GPIOA->BRR = GPIO_Pin_2;// LED PIN_2 ON
  182. GPIOA->BRR = GPIO_Pin_3;// LED PIN_3 ON
  183. GPIOA->BRR = GPIO_Pin_10;// LED PIN_10 ON
  184. GPIOA->BRR = GPIO_Pin_11;// LED PIN_11 ON
  185. GPIOA->BRR = GPIO_Pin_12;// LED PIN_12 ON
  186. break;
  187. case 9:
  188. GPIOA->BRR = GPIO_Pin_0;// LED PIN_0 ON
  189. GPIOA->BSRR = GPIO_Pin_1;// LED PIN_1 ON
  190. GPIOA->BRR = GPIO_Pin_2;// LED PIN_2 ON
  191. GPIOA->BRR = GPIO_Pin_3;// LED PIN_3 OFF
  192. GPIOA->BRR = GPIO_Pin_10;// LED PIN_10 ON
  193. GPIOA->BRR = GPIO_Pin_11;// LED PIN_11 ON
  194. GPIOA->BRR = GPIO_Pin_12;// LED PIN_12 ON
  195. break;
  196. }
  197. }
Advertisement
RAW Paste Data Copied
Advertisement