Servo control by dsPIC using bit banging

This was the bit banging servo control code from last Thursday’s Robotics lecture:

//
// A servo bit banging example for dsPIC
// Written by Ted Burke
// Last updated 26-9-2013
//

#include <xc.h>
#include <libpic30.h>
 
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

void pulse(double, double);
 
int main()
{
	int n;
    TRISD = 0;
 
    while(1)
    {
		for (n=0 ; n<100 ; ++n)	pulse(1 + (n/100.0), 19 - (n/100.0));
    }
 
    return 0;
}

void pulse(double mark, double space)
{
	_LATD0 = 1;
	__delay32(mark * 30000.0);
	_LATD0 = 0;
	__delay32(space * 30000.0);
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s