繁体   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