#include <msp430x14x.h>
#include "TA.c"
#include "direction.c"
#include "Key.c"
#define uchar unsigned char
#define uint unsigned int
uint Red_distance;
uint Optic_reflect;
static unsigned int results[5];uint vth = 0x000f ;
uint adc_flag=0;
void int_AD()
{
P6SEL = 0x001F; // Enable A/D channel inputs
ADC12CTL0 = ADC12ON+MSC+SHT0_2; // Turn on ADC12, set sampling time
ADC12CTL1 = SHP+CONSEQ_1; // Use sampling timer, single sequence
ADC12MCTL0 = INCH_0; // ref+=AVcc, channel = A0
ADC12MCTL1 = INCH_1; // ref+=AVcc, channel = A1
ADC12MCTL2 = INCH_2; // ref+=AVcc, channel = A2
ADC12MCTL3 = INCH_3; // ref+=AVcc, channel = A3
ADC12MCTL4 = INCH_4+EOS; // ref+=AVcc, channel = A4, end seq.
ADC12IE = 0x0001; // Enable ADC12IFG.3
ADC12CTL0 |= ENC; // Enable conversions
}
void main()
{
uint i,k=0; WDTCTL = WDTPW + WDTHOLD;
int_clk();
TAPwmInit('A',1,'P','P'); //将定时器TA初始化成为PWM发生器
//时钟源=ACLK ; 无分频; 通道1和通道2均设为高电平模式。
TAPwmSetPeriod(1000); //通道1/2的PWM方波周期均设为500个时钟周期
P2DIR = 0X00FF;
Go_ahead(800,800); P6DIR = 0X0000;
P5DIR = 0X0000;
P4DIR = 0X0001;
P4OUT = 0X0000;
int_AD();
ADC12CTL0 |= ADC12SC; // Start conversion _EINT();
while(1)
{
Go_ahead(800,800); Red_distance = P5IN & 0x00F8; //蔽障
Delay_Nms(4);
Red_distance = P5IN&0x00F8; //ADC12CTL0 |= ADC12SC; //启动ad转换
// for(i=0;i<5;i++)
//if(results[i] > vth)
//{
//k = 1;
//}
//if(k == 1) Stop();
//if(k == 0)
//{
if(Red_distance != 0x00F8)
{
Red_Key(Red_distance);
k=0;
} else
{ ADC12CTL0 |= ADC12SC; // Start conversion Delay_Nms(1);
Optic_zuobiao(results[0],results[1],results[2],results[3],results[4]);
k=0;
}
}
/* if(adc_flag == 1) {
ADC12CTL0 |= ADC12SC; adc_flag=0;
}*/
// Red_Key(Red_distance);
// Red_reflect = jisuan(Red_reflect);
// PID_Control(uchar Position) //}
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(ADC12_VECTOR))) ADC12ISR (void)
#else
#error Compiler not supported!
#endif
{
results[0] = ADC12MEM0; // Move results, IFG is cleared
results[1] = ADC12MEM1; // Move results, IFG is cleared
results[2] = ADC12MEM2; // Move results, IFG is cleared
results[3] = ADC12MEM3; // Move results, IFG is cleared
results[4] = ADC12MEM4; // Move results, IFG is cleared
//adc_flag=1;
//__bic_SR_register_on_exit(LPM0_bits); // Clear LPM0, SET BREAKPOINT HERE
}
灰小子:
adc12是逐个通道依次转换的。建议参考下官网提供的例程
rui yue:
回复 灰小子:
#include <msp430.h>
static unsigned int results[4]; // Needs to be global in this example // Otherwise, the compiler removes it // because it is not used for anything.
int main(void){ WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer P6SEL = 0x0F; // Enable A/D channel inputs ADC12CTL0 = ADC12ON+MSC+SHT0_2; // Turn on ADC12, set sampling time ADC12CTL1 = SHP+CONSEQ_1; // Use sampling timer, single sequence ADC12MCTL0 = INCH_0; // ref+=AVcc, channel = A0 ADC12MCTL1 = INCH_1; // ref+=AVcc, channel = A1 ADC12MCTL2 = INCH_2; // ref+=AVcc, channel = A2 ADC12MCTL3 = INCH_3+EOS; // ref+=AVcc, channel = A3, end seq. ADC12IE = 0x08; // Enable ADC12IFG.3 ADC12CTL0 |= ENC; // Enable conversions
while(1) { ADC12CTL0 |= ADC12SC; // Start conversion __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, Enable interrupts }}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)#pragma vector=ADC12_VECTOR__interrupt void ADC12ISR (void)#elif defined(__GNUC__)void __attribute__ ((interrupt(ADC12_VECTOR))) ADC12ISR (void)#else#error Compiler not supported!#endif{ results[0] = ADC12MEM0; // Move results, IFG is cleared results[1] = ADC12MEM1; // Move results, IFG is cleared results[2] = ADC12MEM2; // Move results, IFG is cleared results[3] = ADC12MEM3; // Move results, IFG is cleared __bic_SR_register_on_exit(LPM0_bits); // Clear LPM0, SET BREAKPOINT HERE}
这个是官网提供的程序。不知道我的有什么问题。
TI中文支持网



