zoukankan      html  css  js  c++  java
  • [LaunchPad] 超声波模块测试

      1 #include  "msp430g2553.h"
      2 #include  "stdio.h"
      3 
      4 
      5 unsigned char RxFlag;
      6 unsigned char Byte_H;
      7 unsigned char Byte_L;
      8 unsigned int Dist; 
      9 unsigned int DistData[8];
     10 unsigned int DistAverage;
     11 
     12 
     13 int putchar(int ch)
     14 {
     15   while(!(IFG2&UCA0TXIFG));
     16    UCA0TXBUF=ch;
     17    return ch;
     18 }
     19 
     20 
     21  void sendChar(unsigned char c)
     22  {
     23    while(!(IFG2&UCA0TXIFG));
     24    UCA0TXBUF=c;
     25  }
     26 
     27  void sendStr(unsigned char *s)
     28  {
     29        while(*s!='\0')
     30        {
     31                sendChar(*s);
     32                s++;
     33        }
     34  }
     35  
     36  void main(void)
     37  
     38  {
     39    
     40  unsigned char i;
     41    
     42  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
     43  
     44   BCSCTL1 = CALBC1_1MHZ;                    // Set DCO
     45   DCOCTL = CALDCO_1MHZ;
     46   BCSCTL2 &= ~(DIVS_3);
     47  
     48  
     49   P1SEL = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
     50   P1SEL2 = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
     51  
     52   UCA0CTL1 |= UCSSEL_2;                     // SMCLK
     53   UCA0BR0 = 104;                            // 1MHz 9600
     54   UCA0BR1 = 0;                              // 1MHz 9600
     55   UCA0MCTL = UCBRS0;                        // Modulation   UCBRSx = 1
     56   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
     57  
     58   IE2 |= UCA0RXIE;   // Enable USCI_A0 RX interrupt
     59   
     60   
     61   RxFlag=0; 
     62 
     63   _EINT();
     64   
     65  
     66  printf("\n%s\n","========超声波模块测试=========");
     67 
     68  //__bis_SR_register(LPM0_bits + GIE);       // Enter LPM0, interrupts enabled
     69  
     70  
     71   while(1)
     72   {
     73    for(i=0;i<8;i++)
     74    {
     75      sendChar(0x55); 
     76      __delay_cycles(1000);
     77      DistData[i]=Dist;
     78    }
     79    for(i=0;i<8;i++)
     80      
     81    DistAverage += DistData[i];
     82    DistAverage = DistAverage >> 3;
     83    
     84   __delay_cycles(1200000);
     85  if(RxFlag == 0)
     86    
     87    printf("\n距离: %d  mm\n\n",DistAverage); 
     88  
     89   }
     90   
     91   
     92  }
     93  
     94  //  Echo back RXed character, confirm TX buffer is ready first
     95  
     96  #pragma vector=USCIAB0RX_VECTOR
     97  __interrupt void USCI0RX_ISR(void)
     98  {
     99 
    100  while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
    101  
    102   if(RxFlag == 0)
    103   {
    104     Byte_H=UCA0RXBUF;
    105     RxFlag=1;
    106   }
    107   else
    108   { 
    109       Byte_L=UCA0RXBUF;
    110       Dist=Byte_H*256+Byte_L;
    111       RxFlag = 0;
    112   }
    113  }
    114  

    超声波使用串口通讯,与PC串口通讯共用RXD,TXD引脚,MSP430每隔一段时间,向串口发送0x55,在PC机上显示字符‘U’,发送8次,取平均值,在PC机上会看到

    UUUUUUUU
    距离: 3170  mm

    可以知道程序是正确的,下面的工作加入超声波的测温功能,或进一步使用电平触发测量,并加入低功耗

  • 相关阅读:
    atom介绍
    举例介绍重构(译)
    java单双派机制理解
    AngularJS开发指南03:HTML编译器
    AngularJS开发指南02:引导程序
    AngularJS开发指南01:AngularJS简介
    1.angular之Hello World
    31天重构学习笔记(java版本)
    一个农夫的故事 分类: 其他 2015-01-24 16:44 104人阅读 评论(0) 收藏
    一个农夫的故事 分类: 其他 2015-01-24 16:44 103人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/sky1991/p/2653770.html
Copyright © 2011-2022 走看看