ADC12CTL0 = ADC12SHT02 + ADC12ON; // Sampling time, ADC12 on
ADC12CTL1 = ADC12SHP; // Use sampling timer
ADC12MCTL0 = ADC12INCH_5; // Use A5 (wheel) as input
ADC12CTL0 |= ADC12ENC; // Enable conversions
ADC_PORT_SEL |= ADC_INPUT_A5; // P6.5 ADC option select (A5)
}
/***************************************************************************/
uint8_t Wheel_getPosition(void)
{
uint8_t position = 0;
Wheel_getValue();
//determine which position the wheel is in
if (positionData > 0x0806)
position = 7 - (positionData - 0x0806) / 128; //scale the data for 8 different positions
else
position = positionData / 260;
return position;
}
/**************************************************************************/
int Wheel_getValue(void)
{
//measure ADC value
ADC12IE = 0x01; // Enable interrupt
ADC12CTL0 |= ADC12SC; // Start sampling/conversion
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_ISR will force exit
ADC12IE = 0x00; // Disable interrupt
//add hysteresis on wheel to remove fluctuations
if (positionData > positionDataOld)
if ((positionData - positionDataOld) > 10)
positionDataOld = positionData; //use new data if change is beyond
// fluctuation threshold
else
positionData = positionDataOld; //use old data if change is not beyond
// fluctuation threshold
else
if ((positionDataOld - positionData) > 10)
positionDataOld = positionData; //use new data if change is beyond
// fluctuation threshold
else
positionData = positionDataOld; //use old data if change is not beyond
// fluctuation threshold
int main(void)
{
P1DIR=0x3F;
Wheel_enable();
Wheel_init();
while(1)
{
Wheel_getValue();
switch(positionData)
{
case 0:P1OUT=0x01;break;
case 1:P1OUT=0x02;break;
case 2:P1OUT=0x04;break;
case 3:P1OUT=0x08;break;
case 4:P1OUT=0x10;break;
case 5:P1OUT=0x20;break;
}