trucy 发表于 2020-10-10 17:57

GD32307E-START IO模拟SPI控制RGBLED(03)

<p>续上一篇https://bbs.eeworld.com.cn/thread-1143118-1-1.html</p>

<p>今天我们来做一个利用GD32307E-START IO模拟SPI控制RGBLED实验</p>

<p>1.RGBLED控制芯片为P9813</p>

<p>接线图如</p>

<p>参考P9813 datasheet我们知道需要一个时钟信号和一个数据信号才能使其工作</p>

<p>完成以上剩下就是code实战</p>

<p>1.整合3个三原色数据到一个变量</p>

<p>void RGBLED_DataDealWithAndSend(uint8_t r,uint8_t g,uint8_t b)<br />
{<br />
&nbsp;&nbsp; &nbsp;uint32_t dx=0;<br />
&nbsp;&nbsp; &nbsp;dx |= (uint32_t)0x03 &lt;&lt; 30; &nbsp;//前两位&#39;1&#39;位标志位<br />
&nbsp;&nbsp; &nbsp;dx |= (uint32_t)RGBLED_TakeAntiCode(b) &lt;&lt; 28;<br />
&nbsp;&nbsp; &nbsp;dx |= (uint32_t)RGBLED_TakeAntiCode(g) &lt;&lt; 26;<br />
&nbsp;&nbsp; &nbsp;dx |= (uint32_t)RGBLED_TakeAntiCode(r) &lt;&lt; 24;<br />
&nbsp;&nbsp; &nbsp;dx |= (uint32_t)b &lt;&lt; 16;<br />
&nbsp;&nbsp; &nbsp;dx |= (uint32_t)g &lt;&lt; 8;<br />
&nbsp;&nbsp; &nbsp;dx |= r;<br />
&nbsp;&nbsp; &nbsp;RGBLED_DatSend(dx);//发送数据<br />
}</p>

<p>2.&nbsp;MCU发送数据到芯片</p>

<p>void RGBLED_DatSend(uint32_t dx)<br />
{<br />
&nbsp;&nbsp; &nbsp;uint8_t i;<br />
&nbsp;&nbsp; &nbsp;for(i=0;i&lt;32;i++)<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if((dx &amp; 0x80000000) !=0)<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;RGBLED_SDA_H();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;RGBLED_SDA_L();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;dx&lt;&lt;=1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;RGBLED_SCL_L();//delay_us(200);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;RGBLED_SCL_H();///delay_us(200);<br />
&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;<br />
}</p>

<p>3.最终的控制函数</p>

<p>void RGBLED_Show(uint8_t r,uint8_t g,uint8_t b)<br />
{<br />
&nbsp;&nbsp; &nbsp;RGBLED_Send32Zero();//发送前32位&#39;0&#39;起始帧<br />
&nbsp;&nbsp; &nbsp;RGBLED_DataDealWithAndSend(r, g, b);//发送第一个32bit灰度数据<br />
&nbsp;&nbsp; &nbsp;RGBLED_DataDealWithAndSend(r, g, b);//发送第二个32bit灰度数据<br />
}</p>

<p>4.IO初始化</p>

<p>#define RGBLED_SCL_H()&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;GPIO_BOP(GPIOC) = GPIO_PIN_8<br />
#define RGBLED_SCL_L()&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;GPIO_BC(GPIOC) = GPIO_PIN_8<br />
#define RGBLED_SDA_H()&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;GPIO_BOP(GPIOC) = GPIO_PIN_7<br />
#define RGBLED_SDA_L()&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;GPIO_BC(GPIOC) = GPIO_PIN_7</p>

<p>#define RGBLED_SDA_O() &nbsp;{gpio_init(GPIOC, &nbsp;GPIO_MODE_OUT_PP, &nbsp;GPIO_OSPEED_50MHZ, &nbsp;GPIO_PIN_7);}<br />
#define RGBLED_SCL_O() &nbsp;{gpio_init(GPIOC, &nbsp;GPIO_MODE_OUT_PP, &nbsp;GPIO_OSPEED_50MHZ, &nbsp;GPIO_PIN_8);}<br />
#define RGBLED_SDA_I() &nbsp; {gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_7);}<br />
以上完成直接编译donwload至实验板测试验证</p>

<p></p>

<p>实验验证测试结束,期待下一篇IIC OLED SHOW</p>

freebsder 发表于 2020-10-11 21:31

<p>为啥要模拟?</p>

okhxyyo 发表于 2020-10-19 10:15

<p><a href="https://bbs.eeworld.com.cn/thread-1143008-1-1.html" target="_blank">兆易GD32307E-START测评汇总</a></p>

<p>汇总贴:<a href="https://bbs.eeworld.com.cn/thread-1143008-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1143008-1-1.html</a></p>
页: [1]
查看完整版本: GD32307E-START IO模拟SPI控制RGBLED(03)