-
1Step 1
The circuit diagram of the project:
-
2Step 2
Project coding in code composer studio:
#include "msp430.h"
#define ADC_CHANNELS 2
unsigned int samples[ADC_CHANNELS];
#define LED1 BIT4
#define LED2 BIT6
#define SENSOR_LEFT BIT0
#define SENSOR_GND BIT2
#define SENSOR_RIGHT BIT1
#define SENSOR_GND1 BIT3
#define RED_LED LED1
#define GRN_LED LED2
void ConfigureAdc(void){
ADC10CTL1 = INCH_1 | ADC10DIV_0 | CONSEQ_3 | SHS_0;
ADC10CTL0 = SREF_0 | ADC10SHT_2 | MSC | ADC10ON | ADC10IE;
ADC10AE0 =SENSOR_LEFT + SENSOR_RIGHT ;
ADC10DTC1 = ADC_CHANNELS;
}
void main(void) {
WDTCTL = WDTPW | WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
BCSCTL2 &= ~(DIVS_3);
P1DIR = 0; /* set as inputs */
P1SEL = 0; /* set as digital I/Os */
P1OUT = 0; /* set resistors as pull-downs */
P1REN = 0xFF; /* enable pull-down resistors */
P2DIR = 0; /* set as inputs */
P2SEL = 0; /* set as digital I/Os */
P2OUT = 0; /* set resistors as pull-downs */
P2REN = 0xFF; /* enable pull-down resistors */
P1REN &= ~(LED1 | LED2); /* disable pull-up/downs */
P1DIR |= (LED1 | LED2); /* configure as oututs */
P1REN &= ~(SENSOR_GND |SENSOR_GND1); /* disable pull-up/down */
P1OUT &= ~(SENSOR_GND|SENSOR_GND); /* SENSOR_GND should be at GND */
P1DIR |= (SENSOR_GND |SENSOR_GND1); /* SENSOR_GND must be an output */
P1REN |= (SENSOR_LEFT|SENSOR_RIGHT); /* enable pull-up on SENSOR */
P1IN |= (SENSOR_LEFT|SENSOR_RIGHT); /* set resistor as pull-up */
ConfigureAdc();
__enable_interrupt();
while (1) {
__delay_cycles(1000);
ADC10CTL0 &= ~ENC;
while (ADC10CTL1 & BUSY);
ADC10SA = (unsigned int)samples;
ADC10CTL0 |= ENC + ADC10SC;
__bis_SR_register(CPUOFF + GIE);
if (samples[0] < samples[1]) {
P1OUT |=RED_LED;
P1OUT &= ~(GRN_LED);
} else if (samples[0] == samples[1]) {
P1OUT &= ~(RED_LED);
P1OUT &= ~(GRN_LED);
} else {
P1OUT |= GRN_LED;
P1OUT &= ~(RED_LED);
}
}
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void){
__bic_SR_register_on_exit(CPUOFF);
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.