xiyue521 发表于 2021-5-1 01:53

【中科蓝讯AB32VG1 RISC-V板“碰上”RTT测评】+LED与按键控制

本帖最后由 xiyue521 于 2021-5-1 02:30 编辑

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:26px;">由原理图可以知道LED低电平亮,按键按下去低电平,配置按键上拉输入即可检测:</span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:26px;">rt_pin_mode(S3, PIN_MODE_INPUT_PULLUP );</span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:26px;"></span></p>

<p>&nbsp;</p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:26px;">1.先封装底层函数,rt_pin_get获取引脚id值</span></p>

<p class="imagemiddle"><span style="font-size:26px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rt_pin_mode,rt_pin_write,rt_pin_read这三个是按键模式,读,写函数,直接把获取到的id值传进去就可以修改对应的引脚。</span></p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="language-cpp hljs">

<span class="hljs-keyword">void</span> RGB_Init(<span class="hljs-keyword">void</span>){
    <span class="hljs-comment">// 获得led成员</span>
    LED_R = rt_pin_get(<span class="hljs-string">&quot;PE.1&quot;</span>);
    LED_G = rt_pin_get(<span class="hljs-string">&quot;PE.4&quot;</span>);
    LED_B = rt_pin_get(<span class="hljs-string">&quot;PA.2&quot;</span>);
    <span class="hljs-comment">// 设置引脚为输出方式</span>
    rt_pin_mode(LED_R, PIN_MODE_OUTPUT);
    rt_pin_mode(LED_G, PIN_MODE_OUTPUT);
    rt_pin_mode(LED_B, PIN_MODE_OUTPUT);
}
<span class="hljs-comment">// on=1:红灯亮,on=0:全灭</span>
<span class="hljs-keyword">void</span> RGB_Red(rt_bool_t on){
    <span class="hljs-keyword">if</span> (on) {
      rt_pin_write(LED_R, PIN_LOW);
    }<span class="hljs-keyword">else</span> {
      rt_pin_write(LED_R, PIN_HIGH);
    }
    rt_pin_write(LED_G, PIN_HIGH);
    rt_pin_write(LED_B, PIN_HIGH);
}

<span class="hljs-keyword">void</span> RGB_Blue(rt_bool_t on){
    <span class="hljs-keyword">if</span> (on) {
      rt_pin_write(LED_B, PIN_LOW);
    }<span class="hljs-keyword">else</span> {
      rt_pin_write(LED_B, PIN_HIGH);
    }
    rt_pin_write(LED_G, PIN_HIGH);
    rt_pin_write(LED_R, PIN_HIGH);
}

<span class="hljs-keyword">void</span> RGB_Green(rt_bool_t on){
    <span class="hljs-keyword">if</span> (on) {
      rt_pin_write(LED_G, PIN_LOW);
    }<span class="hljs-keyword">else</span> {
      rt_pin_write(LED_G, PIN_HIGH);
    }
    rt_pin_write(LED_R, PIN_HIGH);
    rt_pin_write(LED_B, PIN_HIGH);
}


<span class="hljs-keyword">void</span> RGB_ALL(rt_bool_t on){
    <span class="hljs-keyword">if</span> (on) {
      rt_pin_write(LED_G, PIN_LOW);
      rt_pin_write(LED_R, PIN_LOW);
      rt_pin_write(LED_B, PIN_LOW);
    }
    <span class="hljs-keyword">else</span>
    {
      rt_pin_write(LED_G, PIN_HIGH);
      rt_pin_write(LED_R, PIN_HIGH);
      rt_pin_write(LED_B, PIN_HIGH);
    }

}


<span class="hljs-keyword">void</span> Key_Init(<span class="hljs-keyword">void</span>){
    S3= rt_pin_get(<span class="hljs-string">&quot;PF.0&quot;</span>);
    S2 = rt_pin_get(<span class="hljs-string">&quot;PF.1&quot;</span>);
    <span class="hljs-comment">// 上拉输入</span>
    rt_pin_mode(S2, PIN_MODE_INPUT_PULLUP );
    rt_pin_mode(S3, PIN_MODE_INPUT_PULLUP );
}

</code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background: url(&quot;https://bbs.eeworld.com.cn/static/editor/plugins/widget/images/handle.png&quot;) rgba(220, 220, 220, 0.5); top: -15px; left: 0px; display: block;"><img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:26px;">2.接下来创建一个led的任务,期间用到了全局变量mode,也可以用邮箱来传值。还有就是可以用INIT_APP_EXPORT()函数来直接对函数初始化,不用再去main调用他。</span></p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="language-cpp hljs">

<span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> rgb_thread_entry(<span class="hljs-keyword">void</span>* pdata)
{
    RGB_Init();
    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)
    {


      <span class="hljs-keyword">switch</span>(mode)
       {

            <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:
                RGB_ALL(<span class="hljs-number">0</span>);
                rt_thread_mdelay(<span class="hljs-number">1000</span>);
                RGB_ALL(<span class="hljs-number">1</span>);
                rt_thread_mdelay(<span class="hljs-number">1000</span>);
               <span class="hljs-keyword">break</span>;
            <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:

                rt_thread_mdelay(<span class="hljs-number">1000</span>);
                RGB_Blue(<span class="hljs-number">1</span>);
                rt_thread_mdelay(<span class="hljs-number">1000</span>);
                RGB_Green(<span class="hljs-number">1</span>);
                rt_thread_mdelay(<span class="hljs-number">1000</span>);
                RGB_Red(<span class="hljs-number">1</span>);
                <span class="hljs-keyword">break</span>;

      }

    }
}


