Using the TCRT5000 as a proximity detector

//
// IR proximity detector MSP430G2553
// Written by Ted Burke - Last modified 23-11-2016
//
// P1.0 is used as A0 the analog input for the potentiometer
// P1.1 is used as A1 the analog input for the proximity detector
// P1.2 is used as TXD the UART transmit pin - connect this to Launchpad
// P1.3 is green LED
// P1.4 and P1.5 are joined together to drive the IR LED
// 

#include <msp430.h>
#include <stdio.h>

int read_analog_channel(unsigned int);
int proximity();

void main( void )
{
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
    
    // Digital i/o
    P1DIR = 0b00111000; // P1.3 is green LED, P1.4+P1.5 is IR LED
    P2DIR = 0b00000000; // P2.0-7 are inputs
    
    // Analog inputs
    ADC10AE0 = 0b00000011; // A0 is proximity sensor, A1 is threshold input
    ADC10CTL0 = ADC10ON;   // Turn on the ADC

    // Basic Clock Module (set to 1MHz)
    DCOCTL = CALDCO_1MHZ;
    BCSCTL1 = CALBC1_1MHZ;
     
    // UART
    // Baudrate = 1MHz / (256 * UCA0BR1 + UCA0BR0)
    UCA0BR1 = 0; UCA0BR0 = 104;  // Set baudrate = 9600
    UCA0CTL1 |= UCSSEL_2;        // Set USCI clock to SMCLK
    UCA0MCTL = UCBRS0;           // Modulation UCBRSx = 1    
    P1SEL = BIT2; P1SEL2 = BIT2; // Set P1.2 as TXD
    UCA0CTL1 &= ~UCSWRST;        // Start USCI (release from reset)
    
    // Variables to store voltage readings
    int v_pot, v_off, v_on, v_prox;
    
    while(1)
    {
        // Read the proximity sensor
        v_off = read_analog_channel(0);    // Take baseline reading
        P1OUT |= BIT4 + BIT5;              // IR LED on (i.e. set P1.4 and P1.5 high)
        __delay_cycles(1000);              // 1ms Settling time
        v_on = read_analog_channel(0);     // Take IR reflection reading
        P1OUT &= ~(BIT4 + BIT5);           // IR LED off (i.e. set P1.4 and P1.5 low)
        v_prox = v_on - v_off;             // Subtract out baseline to get v_prox
        
        v_pot = read_analog_channel(1) / 4; // Read threshold voltage from pot (and scale down)
        
        if (v_prox > v_pot) P1OUT |= BIT3; // green LED on when reflection detected
        else P1OUT &= ~BIT3;               // green LED off when no reflection deteceted
        
        printf("v_off=%04d v_on=%04d v_prox=%04d v_pot=%04d\n", v_off, v_on, v_prox, v_pot);
        
        __delay_cycles(250000);
    }
}

//
// For the printf function (from stdio.h) to work, we need to provide
// a putchar function which transmits a single character via the UART.
//
int putchar(int c)
{
    UCA0TXBUF = c;
    while((IFG2 & UCA0TXIFG) == 0); 
}

//
// This function performs a single analog to digital conversion,
// converting the voltage on analog input pin ANx into a 10-bit
// unsigned integer. Execution time for this function is of the
// order of 100us.
//
int read_analog_channel(unsigned int x)
{
    ADC10CTL0 &= ~ENC;            // disable conversion
    ADC10CTL1 = x << 12;          // select channel
    ADC10CTL0 |= ENC;             // enable conversion
    ADC10CTL0 |= ADC10SC;         // start conversion
    while(ADC10CTL1 & ADC10BUSY); // wait until complete
    return ADC10MEM;              // return digital value
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s