Last active
August 29, 2015 13:56
-
-
Save jimmitt/8815265 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* IRProximitySensor by James Trimble, Capt, USAF | |
* USAFA ECE Automata Club | |
* Description: Uses the Analog-to-Digital Converter to read the voltage from an IR Proximity | |
* Sensor circuit and then turns on 0,1, or 2 on-board LEDs based on the reading. | |
*/ | |
#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 | |
ADC10CTL1 = INCH_4; // input A4 | |
ADC10AE0 |= BIT4; // PA.1 ADC option select | |
ADC10CTL1 |= ADC10SSEL1|ADC10SSEL0; // Select SMCLK | |
P1DIR |= 0x41; // Set P1.0 & P1.6 to output direction | |
for (;;) | |
{ | |
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start | |
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit | |
if (ADC10MEM > 0x3FC) | |
P1OUT |= 0x41; // Set P1.0 and P1.6 LEDs (on) | |
else if (ADC10MEM > 0x3F9) | |
P1OUT |= 0x01; // Set P1.0 LED (on) | |
else if (ADC10MEM > 0x3F6) | |
P1OUT |= 0x40; // Set P1.6 LED (on); | |
else | |
P1OUT &= ~0x41; // Clear P1.0 and P1.6 LEDs (off) | |
} | |
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
I'll be updating this to make it more readable.