Skip to content

Instantly share code, notes, and snippets.

@debreuil
Created August 29, 2011 23:22
Show Gist options
  • Select an option

  • Save debreuil/1179685 to your computer and use it in GitHub Desktop.

Select an option

Save debreuil/1179685 to your computer and use it in GitHub Desktop.
/*
Freeware. source [email protected]
ADC pin: P1.0
PWM signal pin: P1.2
ADC signal is just a light sensor with a 1K resistor to 3.3V
*/
#include <msp430g2231.h>
#define pwmPin 0x04
volatile int ADCdata;
int inc = 1;
int ms_1 = 1100;
int motorDir = 550;
int isPulse = 0;
void main(void)
{
WDTCTL = WDT_MDLY_8; // WDT ~8ms interval timer
IE1 |= WDTIE; // Enable WDT interrupt
ADC10CTL0 = ADC10SHT_2 + ADC10ON; // sample and hold 16 cycles, ref on
ADC10AE0 |= INCH_1; // P1.1 ADC option select
ADC10DTC1 = 0x001; // 2 conversions
P1DIR |= pwmPin; // P1.2 = output
BCSCTL1 = CALBC1_1MHZ; // Running at 1 MHz
DCOCTL = CALDCO_1MHZ;
TACTL = MC_0; // off
while(1)
{
__bis_SR_register(LPM0_bits + GIE); // LPM0, WDT_ISR will force exit
if(isPulse)
{
isPulse = 0;
P1OUT |= pwmPin;
motorDir = (ADCdata - 150) * 3;
TACCR0 = motorDir + ms_1;
TACTL = TASSEL_2 + MC_1 + TAIE + TACLR + OUT; // ACLK, upmode, interrupt enabled
TACCTL0 |= CCIE;
}
else
{
isPulse = 1;
ADC10SA = (unsigned int)&ADCdata; // store data here
ADC10CTL0 |= ENC + ADC10SC; // Start sampling
}
}
}
#pragma vector = TIMERA0_VECTOR
__interrupt void CCR0_ISR(void)
{
//TACCTL0 &= ~CCIE;
TACTL = MC_0; // timer A off, interrupt disabled
P1OUT &= ~pwmPin;
}
#pragma vector = WDT_VECTOR
__interrupt void WDT_ISR(void)
{
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment