0

我正在尝试与德州仪器 MSP430 Launchpad 上的 HM-10 芯片建立 UART 通信,但遇到了一个非常基本的问题。

我想要实现的是通过UART向HM-10发送一个“AT”,并收到一个答案。顺便说一句,这是我在这里找到的代码,我为了我的目的做了一些修改。

#include "msp430g2553.h"

const char string[] = { "AT" };
unsigned int i;

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog

  //------------------- Configure the Clocks -------------------//

  if (CALBC1_1MHZ==0xFF)   // If calibration constant erased
     {
        while(1);          // do not load, trap CPU!!
     }

   DCOCTL  = 0;             // Select lowest DCOx and MODx settings
   BCSCTL1 = CALBC1_1MHZ;   // Set range
   DCOCTL  = CALDCO_1MHZ;   // Set DCO step + modulation

  //---------------- Configuring the LED's ----------------------//

   P1DIR  |=  BIT0 + BIT6;  // P1.0 and P1.6 output
   P1OUT  &= ~BIT0 + BIT6;  // P1.0 and P1.6 = 0

  //--------- Setting the UART function for P1.1 & P1.2 --------//

   P1SEL  |=  BIT1 + BIT2;  // P1.1 UCA0RXD input
   P1SEL2 |=  BIT1 + BIT2;  // P1.2 UCA0TXD output

  //------------ Configuring the UART(USCI_A0) ----------------//

   UCA0CTL1 |=  UCSSEL_2 + UCSWRST;  // USCI Clock = SMCLK,USCI_A0 disabled
   UCA0BR0   =  104;                 // 104 From datasheet table-
   UCA0BR1   =  0;                   // -selects baudrate =9600,clk = SMCLK
   UCA0MCTL  =  UCBRS_1;             // Modulation value = 1 from datasheet
   //UCA0STAT |=  UCLISTEN;            // loop back mode enabled
   UCA0CTL1 &= ~UCSWRST;             // Clear UCSWRST to enable USCI_A0

  //---------------- Enabling the interrupts ------------------//

   IE2 |= UCA0TXIE;                  // Enable the Transmit interrupt
   IE2 |= UCA0RXIE;                  // Enable the Receive  interrupt
   _BIS_SR(GIE);                     // Enable the global interrupt

   i = 0;
   UCA0TXBUF = string[i];                  // Transmit a byte

   _BIS_SR(LPM0_bits + GIE);         // Going to LPM0
}

  //-----------------------------------------------------------------------//
  //                Transmit and Receive interrupts                        //
  //-----------------------------------------------------------------------//

  #pragma vector = USCIAB0TX_VECTOR
  __interrupt void TransmitInterrupt(void)
  {
    P1OUT  ^= BIT0;//light up P1.0 Led on Tx
    if (i == sizeof string - 1)
    {
    UC0IE &= ~UCA0TXIE;
    }
    UCA0TXBUF = string[i++];
  }

  #pragma vector = USCIAB0RX_VECTOR
  __interrupt void ReceiveInterrupt(void)
  {
    // light up P1.6 LED on RX
    if (UCA0RXBUF == 'O')
    {
        P1OUT  ^= BIT6;
    }
    IFG2 &= ~UCA0RXIFG; // Clear RX flag
  }

根据数据表,我应该收到此命令的 OK 答案。

如果 RX 缓冲区中有一个“O”,我希望我的板上的 LED 会亮起,但这不会发生。

使用 Code Composer,我还通过向 RX 中断添加断点来验证确实没有 RX 应答。

我相信这完全是一个软件问题,这就是我把它放在这里的原因。我正在使用正确的跳线旋转(http://xanthium.in/Serial-Communication-MSP430-UART-USCI_A)并且RX连接到TX,反之亦然。

如果您能指出我是否在概念上做错了什么,或者我只是犯了错误,我将不胜感激。谢谢!

4

2 回答 2

1

我在中断例程 TransmitInterrupt() 中发现了一个问题:您应该使用UCA0TXBUF = string[++i];,因为使用“i++”会传输两次字母“A”。关于 sizeof(string) 的测试也应该被修改。

然后,我不会太相信数据表。我认为,尽管数据表说什么,发送到调制解调器的每个命令都必须由 CR (\r) 终止,否则调制解调器如何从“AT+RESET”中识别“AT”?我不太确定,但数据表似乎不是高质量的。无论如何,这是一个快速测试(在字符串末尾添加 \r)。

最后,CTS 和 RTS 信号也可以发挥作用。一些调制解调器想要 RTS 断言,其他调制解调器不关心,并且术语有时令人困惑:当数据表说 RTS 时,它是指调制解调器的RTS 还是主机的RTS ?我希望这会有所帮助,你应该做一些科学尝试。

于 2017-02-03T18:26:06.950 回答
0

我想对于未来使用 HM-10 设备的每个人来说,我想回答这个问题,因为我认为它有自己的迷你文学,这首先令人沮丧,但后来我有点喜欢它带来的挑战大部头书。

有些问题与硬件相关,因此这篇文章可能需要移至嵌入式工程部分。(后果很严重——在用示波器检查之前你不能 100% 确定)

了解您的硬件 - HM-10 有很多版本,它使我们的版本需要一个额外的分压器,因为它具有 3.3V 高逻辑电平而不是 5V。这个网站是一个很棒的起点。虽然,我们的结果是 MLT-BT05,它是克隆的克隆。它的固件没有 iBeacon 功能,因此如果您不想重启电源,那么您可能应该避免使用此功能。

关于编码位,最重要的是检查\n,\r和\n\r,正如上面linuxfan简要提到的它的重要性,因为一些设备需要它。最好的起点是AT,如果它有效,然后使用AT+HELP并找到版本,通常是 AT+VERSION 命令,这样您就可以 100% 确定您拥有哪个芯片。

目前它是在 Arduino 上进行原型设计的,但我将在 MSP430 上完成后立即包含工作代码。

Arduino代码:

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(9, 10); // RX, TX
  char commandbuffer[50];
      int j = 0;
void setup()
{

  memset(commandbuffer, 0, sizeof(commandbuffer));
  analogWrite(12, 255);
  analogWrite(11, 0);
  // Start the hardware serial port
  Serial.begin(19200);
  bluetooth.begin(9600);
  // un REM this to set up a Master and connect to a Slave

  Serial.println("BLE CC41A Bluetooth");
  Serial.println("----------------------------------");
  Serial.println("");
  Serial.println("Trying to connect to Slave Bluetooth");
  delay(1000);
  bluetooth.println("AT"); // just a check
  delay(2000);


  bluetooth.println("AT+NAMEHIST");
  delay(2000);
  bluetooth.println("AT+ROLE0");
  delay(2000);
    bluetooth.println("AT+INQ"); // look for nearby Slave
    delay(5000);
    bluetooth.println("AT+CONN1"); // connect to it */
}
void loop()
{
  bluetooth.listen();
  // while there is data coming in, read it
  // and send to the hardware serial port:
  while (bluetooth.available() > 0) {
    char inByte = bluetooth.read();
    Serial.write(inByte);
  }
  // Read user input if available.
  if (Serial.available()) {
    delay(10); // The DELAY!
    char temp = Serial.read();
    if (temp == '\n')
    {
      bluetooth.println(commandbuffer);
      Serial.println(commandbuffer);
      memset(commandbuffer, 0, sizeof(commandbuffer));
      j = 0; // Reset
    }
    else
    {
      commandbuffer[j++] = temp;
    }
    delay(500);
  }
于 2017-03-31T22:39:31.347 回答