Advertisement
Cjurenic

Untitled

Jun 24th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.96 KB | None | 0 0
  1. #include "movement.h"
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include "timer.h"
  5. #include "lcd.h"
  6. #include <inc/tm4c123gh6pm.h>
  7. #include "driverlib/interrupt.h"
  8. #include <string.h>
  9. #include "uart.h"
  10. #include "adc.h"
  11. #include <math.h>
  12. #include "ping.h"
  13.  
  14. unsigned int startTime = 0;
  15. unsigned int endTime = 0;
  16.  
  17. unsigned int clockCycles = 0;
  18.  
  19. char isHighVoltage = 0;
  20.  
  21. void ping_init(void) {
  22.  
  23.     // Port B is used for timer 3B
  24.     SYSCTL_RCGCGPIO_R |= 0b000010;
  25.  
  26.     GPIO_PORTB_DEN_R |= 0b00001000;
  27.  
  28.     GPIO_PORTB_DIR_R |= 0b00001000;
  29.  
  30.     // Set the pulse to low voltage
  31.     GPIO_PORTB_DATA_R &= 0b11110111;
  32.  
  33.     SYSCTL_RCGCTIMER_R |= 0b001000;
  34.  
  35.     TIMER3_CTL_R &= ~0x0100;
  36.  
  37.     TIMER3_CFG_R |= 0b100;
  38.     TIMER3_CFG_R &= 0xFFFFFFFC;
  39.  
  40.     // Count up, edge-time, capture mode
  41.     TIMER3_TBMR_R = 0b10111;
  42.  
  43.     TIMER3_CTL_R |= 0b110000000000;
  44.  
  45.     TIMER3_TBPR_R |= 0x00FF;
  46.  
  47.     TIMER3_TBILR_R |= 0xFFFF;
  48.  
  49.     TIMER3_ICR_R |= 0b010000000000;
  50.  
  51.     // Enable capture event interrupts
  52.     TIMER3_IMR_R |= 0b010000000000;
  53.  
  54.     NVIC_PRI9_R |= 0x00000020;
  55.  
  56.     NVIC_EN1_R |= 0x0010;
  57.  
  58.     IntRegister(52, TIMER3B_handler);
  59.  
  60.     TIMER3_CTL_R |= 0x0100;
  61.  
  62.     IntMasterEnable();
  63. }
  64.  
  65. void TIMER3B_handler(void) {
  66.  
  67.     if (TIMER3_MIS_R & 0b010000000000) {
  68.  
  69.         if (!isHighVoltage) {
  70.  
  71.             startTime = TIMER3_TBR_R;
  72.         }
  73.         else {
  74.  
  75.             endTime = TIMER3_TBR_R;
  76.  
  77.             if (endTime < startTime) {
  78.  
  79.                 endTime += 0x00FFFFFF;
  80.             }
  81.  
  82.             clockCycles = endTime - startTime;
  83.         }
  84.  
  85.         // Clear the interrupt
  86.         TIMER3_ICR_R |= 0b010000000000;
  87.  
  88.         // Flip the value of the boolean for the interrupt handler.
  89.         isHighVoltage = ~isHighVoltage;
  90.     }
  91. }
  92. void ping_read(void) {
  93.  
  94.     GPIO_PORTB_PCTL_R &= 0x00000000;
  95.  
  96.     // Disable the alternate function
  97.     GPIO_PORTB_AFSEL_R &= 0b11110111;
  98.  
  99.     // Set PB3 as an output
  100.     GPIO_PORTB_DIR_R |= 0b00001000;
  101.  
  102.     // Create a starting pulse
  103.     GPIO_PORTB_DATA_R |= 0b00001000;
  104.  
  105.     timer_waitMicros(5);
  106.  
  107.     GPIO_PORTB_DATA_R &= 0b11110111;
  108.  
  109.     // Enable the alternate function
  110.     GPIO_PORTB_AFSEL_R |= 0b00001000;
  111.  
  112.     // Use function T3CCP1
  113.     // See table 23-5 for functions
  114.     GPIO_PORTB_PCTL_R |= 0x00007000;
  115.  
  116.     // Set PB3 as an input
  117.     GPIO_PORTB_DIR_R &= 0b11110111;
  118. }
  119.  
  120. double ping_pulse_time(int clockCycles) {
  121.  
  122.     double time = clockCycles * 6.25 * pow(10, -5);
  123.  
  124.     return time;
  125. }
  126. double ping_pulse_distance(double time_millis) {
  127.  
  128.     double distance = time_millis * 34 / 2;
  129.  
  130.     return distance;
  131. }
  132.  
  133. double getClockCycles(){
  134.     return clockCycles;
  135. }
  136.  
  137. #include "movement.h"
  138. #include <stdint.h>
  139. #include <stdbool.h>
  140. #include "timer.h"
  141. #include "lcd.h"
  142. #include <inc/tm4c123gh6pm.h>
  143. #include "driverlib/interrupt.h"
  144. #include <string.h>
  145. #include "uart.h"
  146. #include "adc.h"
  147. #include <math.h>
  148. #include "ping.h"
  149. #include "servo.h"
  150. #include "button.h"
  151. #include "servo.h"
  152.  
  153.  
  154. void main()
  155. {
  156.     double angleCounter = 0; //angle starts at 0
  157.     init_Servo(); //initialization
  158.     adc_init(); //initialize adc
  159.     uart_init(); //initialize UART
  160.     lcd_init(); //initialization
  161.     ping_init();
  162.     int dataValue = 0; //IR VALUE
  163.     char unConvert[100];//array that stores the number values
  164.     char pingConvert[100];
  165.     char spaces[12] = "         ";
  166.     char template[100] = "Degrees IR Distance (cm) Sonar Distance (cm)";
  167.     move_servo((int) angleCounter);
  168.     char degreeConvert[100];
  169.     timer_waitMillis(400);
  170.     uart_sendStr(&template);
  171.     uart_sendChar('\n');//new line
  172.     uart_sendChar('\r');
  173.     while (1)
  174.     {
  175.         while (angleCounter <= 180)
  176.         {
  177.             move_servo((int) angleCounter);
  178.             dataValue = adc_read(); //get the value from the sensor
  179.             ping_read();
  180.             double time = ping_pulse_time(getClockCycles());
  181.             double distance = ping_pulse_distance(time);
  182.             timer_waitMillis(500);
  183.             int ConvertIRtoCM = 125718 * (pow(dataValue, -1.185));
  184.             sprintf(pingConvert, "%.2f", distance);
  185.             sprintf(unConvert, "%d", ConvertIRtoCM); //store values into sprintf
  186.             sprintf(degreeConvert, "%.1f", angleCounter);
  187.             uart_sendStr(&degreeConvert);
  188.             uart_sendStr(&spaces);
  189.             uart_sendStr(&unConvert); //send the centimeters to putty
  190.             uart_sendStr(&spaces);
  191.             uart_sendStr(&pingConvert);
  192.             uart_sendChar('\n');//new line
  193.             uart_sendChar('\r');
  194.             angleCounter += 2;
  195.         }
  196.     }
  197.  
  198. }
  199.  
  200.  
  201. #ifndef PING_H_
  202. #define PING_H_
  203.  
  204. #include <stdio.h>
  205. #include <stdint.h>
  206. #include <string.h>
  207. #include <inc/tm4c123gh6pm.h>
  208. #include "timer.h"
  209. #include <math.h>
  210.  
  211. void ping_init(void);
  212. void ping_read(void);
  213. void TIMER3B_handler(void);
  214. double ping_pulse_time(int clockCycles);
  215. double ping_pulse_distance(double time_millis);
  216. double getClockCycles();
  217.  
  218. #endif /* PING_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement