基于GD32F103RCT6的0.96寸oled显示代码
时间: 2023-10-07 17:11:30 浏览: 142
由于涉及到硬件的具体实现,我无法提供完整的代码。但是,以下是基于GD32F103RCT6的0.96寸oled显示的一些关键步骤和代码示例,供您参考:
1. 配置SPI通信
```
//配置SPI1
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_SPI1);
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);//SCK
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);//MOSI
spi_parameter_struct spi_init_struct;
spi_i2s_parameter_struct spi_i2s_init_struct;
spi_i2s_deinit(SPI1);
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_2;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI1, &spi_init_struct);
spi_i2s_init_struct.i2s_mode = SPI_I2S_MODE_TX;
spi_i2s_init_struct.i2s_standard = SPI_I2S_STD_MSB;
spi_i2s_init_struct.data_format = SPI_I2S_DF_MSB;
spi_i2s_init_struct.i2s_mclk_output = SPI_I2S_MCLK_OUTPUT_ENABLE;
spi_i2s_init_struct.i2s_audio_freq = SPI_I2S_AUDIO_FREQ_DEFAULT;
spi_i2s_init_struct.i2s_frame_format = SPI_I2S_FRAMEFORMAT_MSBJUSTIFIED;
spi_i2s_init_struct.i2s_ws_polarity = SPI_I2S_WS_POLARITY_HIGH;
spi_i2s_init(SPI1, &spi_i2s_init_struct);
```
2. 配置OLED显示
```
#define OLED_CMD 0x00 //写命令
#define OLED_DATA 0x01 //写数据
void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
{
if(cmd) OLED_DC_Set();
else OLED_DC_Reset();
OLED_CS_Reset();
spi_i2s_data_transmit(SPI1, dat);
while (RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_TXE));
OLED_CS_Set();
}
void OLED_Init(void)
{
OLED_RST_Reset();
delay_ms(200);
OLED_RST_Set();
delay_ms(200);
OLED_WR_Byte(0xAE, OLED_CMD); //关闭显示
OLED_WR_Byte(0xD5, OLED_CMD); //设置时钟分频因子/振荡器频率
OLED_WR_Byte(0x80, OLED_CMD); //振荡器频率
OLED_WR_Byte(0xA8, OLED_CMD); //设置驱动路数
OLED_WR_Byte(0x3F, OLED_CMD); //默认值(1/64)
OLED_WR_Byte(0xD3, OLED_CMD); //设置显示偏移
OLED_WR_Byte(0x00, OLED_CMD); //默认值
OLED_WR_Byte(0x40, OLED_CMD); //设置显示开始行 [5:0]
OLED_WR_Byte(0xA0, OLED_CMD); //设置段重定义设置,bit0:0,0->0;1,0->127;
//bit1:0,0->0;1,0->63;
//默认:0XA0
OLED_WR_Byte(0xC0, OLED_CMD); //设置扫描方向;bit0:0,从左往右;1,从右往左
//bit1:0,从上往下;1,从下往上
//默认:0XC0
OLED_WR_Byte(0xDA, OLED_CMD); //设置COM硬件引脚配置
OLED_WR_Byte(0x12, OLED_CMD); //默认值(0x12)
OLED_WR_Byte(0x81, OLED_CMD); //对比度设置
OLED_WR_Byte(0xCF, OLED_CMD); //1~255;默认:0X7F (亮度设置,越大越亮)
OLED_WR_Byte(0xD9, OLED_CMD); //设置预充电周期
OLED_WR_Byte(0xF1, OLED_CMD); //默认值
OLED_WR_Byte(0xDB, OLED_CMD); //设置VCOMH 电压倍率
OLED_WR_Byte(0x40, OLED_CMD); //默认值
OLED_WR_Byte(0xA4, OLED_CMD); //全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)
//默认:开启;
OLED_WR_Byte(0xA6, OLED_CMD); //设置显示方式;bit0:1,反相显示;0,正常显示
//默认:正常显示
OLED_WR_Byte(0xAF, OLED_CMD); //开启显示
}
```
3. 显示内容
```
void OLED_Fill(uint8_t dat)
{
uint8_t i, j;
for (i = 0; i < 8; i++)
{
OLED_WR_Byte(0xb0 + i, OLED_CMD);
OLED_WR_Byte(0x00, OLED_CMD);
OLED_WR_Byte(0x10, OLED_CMD);
for (j = 0; j < 128; j++)
OLED_WR_Byte(dat, OLED_DATA);
}
}
void OLED_ShowStr(uint8_t x,uint8_t y,uint8_t *str)
{
uint8_t c = 0, i = 0, j = 0;
while (str[j] != '\0')
{
c = str[j] - 32;
if (x > 128 - 1)
{
x = 0;
y++;
}
OLED_Set_Pos(x, y);
for (i = 0; i < 6; i++)
OLED_WR_Byte(F6x8[c][i], OLED_DATA);
x += 6;
j++;
}
}
```
以上是基于GD32F103RCT6的0.96寸oled显示的一些关键步骤和代码示例,希望能帮助到您。
阅读全文