Simple PWM implemented on a MSP430 designed specifically to drive the TB6612FNG Motor Driver

main.c

/*	PWM in MSP430 Launchpad
 *  
 * 
 * 
 * 	John Alder
 */
 

#include "msp430g2211.h"

//PORT1
#define AIN1 BIT0
#define AIN2 BIT3
#define BIN1 BIT4
#define BIN2 BIT5
#define PWMA BIT6
#define DIRA BIT7
#define DIRB BIT1
#define PWMB BIT2

//PORT2
#define STBY BIT7

//#define RXD BIT2

unsigned int ticks = 0;
unsigned int aup = 95;	//duty cycle up
unsigned int bup = 75; //duty cycle up
unsigned int freq = 100; //divide system clock by 10 to get count frequency ALSO account for 100 ticks to 100%

void main(void)
{
	WDTCTL = WDTPW + WDTHOLD;  // Stop Dog
	P1DIR |= (PWMA+AIN1+AIN2+BIN1+BIN2+PWMB);             // port1 outputs
	P2SEL = 0x00;
	P2DIR |= (STBY);
	P2OUT |= STBY;
	
	//P1SEL |= (PWMA+PWMB);             // pwma assign to TA0.1
	//P2SEL |= (PWMB);
	//P2OUT |= PWMB;
	CCR0 = freq;               // Freq 1MHz/freq = base tick freq
	CCTL0 = CCIE;				//reset/set, interrupt enable
	//CCTL1 = OUTMOD_7 + CCIE;          // reset/set mode, interrupt enable
	//CCR1 = 200;                // CCR1 PWM duty cycle
	TACTL = TASSEL_2 + MC_1;   // SMCLK (~1MHz), up mode

	_BIS_SR(GIE);        // LPM0 Power Mode	
	while(1)
	{
		if(P1IN&DIRA)
		{
			P1OUT |= AIN1;
			P1OUT &= ~AIN2; //Go Clockwise
		} 
		else
		{
			P1OUT &= ~AIN1;
			P1OUT |= AIN2;	//Go CounterClockwise
		}
		if(P1IN&DIRB)
		{
			P1OUT |= BIN1;
			P1OUT &= ~BIN2; //Go Clockwise
		} 
		else
		{
			P1OUT &= ~BIN1;
			P1OUT |= BIN2;	//Go CounterClockwise
		}
		if(ticks > 100)
		{
			ticks = 0;	
		}
		if(ticks > aup)
		{
			P1OUT &= ~PWMA;
		}
		else
		{
			P1OUT |= PWMA;
		}
		if(ticks > 	bup)
		{
			P1OUT &= ~PWMB;
		}
		else
		{
			P1OUT |= PWMB;
		}
	}
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A0 (void)
{
	ticks++;
}
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A1 (void)
{

}