请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:EK-TM4C123GXL 主题中讨论的其他器件:Energia
大家好、
我已将超声波传感器连接到 MCU、并使用以下简单代码刷写 MCU、以获取超声波距离测量并通过 UART 发送:
#include "Wire.h"
//处理传入串行数据
#include "Messenger.h"
//包含各种数据类型
#include 的最大限制定义
Messenger Messenger_Handler = Messenger ();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//DMP 选项
//set true if DMP init was successful
bool dmpReady = false;
//保存来自 MPU
uint8_t mpuIntStatus 的实际中断状态字节;
//每个器件操作
uint8_t devStatus 后的返回状态;
//预期的 DMP 让大小 uint16_t
packetSize;
//当前所有字节的计数 uint8_t
FIFO;/uinto64_t buffer
float YPR[3];
//========================================================================================
//===中断检测例程===========================================================================================
易失性 bool mpuInterrupt = false;//指示 MPU 中断引脚是否已变为高
电平 void dmpDataReady(){
mpuInterrupt = true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//超声波引脚定义
const int echo = 9,Trig = 10;
长时长,cm;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//时间更新变量
unsigned long LastUpdateMicrosecs = 0;
unsigned long LastUpdateMillisecs = 0;
unsigned long CurrentMicrosecs = 0;
unsigned long MicrosecsSinceLastUpdate = 0;
float SetupSecondsSinceLastUpdate = 0;
//设置串行、编码器、超声波、MPU6050和重置函
数 void setup()
{
// Init Serial Port 115200;
Serial.begin(115200);
//Setup Reset Pins
SetupReset();
//设置 Messenger
Messenger_Handler.attach(OnMssageCompled);
}//SetupEncoders
() Definition
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Setup UltrasonicsSensor () function
void SetupUltrasonic ()
{
pinMode (Trig、output);
pinMode (echo、input);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Setup Reset () function
void SetupReset ()
{
pinMode (green_LED、output);
pinMode (reset_PIN、output);
///将 RESET 引脚连接到 launchpad 的 RESET 引脚、其第16引脚
digitalWrite (reset_PIN、high);//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//main loop
void loop ()
{
//从串行端口读取
read_from _Serial ();
//通过串行端口发送时间信息
Update_time ();
//通过串行端口
Update_Ultra_Sonic 发送超声波值();////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//read from Serial Function
void Read_ffrom _Serial()
{
while (Serial.available ()>0)
{
int data = Serial.Read();
Messenger_Handler.process(data);
}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//OnMssg 完整函数定义
void OnMssageeding()
{
char reset[]="r";
char set_speed[]="s";
if (Messenger_Handler.checkString(reset)
){
Serial.println ("已完成重置");
Reset ();
}
if (Messenger_Handler.checkString(set_speed));
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Reset Function
void Reset ()
{
digitalWrite (green_LED、HIGH);
Delay (1000);
digitalWrite (reset_PIN、low);
digitalWrite (green_LED、low);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//将通过串行端口
void Update_Ultra_Sonic ()
{
digitalWrite (Trig、low);
delayMicroseconds (2);
digitalWrite (Trig、high)更新超声波传感器;
delayMicroseconds (10);
digitalWrite (Trig、low);
//回显引脚用于从 ping 读取信号)):高
//脉冲,其持续时间为
从发送 ping 到接收来自对象的回显的时间(以微秒为单位)。
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////
//更新时间函数
void Update_time()
{
CurrentMicrosecs = micros();
LastUpdateMillisecs =毫秒();
MicrosecsSinceLastUpdate = CurrentMicrosecs - LastUpdateMicrosecs;
if (MicrosecsSinceLastUpdate<0)
{
MicrosecsinceUpdate= INT_min - CurrentUpdateMicroseconds"(<100Sinc.LastSerial
)
;Microsec.LastMicrosecrateMicroprint.Sinc.Sinc.Sincr.Sinct (<0);Microsec.CurrentSinceSerial = Microsec.Sinc.SincrateMicroUpdate.Sinc.Sinc.Sinc.Sinc.Sinct (<0)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//将微秒转换为厘米
长微秒数到摄氏度(长微秒)
{
//声音速度为340 m/s 或29 microseconds /厘米。
// Ping 来回移动,因此要找到
//对象的距离,我们需要的距离是所行驶距离的一半。
返回微秒/ 29 / 2;
}
因此、我已将超声波传感器连接到5V、GND 和 PA7引脚、然后在闪存软件后、我将模式更改为器件、并使用引脚 PB1发送串行消息。 然后、该 PB1引脚连接到我的 Odroid (如 rasbperry)和 UART Rx 引脚、我得到的所有值都是一些垃圾值。 我在两个器件上都使用了115200的波特率。