gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:08:19 25 4
gpt4 key购买 nike

我目前正在尝试将 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

最佳答案

我在不同的传感器上运行了该程序(我批量购买了它们)并且该程序运行完美。应该注意的是,当您以折扣价购买一堆传感器时,它们不会全部起作用。

关于CCS/MSP430FR6989 : Capture/Compare Interrupt not working with HR-S04 Ultrasonic Sensor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59378646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com