jinglixixi 发表于 2022-6-8 00:08

【兆易GD32F310测评】+传感器检测

<p style="text-align:justify">为了进行环境方面的检测,对光强传感器及温度大气压传感器的驱动程序进行了移植,经验证在GD32F310开发板上进行功能验证是十分方便的,能在半小时左右完成。</p>

<p>对光强传感器的测试结果如图1所示,该传感器所连接的引脚为PB0和PB1,具体的对应关系为:</p>

<p>CLK --PB0</p>

<p>DIN--PB1</p>

<p> &nbsp;</p>

<p>图1 光强检测效果</p>

<p>&nbsp;</p>

<p>使用GPIO口模拟高低电平输出及引脚状态读取的语句定义为:</p>

<p>#define SCL_Clr1()&nbsp; &nbsp;&nbsp;&nbsp;gpio_bit_write(GPIOB, GPIO_PIN_0, RESET)</p>

<p>#define SCL_Set1() &nbsp;&nbsp;&nbsp;&nbsp;gpio_bit_write(GPIOB, GPIO_PIN_0, SET)</p>

<p>&nbsp;</p>

<p>#define SDA_Clr1() &nbsp;&nbsp;&nbsp;&nbsp;gpio_bit_write(GPIOB, GPIO_PIN_1, RESET)</p>

<p>#define SDA_Set1() &nbsp;&nbsp;&nbsp;&nbsp;gpio_bit_write(GPIOB, GPIO_PIN_1, SET)</p>

<p>#define IIC_SDA_IN1&nbsp;&nbsp;&nbsp; gpio_input_bit_get(GPIOB, GPIO_PIN_1)</p>

<p>&nbsp;</p>

<p>对引脚进行初始化配置的函数如下:</p>

<pre>
<code class="language-cpp">void BH1750_Init(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);
    gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0|GPIO_PIN_1);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_1);
    gpio_bit_reset(GPIOB, GPIO_PIN_0);
    gpio_bit_reset(GPIOB, GPIO_PIN_1);
}</code></pre>

<p>&nbsp;</p>

<p>实现输入与输出模式切换的2个函数为:</p>

<pre>
<code class="language-cpp">void IIC_INPUT_MODE()
{
    gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_PIN_1);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
}


void IIC_OUTPUT_MODE()
{
    gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
}</code></pre>

<p>以GPIO口模拟I2C方式字节数据的函数如下:</p>

<pre>
<code class="language-cpp">void BH1750_SendByte(char data)
{
    char i;
    IIC_OUTPUT_MODE();
    SCL_Clr1();
    delay_us(2);
    for (i=0;i&lt;8;i++)
    {
        if(data&amp;0x80) SDA_Set1();
        else SDA_Clr1();
        data &lt;&lt;= 1;
        SCL_Set1();
        delay_us(2);
        SCL_Clr1();
        delay_us(2);
    }
}</code></pre>

<p>&nbsp;</p>

<p>实现光强检测与显示的函数为:</p>

<pre>
<code class="language-cpp">void Get_Sunlight_Value()
{
    int dis_data=0;
    float temp;
    unsigned int sd;
    Single_Write_BH1750(0x01);
    Single_Write_BH1750(0x10);
    delay_1ms(180);
    Multiple_Read_BH1750();
    dis_data=BUF;
    dis_data=(dis_data&lt;&lt;8)+BUF;
    temp=(float) dis_data/1.2;
    sd=temp;
    OLED_ShowNum(20,6,sd,5,16);
}</code></pre>

<p>实现图1所示效果的主程序为:</p>

<pre>
<code class="language-cpp">int main(void)
{
    systick_config();
    rcu_periph_clock_enable(RCU_GPIOB);
    gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6|GPIO_PIN_7);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7);
    gpio_bit_reset(GPIOB, GPIO_PIN_6);
    gpio_bit_reset(GPIOB, GPIO_PIN_7);
    OLED_Init();
    OLED_Clear();
    OLED_ShowString(20,0,"GD32F310G",16);
    OLED_ShowString(20,2,"OLED &amp; BH1750",16);
    OLED_ShowString(20,4,"Sunlight=",16);
    OLED_ShowString(68,6,"lx",16);
    BH1750_Init();
    while(1){
         Get_Sunlight_Value();
         delay_1ms(1000);
   }
}</code></pre>

<p>&nbsp;</p>

<p>以光强检测为基础,对温度大气压的检测就顺利相对,其在GPIO口的使用、输入与输出模式的切换函数、高低电平的输出及引脚状态检测均是相似的。</p>

<p>两者的主要差别,则是在数据读取与辅助函数方面,温度大气压的测试效果如图2所示。</p>

<p> &nbsp;</p>

<p>图2 温度大气压检测效果</p>

<p>&nbsp;</p>

