我需要做一个正弦波的极值点采样程序,以下是我的峰值采样程序,该怎么修改呢,我的想法是采样得到的数的数组序号乘以采样间隔时间即可得到对应点的横坐标,不知道能不能行。本人新手不知道采样间隔怎么确定。。。
#include
#define n 255
int ADC_buf[n];
unsigned char j;
unsigned char i=0;
int g_zhong,ADC_MAX;
float MAX_true;
float ADC_true;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC12CTL0 = ADC12SHT0_2 + ADC12ON; // Sampling time, ADC12 on
ADC12CTL1 = ADC12SHP; // Use sampling timer
ADC12IE = 0x01; // Enable interrupt
ADC12CTL0 |=ADC12ENC;
P6SEL |= 0x01; // P6.0 ADC option select
//P1DIR |= 0x1C; // P1.0 output
P1DIR |= 0x01;
while (1)
{
ADC12CTL0 |= ADC12SC; // Start sampling/conversion
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_ISR will force exit
//__no_operation(); // For debugger
while(i==0)
{
for(j=1;j
{
if(ADC_buf[j-1]>ADC_buf[j])
{
g_zhong=ADC_buf[j-1];
ADC_buf[j-1]=ADC_buf[j];
ADC_buf[j]=g_zhong;
}
ADC_MAX=ADC_buf[n-1];
}
MAX_true=ADC_MAX*3.30/4095;
P1OUT |= BIT0;
}
}
}
#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
if(ADC12MEM0>=0)
{//P1OUT|=0x1C;
P1OUT |= BIT0;
ADC_buf[i]=ADC12MEM0;
i++;
i%=n;}
__bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
}
这个看你AD是怎么触发的,连续转换的情况ad转换时间,软触发间隔就是触发间隔
你这个是例程
下面是我修改后的程序,正弦波极值点的连续采样,能够采集到数据,但是极值点 MAX_true得不到,
#include
#define n 100
int ADC_buf[n];
unsigned char j;
unsigned char i=0;
int ADC_MAX;
float MAX_true;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P6SEL |= 0x01; // P6.0 ADC option select
ADC12CTL0 = ADC12SHT0_8 + ADC12ON+ADC12MSC; // Sampling time, ADC12 on
ADC12CTL1 = ADC12SHP+ADC12CONSEQ_2; // Use sampling timer
ADC12IE = 0x01; // Enable interrupt
ADC12CTL0 |=ADC12ENC;
P1DIR |= 0x01;
while (1)
{
ADC12CTL0 |= ADC12SC; // Start sampling/conversion
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_ISR will force exit
while(i==0)
{
for(j=1;j
{
if(ADC_buf[j-1]>=ADC_buf[j])
{
ADC_MAX=ADC_buf[j-1];
MAX_true=ADC_MAX*3.30/4095;
P1OUT |= BIT0;
j=1;
}
P1OUT &=~ BIT0;
}
}
}
}
#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
if(ADC12MEM0>=0)
{
ADC_buf[i]=ADC12MEM0;
i++;
i%=n;}
__bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
}