我目前正在尝试将HR-S04超声波距离传感器连接到MSP430FR6989。我发现代码的编写位置已经有好几次了,但是本来可以为他人使用的代码对我却不起作用。我的方案与看到其他人使用的方案之间的区别是,我没有要打印到的外部LCD,而是试图将输出显示到启动板上的板载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
我目前正在尝试将HR-S04超声波距离传感器连接到MSP430FR6989。我已经发现了写代码的位置的好几次,但据称适用于...
我在另一个传感器上运行了该程序(我批量购买了它们),该程序运行正常。应该注意的是,当您以折扣价购买一堆传感器时,它们将不会全部起作用。