<p>对温度大气压传感器的初始化函数为:</p>

<pre>
<code class="language-cpp">void Init_BMP085()
{
    ac1 = Multiple_read(0xAA);
    ac2 = Multiple_read(0xAC);
    ac3 = Multiple_read(0xAE);
    ac4 = Multiple_read(0xB0);
    ac5 = Multiple_read(0xB2);
    ac6 = Multiple_read(0xB4);
    b1 =  Multiple_read(0xB6);
    b2 =  Multiple_read(0xB8);
    mb =  Multiple_read(0xBA);
    mc =  Multiple_read(0xBC);
    md =  Multiple_read(0xBE);
}</code></pre>

<p>实现温度大气压检测及数值转换的函数如下:</p>

<pre>
<code class="language-cpp">void bmp085Convert()
{
      unsigned int ut;
      unsigned long up;
      long x1, x2, b5, b6, x3, b3, p;
      unsigned long b4, b7;
      ut = bmp085ReadTemp();
      up = bmp085ReadPressure();
      x1 = (((long)ut - (long)ac6)*(long)ac5) &gt;&gt; 15;
      x2 = ((long) mc &lt;&lt; 11) / (x1 + md);
      b5 = x1 + x2;
      temperature = ((b5 + 8) &gt;&gt; 4);
      b6 = b5 - 4000;
      x1 = (b2 * (b6 * b6)&gt;&gt;12)&gt;&gt;11;
      x2 = (ac2 * b6)&gt;&gt;11;
      x3 = x1 + x2;
      b3 = (((((long)ac1)*4 + x3)&lt;&lt;OSS) + 2)&gt;&gt;2;
      x1 = (ac3 * b6)&gt;&gt;13;
      x2 = (b1 * ((b6 * b6)&gt;&gt;12))&gt;&gt;16;
      x3 = ((x1 + x2) + 2)&gt;&gt;2;
      b4 = (ac4 * (unsigned long)(x3 + 32768))&gt;&gt;15;
      b7 = ((unsigned long)(up - b3) * (50000&gt;&gt;OSS));
      if (b7 &lt; 0x80000000)
        p = (b7&lt;&lt;1)/b4;
      else
        p = (b7/b4)&lt;&lt;1;
      x1 = (p&gt;&gt;8) * (p&gt;&gt;8);
      x1 = (x1 * 3038)&gt;&gt;16;
      x2 = (-7357 * p)&gt;&gt;16;
      pressure = p+((x1 + x2 + 3791)&gt;&gt;4);
}</code></pre>

<p>实现图2所示效果的主程序为:</p>

<pre>
<code class="language-cpp">int main(void)
{
    systick_config();
    rcu_periph_clock_enable(RCU_GPIOB);
    gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6|GPIO_PIN_7);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7);
    gpio_bit_reset(GPIOB, GPIO_PIN_6);
    gpio_bit_reset(GPIOB, GPIO_PIN_7);
    OLED_Init();
    OLED_Clear();
    OLED_ShowString(20,0,"GD32F310G",16);
    OLED_ShowString(20,2,"OLED &amp; bmp085",16);
    OLED_ShowString(20,4,"t=     C",16);
    OLED_ShowString(20,6,"p=       KPa",16);
    BMP085_Init();
    Init_BMP085();
    while(1){
        bmp085Convert();
        OLED_ShowNum(44,4,temperature/10,3,16);
        OLED_ShowNum(44,6,pressure/100,5,16);
        delay_1ms(1000);
    }
}</code></pre>

<p>&nbsp;</p>

<p>实践证明GD32F310在功能验证方面还是十分方便的,能够将其它MCU上的功能快速移植到该芯片上来实现设计目标。</p>

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

Jacktang 发表于 2022-6-8 07:09

<p>兆易的GD32F310性能还是值得验证的</p>

lugl4313820 发表于 2022-6-8 08:28

教学级帖子,感谢分享。

littleshrimp 发表于 2022-6-8 09:33

<p>GD32应该有硬件I2C吧 这种软件模拟硬件接口基本上一个单片机里弄好了所有单片机改改GPIO就能用 和GD32关系不大</p>

freebsder 发表于 2022-6-8 16:28

<p>谢谢分享,期待后续评测</p>

yang8555u 发表于 2022-6-10 16:51

<p>Wow,居然用的是软件I2C,是因为硬件I2C有缺陷才不用吗?</p>

jinglixixi 发表于 2022-6-11 08:32

yang8555u 发表于 2022-6-10 16:51
Wow,居然用的是软件I2C,是因为硬件I2C有缺陷才不用吗?

<p>这到不是,只是这一样用着比较方便不受引脚位置的限制,功能移植比较块。</p>
页: [1]
查看完整版本: 【兆易GD32F310测评】+传感器检测