final project  1
servo.c
Go to the documentation of this file.
1 
10 #include "main.h"
11 
12 #define period 20
13 //we found these by trial and error basically
14 //( 20ms - 1.5 )* 16,000 = 296000 should be the neutral position at 90 degrees, but we had to calibrate
15 //calibrated for robot 3
16 #define clockwise 319488 //319488 -- starting point
17 #define cclockwise 292096 //292096 -- ending point
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 
28 
29 
31 
34 void timer_init()
35 {
36  unsigned int pulse_period = period * 16000;
37  unsigned int match = ((period-1.5)*16000);
38 
39  SYSCTL_RCGCTIMER_R |= SYSCTL_RCGCTIMER_R1;
40  TIMER1_CTL_R &= ~TIMER_CTL_TBEN; //disable timer1B
41  TIMER1_TBMR_R |= 0xA; //set to periodic and PWM
42  TIMER1_TBMR_R &= ~0x4; //disable capture mode
43  TIMER1_CFG_R |= TIMER_CFG_16_BIT; //set to 16bit
44  TIMER1_TBPR_R |= pulse_period>>16; //set timer prescaler
45  TIMER1_TBILR_R |= pulse_period & 0xFFFF; //set period
46  TIMER1_TBPMR_R |= (match>>16); // set match prescaler
47  TIMER1_TBMATCHR_R |= match&0xFFFF; // set timer value for initial intpt
48  TIMER1_CTL_R |= TIMER_CTL_TBEN;
49 }
50 
52 
55 void clock_timer_init(void)
56 {
57  SYSCTL_RCGCTIMER_R |= SYSCTL_RCGCTIMER_R4; // Turn on clock to TIMER4
58 
59  //Configure the timer for input capture mode
60  TIMER4_CTL_R &= ~(TIMER_CTL_TBEN); //disable timerB to allow us to change settings
61  TIMER4_CFG_R |= TIMER_CFG_16_BIT; //set to 16 bit timer
62 
63  TIMER4_TBMR_R |= TIMER_TBMR_TBMR_PERIOD; //set for periodic mode, down count
64 
65  TIMER4_TBPR_R |= TIMER4B_PRESCALER - 1; // 200 ms clock
66 
67  TIMER4_TBILR_R = (int)(16000000/(TIMER4B_PRESCALER * 5)); // set the load value for the 0.2 second clock
68 
69  //clear TIMER3B interrupt flags
70  TIMER4_ICR_R = ( TIMER_ICR_TBTOCINT); //clears TIMER4 time-out interrupt flags
71  TIMER4_IMR_R |= ( TIMER_IMR_TBTOIM); //enable TIMER4(A&B) time-out interrupts
72 
73  //initialize local interrupts
74  //NVIC_EN2_R = 0b11000000;
75 // NVIC_ENn_R = 0x????????; //#warning "enable interrupts for TIMER4A and TIMER4B" n = 0, 1, 2, 3, or 4
76  //go to page 105 and find the corresponding interrupt numbers for TIMER4 A&B
77  //then set those bits in the correct interrupt set EN register (p. 142)
78 
79  //IntRegister(INT_TIMER4B, TIMER4B_Handler); //register TIMER4B interrupt handler
80 
81  IntMasterEnable(); //intialize global interrupts
82 
83  TIMER4_CTL_R |= (TIMER_CTL_TBEN); //Enable TIMER4A & TIMER4B
84 }
85 
87 
90 void servo_init(){
91  SYSCTL_RCGCGPIO_R|= SYSCTL_RCGCGPIO_R1;
92  GPIO_PORTB_PCTL_R |= GPIO_PCTL_PB5_T1CCP1;
93  GPIO_PORTB_DEN_R &= ~0x20;
94  GPIO_PORTB_DIR_R|= 0x20;
95  GPIO_PORTB_AFSEL_R|=0x20;
96  GPIO_PORTB_DEN_R |= 0x20;
97 }
98 
100 
105 void match_set(int match)
106 {
107  TIMER1_TBMATCHR_R = (match & 0xFFFF); //set match, 16 LSB's
108  TIMER1_TBPMR_R = (match >> 16); //set match, 8 MSB's
109 }
110 
112 
115 void servo_setAngle(double degrees)
116 {
117  //cclockwise = 180 degrees and clockwise = 0 degrees
118  double slope = (cclockwise- clockwise)/(180.0);
119 
120  //set match value
121  //slope is the amount the match value will change per degree
122  //clockwise is the intercept of the line
123  //y = mx+b
124  unsigned int match = slope*(degrees) + clockwise;
125 
126  match_set(match);
127  timer_waitMillis(50);
128 
129  servo_angle = degrees;
130 }
131 
void match_set(int match)
sets match value
Definition: servo.c:105
void servo_setAngle(double degrees)
sets the servo angle
Definition: servo.c:115
the main code for the robot to interact with the user
void timer_init()
initializes the timer for the servo
Definition: servo.c:34
void clock_timer_init(void)
intializes the clock timer
Definition: servo.c:55
void servo_init()
initializes the servo motor
Definition: servo.c:90