<span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> Thread_RGB(<span class="hljs-keyword">void</span>){
    rt_thread_t thread = RT_NULL;
    thread = rt_thread_create(<span class="hljs-string">&quot;rgb&quot;</span>, rgb_thread_entry, RT_NULL, <span class="hljs-number">512</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>);
    <span class="hljs-keyword">if</span>(thread == RT_NULL)
    {
      rt_kprintf(<span class="hljs-string">&quot;Thread_GRB Init ERROR&quot;</span>);
      <span class="hljs-keyword">return</span> RT_ERROR;
    }
    rt_thread_startup(thread);
}

INIT_APP_EXPORT(Thread_RGB); <span class="hljs-comment">// 线程自动初始化</span>
</code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background: url(&quot;https://bbs.eeworld.com.cn/static/editor/plugins/widget/images/handle.png&quot;) rgba(220, 220, 220, 0.5); top: -15px; left: 0px; display: block;"><img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:26px;">3.创建按键任务,这里用到了轮询的方式检测按键,100ms检测一次,检测到对应按键修改mode来切换不同的led闪烁模式。</span></p>

<p class="imagemiddle"><span style="font-size:26px;">&nbsp;&nbsp;&nbsp;&nbsp;两个任务都是用动态创建的,格式也差不多,所以用RTOS其实可以简化很多东西。特别是RTTHREAD还有控制台,LOG的一些功能,调试的时候比较方便,极力推荐!</span></p>

<p class="imagemiddle">&nbsp;</p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="language-cpp hljs">

<span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> key_thread_entry(<span class="hljs-keyword">void</span> *parameter){
    Key_Init();
    RGB_Init();
    RGB_Red(<span class="hljs-number">1</span>);
    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>){
      <span class="hljs-keyword">if</span> (rt_pin_read(S2)==PIN_LOW) mode=<span class="hljs-number">0</span>;

      <span class="hljs-keyword">if</span> (rt_pin_read(S3)==PIN_LOW) mode=<span class="hljs-number">1</span>;

      rt_thread_mdelay(<span class="hljs-number">100</span>);
    }
}

<span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> Key_Thread_Init(<span class="hljs-keyword">void</span>){


    rt_thread_t thread = RT_NULL;
    thread = rt_thread_create(<span class="hljs-string">&quot;key_thread&quot;</span>, key_thread_entry, RT_NULL, <span class="hljs-number">512</span>, <span class="hljs-number">10</span>, <span class="hljs-number">10</span>);
    <span class="hljs-keyword">if</span>(thread == RT_NULL)
    {
      rt_kprintf(<span class="hljs-string">&quot;Thread_GRB Init ERROR&quot;</span>);
      <span class="hljs-keyword">return</span> RT_ERROR;
    }
    rt_thread_startup(thread);




}
INIT_APP_EXPORT(Key_Thread_Init);<span class="hljs-comment">// 初始化线程</span>
</code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background:rgba(220,220,220,0.5);background-image:url(https://bbs.eeworld.com.cn/static/editor/plugins/widget/images/handle.png)"><img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:26px;">4.main函数就是没1s打印一次mode,其他没有了。</span></p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="hljs">#include &lt;rtthread.h&gt;
#include &quot;board.h&quot;
uint8_t LED_R;
uint8_t LED_B;
uint8_t LED_G;
uint8_t S2;
uint8_t S3;
uint8_t mode ;
int main(void)
{

    rt_kprintf(&quot;Hello, world\n&quot;);


    while (1)
    {
      rt_kprintf(&quot;mode:%d\r\n&quot;,mode);
      rt_thread_mdelay(1000); //空延时
    };




    return 0;
}
</code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background:rgba(220,220,220,0.5);background-image:url(https://bbs.eeworld.com.cn/static/editor/plugins/widget/images/handle.png)"><img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p>&nbsp;</p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:26px;"></span></p>

<p class="imagemiddle"><span style="font-size:26px;">5.输入ps可以查看线程信息,可以看见多了按键线程和led线程。</span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:26px;"></span></p>

<p class="imagemiddle"><span style="font-size:26px;">本文就到这里,下文介绍下定时器。</span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

Jacktang 发表于 2021-5-1 17:51

<p>看楼主用RTOS可以简化很多东西,调试确实是方便</p>
页: [1]
查看完整版本: 【中科蓝讯AB32VG1 RISC-V板“碰上”RTT测评】+LED与按键控制