| #include #include "msp430.h" #define WHEEL_DIR P8DIR #define WHEEL_OUT P8OUT #define WHEEL_EN BIT0 #define ADC_PORT_SEL P6SEL #define ADC_INPUT_A5 BIT5 unsigned int positionData; unsigned int positionDataOld; /******************************************************************************/ void Wheel_init(void) { WHEEL_DIR |= WHEEL_EN; WHEEL_OUT |= WHEEL_EN; // Enable wheel 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 return positionData; } /***************************************************************************/ void Wheel_disable(void) { WHEEL_OUT &= ~WHEEL_EN; //disable wheel ADC12CTL0 &= ~ADC12ENC; // Disable conversions ADC12CTL0 &= ~ADC12ON; // ADC12 off } /******************************************************************************/ void Wheel_enable(void) { WHEEL_OUT |= WHEEL_EN; //enable wheel ADC12CTL0 |= ADC12ON; // ADC12 on ADC12CTL0 |= ADC12ENC; // Enable conversions } /******************************************************************************/ 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; } } } /******************************************************************************/ #pragma vector = ADC12_VECTOR __interrupt void ADC12_ISR(void) { switch (__even_in_range(ADC12IV, ADC12IV_ADC12IFG15)) { // Vector ADC12IV_NONE: No interrupt case ADC12IV_NONE: break; // Vector ADC12IV_ADC12OVIFG: ADC overflow case ADC12IV_ADC12OVIFG: break; // Vector ADC12IV_ADC12TOVIFG: ADC timing overflow case ADC12IV_ADC12TOVIFG: break; // Vector ADC12IV_ADC12IFG0: ADC12IFG0: case ADC12IV_ADC12IFG0: positionData = ADC12MEM0; // ADC12MEM = A0 > 0.5AVcc? __bic_SR_register_on_exit(LPM0_bits); // Exit active CPU break; // Vector ADC12IV_ADC12IFG1: ADC12IFG1 case ADC12IV_ADC12IFG1: break; // Vector ADC12IV_ADC12IFG2: ADC12IFG2 case ADC12IV_ADC12IFG2: break; // Vector ADC12IV_ADC12IFG3: ADC12IFG3 case ADC12IV_ADC12IFG3: break; // Vector ADC12IV_ADC12IFG4: ADC12IFG4 case ADC12IV_ADC12IFG4: break; // Vector ADC12IV_ADC12IFG5: ADC12IFG5 case ADC12IV_ADC12IFG5: break; // Vector ADC12IV_ADC12IFG6: ADC12IFG6 case ADC12IV_ADC12IFG6: break; // Vector ADC12IV_ADC12IFG7: ADC12IFG7 case ADC12IV_ADC12IFG7: break; // Vector ADC12IV_ADC12IFG8: ADC12IFG8 case ADC12IV_ADC12IFG8: break; // Vector ADC12IV_ADC12IFG9: ADC12IFG9 case ADC12IV_ADC12IFG9: break; // Vector ADC12IV_ADC12IFG10: ADC12IFG10 case ADC12IV_ADC12IFG10: break; // Vector ADC12IV_ADC12IFG11: ADC12IFG11 case ADC12IV_ADC12IFG11: break; // Vector ADC12IV_ADC12IFG12: ADC12IFG12 case ADC12IV_ADC12IFG12: break; // Vector ADC12IV_ADC12IFG13: ADC12IFG13 case ADC12IV_ADC12IFG13: break; // Vector ADC12IV_ADC12IFG14: ADC12IFG14 case ADC12IV_ADC12IFG14: break; // Vector ADC12IV_ADC12IFG15: ADC12IFG15 case ADC12IV_ADC12IFG15: break; default: break; } } |
更多回帖