Using UART and PC communication to achieve msp430g2553 single chip ultrasonic ranging example

  • 2020-04-02 02:22:31
  • OfStack

Applicable to msp430g2553 SCM   Hc-sr04 ultrasonic ranging module was used, and UART was used to communicate with PC.


#include <msp430.h>
long current_time;//Last time measured

#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

#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 
}

void init_uart()                        //USCI initializes the function
{
  UCA0CTL1 |= UCSWRST;                  //Initializes the serial port register & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent
  UCA0CTL1 |= UCSSEL_2;                 //Select subsystem clock 1.045MHz
  UCA0BR0 = 0x6d;                       //The baud rate is 9,600
  UCA0BR1 = 0x00;
  P1SEL |= UART_TXD;
  P1SEL2 |= UART_TXD;
  P1DIR |= UART_TXD;                    //Set P1.2 as the second function
  UCA0CTL1 &= ~UCSWRST;                 //End of initialization
}
long cal_distance()                     //Calculate the distance by measuring the time
{
 return (long)((340*(0.5000*current_time/SYS_CLK)*MIN_UNIT_TO1M));
}
void delay()    //A rough delay
{
 unsigned char i,j;
 for(i=124;i>0;i--)
  for(j=8;j>0;j--);
}
static char * translater(long distance)  //Stores the measured distance as a string
{
  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++)            //Transform core algorithm
  {
    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()                         //Send out an ultrasound
{
 if(TA1CCTL1 & COV)
  TA1CCTL1 &= ~COV;
 if(!(P1IN & BIT3))                    
 { 
   TA1R = 0;
   P1OUT |= TRIG;                       //Trig 10 us high level

   _EINT();
   TA1CTL |= MC_2;   //continue mode
   P1OUT &= ~TRIG;
   P1OUT |= LED_1;
   while(TA1CCTL1 & CCIFG);          //Wait for the capture interrupt to end
 }
 else 
 {
   P1OUT &= ~LED_1;
   _DINT();
 }    
}
void uart_txstring(char *string)        //UART_TX sends a string
{
  int i=0;
  while(string[i++])
  {
   switch (i)      //Invalid filter '0'
    {
     case 1:if(string[i]=='0') continue;
     case 2:if(string[i]=='0'&&string[i-1]=='0') continue;
    }
    UCA0TXBUF = string[i];
    delay();
  }
}

#pragma vector = USCIAB0TX_VECTOR
__interrupt void usci_txdistance()      //Send the measured distance to the PC
{ 
  uart_txstring("nr");
  uart_txstring(" Current ");
  uart_txstring(" distance: ");
  uart_txstring(translater(cal_distance()));
  uart_txstring(" cm");
  IE2 &= ~UCA0TXIE; 
}

#pragma vector = TIMER1_A1_VECTOR
__interrupt void capture()            
{ 
 current_time = TA1CCR1;
 TA1CTL &= ~MC_2;
 TA1CCTL1 &= ~CCIFG;           //CC1 interrupt mark bit & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent & have spent
 IE2 |= UCA0TXIE;
}

void main()
{
  WDTCTL = WDTPW + WDTHOLD;              //Shut the dog
  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();
  }
}


Related articles: