This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
工具/软件:Code Composer Studio
大家好、我想使用 HC-SR04传感器来运行 MSP432 Launchpad。 代码不显示任何错误、但在我运行时不显示任何值。 我正在接受培训、以便从 Timer_A_getCaptureCompareCount 中获取值、该值是超声波传感器和转换器的回显引脚、单位为厘米。
如何获得以厘米为单位的回波宽度?
#include
#include
#include //精确宽度整数类型
#include //驱动程序库
/* Timer_A 连续模式配置参数*/
CONST Timer_A_ContinuousModeConfig ContinuousModeConfig =
{
Timer_A_CLOCKSOURCE_SMCLK、// SMCLK 时钟源
Timer_A_CLOCKSOURCE_DEVIDER_1、// SMCLK/1 = 3MHz
Timer_A_TAIE_INTERRUPT_DISABLE、//禁用计时器 ISR
Timer_A_skip_clear //向上清除计数器
};
/* Timer_A 捕获模式配置参数*/
CONST Timer_A_CaptureModeConfig CaptureModeConfig =
{
Timer_A_CAPTURECOMPARE 寄存器_1、// CC 寄存器2
Timer_A_CAPTUREMODE_RISE_EDGE、//上升沿和下降
Timer_A_CAPTURE_INPUTSELECT_CCIxA、// CCIxA 输入选择
Timer_A_capture_synchronous、//同步捕捉
Timer_A_CAPTURECMPARE 中断_ENABLE、//启用中断
Timer_A_OUTPUTMODE_OUTBITVALUE //输出位值
};
静态空延迟(uint32_t loop)
{
volatile uint32_t i;
对于(i = 0;i < loop;i++);
}
int meas1 = 0;
int meas2 = 0;
int time1 = 0;
int distance1=0;
int main (空)
{
/*停止看门狗计时器*/
MAP_WDT_A_HOLDTimer();
TA0_N_IRQHandler();
CS_setDCOCenteredFrequency (CS_DCO_FREQUENCY 24);// 24000000 Hz
CS_initClockSignal (CS_MCLK、CS_DCOCLK_select、CS_clock_divider);// 24000000 Hz
CS_initClockSignal (CS_SMCLK、CS_DCOCLK_select、CS_clock_divider);// 3000000 Hz
/*将 P2.4配置为捕捉的外设输入*/
//GPIO_setPeripheralModuleFunctionOutputPin (GPIO_PORT_P2、GPIO_PIN4、
// GPIO_PRIMARY_MODULE_FUNCTION);
GPIO_setPeripheralModuleFunctionInputPin (GPIO_PORT_P2、GPIO_PIN4、GPIO_PRIMARY_MODULE_Function);
GPIO_setAsOutputPin (GPIO_PORT_P1、GPIO_PIN5);
/*配置捕获模式*/
Timer_A_initCapture (timer_A0_BASE、&captureModeConfig);
/*配置连续模式*/
Timer_A_configureContinuousMode (timer_A0_BASE、连续模式配置);
/*启用中断*/
INTERRUPT_enableInterrupt (INT_TA0_N);
interrupt_enableMaster();
/*启动 Timer32 */
Timer32_initModule (TIMER32_0_base、TIMER32_prescaler_1、TIMER32_32位、TIMER32_PERiod_MODE);
Timer32_disableInterrupt (TIMER32_0_BASE);
Timer32_setCount (TIMER32_0_BASE、1);
Timer32_startTimer (TIMER32_0_base、true);
/*在连续模式下启动 Timer_A0 */
Timer_A_startCounter (timer_A0_BASE、timer_A_Continuous_mode);
while (1)
{
GPIO_setOutputHighOnPin (GPIO_PORT_P1、GPIO_PIN5);
Timer32_setCount (TIMER32_0_BASE、24 * 10);
while (Timer32_getValue (TIMER32_0_BASE)> 0);//等待10us
GPIO_setOutputLowOnPin (GPIO_PORT_P1、GPIO_PIN5);//软件延迟
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
延迟(40000);
}
}
void TA0_N_IRQHandler (void)
{
INT Rising = 0;
Timer_A_clearCaptureCompareInterrupt (TIMER_A0_BASE、TIMER_A_CAPTURECOMPARE 寄存器_1);
if (P2IN&0x10) rising=1;否则 rising=0;
if (上升)//开始
{
meas1 = Timer_A_getCaptureCompareCount (timer_A0_BASE、timer_A_CAPTUREMPARE 寄存器_1);
// Timer_A_clearCaptureCompareInterrupt (timer_A0_BASE、timer_A_CAPTURECOMPARE 寄存器_1);
}
其他
{
meas2 = Timer_A_getCaptureCompareCount (timer_A0_BASE、timer_A_CAPTURECMPARE 寄存器_1);
Time1 = meas2-meas1;
//distance1=34400*time1/2/375000;
距离1 = time1/58;
printf ("已接收:%d\n"、距离1);
printf ("Meas %d\n"meas2);
// Timer_A_clearCaptureCompareInterrupt (timer_A0_BASE、timer_A_CAPTURECOMPARE 寄存器_1);
}
}