0% found this document useful (0 votes)
145 views2 pages

Servo C

This C code defines functions for initializing and controlling servos using pulse-width modulation on a Tiva microcontroller. It initializes hardware timers and a shift register for servo control. The ServoInit function initializes the last selected servo to 0 and the servo frequency. The ServoWrite function writes a position to one or more servos, selecting the servos using the shift register if the selection has changed. It calls ServoPosition to set the pulse width and ServoSelect to enable the selected servos. ServoPosition calculates the pulse width based on the position. ServoSelect writes the selection to the shift register and updates the last selected servo.

Uploaded by

api-340729449
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views2 pages

Servo C

This C code defines functions for initializing and controlling servos using pulse-width modulation on a Tiva microcontroller. It initializes hardware timers and a shift register for servo control. The ServoInit function initializes the last selected servo to 0 and the servo frequency. The ServoWrite function writes a position to one or more servos, selecting the servos using the shift register if the selection has changed. It calls ServoPosition to set the pulse width and ServoSelect to enable the selected servos. ServoPosition calculates the pulse width based on the position. ServoSelect writes the selection to the shift register and updates the last selected servo.

Uploaded by

api-340729449
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 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