簡體   English   中英

CCS/MSP430FR6989:捕獲/比較中斷不適用於 HR-S04 超聲波傳感器

[英]CCS/MSP430FR6989: Capture/Compare Interrupt not working with HR-S04 Ultrasonic Sensor

我目前正在嘗試將 HR-S04 超聲波距離傳感器連接到 MSP430FR6989。 我已經多次發現代碼的編寫位置,但據稱對其他人有效的代碼對我卻不起作用。 我的場景與其他人使用的場景之間的一個區別是,我沒有要打印到的外部 LCD,而是嘗試將輸出顯示到 Launchpad 上的板載 LCD,或者(從那時起)對我不起作用)我試圖通過 UART 將它寫入終端。 以下是我的代碼,以及我對可能存在問題的猜測:

該計划的想法是將“低谷時間”與“高谷時間”進行比較。 這應該通過存儲每次中斷發生的上升沿和下降沿的 TA0CCR0 值並使用這兩個值進行基本算術來完成。

#include <msp430fr6989.h>

#define redLED BIT0
#define greenLED BIT7

#define US_TRIG         BIT4
#define US_ECHO         BIT5
#define US_MASK         US_TRIG | US_ECHO

#define FLAGS_TERM      UCA1IFG
#define RXFLAG          UCRXIFG
#define TXFLAG          UCTXIFG
#define TERM_TXBUFFER   UCA1TXBUF
#define TERM_RXBUFFER   UCA1RXBUF

unsigned int up_counter = 0,down_counter, distance_cm = 0, count = 0;

void initialize_TERM_UART(void)
{
    P3SEL1 &= ~ (BIT4|BIT5);
    P3SEL0 |= (BIT4|BIT5);
    UCA1CTLW0 |= UCSSEL_2;
    UCA1BRW = 8;
    UCA1MCTLW = UCBRS3 | UCBRS2| UCBRS0 | UCBRS6_H | UCBRS5_H;
    UCA1MCTLW &= ~UCOS16;
    UCA1CTLW0 &= ~ UCSWRST;
    UCA1IE |= UCRXIE;
    UCA1IFG &= ~RXFLAG;
}

void uart_write_char_TERM(unsigned char ch)
{
    // Wait for any ongoing transmission to complete
    while ( (FLAGS_TERM & TXFLAG)==0 ) {}
    // Write the byte to the transmit buffer
    TERM_TXBUFFER = ch;
}

void uart_digit_sep_TERM(unsigned int n)
{
    unsigned int digit;
    if (n >= 10000)
        {
            digit = (n/10000) % 10;
            uart_write_char_TERM(digit + '0');
        }
        else
            uart_write_char_TERM('0');
    if (n >= 1000)
        {
            digit = (n/1000) % 10;
            uart_write_char_TERM(digit + '0');
        }
        else
            uart_write_char_TERM('0');
    if (n >= 100)
    {
        digit = (n/100) % 10;
        uart_write_char_TERM(digit + '0');
    }
    else
        uart_write_char_TERM('0');
    if (n >= 10)
    {
        digit = (n/10) % 10;
        uart_write_char_TERM(digit + '0');
    }
    else
        uart_write_char_TERM('0');

    digit = (n % 10);
    uart_write_char_TERM(digit + '0');
    uart_write_char_TERM(' ');
}

#pragma vector=TIMER0_A0_VECTOR
__interrupt void TimerA0(void)
{
    P1OUT ^= redLED;
    P9OUT ^= greenLED;
    if (TA0CCTL0 & CCI)            // Raising edge
    {
        up_counter = TA0CCR0;      // Copy counter to variable
    }
    else                        // Falling edge
    {
        // Formula: Distance in cm = (Time in uSec)/58
        distance_cm = (TA0CCR0 - up_counter)/58;
        uart_digit_sep_TERM(distance_cm);
    }
    TA0CCTL0 &= ~CCIFG;
    TA0CTL &= ~TAIFG;           // Clear interrupt flag - handled
}
void timer_init()
{
    /* Timer A0 configure to read echo signal:
    Timer A Capture/Compare Control 0 =>
    capture mode: 1 - both edges +
    capture synchronize +
    capture input select 0 => P1.5 +
    capture mode +
    capture compare interrupt enable */
    TA0CCTL0 |= CM_3 + SCS + CCIS_0 + CAP + CCIE;
    TA0CCTL0 &= ~ CCIFG;

    /* Timer A Control configuration =>
    Timer A clock source select: 1 - SMClock +
    Timer A mode control: 2 - Continous up +
    Timer A clock input divider 0 - No divider */
    TA0CTL |= TASSEL_2 + MC_2 + ID_0;
}

int main()
{
    WDTCTL = WDTPW | WDTHOLD;       // Stop Watch Dog Timer
    PM5CTL0 &=  ~LOCKLPM5;

    P1DIR |= redLED + US_TRIG;
    P9DIR |= greenLED;
    P1OUT &= ~(redLED | US_TRIG) ;
    P9OUT |= greenLED;

    P1DIR &= ~US_ECHO;        // Input direction for echo from sensor
    P1SEL1 |= US_ECHO;                // set US_ECHO as trigger for Timer from Port-1
    P1SEL0 |= US_ECHO;

    // Initialize timer for Ultrasonice distance sensing
    timer_init();
    initialize_TERM_UART();
    _enable_interrupts();

    while (1)
    {
        // measuring the distance
        P1OUT |= US_TRIG;                 // assert
        __delay_cycles(15);                 // 10us wide
        P1OUT &= ~US_TRIG;                 // deassert
        //__delay_cycles(60000);            // 60ms measurement cycle
        __delay_cycles(500000);             // 0.5sec measurement cycle
        //uart_digit_sep_TERM(i);
        //i++;
    }

}

當我在 TimerA0(void) ISR 內設置硬件斷點的情況下運行上述代碼時,斷點永遠不會觸發,因為程序永遠不會進入 ISR。
這是我第一次使用捕獲/比較中斷,所以我的第一個想法是我可能錯誤地配置了中斷/定時器——但這很奇怪,因為其他人在類似的網站上發布了代碼,說代碼適用於他們。
我的另一個想法是,我的定時器配置和 UART 配置可能會以某種方式發生沖突,但這不應該影響 ISR 是否被觸發。
不確定它是否有幫助,但我在 OSX 10.10.5 上運行 CCS 9.2.0.00013

我在不同的傳感器上運行該程序(我是批量購買的)並且該程序運行完美。 應該注意的是,當您以折扣價購買一堆傳感器時,它們不會全部起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM