430单片机 求助
时间:10-02
整理:3721RD
点击:
小弟初学430 写程序 求改正 要求p1.2输出脉冲信号,p1.3利用自带AD采样 附程序如下:
#include <msp430g2230.h>
long int i,p,q,m,ave,AD_Result=0;
long int num,num1=0;
void delay (long int x)
{
long int p;
for(p=0;p<x;p++);
}
void ADC10_init(void)
{
ADC10CTL0 = ADC10SHT_2 | ADC10ON | ADC10IE; // ADC10ON, interrupt enabled
ADC10CTL1 = INCH_5; // input A5
ADC10AE0 |= BIT5; // PA.5 ADC option select
}
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
/* Initialization Code */
P1REN = 0x1B; // Terminate unavailable Port1 pins (P1.0/1/3/4) properly
// Config as Input with pull-down enabled
BCSCTL3 |= LFXT1S_2; // Select VLO as low freq clock
/* End Initialization Code */
P1DIR |= BIT2; // P1.2 output
ADC10_init();
_enable_interrupt() ;
TA0CCTL0 = CCIE; // TA0CCR0 interrupt enabled
TA0CCR0 = 50000;
TA0CTL = TASSEL_2 | MC_2; // SMCLK, contmode
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
while(1)
{
ADC10CTL0 |= ENC + ADC10SC;
m++; //计数变量加一,记录进入AD中断的次数
AD_Result+=ADC10MEM; //累加每次采样值
if(m>=8) //判断采样的次数,若采样次数等于8 ,作下面处理
{ m=0;
ave=AD_Result>>3; //对累加和求平均
AD_Result=0;}
if (ave < 0x1FF)
{ num=0,num++;
if (num==8)
{
P1OUT &= ~BIT2;
} // Toggle P1.2
else if (num==20)
{
// Toggle P1.2
P1OUT |= BIT2;
}
num=0;
}
else if (ave > 0x1FF)
{
num=0,num++;
if (num==8)
{
P1OUT &= ~BIT2;
} // Toggle P1.2
else if (num==60)
{ num=0;
// Toggle P1.2
P1OUT |= BIT2;
}
num=0;
}
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
TACTL=0;
LPM0_EXIT;
} // Toggle P1.2
#include <msp430g2230.h>
long int i,p,q,m,ave,AD_Result=0;
long int num,num1=0;
void delay (long int x)
{
long int p;
for(p=0;p<x;p++);
}
void ADC10_init(void)
{
ADC10CTL0 = ADC10SHT_2 | ADC10ON | ADC10IE; // ADC10ON, interrupt enabled
ADC10CTL1 = INCH_5; // input A5
ADC10AE0 |= BIT5; // PA.5 ADC option select
}
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
/* Initialization Code */
P1REN = 0x1B; // Terminate unavailable Port1 pins (P1.0/1/3/4) properly
// Config as Input with pull-down enabled
BCSCTL3 |= LFXT1S_2; // Select VLO as low freq clock
/* End Initialization Code */
P1DIR |= BIT2; // P1.2 output
ADC10_init();
_enable_interrupt() ;
TA0CCTL0 = CCIE; // TA0CCR0 interrupt enabled
TA0CCR0 = 50000;
TA0CTL = TASSEL_2 | MC_2; // SMCLK, contmode
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
while(1)
{
ADC10CTL0 |= ENC + ADC10SC;
m++; //计数变量加一,记录进入AD中断的次数
AD_Result+=ADC10MEM; //累加每次采样值
if(m>=8) //判断采样的次数,若采样次数等于8 ,作下面处理
{ m=0;
ave=AD_Result>>3; //对累加和求平均
AD_Result=0;}
if (ave < 0x1FF)
{ num=0,num++;
if (num==8)
{
P1OUT &= ~BIT2;
} // Toggle P1.2
else if (num==20)
{
// Toggle P1.2
P1OUT |= BIT2;
}
num=0;
}
else if (ave > 0x1FF)
{
num=0,num++;
if (num==8)
{
P1OUT &= ~BIT2;
} // Toggle P1.2
else if (num==60)
{ num=0;
// Toggle P1.2
P1OUT |= BIT2;
}
num=0;
}
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
TACTL=0;
LPM0_EXIT;
} // Toggle P1.2