当前位置:主页 > 软件编程 > C代码 >

使用UART与PC通信实现msp430g2553单片机超声波测距示例

时间:2021-06-29 08:26:59 | 栏目:C代码 | 点击:

适用于msp430g2553单片机  使用到了hc-sr04超声测距模块,使用UART与PC通信。

复制代码 代码如下:

#include <msp430.h>
long current_time;//最近一次测得时间
/*MyPro*/
#define LED_1 BIT0                     
#define SW_2 BIT3                      
#define TA1_1 BIT2                      //TA0.1 HC-SR04 Echo
#define TRIG BIT4                       //HC-SR04 Trig
#define ACCU_GRD 7   //Accuracy Grade "xxx.x"+'\0'                   
#define MIN_UNIT_TO1M 1000  //1 m to 1 mm

/*TIMER0*/
#define SOUR_CLK  1045000
#define DIV_CLK_1  1
#define SYS_CLK_SIG_1  SOUR_CLK/DIV_CLK_1
#define DISTANCE 45//dm
#define TIMER_RIG_MAX 0xffff
#define MAX_TIME_1 DISTANCE*2/34*SYS_CLK_SIG_1
#ifdef  MAX_TIME_1
#define SYS_CLK SYS_CLK_SIG_1
#endif
#define UART_TXD BIT2 

void init_timer1()                     
{
  P2SEL |= TA1_1;                       //TA1.1 CCI1B be used                         
  P2DIR &= ~TA1_1;
  TA1CTL = MC_0 + TASSEL_2;             //TimerA_0 stop mode,clock=1Mhz                    
  TA1CCTL1 = CCIE + SCCI + CCIS_1 + SCS + CAP + CM_2; //TA0CC1 capture mode + down edge
}
/*UART*/
void init_uart()                        //USCI 初始化函数
{
  UCA0CTL1 |= UCSWRST;                  //初始化串口寄存器                   
  UCA0CTL1 |= UCSSEL_2;                 //选择子系统时钟 1.045MHz
  UCA0BR0 = 0x6d;                       //波特率为9600
  UCA0BR1 = 0x00;
  P1SEL |= UART_TXD;
  P1SEL2 |= UART_TXD;
  P1DIR |= UART_TXD;                    //将P1.2设为第二功能
  UCA0CTL1 &= ~UCSWRST;                 //初始化结束
}
long cal_distance()                     //通过测得时间计算距离
{
 return (long)((340*(0.5000*current_time/SYS_CLK)*MIN_UNIT_TO1M));
}
void delay()    //粗略延迟
{
 unsigned char i,j;
 for(i=124;i>0;i--)
  for(j=8;j>0;j--);
}
static char * translater(long distance)  //将测得的距离以字符串形式存储
{
  static char trans[ACCU_GRD];
  int i;
  long f;
  trans[0]=' ';
  if(distance)
  for(i=1,f=MIN_UNIT_TO1M;i<ACCU_GRD-1;i++)            //转化核心算法
  {
    if(i==4)
    {
      trans[i] = '.';                  
      continue;
    }
    trans[i] = '0'+ distance/f;
    distance %= f;
    f /= 10;
  }
  trans[ACCU_GRD-1] = '\0';                   
  return trans;
}
void once_pro()                         //发一次超声波
{
 if(TA1CCTL1 & COV)
  TA1CCTL1 &= ~COV;
 if(!(P1IN & BIT3))                   
 {
   TA1R = 0;
   P1OUT |= TRIG;                       //Trig 10 us 高电平

   _EINT();
   TA1CTL |= MC_2;   //continue mode
   P1OUT &= ~TRIG;
   P1OUT |= LED_1;
   while(TA1CCTL1 & CCIFG);          //等待 捕捉中断结束
 }
 else
 {
   P1OUT &= ~LED_1;
   _DINT();
 }    
}
void uart_txstring(char *string)        //UART_TX 发送一个串
{
  int i=0;
  while(string[i++])
  {
   switch (i)      //过滤无效 '0'
    {
     case 1:if(string[i]=='0') continue;
     case 2:if(string[i]=='0'&&string[i-1]=='0') continue;
    }
    UCA0TXBUF = string[i];
    delay();
  }
}
/*UART_ISR*/
#pragma vector = USCIAB0TX_VECTOR
__interrupt void usci_txdistance()      //向 PC 发送 测得的距离
{
  uart_txstring("\n\r");
  uart_txstring(" Current ");
  uart_txstring(" distance: ");
  uart_txstring(translater(cal_distance()));
  uart_txstring(" cm");
  IE2 &= ~UCA0TXIE;
}
/*TIMER0_INT_ISR*/
#pragma vector = TIMER1_A1_VECTOR
__interrupt void capture()           
{
 current_time = TA1CCR1;
 TA1CTL &= ~MC_2;
 TA1CCTL1 &= ~CCIFG;           //清CC1中断标志位        
 IE2 |= UCA0TXIE;
}
/*Main*/
void main()
{
  WDTCTL = WDTPW + WDTHOLD;              //关狗
  DCOCTL = 0;                           
  BCSCTL1 = CALBC1_1MHZ;
  DCOCTL  = CALDCO_1MHZ;
  P1OUT = 0;
  P2OUT = 0;
  P1REN |= SW_2;
  P2REN |= TA1_1;
  P1OUT |= SW_2;
  P1DIR &= ~SW_2;                       
  P1DIR = TRIG + LED_1;
  init_timer1();
  init_uart();
  while(1)
  {  
   int c = 8;
   while(c--)
   delay();
   if(TA1CCTL1 & CCIFG)
   TA1CCTL1 &= ~CCIFG;
   once_pro();
  }
}

您可能感兴趣的文章:

相关文章