Ansersion 发表于 2019-10-22 10:05

【ATmega4809 Curiosity Nano测评】串口按键控灯(IoT Led前篇)

本帖最后由 Ansersion 于 2019-10-22 10:07 编辑

<p style="text-align:justify"><span style="font-size:24px;"><strong><span style="font-family:Calibri"><span style="font-family:宋体"><font face="宋体">1. 简介</font></span></span></strong></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">放眼市场上现有的</font>IoT产品,其系统设计可简化如下图所示,其中&ldquo;远端&rdquo;为APP、网页等交互方式,红色虚线框中则为IoT设备。</span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><br />
<span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">本篇内容将用</font>ATmega4809 Curiosity Nano按照以下模块结构关系实现按键和串口共同控制LED明灭,并且当按键控制LED时通知串口。</span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">其中我们将用到</font>ATMega4809的</span></span><b><span style="font-size:12.0000pt"><span style="font-family:宋体"><span style="font-weight:bold">GPIO中断、定时器中断和串口中断功能。</span></span></span></b></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:24px;"><strong><span style="font-family:Calibri"><span style="font-family:宋体">2. MCC配置</span></span></strong></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">首先是添加</font>USART1串口,并且使能中断,波特率设置为9600(TTL转USB的GND、RX、TX分别接GND、PC0、PC1)</span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">然后我们添加</font>TC0定时器,并且使能溢出中断,超时时间设置为1ms。之所以需要定时器是为了记录系统时钟,从而可以辅助按键去除抖动。</span></span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">根据<a href="https://bbs.eeworld.com.cn/data/attachment/elecplay/upload/file/20190904/1567597472775226.pdf" target="_blank">官方文档</a>,</font></span></span><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">我们知道</font>PF5是LED,PF6是按键,所以我们将PF5设置为输出端口,PF6设置为输入端口并且使能上升沿触发中断。</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">最后在中断管理器中打开全局中断</font></span></span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify"><span style="font-size:24px;"><strong><span style="font-family:Calibri"><span style="font-family:宋体"><font face="宋体">3. 系统流程</font></span></span></strong></span></p>

<p style="text-align:justify"></p>

<p style="text-align:left"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体">CheckNetOK:</span></span></span></span></span></p>

<p style="text-align:left"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">检查通讯模块是否联网正常(由于此处没有通讯模块,所以恒返回</font>YES)</span></span></span></span></span></p>

<p style="text-align:left"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体">Parse Packet:</span></span></span></span></span></p>

<p style="text-align:left"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">解析串口数据(收到</font>&ldquo;0&rdquo;表示关闭LED,收到其他则表示打开LED)</span></span></span></span></span></p>

<p style="text-align:left"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体">Judge The Signal:</span></span></span></span></span></p>

<p style="text-align:left"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">根据按键状态和串口命令决定</font>LED明灭</span></span></span></span></span></p>

<p style="text-align:left"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体">Response/Notify:</span></span></span></span></span></p>

