Advertisement
Guest User

timer

a guest
Nov 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. #include "Lib_Pedagobot_TP4.h"
  2. #include <stm32f4xx_hal.h>
  3. #include <stdio.h>
  4. #include <Board_Buttons.h>
  5. #include <Board_LED.h>
  6.  
  7.  
  8. typedef enum
  9. {
  10.  
  11. ATTENTE_DEPART,
  12. DEMARRAGE_DROIT,
  13. TOURNER_DROITE,
  14. CHOC,
  15. ARRET,
  16.  
  17. } type_etat;
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. // Prototypes
  25. void tempo_logicielle(unsigned long long int n_iter);
  26. void init_TIMER6(void);
  27. void TIMER6(short int x,type_etat etat);
  28. void init_E_S(void);
  29. unsigned char getCollisionValues();
  30. void test (unsigned char *pchoc);
  31.  
  32.  
  33. int main()
  34. {
  35. unsigned char GO,fin_tempo=0,choc=0 ;
  36. init_PedagoBot_TP4();
  37. Buttons_Initialize();
  38. LED_Initialize();
  39. load_Processor(); //Surcharge du processeur
  40. init_TIMER6();
  41. init_E_S();
  42. printf("Initialization done !\n");
  43. type_etat etat = ATTENTE_DEPART;
  44.  
  45. while(1){
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. switch (etat) //transition
  53. {
  54. case ATTENTE_DEPART :
  55.  
  56. if (Buttons_GetState())
  57. {
  58. etat = DEMARRAGE_DROIT;
  59.  
  60. }
  61. break;
  62.  
  63. case DEMARRAGE_DROIT :
  64. if (fin_tempo==1)
  65. {
  66. fin_tempo=0;
  67. etat = TOURNER_DROITE;
  68.  
  69. }
  70. if (choc==1)
  71. {
  72. etat = CHOC;
  73.  
  74. }
  75.  
  76. break;
  77.  
  78. case TOURNER_DROITE :
  79. if (fin_tempo==1)
  80. {
  81. fin_tempo=0;
  82. etat = DEMARRAGE_DROIT;
  83.  
  84. }
  85. if (choc==1)
  86. {
  87. etat = CHOC;
  88.  
  89. }
  90.  
  91.  
  92. break;
  93.  
  94. case CHOC :
  95.  
  96.  
  97. etat = ARRET;
  98.  
  99. break;
  100.  
  101. case ARRET :
  102.  
  103. etat = ATTENTE_DEPART;
  104.  
  105. break;
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. switch (etat) //action
  116. {
  117. case ATTENTE_DEPART :
  118.  
  119.  
  120.  
  121. break;
  122.  
  123. case DEMARRAGE_DROIT :
  124. goStraight();
  125. TIMER6(6400,etat);
  126.  
  127. fin_tempo=1;
  128.  
  129.  
  130.  
  131.  
  132. break;
  133.  
  134. case TOURNER_DROITE :
  135.  
  136. rotateRight();
  137. TIMER6(1640,etat);
  138. fin_tempo=1;
  139.  
  140. break;
  141.  
  142. case CHOC :
  143.  
  144. TIMER6(1000,etat);
  145.  
  146. break;
  147.  
  148. case ARRET :
  149.  
  150. stopRobot();
  151. break;
  152.  
  153.  
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160. /*if (Buttons_GetState())
  161. {
  162. GO=1;
  163. }
  164. if(GO==1)
  165. {
  166. goStraight();
  167. TIMER6(6400);
  168. rotateRight();
  169. TIMER6(1600);
  170. goStraight();
  171. TIMER6(6400);
  172. rotateRight();
  173. TIMER6(1600);
  174. goStraight();
  175. TIMER6(6400);
  176. rotateRight();
  177. TIMER6(1600);
  178. goStraight();
  179. TIMER6(6400);
  180. rotateRight();
  181. TIMER6(1600);
  182.  
  183.  
  184. //}
  185.  
  186.  
  187. if(GO==1)
  188. {
  189. goStraight();
  190. tempo_logicielle(150100000);
  191.  
  192.  
  193. rotateRight();
  194. tempo_logicielle(36600000);
  195.  
  196.  
  197.  
  198. goStraight();
  199. tempo_logicielle(150100000);
  200.  
  201.  
  202. rotateRight();
  203. tempo_logicielle(36600000);
  204.  
  205.  
  206. goStraight();
  207. tempo_logicielle(150100000);
  208.  
  209. rotateRight();
  210. tempo_logicielle(36600000);
  211.  
  212. goStraight();
  213. tempo_logicielle(150100000);
  214.  
  215. rotateRight();
  216. tempo_logicielle(36600000);
  217.  
  218. } */
  219.  
  220. }
  221.  
  222.  
  223.  
  224. }
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. void tempo_logicielle(unsigned long long int n_iter){
  236. while(n_iter>0){
  237. n_iter--;
  238. }
  239. }
  240.  
  241. void init_TIMER6(void)
  242. {
  243. RCC->APB1ENR |=(1<<4); //Init horloge TIMER 6
  244. TIM6->CR1 = (1<<3); //Mode Auto reload preload enable
  245. TIM6->PSC = 8999; //Prescaleur de 900
  246.  
  247. TIM6->EGR |= (1<<0) ; //Enable clock timer 6
  248. }
  249.  
  250. void TIMER6(short int x,type_etat etat)
  251. {
  252. unsigned char Cpt_col,col=0,Rappel;
  253.  
  254. if(x==6400)
  255. {
  256. TIM6->ARR = 63999;
  257. }
  258. if(x==1640)
  259. {
  260. TIM6->ARR = 15999;
  261. }
  262. if(x==1000)
  263. {
  264. TIM6->ARR = 5000;
  265. }
  266.  
  267.  
  268.  
  269. TIM6->CR1 |= 1; //activation compteur
  270. while((TIM6->CR1&1)==1)
  271. {
  272. Cpt_col=getCollisionValues();
  273. if ((Cpt_col==0x00)||(Cpt_col==0x01)||(Cpt_col==0x10))
  274. {
  275. col=1;
  276. stopRobot();
  277. Rappel=TIM6->CNT;
  278. }
  279. }
  280.  
  281. TIM6->CNT=Rappel;
  282. if((etat==DEMARRAGE_DROIT)&&(col==1))
  283. {
  284. TIM6->CR1 |= 1; //activation compteur
  285. goStraight();
  286. while((TIM6->CR1&1)==1);
  287.  
  288. }
  289. if((etat==TOURNER_DROITE)&&(col==1))
  290. {
  291. TIM6->CR1 |= 1; //activation compteur
  292. rotateRight();
  293. while((TIM6->CR1&1)==1);
  294. }
  295.  
  296. }
  297.  
  298. void init_E_S(void)
  299. {
  300.  
  301. //INIT CAPTEUR COLISION 1 et 2
  302. RCC->AHB1ENR |= (1<<0)|(1<<7); //CLOCK POUR A et H
  303. GPIOA->MODER &= ~((1<<8)|(1<<9)); //CAPTEUR COLISION 1 en ENTREE sur GPIOA
  304. GPIOA->PUPDR |= (1<<8); // pull up
  305. GPIOA->PUPDR &= ~(1<<9);
  306. GPIOH->MODER &= ~((1<<0)|(1<<1)); //CAPTEUR COLISION 2 en ENTREE sur GPIOH
  307. GPIOH->PUPDR |= (1<<2);
  308. GPIOH->PUPDR &= ~(1<<3);
  309.  
  310. }
  311.  
  312.  
  313. unsigned char getCollisionValues(void){
  314. unsigned short idr_A , idr_H ;
  315. unsigned short Col_D , Col_G;
  316. unsigned char Sortie_col=0;
  317.  
  318.  
  319. idr_A=GPIOA->IDR;
  320. idr_H=GPIOH->IDR;
  321.  
  322.  
  323. Col_D =idr_A & 0x0010; // pas appuyé : IDRx=1 appuyé IDRx=0
  324. Col_G =idr_H & 0x0002;
  325.  
  326. if (Col_D!=0)
  327. {
  328. Sortie_col |= (1<<0); // pas appuyé
  329. }
  330. else
  331. {
  332. Sortie_col &= ~(1<<0); // appuyé
  333. }
  334.  
  335. if (Col_G!=0)
  336. {
  337. Sortie_col |= (1<<4);
  338. }
  339. else
  340. {
  341. Sortie_col &= ~(1<<4);
  342. }
  343.  
  344. return(Sortie_col);
  345.  
  346.  
  347.  
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement