【ufun学习】学习篇2:《基础例程1 - 三色LED控制》
<div class='showpostmsg'><p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">《基础例程</span>1 - <span style="font-family:宋体">三色</span>LED<span style="font-family:宋体">控制》</span></span></span></p><p style="text-align:justify">先介绍一下32的代码主要结构:一个功能完整的工程由一个main.c文件和若干个***.c文件组成,其中每个***.c文件对应着***.h文件,***.c文件主要是将各种外设实现的功能封装在一个***.c文件中,main函数通过调用多个***.c文件实现一个比较完善的功能。代码结构层次清晰。下面为《基础例程1 - 三色LED控制》的具体详情:</p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">1.Main<span style="font-family:宋体">函数中要对外设进行初始化,以及功能程序代码编写,该例程</span>main<span style="font-family:宋体">函数如下图:</span></span></span></p>
<p style="text-align:justify"></p>
<ol>
<li style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">外设初始化(</span>LED<span style="font-family:宋体">初始化)</span></span></span></li>
</ol>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">该例程主要是对</span>GPIO<span style="font-family:宋体">的控制,要实现</span> GPIO <span style="font-family:宋体">的控制,需要多</span>GPIO<span style="font-family:宋体">进行初始化。</span><br />
<span style="font-family:宋体">初始化步骤分为:</span>a.<span style="font-family:宋体">使能端口时钟</span>——<span style="font-family:宋体">》</span>b.<span style="font-family:宋体">引脚定义</span>——<span style="font-family:宋体">》</span>c.<span style="font-family:宋体">设置引脚输出类型</span>——<span style="font-family:宋体">》</span>d.<span style="font-family:宋体">初始化</span>GPIO</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">b.<span style="font-family:宋体">由</span>ufun<span style="font-family:宋体">板的原理图中可以得知</span>RGB<span style="font-family:宋体">灯对应的</span>GPIO<span style="font-family:宋体">分别为</span>PA0<span style="font-family:宋体">、</span>PA1<span style="font-family:宋体">和</span>PA2<span style="font-family:宋体">,所以引脚定义时要对</span>Pin0~2<span style="font-family:宋体">进行定义。</span></span></span></p>
<p style="text-align:justify"></p>
<p style="text-align:justify"></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体">c.设置GPIO输入输出类型一共分为8类:<strong><span style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">输入模式</span></span></strong></span></span></span></span></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d"> -</span></span><span style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d">输入浮空</span></span><span style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">(GPIO_Mode_IN_FLOATING)</span></span></span></span></span></span></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d"> -</span></span><span style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d">输入上拉</span></span><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">(GPIO_Mode_IPU)</span></span></span></span></span></span></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d"> -</span></span><span style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d">输入下拉</span></span><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">(GPIO_Mode_IPD)</span></span></span></span></span></span></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d"> -</span></span><span style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d">模拟输入</span></span><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">(GPIO_Mode_AIN)</span></span></span></span></span></span></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体"><strong><span style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">输出模式</span></span></strong></span></span></span></span></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d"> -</span></span><span style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d">开漏输出</span></span><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">(GPIO_Mode_Out_OD)</span></span></span></span></span></span></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d"> -</span></span><span style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d">开漏复用功能</span></span><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">(GPIO_Mode_AF_OD)</span></span></span></span></span></span></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d"> -</span></span><span style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d">推挽式输出</span></span><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">(GPIO_Mode_Out_PP)</span></span></span></span></span></span></p>
<p style="margin-bottom:16px"><span style="font-size:12pt"><span style="background:white"><span style="line-height:19.5pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d"> -</span></span><span style="font-family:"微软雅黑","sans-serif""><span style="color:#4d4d4d">推挽式复用功能</span></span><span lang="EN-US" style="font-family:"微软雅黑","sans-serif""><span style="color:#990000">(GPIO_Mode_AF_PP)</span></span></span></span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">该例程用于控制</span>RGB<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">d.<span style="font-family:宋体">初始化</span>GPIO</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">下图为该例程</span>LED<span style="font-family:宋体">初始化部分:</span></span></span></p>
<p style="text-align:justify"></p>
<ol start="2">
<li style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span class="fontstyle01" style="font-family:TimesNewRomanPSMT,serif"><span style="color:black"><span style="font-weight:normal"><span style="font-style:normal"><span style="font-size:11.0pt"><span style="font-family:宋体">了</span></span></span></span></span></span><span class="fontstyle01" style="font-family:TimesNewRomanPSMT,serif"><span style="color:black"><span style="font-weight:normal"><span style="font-style:normal"><span lang="EN-US" style="font-size:11.0pt">SysTick</span></span></span></span></span><span class="fontstyle21" style="font-family:宋体"><span style="color:black"><span style="font-weight:normal"><span style="font-style:normal"><span style="font-size:11.0pt">也叫时钟滴答,其实质就是一个递减的定时器。在历程中用于毫秒级的延时。</span></span></span></span></span></span></span></li>
</ol>
<p style="text-align:justify"></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">3.<span style="font-family:宋体">例程演示效果:绿、蓝、红灯一次点亮,间隔为</span>1<span style="font-family:宋体">秒钟。</span></span></span></p>
<p style="text-align:justify"><iframe allowfullscreen="true" frameborder="0" height="450" src="http://player.youku.com/embed/XNDI4MDIzODM4NA" style="background:#eee;margin-bottom:10px;" width="100%"></iframe><br />
电源指示灯和RGB灯亮度有点高就用纸片贴住了,这样拍摄效果会好点</p>
<p style="text-align:justify">文中如果有讲述得不对的地方望指出,本人必将虚心接受,共同学习。或者有什么好的想法也可以评论分享,欢迎大家共同学习进步。<br />
<br />
<br />
<b><font color="#5E7384">此内容由EEWORLD论坛网友<font size="3">1nnocet-</font>原创,如需转载或用于商业用途需征得作者同意并注明出处</font></b></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>非常好的资料,感谢分享;<br />
共享精神万岁。<br />
希望楼主以后多多分享资料,帮助更多的电子爱好者。</p>
页:
[1]