本帖最后由 执着666666 于 2019-7-12 16:26 编辑
请教各位大佬,为什么我用MSP430F149, HC_SR04测距,中断里的S值一直不能返回到主程序中。液晶屏一直显示0M,到底错哪里了
#include <msp430x14x.h>
#include "cry1602.h"
#include"SR04.H"
#define RISE 1
#define FALL 0
unsigned char ASCII[15] ={'0','1','2','3','4','5','6','7','8','9','.','-','M'};
unsigned char disbuff[4]={ 0,0,0,0,};
unsigned int TA_Overflow_Cnt;//TA溢出次数存放变量
unsigned long int Period;//脉宽存放结果变量
uint S;
uint RiseCapVal;//上升沿时刻捕获值存放变量
uchar Edge=1;//当前触发沿
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;
//初始化液晶
Clock_Init(); //系统时钟设置
Port_init(); //系统初始化,设置IO口属性
delay_ms(100); //延时100ms
LCD_init(); //液晶参数初始化设置
LCD_clear(); //清屏
//初始化SR04和TIMEA
Init_IOch();
Init_TimerA();
_EINT();
LCD_write_str(0,0,"ditance:");
while(1)
{
void SR_Start();
while(!(P1IN&BIT2));
disbuff[0]=S%1000/100;
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%10 %10;
LCD_write_char(0, 1, ASCII[disbuff[0]]);//disbuff[0]
LCD_write_char(1, 1, ASCII[10]); //显示点
LCD_write_char(2, 1, ASCII[disbuff[1]]);
LCD_write_char(3, 1, ASCII[disbuff[2]]);
LCD_write_char(4, 1, ASCII[12]); //显示M
delay_ms(100);
}
}
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A(void)
{
switch(TAIV)
{
case 2 :if(Edge==RISE)
{
RiseCapVal = TACCR1;
TACCTL1 |= CAP+CM_2+CCIS_0+SCS+CCIE;//改为下降沿捕获
Edge = FALL;//下降沿捕获标志
}
else
{
RiseCapVal = RiseCapVal;
//_DINT();
Period = TACCR1-RiseCapVal;
S=(Period*17)/100;
TACCR1 = 0;
TACCTL1|= CAP+CM_1+CCIS_0+SCS+CCIE;//改为上升沿捕获
Edge = RISE;//上升沿捕获标志
// _EINT();
}
break;
case 4 : break;
case 10: TA_Overflow_Cnt++;
break;
|