用的是430F5529开发板
我用一路通道对外部输入的信号进行采样,寄存器设置如下
P6SEL |= BIT7; // Enable A/D channel A7
ADC12CTL0 = ADC12ON+ADC12SHT0_2+ADC12MSC; // Turn on ADC12, set sampling time, set multiple sample conversion
ADC12CTL1 = ADC12CONSEQ_2+ADC12SHP+ADC12CSTARTADD_7; // Use sampling timer, set mode,Repeat-single-channel
ADC12MCTL7 = ADC12INCH_7; //channel = A7
ADC12IE = BIT7; // Enable ADC12IFG.7
ADC12CTL0 |= ADC12ENC; // Enable conversions
ADC12CTL0 |= ADC12SC; // Start conversion
ADC12中断中的内容如下:
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
switch(__even_in_range(ADC12IV,34))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC overflow
case 4: break; // Vector 4: ADC timing overflow
case 6: break; // Vector 6: ADC12IFG0
case 8: break; // Vector 8: ADC12IFG1
case 10: break; // Vector 10: ADC12IFG2
case 12: break; // Vector 12: ADC12IFG3
case 14: break; // Vector 14: ADC12IFG4
case 16: break; // Vector 16: ADC12IFG5
case 18: break; // Vector 18: ADC12IFG6
case 20: results[index] = ADC12MEM7; // Move results
index++; // Increment results index, modulo; Set Breakpoint1 here
if (index == 408)
{
ADC12IE = 0x00; // Disable ADC12IFG.7
flag=1;
index = 0;
} // Vector 20: ADC12IFG7
case 22: break; // Vector 22: ADC12IFG8
case 24: break; // Vector 24: ADC12IFG9
case 26: break; // Vector 26: ADC12IFG10
case 28: break; // Vector 28: ADC12IFG11
case 30: break; // Vector 30: ADC12IFG12
case 32: break; // Vector 32: ADC12IFG13
case 34: break; // Vector 34: ADC12IFG14
default: break;
}
}
另外我想用TI提供的齿轮电位计的程序来调节,关于齿轮电位计的程序如下:
#include "msp430.h"
#include "HAL_Wheel.h"
#define WHEEL_PORT_DIR P8DIR
#define WHEEL_PORT_OUT P8OUT
#define WHEEL_ENABLE BIT0
#define ADC_PORT_SEL P6SEL
#define ADC_INPUT_A5 BIT5
uint16_t positionData;
uint16_t positionDataOld;
/***************************************************************************//**
* @brief Set up the wheel
* @param None
* @return None
******************************************************************************/
void Wheel_init(void)
{
WHEEL_PORT_DIR |= WHEEL_ENABLE;
WHEEL_PORT_OUT |= WHEEL_ENABLE; // 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)
}
/***************************************************************************//**
* @brief Determine the wheel's position
* @param None
* @return Wheel position (0~7)
******************************************************************************/
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) / 260; //scale the data for 8 different positions
else
position = positionData / 260;
return position;
}
/***************************************************************************//**
* @brief Determine the raw voltage value across the potentiometer
* @param None
* @return Value
******************************************************************************/
uint16_t 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;
}
/***************************************************************************//**
* @brief Disable wheel
* @param None
* @return none
******************************************************************************/
void Wheel_disable(void)
{
WHEEL_PORT_OUT &= ~WHEEL_ENABLE; //disable wheel
ADC12CTL0 &= ~ADC12ENC; // Disable conversions
ADC12CTL0 &= ~ADC12ON; // ADC12 off
}
/***************************************************************************//**
* @brief Enable wheel
* @param None
* @return none
******************************************************************************/
void Wheel_enable(void)
{
WHEEL_PORT_OUT |= WHEEL_ENABLE; //disable wheel
ADC12CTL0 |= ADC12ON; // ADC12 on
ADC12CTL0 |= ADC12ENC; // Enable conversions
}
/***************************************************************************//**
* @brief Handles ADC interrupts.
*
* Stores result of single ADC conversion for reading position of the scroll wheel.
* @param none
* @return none
******************************************************************************/
#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;
}
}
我尝试了很多方法,经常就是齿轮电位计采样不了,这两个AD如何才能融合在一起?