程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 應用UART與PC通訊完成msp430g2553單片機超聲波測距示例

應用UART與PC通訊完成msp430g2553單片機超聲波測距示例

編輯:關於C++

應用UART與PC通訊完成msp430g2553單片機超聲波測距示例。本站提示廣大學習愛好者:(應用UART與PC通訊完成msp430g2553單片機超聲波測距示例)文章只能為提供參考,不一定能成為您想要的結果。以下是應用UART與PC通訊完成msp430g2553單片機超聲波測距示例正文


實用於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();
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved