Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.87 KB | None | 0 0
  1. #include <intrinsics.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include "driverlib/driverlib.h"
  5. #include "hal_LCD.h"
  6. #include "main.h"
  7.  
  8. /* Definitions */
  9. #define GREEN_LED 0
  10. #define YELLOW_LED 1
  11. #define ORANGE_LED 2
  12. #define RED_LED 3
  13.  
  14. #define HIGH_BEEP 900
  15. #define LOW_BEEP 200
  16. #define BEEP_DUR 500
  17.  
  18. #define DATA_BOUND 3
  19.  
  20. int F_FRONT = -1;
  21. int F_REAR = -1;
  22. int B_FRONT = -1;
  23. int B_REAR = -1;
  24. unsigned int back_sensor_data[3];
  25. unsigned int front_sensor_data[3];
  26.  
  27. /* Function Declarations */
  28. void send_pulse_front_trig();
  29.  
  30. void light_LED(int LED);
  31. void print_to_LCD(char *str);
  32. void delay_ms(unsigned int ms);
  33. void delay_us(unsigned int us);
  34. void beep(unsigned int note, unsigned int duration);
  35. void low_beep_warning();
  36. void high_beep_warning();
  37.  
  38.  
  39.  
  40. void sensor_data_init()
  41. {
  42. int i;
  43. for (i = 0; i < DATA_BOUND; i++)
  44. {
  45. back_sensor_data[i] = 0;
  46. front_sensor_data[i] = 0;
  47. }
  48. }
  49.  
  50. //...
  51.  
  52. /* Globals */
  53. char ADCState = 0; //Busy state of the ADC
  54. int16_t ADCResult = 0; //Storage for the ADC conversion result
  55.  
  56. //...
  57.  
  58. void main(void)
  59. {
  60. __disable_interrupt(); //Turns off interrupts during init
  61. WDT_A_hold(WDT_A_BASE); //Stop watchdog timer
  62.  
  63. Init_GPIO(); //Sets all pins to output low as a default
  64. Init_LCD(); //Sets up the LaunchPad LCD display
  65.  
  66. PMM_unlockLPM5(); //Disable the GPIO power-on default high-impedance mode to activate previously configured port settings
  67. __enable_interrupt();
  68.  
  69. //GPIO_enableInterrupt(GPIO_PORT_P2, GPIO_PIN5);
  70. //GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN5, GPIO_LOW_TO_HIGH_TRANSITION);
  71. //GPIO_clearInterrupt(GPIO_PORT_P2, GPIO_PIN5);
  72.  
  73. for(;;) //Infinite loop
  74. {
  75. // if (GPIO_getInputPinValue(SW1_PORT, SW1_PIN) == 0)
  76. // {
  77. // GPIO_setOutputHighOnPin(LED1_PORT, LED1_PIN);
  78. // GPIO_setOutputHighOnPin(LED2_PORT, LED2_PIN);
  79. // }
  80. // if (GPIO_getInputPinValue(SW2_PORT, SW2_PIN) == 0)
  81. // {
  82. // GPIO_setOutputLowOnPin(LED1_PORT, LED1_PIN);
  83. // GPIO_setOutputLowOnPin(LED2_PORT, LED2_PIN);
  84. // }
  85.  
  86. unsigned int front_sensor_val = 0;
  87. unsigned int back_sensor_val = 0;
  88. // char sensor_data[6];
  89.  
  90. send_pulse_front_trig();
  91. while(GPIO_getInputPinValue(GPIO_PORT_P2, GPIO_PIN7) == 0);
  92. while(GPIO_getInputPinValue(GPIO_PORT_P2, GPIO_PIN7) != 0)
  93. {
  94. front_sensor_val += 1;
  95. __delay_cycles(16);
  96. }
  97.  
  98. if (front_sensor_val <= 10)
  99. {
  100. high_beep_warning();
  101. }
  102. else if (front_sensor_val <= 20)
  103. {
  104. low_beep_warning();
  105. }
  106.  
  107. send_pulse_back_trig();
  108. while(GPIO_getInputPinValue(GPIO_PORT_P2, GPIO_PIN5) == 0);
  109. while(GPIO_getInputPinValue(GPIO_PORT_P2, GPIO_PIN5) != 0)
  110. {
  111. back_sensor_val += 1;
  112. __delay_cycles(16);
  113. }
  114. // unsigned int i;
  115. // int pos[6] = {pos1, pos2, pos3, pos4, pos5, pos6};
  116. // unsigned int dummy = back_sensor_val;
  117. // for (i = 0; i < 6; i++)
  118. // {
  119. // sensor_data[i] = dummy % 10;
  120. // dummy /= 10;
  121. // showChar('0' + (sensor_data[i]), pos[5 - i]);
  122. // }
  123.  
  124. if (back_sensor_val <= 10)
  125. {
  126. light_LED(RED_LED);
  127. }
  128. else if (back_sensor_val <= 20)
  129. {
  130. light_LED(ORANGE_LED);
  131. }
  132. else if (back_sensor_val <= 30)
  133. {
  134. light_LED(YELLOW_LED);
  135. }
  136. else
  137. {
  138. light_LED(GREEN_LED);
  139. }
  140. }
  141. }
  142.  
  143. void send_pulse_front_trig()
  144. {
  145. GPIO_setOutputHighOnPin(GPIO_PORT_P8, GPIO_PIN1); //Send a pulse
  146. __delay_cycles(16);
  147. GPIO_setOutputLowOnPin(GPIO_PORT_P8, GPIO_PIN1);
  148. }
  149.  
  150. void send_pulse_back_trig()
  151. {
  152. GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN1); //Send a pulse
  153. __delay_cycles(16);
  154. GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN1);
  155. }
  156.  
  157. void light_LED(int LED)
  158. {
  159. GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN2);
  160. GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN3);
  161. GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN3);
  162. GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
  163.  
  164. switch (LED)
  165. {
  166. case GREEN_LED:
  167. GPIO_setOutputHighOnPin(GPIO_PORT_P5, GPIO_PIN2);
  168. break;
  169. case YELLOW_LED:
  170. GPIO_setOutputHighOnPin(GPIO_PORT_P5, GPIO_PIN3);
  171. break;
  172. case ORANGE_LED:
  173. GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN3);
  174. break;
  175. case RED_LED:
  176. GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
  177. break;
  178. }
  179. return;
  180. }
  181.  
  182. void print_to_LCD(char *str)
  183. {
  184. int len = strlen(str);
  185. int pos[6] = {pos1, pos2, pos3, pos4, pos5, pos6};
  186.  
  187. if (len > 6) //String is too long for LCD display
  188. {
  189. displayScrollText(str);
  190. return;
  191. }
  192.  
  193. int i;
  194. for (i = 0; i < len; i++)
  195. {
  196. showChar(str[i], pos[i]);
  197. }
  198. }
  199.  
  200. void delay_ms(unsigned int ms)
  201. {
  202. unsigned int i;
  203. for (i = 0; i<= ms; i++)
  204. __delay_cycles(500); //Suspend execution for 500 cycles
  205. }
  206.  
  207. void delay_us(unsigned int us)
  208. {
  209. unsigned int i;
  210. for (i = 0; i<= us/2; i++)
  211. __delay_cycles(1); //Delay for one cycle, used for generating square wave
  212. }
  213.  
  214. //This function generates the square wave that makes the piezo speaker sound at a determinated frequency.
  215. void beep(unsigned int note, unsigned int duration)
  216. {
  217. long delay = (long)(10000/note); //Determine the period for each note
  218. long time = (long)((duration*100)/(delay*2)); //Time the note is held for
  219. int i;
  220. for (i=0;i<time;i++)
  221. {
  222. GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN5); //Start pulse
  223. delay_us(delay); //For a semiperiod
  224. GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN5); //Stop pulse
  225. delay_us(delay); //For the other semiperiod
  226. }
  227. delay_ms(20); //Add a little delay to separate the single notes
  228. }
  229.  
  230. void low_beep_warning()
  231. {
  232. int i;
  233. for (i = 0; i < 2; i++)
  234. {
  235. delay_ms(300);
  236. beep(LOW_BEEP, BEEP_DUR);
  237. }
  238. }
  239.  
  240. void high_beep_warning()
  241. {
  242. int i;
  243. for (i = 0; i < 4; i++)
  244. {
  245. delay_ms(300);
  246. beep(HIGH_BEEP, BEEP_DUR/5);
  247. }
  248. }
  249.  
  250. //#pragma vector=PORT2_VECTOR
  251. //__interrupt void Port_2(void)
  252. //{
  253. //
  254. //}
  255.  
  256. void Init_GPIO(void)
  257. {
  258. //Set all GPIO pins to output low to prevent floating input and reduce power consumption
  259. GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  260. GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  261. GPIO_setOutputLowOnPin(GPIO_PORT_P3, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  262. GPIO_setOutputLowOnPin(GPIO_PORT_P4, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  263. GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  264. GPIO_setOutputLowOnPin(GPIO_PORT_P6, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  265. GPIO_setOutputLowOnPin(GPIO_PORT_P7, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  266. GPIO_setOutputLowOnPin(GPIO_PORT_P8, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  267.  
  268. //GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  269. //GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  270. //GPIO_setAsOutputPin(GPIO_PORT_P3, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  271. //GPIO_setAsOutputPin(GPIO_PORT_P4, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  272. //GPIO_setAsOutputPin(GPIO_PORT_P5, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  273. //GPIO_setAsOutputPin(GPIO_PORT_P6, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  274. //GPIO_setAsOutputPin(GPIO_PORT_P7, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  275. //GPIO_setAsOutputPin(GPIO_PORT_P8, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2|GPIO_PIN3|GPIO_PIN4|GPIO_PIN5|GPIO_PIN6|GPIO_PIN7);
  276.  
  277. /* Outputs */
  278. GPIO_setAsOutputPin(LED1_PORT, LED1_PIN); //P1.0 - internal LED 1
  279. GPIO_setAsOutputPin(LED2_PORT, LED2_PIN); //P4.0 - internal LED 2
  280.  
  281. GPIO_setAsOutputPin(GPIO_PORT_P5, GPIO_PIN2); //P5.2 - green LED
  282. GPIO_setAsOutputPin(GPIO_PORT_P5, GPIO_PIN3); //P5.3 - yellow LED
  283. GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN3); //P1.3 - orange LED
  284. GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4); //P1.4 - red LED
  285. GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN5); //P1.5 - buzzer
  286.  
  287. GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN1); //P1.1 - back ultrasonic trig pin
  288. GPIO_setAsOutputPin(GPIO_PORT_P8, GPIO_PIN1); //P8.1 - front ultrasonic trig pin
  289.  
  290. /* Inputs */
  291. GPIO_setAsInputPinWithPullUpResistor(SW1_PORT, SW1_PIN); //P1.2 - built-on pb 1
  292. GPIO_setAsInputPinWithPullUpResistor(SW2_PORT, SW2_PIN); //P2.6 - built-on pb 2
  293.  
  294. GPIO_setAsInputPin(GPIO_PORT_P2, GPIO_PIN5); //P2.5 - back ultrasonic echo pin
  295. GPIO_setAsInputPin(GPIO_PORT_P2, GPIO_PIN7); //P2.7 - front ultrasonic echo pin
  296.  
  297. //Set LaunchPad switches as inputs - they are active low, meaning '1' until pressed
  298. GPIO_setAsInputPinWithPullUpResistor(SW1_PORT, SW1_PIN);
  299. GPIO_setAsInputPinWithPullUpResistor(SW2_PORT, SW2_PIN);
  300.  
  301. //Set LED1 and LED2 as outputs
  302. GPIO_setAsOutputPin(LED1_PORT, LED1_PIN); //Comment if using UART
  303. GPIO_setAsOutputPin(LED2_PORT, LED2_PIN);
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement