你好 能留个联系方式吗? 我是个新手,编的程序有点问题。想请您帮帮忙,。行吗 ?
下面这个程序,是一个自动检测和控制开关的程序。P1.7为ADC检测,当检测到电压时,让P1.5输出高电平;没有
检测到时,隔段时间给P1.5一个脉冲。 P1.0可以忽略不看
#include <msp430g2231.h>
#include<in430.h>
#define uint unsigned int
#define uchar unsigned char
uint a=0;
void delay(uint m);
void init();
void main(void)
{
init();
while(1)
{
ADC10CTL0 |= ADC10SC; // Sampling open
if(a==1)
{
P1OUT |= BIT5;
delay(500);
P1OUT &=~BIT0;
}
else
{
while(a==0)
{
ADC10CTL0 |= ADC10SC;
delay(500);
if(a==1)
break;
P1OUT &=~BIT5;
P1OUT &=~BIT0;
//_NOP();
delay(20000);
P1OUT |= BIT5;
}
}
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
P1DIR|=BIT0;
P1OUT|=BIT0;
if (ADC10MEM > 0x1FF)
{a=1;
}
else
a=0;
}
void init()
{
WDTCTL = WDTPW + WDTHOLD;
ADC10CTL0 = ADC10IE + ADC10ON; // Set sampling time, turn on ADC12
//ADC10CTL1 = SHS_0; // Use sampling timer
ADC10CTL0 |= ENC; // Conversion enabled
ADC10CTL1 |=INCH_7;
ADC10AE0 |=BIT7;
P1DIR |=BIT5;
P1OUT |=BIT5;
_EINT();
}
void delay(uint m )
{
int x,y;
for(x=m;x>0;x--)
for(y=100;y>0;y--);
}