430想通过判断AD读取的值来控制串口发送字符串
时间:10-02
整理:3721RD
点击:
仿真时发生了奇怪的事:k的值变成了72;
AD读取的值
这是为什么呢?哪里的问题 ?
#include <msp430f5529.h>
#define uchar unsigned char
void Uart1WriteStr(uchar *str); //UART-A1向串口写一个字符
volatile unsigned int results[8];
int k=0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
P1DIR|=BIT0; //p1.0作指示灯
P7SEL|=BIT1;
ADC12CTL0&=~ENC; //复位,开始对ADC12CTL0,ADC12CTL1设置
ADC12CTL0=ADC12ON+MSC+SHT0_4; //打开转换内核,多次转换,采样定时确定
ADC12CTL1=CONSEQ_2+SHP+CSTARTADD_4; //单通道多次转换,使用定时器触发采样
//从第4个存储器开始
ADC12MCTL4=INCH_0; //参考电压默认,通道0
ADC12IE=0x0010; //容许通道转换完成中断,存储器4中断容许,0001 0000
ADC12CTL0|=ENC; //使能转换
//UART(); //uart中断配置
P4SEL |=BIT4+BIT5; // P4.4 P4.5 = USCI_A1 TXD/RXD
UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
UCA1CTL1 |= UCSSEL_2; // SMCLK=1048576HZ=1MHZ
UCA1BR0 = 0x06; // (see User's Guide)
UCA1BR1 = 0x00; // 9600bps
UCA1MCTL |= UCBRS_0 + UCBRF_13 + UCOS16; // Modulation UCBRSx=0, UCBRFx=13
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA1IE |= UCRXIE + UCTXIE; // Enable USCI_A1 RX,TX interrupt
while(1)
{
ADC12CTL0|=ADC12SC; //启动转换
__bis_SR_register(LPM4_bits + GIE); // Enter LPM0, interrupts enabled
}
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
_EINT();
static unsigned char index=0;
switch(__even_in_range(ADC12IV,34))
{
case 6:results[index]=ADC12MEM0; //读取转换结果
if(results[index]>0)
{
k=1;
}else k=0;
index++;
if(index==8)
{
index=0;
}
default:break;
}
}
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
{
_EINT();
switch(__even_in_range(UCA1IV,4))
{
case 0:
break;
case 2: //中断向量2--接收中断
while (!(UCA1IFG & UCTXIFG)); //USCI A1是否接收完成
UCA1TXBUF = UCA1RXBUF; //在串口助手中回显
break;
case 4: //中断向量4--发送中断
if(k==1)
{
__delay_cycles(1000000);
__delay_cycles(1000000);
__delay_cycles(1000000);
__delay_cycles(1000000);
Uart1WriteStr("AT\r\n"); //AT测试
__delay_cycles(1000000); //延时1s,下同
Uart1WriteStr("AT\r\n"); //同步模块波特率
__delay_cycles(1000000);
Uart1WriteStr("AT+CSCS=\"GSM\"\r\n"); //编码设置
__delay_cycles(1000000);
Uart1WriteStr("AT+CMGF=1\r\n"); //选择短消息格式
__delay_cycles(1000000);
Uart1WriteStr("AT+CSMP=17,167,0,241\r\n"); //数据编码格式
__delay_cycles(1000000);
Uart1WriteStr("AT+CMGS=\"18896115258\"\r\n"); //设置接收短信的电话号码
__delay_cycles(1000000);
Uart1WriteStr("NiHao,ZheShi#\r\n"); //信息内容
__delay_cycles(1000000);
Uart1WriteStr("YiTiaoCeShiDuanXin#\r\n");
UCA1TXBUF = 0x1a; //信息内容结束符
while (!(UCA1IFG & UCTXIFG));
__delay_cycles(1000000);
__delay_cycles(1000000);
__delay_cycles(1000000);
__delay_cycles(1000000);
k=0;
P1OUT &= ~BIT0;
}
else
P1OUT |= BIT0;
break;
default:
break;
}
}
AD读取的值
这是为什么呢?哪里的问题 ?
#include <msp430f5529.h>
#define uchar unsigned char
void Uart1WriteStr(uchar *str); //UART-A1向串口写一个字符
volatile unsigned int results[8];
int k=0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
P1DIR|=BIT0; //p1.0作指示灯
P7SEL|=BIT1;
ADC12CTL0&=~ENC; //复位,开始对ADC12CTL0,ADC12CTL1设置
ADC12CTL0=ADC12ON+MSC+SHT0_4; //打开转换内核,多次转换,采样定时确定
ADC12CTL1=CONSEQ_2+SHP+CSTARTADD_4; //单通道多次转换,使用定时器触发采样
//从第4个存储器开始
ADC12MCTL4=INCH_0; //参考电压默认,通道0
ADC12IE=0x0010; //容许通道转换完成中断,存储器4中断容许,0001 0000
ADC12CTL0|=ENC; //使能转换
//UART(); //uart中断配置
P4SEL |=BIT4+BIT5; // P4.4 P4.5 = USCI_A1 TXD/RXD
UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
UCA1CTL1 |= UCSSEL_2; // SMCLK=1048576HZ=1MHZ
UCA1BR0 = 0x06; // (see User's Guide)
UCA1BR1 = 0x00; // 9600bps
UCA1MCTL |= UCBRS_0 + UCBRF_13 + UCOS16; // Modulation UCBRSx=0, UCBRFx=13
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA1IE |= UCRXIE + UCTXIE; // Enable USCI_A1 RX,TX interrupt
while(1)
{
ADC12CTL0|=ADC12SC; //启动转换
__bis_SR_register(LPM4_bits + GIE); // Enter LPM0, interrupts enabled
}
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
_EINT();
static unsigned char index=0;
switch(__even_in_range(ADC12IV,34))
{
case 6:results[index]=ADC12MEM0; //读取转换结果
if(results[index]>0)
{
k=1;
}else k=0;
index++;
if(index==8)
{
index=0;
}
default:break;
}
}
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
{
_EINT();
switch(__even_in_range(UCA1IV,4))
{
case 0:
break;
case 2: //中断向量2--接收中断
while (!(UCA1IFG & UCTXIFG)); //USCI A1是否接收完成
UCA1TXBUF = UCA1RXBUF; //在串口助手中回显
break;
case 4: //中断向量4--发送中断
if(k==1)
{
__delay_cycles(1000000);
__delay_cycles(1000000);
__delay_cycles(1000000);
__delay_cycles(1000000);
Uart1WriteStr("AT\r\n"); //AT测试
__delay_cycles(1000000); //延时1s,下同
Uart1WriteStr("AT\r\n"); //同步模块波特率
__delay_cycles(1000000);
Uart1WriteStr("AT+CSCS=\"GSM\"\r\n"); //编码设置
__delay_cycles(1000000);
Uart1WriteStr("AT+CMGF=1\r\n"); //选择短消息格式
__delay_cycles(1000000);
Uart1WriteStr("AT+CSMP=17,167,0,241\r\n"); //数据编码格式
__delay_cycles(1000000);
Uart1WriteStr("AT+CMGS=\"18896115258\"\r\n"); //设置接收短信的电话号码
__delay_cycles(1000000);
Uart1WriteStr("NiHao,ZheShi#\r\n"); //信息内容
__delay_cycles(1000000);
Uart1WriteStr("YiTiaoCeShiDuanXin#\r\n");
UCA1TXBUF = 0x1a; //信息内容结束符
while (!(UCA1IFG & UCTXIFG));
__delay_cycles(1000000);
__delay_cycles(1000000);
__delay_cycles(1000000);
__delay_cycles(1000000);
k=0;
P1OUT &= ~BIT0;
}
else
P1OUT |= BIT0;
break;
default:
break;
}
}
6
一定是你的K哪里给赋值啦 你找找看
ADC12IE=0x0010;
······很久以前的了
你的意思是这句中断导致的问题吗
ADC12MCTL4=INCH_0; //参考电压默认,通道0
这里也看着不太对,,,我也用的F5529,,AD的部分跟你写的不太一样
不一样很正常