如何快速通过STM32单片机实现ST传感器的驱动测试
<div class='showpostmsg'><p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">今天我将要向大家介绍如何快速通过</span>STM32<span style="font-family:宋体">单片机实现</span>ST<span style="font-family:宋体">传感器的驱动测试。本次我们使用的传感器是</span>LSM6DSOX<span style="font-family:宋体">,因为</span>SensorTile.box<span style="font-family:宋体">开发板集成了这颗传感器,所以我将用</span>SensorTile.box<span style="font-family:宋体">来演示,如何通过开发板上的</span>STM32L4R9<span style="font-family:宋体">单片机驱动</span>LSM6DSOX<span style="font-family:宋体">,实现加速度的陀螺仪的数据读取。</span></span></span></p><p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">因为</span>ST<span style="font-family:宋体">传感器提供了非常强大的传感器驱动和演示工程,在</span>STM32<span style="font-family:宋体">单片机里甚至不用写一行代码就可以完成传感器测试。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">本帖适合对传感器或者</span>STM32<span style="font-family:宋体">单片机不是特别熟悉的新手,对于有一定的开发经验的网友,本帖所授技术一搬经过自己简单摸索就能完成,看到这里下边内容应该就不需要再看了。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">以下内容同样适用于</span>ST<span style="font-family:宋体">的其它传感器和</span>STM32<span style="font-family:宋体">其它单片机,不同之处在于连接传感器的端口要根据传感器的实际连接情况来配置。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">首先在</span>ST<span style="font-family:宋体">官网找到</span>LSM6DSOX<span style="font-family:宋体">的产品页面,然后点开</span>Tool&Software<span style="font-family:宋体">选项卡,找到</span>C-Driver-MEMS<span style="font-family:宋体">软件资源,点开。找到</span>Get from Github<span style="font-family:宋体">跳转到</span>Github<span style="font-family:宋体">网站,再选</span>Code->Download ZIP<span style="font-family:宋体">,将</span>STMems_Standard_C_drivers<span style="font-family:宋体">驱动包下载到本地。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"> <span style="font-family:宋体">将下载好的</span>STMems_Standard_C_drivers<span style="font-family:宋体">解压,找到</span>LSM6DSOX<span style="font-family:宋体">文件夹,在</span>drivers<span style="font-family:宋体">下的</span>2<span style="font-family:宋体">个文件就是</span>lsm6dsox<span style="font-family:宋体">的驱动文件。再打开</span>example<span style="font-family:宋体">文件夹,可以看到很多演示代码,本次使用</span>lsm6dsox_read_data_polling.c<span style="font-family:宋体">文件实现传感器数据的轮询读取,再通过</span>STM32L4R9<span style="font-family:宋体">的</span>USB<span style="font-family:宋体">虚拟串口将数据发送到电脑。</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"> </span></span></p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">打开</span>STM32CubeMX<span style="font-family:宋体">,创建一个工程,选择对应的单片机型号后点</span>Start Project<span style="font-family:宋体">,在</span>Pinout&Configuration<span style="font-family:宋体">里找到</span>Connectivity<span style="font-family:宋体">下的</span>SPI1<span style="font-family:宋体">,</span>Mode<span style="font-family:宋体">选择</span>Full-Duplex Master<span style="font-family:宋体">,修改</span>Prescaler<span style="font-family:宋体">数值使时钟频率符合要求,将</span>Data Size<span style="font-family:宋体">设置为</span>8 Bit<span style="font-family:宋体">。根据</span>SensorTile.box<span style="font-family:宋体">原理图设置</span>SPI<span style="font-family:宋体">对应的引脚,设置</span>LSM6DSOX<span style="font-family:宋体">的</span>CS<span style="font-family:宋体">引脚,设置</span>CS<span style="font-family:宋体">引脚的标签比如</span>CS_LSM6DSOX<span style="font-family:宋体">。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">接着在</span>Pinout&Configuration<span style="font-family:宋体">里找到</span>Connectivity<span style="font-family:宋体">下的</span>USB_OTG_FS<span style="font-family:宋体">,</span>Mode<span style="font-family:宋体">选择</span>Device_Only<span style="font-family:宋体">,其它默认。在</span>Middleware<span style="font-family:宋体">里找到</span>USB_DEVICE<span style="font-family:宋体">,</span>Class For FS IP<span style="font-family:宋体">设置为</span>Communication Device Class(Virtual Port Com)<span style="font-family:宋体">其它默认,这样</span>USB<span style="font-family:宋体">虚拟串口就设置好了。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"> </span></span></p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">在</span>Clock Configuration<span style="font-family:宋体">里把时钟改为</span>120MHz<span style="font-family:宋体">,</span>CLK48 Clock Mux<span style="font-family:宋体">选择</span>HSI48<span style="font-family:宋体">,接着在</span>Project Manager<span style="font-family:宋体">的</span>Project<span style="font-family:宋体">里给工程起一个名字,选择对应的工程路径,选择对应的</span>IDE<span style="font-family:宋体">,这里我选择了</span>STM32CubeIDE<span style="font-family:宋体">,它是</span>ST<span style="font-family:宋体">官方推出的基于</span>eclipse<span style="font-family:宋体">开发的免费工具,非常好用。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">然后在</span>Code Generator<span style="font-family:宋体">里把</span>Copy all used libraries into the project folder<span style="font-family:宋体">改为</span>Copy only the necessary library files<span style="font-family:宋体">,只复制用到的库,这样生成的工程会小一些。点击右上角的</span>GENERATE CODE<span style="font-family:宋体">按钮生成工程文件和初始化代码。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"> <span style="font-family:宋体">工程生成好后点</span>Open Project<span style="font-family:宋体">,使用对应的</span>IDE<span style="font-family:宋体">打开工程文件。在</span>STM32CubeIDE<span style="font-family:宋体">里将</span>lsm6dsox_read_data_polling.c<span style="font-family:宋体">和</span>lsm6dsox_reg.c<span style="font-family:宋体">复现到</span>Src<span style="font-family:宋体">文件夹,将</span>lsm6dsox_reg.h<span style="font-family:宋体">复制到</span>Inc<span style="font-family:宋体">文件夹。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">打开</span>lsm6dsox_read_data_polling.c<span style="font-family:宋体">引用</span>main.h<span style="font-family:宋体">头文件,添加</span>hspi1<span style="font-family:宋体">的引用,将</span>dev_ctx.handle<span style="font-family:宋体">设置成</span>hspi1<span style="font-family:宋体">,去掉</span>platform_write<span style="font-family:宋体">和</span>platform_read<span style="font-family:宋体">函数里和</span>SPI<span style="font-family:宋体">无关的代码,将</span>CS<span style="font-family:宋体">操作改为</span>CS_LSM6DSOX<span style="font-family:宋体">对应的端口和引脚,去掉</span>tx_com<span style="font-family:宋体">函数里除</span>CDC_Transmit_FS<span style="font-family:宋体">以外的代码。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">在</span>main.c<span style="font-family:宋体">文件里引用并在</span>main<span style="font-family:宋体">函数里执行</span>lsm6dsox_read_data_polling<span style="font-family:宋体">函数。工程设置里勾选</span>use float with printf from newlib-nano(-u _printf_float)<span style="font-family:宋体">解除编译器警告,然后编译工程。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"> <span style="font-family:宋体">连接调试器和开发板,下载固件到</span>STM32<span style="font-family:宋体">单片机,打开串口软件连接后便能看到对应的传感器数据。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">下期我会通过视频做一个完整的演示。</span></span></span></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">熟悉</span>STM32CubeMX<span style="font-family:宋体">工具的网友应该会发现</span>STM32CubeMX<span style="font-family:宋体">里集成的</span>X-CUBE-MEMS1<span style="font-family:宋体">扩展包已经集成了几乎所有的</span>ST<span style="font-family:宋体">传感器驱动,使用</span>STM32CubeMX<span style="font-family:宋体">添加对应的传感器驱动也比较方便,选择哪种方式就看个人习惯了。</span></span></span></p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>@jinglixixi </p>
<p>可以看看</p>
nmg 发表于 2021-2-26 08:53
@jinglixixi
可以看看
<p>已看到,有参考价值。</p>
<p> </p>
感谢分享,写的非常详细 <p>感谢分享,写的非常详细</p>
<p>大佬出品必属精品</p>
页:
[1]