<p style="text-align:left"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">如果收到串口命令则应答</font>&ldquo;got&rdquo;;如果获取按键命令,则通知串口&ldquo;0&rdquo;/&ldquo;1&rdquo;。</span></span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:24px;"><strong><span style="font-family:Calibri"><span style="font-family:宋体"><font face="宋体">4. 核心代码分析</font></span></span></strong></span></p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code></code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span bbs.eeworld.com.cn="" editor="" handle.png="" http:="" images="" plugins="" static="" style="background: url(&quot;&lt;imgdata-cke-saved-src=" widget="">&quot;) rgba(220, 220, 220, 0.5); top: -15px; left: 0px; display: block;&quot;&gt;<img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体">SystemClockHandler为TC0的中断处理函数,该函数每1ms执行一次,g_SystemTicks记录了当前系统时钟。</span></span></span></span></p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code></code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span bbs.eeworld.com.cn="" editor="" handle.png="" http:="" images="" plugins="" static="" style="background: url(&quot;&lt;imgdata-cke-saved-src=" widget="">&quot;) rgba(220, 220, 220, 0.5); top: -15px; left: 0px; display: block;&quot;&gt;<img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体">KeyInterruptHandler为按键中断函数,每次执行会置位按键标志(KEY_VAL_MASK),并翻转按键LED值(</span></span><span style="font-size:12.0000pt"><span style="font-family:宋体">g_KeyValue</span></span><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">)。</font>100ms内多次发生中断视为信号抖动,并无视。</span></span></span></span></span></p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code></code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span bbs.eeworld.com.cn="" editor="" handle.png="" http:="" images="" plugins="" static="" style="background: url(&quot;&lt;imgdata-cke-saved-src=" widget="">&quot;) rgba(220, 220, 220, 0.5); top: -15px; left: 0px; display: block;&quot;&gt;<img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">分别获取串口和按键值,如果</font>LED值翻转,则重置LED。</span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify">&nbsp;</p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:24px"><strong><span style="font-family:Calibri"><span style="font-family:宋体"><font face="宋体">5. 演示截图</font></span></span></strong></span></p>

<p style="text-indent:24.0000pt; text-align:justify"></p>

<p style="text-indent:24.0000pt; text-align:justify"></p>

<p style="text-indent:24.0000pt; text-align:justify">&nbsp;</p>

<p style="text-indent:24.0000pt; text-align:justify">最后附上工程文件</p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="text-autospace:ideograph-numeric"><span style="font-family:Calibri"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">繁絮至此,但求明细。</font></span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify">&nbsp;</p>

<p style="text-indent:24.0000pt; text-align:justify">&nbsp;</p>

ZHANGKAOLEI 发表于 2019-10-26 18:29

<p>博主能不能将芯片的库函数开发手册发给我一下</p>

Ansersion 发表于 2019-10-27 22:44

本帖最后由 Ansersion 于 2019-10-29 10:57 编辑

<div class="quote">
<blockquote><font size="2"><a href="forum.php?mod=redirect&amp;goto=findpost&amp;pid=2916139&amp;ptid=1094542" target="_blank"><font color="#999999">ZHANGKAOLEI 发表于 2019-10-26 18:29</font></a></font> 博主能不能将芯片的库函数开发手册发给我一下</blockquote>
</div>

<p>在官网上没找到过函数库相关的开发手册,主要是凭借MCC生成的代码函数名猜用法。</p>

<p>如果想要了解寄存器级的代码库,这里有个ResearchGate的入门指导(很简单,只是个指导)</p>

<p><a href="https://www.researchgate.net/profile/Aravind_E_Vijayan/publication/263084656_A_Beginners_Guide_to_AVR/links/0f317539bfdd4eab76000000/A-Beginners-Guide-to-AVR.pdf">https://www.researchgate.net/profile/Aravind_E_Vijayan/publication/263084656_A_Beginners_Guide_to_AVR/links/0f317539bfdd4eab76000000/A-Beginners-Guide-to-AVR.pdf</a></p>

<p>&nbsp;</p>

<p>更深一层可以参考mega0系列的datasheet</p>

<p><a href="http://ww1.microchip.com/downloads/en/DeviceDoc/megaAVR-0-series-Family-Data-Sheet-40002015C.pdf">http://ww1.microchip.com/downloads/en/DeviceDoc/megaAVR-0-series-Family-Data-Sheet-40002015C.pdf</a></p>

<p>&nbsp;</p>

<p>另外建议多使用快捷键&ldquo;Ctrl+|&rdquo;自动补全,也可以提供一些信息。</p>

<p>&nbsp;</p>

ZHANGKAOLEI 发表于 2019-10-29 11:07

本帖最后由 ZHANGKAOLEI 于 2019-10-29 11:09 编辑

<div class="quote">
<blockquote><font size="2"><a href="forum.php?mod=redirect&amp;goto=findpost&amp;pid=2916139&amp;ptid=1094542" target="_blank"><font color="#999999">ZHANGKAOLEI 发表于 2019-10-26 18:29</font></a></font> 博主能不能将芯片的库函数开发手册发给我一下</blockquote>
</div>

<p>谢谢,我自己配了一个ADC和串口通信,串口通信行了,可是ADC没有数据出来。希望博主出一个这方面的配置,给我学习学习</p>

ZHANGKAOLEI 发表于 2019-11-1 15:53

<p>感谢博主,问题已经解决,谢谢</p>
页: [1]
查看完整版本: 【ATmega4809 Curiosity Nano测评】串口按键控灯(IoT Led前篇)