Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 2

// #define TEST

#include
#include
#include
#include
#include
#include
#include
#include
#include

<stdint.h>
<stdbool.h>
<stdio.h>
"inc/hw_types.h"
"inc/hw_memmap.h"
"driverlib/sysctl.h"
"driverlib/gpio.h"
"driverlib/interrupt.h"
"utils/uartstdio.h"

#include
#include
#include
#include

"ES_Configure.h"
"ES_Framework.h"
"ES_Port.h"
"termio.h"

#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "ShiftRegister.h"
#include "Servo.h"
#include "PWM10Tiva.h"
#define BASE_POSITION 550
#define ANGLE_MULT 2200
static uint8_t LastServo;
static void ServoPosition(uint8_t position);
static void ServoSelect(uint8_t servo);
// ServoInit: initialize hardware and timers for servo functions
// Takes: nothing
// Returns: nothing
void ServoInit(void) {
uint16_t reqPeriod=25000;
uint8_t group=2;
// initialize the last servo selected to 0
LastServo=0;
// initialize servo freq
PWM_TIVA_SetPeriod(reqPeriod, group);
// initialize shift register
SR_Init();
}
// ServoWrite: writes a desired position to the desired servos
// Takes:
uint8_t servo: 8 bit number with each bit referring to a servo, ON
is 1 and OFF is 0
//
uint8_t position: desired servo angular position from 0 to 180
degrees
// Returns: nothing
void ServoWrite(uint8_t servo, uint8_t position) {
// if the selected servos have changed
if (servo!=LastServo) {
// deselect all servos
ServoSelect((uint8_t)0);
// write desired position
ServoPosition(position);
// select desired servos
ServoSelect(servo);
}
// else
else {

// write desired position


ServoPosition(position);
}

// ServoPosition: pulse the servo control input to achieve desired position,


loses precision
// at higher angles and modifies the whole PWM group
// Takes: uint8_t position: specification of rotation angle, with 0 as 0 degrees
and 180 as 180 degrees
// Returns: nothing
static void ServoPosition(uint8_t position) {
// if position is less than 180 degrees
if (position<=180) {
// write angle
uint16_t newPW = BASE_POSITION+position/180.0*ANGLE_MULT;
uint8_t channel = 4;
PWM_TIVA_SetPulseWidth(newPW, channel);
}
}
// ServoSelect: turns only selected servos on
// Takes: uint8_t servo: 8 bit number with each bit corresponding to ON (1) or
OFF (0) for the associated
//
servo
// Returns: nothing
static void ServoSelect(uint8_t servo) {
// write servo to the shift registers
SR_Write(servo);
// update the last write to servo
LastServo=servo;
}
#ifdef TEST
// testharness
int main(void) {
SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN
| SYSCTL_XTAL_16MHZ);
PWM_TIVA_Init(8);
ServoInit();
TERMIO_Init();
puts("\r\nWriting!\r\n");
ServoWrite(8, 90);
return 0;
}
#endif

You might also like