Advertisement
Guest User

Super program kurwa mać

a guest
Mar 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include "stm32f4_discovery.h"
  3. #include <stdio.h>
  4.  
  5. static unsigned int us=0, dst=0;
  6. double distance=0;
  7. int main(void)
  8. {
  9. void tim_start(void);
  10. void tim_stop();
  11. void delay(int l);
  12. void odczyt();
  13.  
  14. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  15. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  16. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  17.  
  18. //Trigger PD4, jako wyjscie
  19. GPIO_InitTypeDef Trigger1;
  20. Trigger1.GPIO_Pin = GPIO_Pin_4;
  21. Trigger1.GPIO_Mode = GPIO_Mode_OUT;
  22. Trigger1.GPIO_OType = GPIO_OType_PP;
  23. Trigger1.GPIO_Speed = GPIO_Speed_100MHz;
  24. Trigger1.GPIO_PuPd = GPIO_PuPd_NOPULL;
  25. GPIO_Init(GPIOD, &Trigger1);
  26.  
  27. GPIO_InitTypeDef Echo1;
  28. Echo1.GPIO_Pin = GPIO_Pin_6;
  29. Echo1.GPIO_Mode = GPIO_Mode_IN;
  30. Echo1.GPIO_OType = GPIO_OType_PP;
  31. Echo1.GPIO_Speed = GPIO_Speed_100MHz;
  32. Echo1.GPIO_PuPd = GPIO_PuPd_UP;
  33. GPIO_Init(GPIOD, &Echo1);
  34.  
  35. GPIO_InitTypeDef GPIO_InitStructure;
  36. /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  37. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
  38. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  39. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  40. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  41. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  42. GPIO_Init(GPIOD, &GPIO_InitStructure);
  43.  
  44. TIM_TimeBaseInitTypeDef Timer2;
  45. /* Przeladowanie nastepuje co 10us */
  46. Timer2.TIM_Period =9999;
  47. Timer2.TIM_Prescaler = 83;
  48. Timer2.TIM_ClockDivision = TIM_CKD_DIV1;
  49. Timer2.TIM_CounterMode = TIM_CounterMode_Up;
  50. Timer2.TIM_RepetitionCounter = 0;
  51. TIM_TimeBaseInit(TIM2, &Timer2);
  52.  
  53. TIM_TimeBaseInitTypeDef Timer3;
  54. /* Przeladowanie nastepuje co 1us */
  55. Timer3.TIM_Period =7-1;
  56. Timer3.TIM_Prescaler = 12-1;
  57. Timer3.TIM_ClockDivision = TIM_CKD_DIV1;
  58. Timer3.TIM_CounterMode = TIM_CounterMode_Up;
  59. Timer3.TIM_RepetitionCounter = 0;
  60. TIM_TimeBaseInit(TIM3, &Timer3);
  61.  
  62. //Przerwanie dla timera2
  63. NVIC_InitTypeDef NVIC_InitStructure;
  64. // numer przerwania
  65. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  66. // priorytet główny
  67. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
  68. // subpriorytet
  69. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  70. // uruchom dany kanał
  71. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  72. // zapisz wypełnioną strukturę do rejestrów
  73. NVIC_Init(&NVIC_InitStructure);
  74. // wyczyszczenie przerwania od timera 2 (wystąpiło przy konfiguracji timera)
  75. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  76. // zezwolenie na przerwania od przepełnienia dla timera 2
  77. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  78.  
  79. double zm1=0, zm2=58;
  80. for(;;)
  81. {
  82. odczyt();
  83. delay(1000000);
  84. zm1=us;
  85. distance = zm1/zm2; // odleglosc w centymetrach
  86. }
  87. }
  88. void delay(int l)// 1 to 1us --- funkcja trochę nieprecyzyjna
  89. {
  90. int i;
  91. TIM_Cmd(TIM3, ENABLE);
  92. for(i=0;i<l;i++)
  93. {
  94. while(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update))
  95. {
  96. TIM_ClearFlag(TIM3, TIM_FLAG_Update);
  97. }
  98. }
  99. TIM_Cmd(TIM3, DISABLE);
  100. TIM3->CNT=0;
  101.  
  102. }
  103. void odczyt()
  104. {
  105. int time;
  106. float dystans = 0;
  107. //Sygnal niski na trigger
  108. GPIO_ResetBits(GPIOD, GPIO_Pin_4);
  109. //Opóznienie 2us
  110. delay(10);
  111. //Sygnal wysoki na trigger
  112. GPIO_SetBits(GPIOD, GPIO_Pin_4);
  113. //Opóznienie 10 us
  114. delay(30);
  115. //Sygnal niski na trigger
  116. GPIO_ResetBits(GPIOD, GPIO_Pin_4);
  117. while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == Bit_RESET);
  118. //Po takiej inicjalizacji nastepuje dokonanie pomiarow
  119. //Troche czasu na odpowiedz
  120. TIM_Cmd(TIM2, ENABLE);
  121. //Petla while czeka do momentu kiedy sygnal bedzie 0
  122. while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6) == Bit_SET);
  123. TIM_Cmd(TIM2,DISABLE);
  124. //Pobranie czasu trwania stanu wysokiego
  125. time = TIM2->CNT;
  126. us=time;
  127. TIM2->CNT=0;
  128. //Obliczenie odleglosci
  129. //tutaj trzeba obliczyc dystans tak aby nie zawiesic procesora
  130. //Zwrócenie odleglosci
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement