2017-02-03 82 views
0

我試圖在德州儀器MSP430 Launchpad上設置與HM-10芯片的UART通信,但是我遇到了一個非常基本的問題。爲什麼如果我從MSP430 Launchpad發送AT,HM-10不會發送OK?

我想達到的目標是通過UART發送「AT」到HM-10,並獲得一個答案。順便說一下,這是一個代碼我發現here和我稍微修改我的目的。

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

根據datasheet我應該收到此命令的確定答案。

如果在接收緩衝區的「O」,我預計LED點亮我的船上,但是這不會發生。

使用的Code Composer,我也加入了斷點到RX驗證中斷確實是有沒有RX答案。

我相信這完全是一個軟件問題,這就是爲什麼我把它放在這裏。我正在使用跳線的正確旋轉(http://xanthium.in/Serial-Communication-MSP430-UART-USCI_A),並將RX連接到TX,反之亦然。

如果您能指出我是否做了任何錯誤的想法或者我犯了什麼錯誤,我將不勝感激。謝謝!

+1

1.您必須終止 「AT」 與CR(回車); 2.也許你應該修改'UCA0TXBUF =字符串[我++]' - >'UCA0TXBUF =字符串[++ i];' – linuxfan

回答

0

我認爲對於將來與HM-10設備一起工作的每個人,我都想回答這個問題,因爲它有我自己的迷你文學,這首先令人沮喪,但後來我很喜歡它給我帶來的挑戰。

一些問題是硬件相關的,所以這篇文章可能需要轉移到嵌入式工程部分。 (大的後果 - 你不能用示波器檢查它之前是100%確定)

知道你的硬件 - HM-10萬噸的版本,它把我們的人需要一個附加分壓器,因爲它有一個3.3 V邏輯電平高而不是5V。 This website is a fantastic place to start。雖然,我們的結果是一個MLT-BT05這是一個clone of a clone。它的固件上沒有iBeacon功能,所以如果你不想給循環上電,那麼你應該避免這個。

關於編碼位最重要的是檢查\ n,\ r和\ n \ r,因爲linuxfan在上面簡單地提到了它的重要性,因爲有些設備需要它。開始的最好的地方是AT,如果它的工作原理,然後用AT + HELP,找到的版本,通常AT + version命令,所以你可以100%的把握這片你有鑑別。

Currenetly它是在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); 
    } 
1

我看到在中斷程序TransmitInterrupt()的一個問題:你應該,因爲使用的「i ++」你發送兩次字母「A」使用UCA0TXBUF = string[++i];。關於sizeof(string)的測試也應該修改。

話,我就會信任太多的數據表。我認爲,儘管數據表說了什麼,發送到調制解調器的每個命令都必須由CR(\ r)終止,否則調制解調器如何能從「AT + RESET」識別出「AT」?我不太確定,但數據表看起來並不是很高質量的。無論如何,這是一個快速測試(在字符串的末尾添加\ r)。

最後,CTS和RTS信號也可以發揮作用。有些調制解調器希望RTS斷言,其他調制解調器不在乎,和術語有時是令人困惑:當數據表說RTS,這是否意味着RTS的主機的調制解調器或RTS ?我希望這有助於你,你應該做一些科學嘗試。

+0

謝謝你的提示,我嘗試添加\ r和改變+ II,但是事實並非如此使它更好。 – boomkin

+0

您應該確認1)MSP430通過將其連接到PC輸出您想要的內容,以及2)調制解調器真正理解(也將其連接到PC)。謹防電壓水平。否則難以理解哪一方(msp/modem)有問題。 – linuxfan

相關問題