【GD32L233C-START评测】0.96inch oled屏幕驱动
本帖最后由 YangTwo 于 2022-3-13 10:41 编辑<p>本次实验继续利用GD32L233C的外设库函数驱动GPIO引脚来实现OLED屏幕的读写。手边有一块OLED 12864屏幕:</p>
<p> </p>
<p>我们采用GPIO模拟SPI协议来控制OLED屏幕。首先定义GPIO A1,A2, A3, A4 A5对应OLED屏幕的SCL, SDA, RES, DC, CS引脚</p>
<pre>
<code class="language-cpp">#define OLED_SCL_Clr() gpio_bit_reset(GPIOA, GPIO_PIN_1)
#define OLED_SCL_Set() gpio_bit_set(GPIOA, GPIO_PIN_1)
#define OLED_SDA_Clr() gpio_bit_reset(GPIOA, GPIO_PIN_2)
#define OLED_SDA_Set() gpio_bit_set(GPIOA, GPIO_PIN_2)
#define OLED_RES_Clr() gpio_bit_reset(GPIOA, GPIO_PIN_3)
#define OLED_RES_Set() gpio_bit_set(GPIOA, GPIO_PIN_3)
#define OLED_DC_Clr()gpio_bit_reset(GPIOA, GPIO_PIN_4)
#define OLED_DC_Set()gpio_bit_set(GPIOA, GPIO_PIN_4)
#define OLED_CS_Clr()gpio_bit_reset(GPIOA, GPIO_PIN_5)
#define OLED_CS_Set()gpio_bit_set(GPIOA, GPIO_PIN_5) </code></pre>
<p> </p>
<p>然后设置引脚为输出模式:</p>
<pre>
<code class="language-cpp"> gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_7 | GPIO_PIN_8);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_7 | GPIO_PIN_8);</code></pre>
<p> </p>
<p>简单的写了一个小球弹跳的例子:</p>
<pre>
<code class="language-cpp">while(1){
//Draw the Ball based on current parameters
//OLED_Clear();
My_OLED_Clear();
/***/
OLED_DrawCircle(x_pos, y_pos, CircleRadius);
OLED_DrawCircle(127 - x_pos, 63 - y_pos, CircleRadius);
OLED_DrawCircle(127 - x_pos, y_pos, CircleRadius);
OLED_DrawCircle(x_pos, 63 - y_pos, CircleRadius);
OLED_ShowPicture(40,7,50,50,SuperSpaceMan[(index%8)],1);
//OLED_ShowPicture(40,7,50,50,MySuperSpaceMan[(index%49)],1);
index++;
OLED_Refresh();
//Circle Ball Moving State Machine Details:
switch(CircleDir){
case UpRight:
x_pos++; y_pos--;
if((y_pos == CircleRadius) && (x_pos == 127 - CircleRadius)){
CircleDir = BottomLeft;
}else if(y_pos == CircleRadius){
CircleDir = BottomRight;
}else if(x_pos == 127 - CircleRadius){
CircleDir = UpLeft;
}
break;
case UpLeft:
x_pos--; y_pos--;
if((y_pos == CircleRadius) && (x_pos == CircleRadius)){
CircleDir = BottomRight;
}else if(y_pos == CircleRadius){
CircleDir = BottomLeft;
}else if(x_pos == CircleRadius){
CircleDir = UpRight;
}
break;
case BottomRight:
x_pos++; y_pos++;
if((x_pos == 127 - CircleRadius) && (y_pos == 63 - CircleRadius)){
CircleDir = UpLeft;
}else if(x_pos == 127 - CircleRadius){
CircleDir = BottomLeft;
}else if(y_pos == 63 - CircleRadius){
CircleDir = UpRight;
}
break;
case BottomLeft:
x_pos--; y_pos++;
if((x_pos == CircleRadius)&&(y_pos == 63-CircleRadius)){
CircleDir = UpRight;
}else if(x_pos == CircleRadius){
CircleDir = BottomRight;
}else if(y_pos == 63 - CircleRadius){
CircleDir = UpLeft;
}
break;
default:
break;
}// end of switch case
}</code></pre>
<p>最后的效果:</p>
<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1y44y1M7Sa&page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
使用库函数开发GD32L233C非常的方便,后续看看在屏幕上显示些有意义的内容</p>
页:
[1]