Skip to content

Instantly share code, notes, and snippets.

@jimmitt
Created February 19, 2014 22:48
Show Gist options
  • Save jimmitt/9103301 to your computer and use it in GitHub Desktop.
Save jimmitt/9103301 to your computer and use it in GitHub Desktop.
/*
* MotorController by James Trimble, Capt, USAF
* USAFA ECE Automata Club
* Description: Uses the Analog-to-Digital Converter to read the voltage from a voltage divider (100K resistor w/ 100K pot).
* Sets the PWM duty cycle based on the ADC value to allow controlling a DC motor's speed using the potentiometer.
*/
#include <msp430.h>
int main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL0 = ADC10SHT_3 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled
ADC10CTL0 &= ~0xD000; // Use GND to VCC as the reference range (0V to 3.3V)
ADC10CTL1 = INCH_4; // input A4
ADC10AE0 |= BIT4; // PA.1 ADC option select
ADC10CTL1 |= ADC10SSEL1|ADC10SSEL0; // Select SMCLK
P1DIR |= BIT0; // Set P1.0 to output
P1DIR |= BIT6; // P1.6 to output
P1DIR |= BIT2; // P1.2 to output
P1SEL |= BIT2; // P1.2 to TA0.1 (Tie P1.2 to the Timer output)
CCR0 = 0x2C0; // PWM Period
CCTL1 = OUTMOD_7; // CCR1 reset/set
CCR1 = 0x2; // CCR1 PWM duty cycle
TACTL = TASSEL_2 + MC_1; // SMCLK, up mode
for (;;)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
CCR1 = ADC10MEM; // Set the PWM Duty Cycle to the ADC Value
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
}
return 0;
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment