final project  1
ping.c
Go to the documentation of this file.
1 
11 #include "main.h"
12 
13 #define period 20
14 //we found these by trial and error basically
15 //( 20ms - 1.5 )* 16,000 = 296000 should be the neutral position at 90 degrees, but we had to calibrate
16 #define clockwise 0x4E000 //319488
17 #define cclockwise 0x47500 //292096
18 #define TIMER4B_PRESCALER 50
19 
20 
21 volatile int overflow;
22 volatile int rise_time; // start time of the return pulse
23 volatile int fall_time;// end time of the return pulse
24 volatile int j;
25 
26 volatile float servo_angle;
27 
29 
32 void TIMER3B_Handler(void)
33 {
34  TIMER3_ICR_R |= TIMER_ICR_CBECINT; //clear interrupt flag
35 
36  if(j%2)
37  {
38  //if j is even, it must be the fall time
39  fall_time = TIMER3_TBR_R;
40  }
41  else
42  {
43  //must be a rise time
44  rise_time = TIMER3_TBR_R;
45  }
46 
47  //j is incremented every clock cycle
48  j++;
49 }
50 
52 
55 void ping_init()
56 {
57  //enable the clock to port B; turn on the clock
58  SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R1;
59  GPIO_PORTB_PCTL_R|=0x7000;
60  GPIO_PORTB_DEN_R|=0x08;
61 }
62 
63 
65 
69 void send_pulse()
70 {
71  GPIO_PORTB_DIR_R|=0x08;
72  GPIO_PORTB_AFSEL_R&=~0x08;
73  GPIO_PORTB_DATA_R|=0x08;
74  //wait at least 5 microseconds
75  timer_waitMicros(5);
76  GPIO_PORTB_DATA_R&=~0x08;
77  GPIO_PORTB_AFSEL_R|=0x08;
78  GPIO_PORTB_DIR_R&=~0x08;
79 }
80 
82 
85 int ping_read()
86 {
87  send_pulse();
88 
89  //wait for interrupts
90  timer_waitMillis(3);
91 
92  //we don't want the width to be negative, so we have to account for overflow
93  if(fall_time < rise_time)
94  {
95  overflow++;
96 
97  return ping_read();
98  }
99 
100  timer_waitMillis(4);
101 
102  //fall time - rise time = pulse width in clock cycles
103  return (fall_time - rise_time);
104 }
105 
107 
112 double time2dist(int time)
113 {
114  //(speed of sound in ms / 16MHz clock) / 2
115  //340000/ 16000000 / 2
116  return time*0.010622;
117 }
118 
120 
124 {
125  SYSCTL_RCGCTIMER_R |= SYSCTL_RCGCTIMER_R3; // Enable clock
126 
127  //Page724 in datasheet
128  //Configuration settings for timer
129  TIMER3_CTL_R &= 0x04; //disable timerB
130  TIMER3_CFG_R |= 0x04;
131  TIMER3_CTL_R |= TIMER_CTL_TBEVENT_BOTH; //enable both edges
132  TIMER3_TBMR_R |= 0x03; //enable capture
133  TIMER3_TBMR_R |= TIMER_TBMR_TBCDIR; //count
134  TIMER3_TBMR_R |= TIMER_TBMR_TBCMR; //edge-time mode
135  TIMER3_ICR_R = 0x400;; //clear TIMER3B interrupt flags
136  TIMER3_IMR_R |= TIMER_IMR_CBEIM; // Setting interrupt mask to event
137  //initialize local interrupts
138  NVIC_EN1_R = 0x18; ;// 0001 1000; //#warning "enable interrupts for TIMER3A and TIMER3B" n = 0, 1, 2, 3, or 4
139  IntRegister(INT_TIMER3B, TIMER3B_Handler); //register TIMER3B interrupt handler
140  IntMasterEnable(); //intialize global interrupts
141  TIMER3_CTL_R |= (TIMER_CTL_TBEN); //Enable TIMER3A & TIMER3B
142 }
143 
144 
void send_pulse()
sends a pulse to the ping sensor
Definition: ping.c:69
the main code for the robot to interact with the user
double time2dist(int time)
convert a sonar pulse width time to a distance
Definition: ping.c:112
void ping_init()
initializes ping sensor
Definition: ping.c:55
int ping_read()
reads the response pulse from the ping sensor
Definition: ping.c:85
void TIMER3B_Handler(void)
handles interrupts
Definition: ping.c:32
void clock_timer_init3(void)
initializes a clock timer
Definition: ping.c:123