stm32f103c8t60.96oled显示屏llc四脚显示光照值oled.c代码
时间: 2023-07-29 22:14:49 浏览: 193
以下是基于STM32F103C8T6和0.96寸OLED屏幕的OLED显示屏驱动代码,包括初始化、清屏、显示字符串等操作:
```c
#include "oled.h"
#include "stdlib.h"
#include "oledfont.h"
void OLED_WR_Byte(uint8_t dat, uint8_t cmd)
{
if (cmd) OLED_DC_Set();
else OLED_DC_Reset();
OLED_CS_Reset();
SPI1->DR = dat;
while((SPI1->SR & SPI_I2S_FLAG_TXE) == 0);
while((SPI1->SR & SPI_I2S_FLAG_BSY) != 0);
OLED_CS_Set();
}
void OLED_Display_On(void)
{
OLED_WR_Byte(0x8D, OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0x14, OLED_CMD); //DCDC ON
OLED_WR_Byte(0xAF, OLED_CMD); //DISPLAY ON
}
void OLED_Display_Off(void)
{
OLED_WR_Byte(0x8D, OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0x10, OLED_CMD); //DCDC OFF
OLED_WR_Byte(0xAE, OLED_CMD); //DISPLAY OFF
}
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
GPIO_SetBits(GPIOA, GPIO_Pin_7);
GPIO_SetBits(GPIOB, GPIO_Pin_6);
OLED_WR_Byte(0xAE, OLED_CMD); //DISPLAY OFF
OLED_WR_Byte(0x00, OLED_CMD); //SET LOW COLUMN ADDRESS
OLED_WR_Byte(0x10, OLED_CMD); //SET HIGH COLUMN ADDRESS
OLED_WR_Byte(0x40, OLED_CMD); //SET START LINE ADDRESS
OLED_WR_Byte(0xB0, OLED_CMD); //SET PAGE ADDRESS
OLED_WR_Byte(0x81, OLED_CMD); //SET CONTRAST CONTROL
OLED_WR_Byte(0xCF, OLED_CMD);
OLED_WR_Byte(0xA1, OLED_CMD); //SET SEGMENT REMAP
OLED_WR_Byte(0xA6, OLED_CMD); //NORMAL DISPLAY
OLED_WR_Byte(0xA8, OLED_CMD); //SET MULTIPLEX RATIO
OLED_WR_Byte(0x3F, OLED_CMD);
OLED_WR_Byte(0xC8, OLED_CMD); //SET COM SCAN DIRECTION
OLED_WR_Byte(0xD3, OLED_CMD); //SET DISPLAY OFFSET
OLED_WR_Byte(0x00, OLED_CMD);
OLED_WR_Byte(0xD5, OLED_CMD); //SET DISPLAY CLOCK DIVIDE RATIO/OSCILLATOR FREQUENCY
OLED_WR_Byte(0x80, OLED_CMD);
OLED_WR_Byte(0xD9, OLED_CMD); //SET PRE-CHARGE PERIOD
OLED_WR_Byte(0xF1, OLED_CMD);
OLED_WR_Byte(0xDA, OLED_CMD); //SET COM PINS
OLED_WR_Byte(0x12, OLED_CMD);
OLED_WR_Byte(0xDB, OLED_CMD); //SET VCOMH
OLED_WR_Byte(0x40, OLED_CMD);
OLED_WR_Byte(0x8D, OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0x14, OLED_CMD); //DCDC ON
OLED_WR_Byte(0xAF, OLED_CMD); //DISPLAY ON
OLED_Clear();
}
void OLED_Clear(void)
{
uint8_t i, n;
for (i = 0; i < 8; i++)
{
OLED_WR_Byte(0xb0 + i, OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte(0x00, OLED_CMD); //设置显示位置—列低地址
OLED_WR_Byte(0x10, OLED_CMD); //设置显示位置—列高地址
for (n = 0; n < 128; n++)
{
OLED_WR_Byte(0, OLED_DATA);
}
}
}
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr, uint8_t size)
{
uint8_t c = 0, i = 0, j = 0;
c = chr - ' '; //得到偏移后的值
if (x > 128 - 1 || y > 64 - 1)
{
return;
}
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
if (size == 12)
{
OLED_WR_Byte(F12x12[c * 24 + i * 2 + j], OLED_DATA);
}
else
{
OLED_WR_Byte(F6x8[c][i], OLED_DATA);
}
}
y++;
OLED_WR_Byte(0xb0 + y, OLED_CMD);
OLED_WR_Byte(((x & 0xf0) >> 4) | 0x10, OLED_CMD);
OLED_WR_Byte((x & 0x0f) | 0x01, OLED_CMD);
}
}
void OLED_ShowString(uint8_t x, uint8_t y, char *chr, uint8_t size)
{
while (*chr)
{
OLED_ShowChar(x, y, *chr, size);
x += size / 2;
chr++;
}
}
```
其中,`OLED_WR_Byte`函数用于向OLED屏幕写入数据或命令,`OLED_Display_On`和`OLED_Display_Off`函数用于打开或关闭OLED屏幕,`OLED_Init`函数用于初始化OLED屏幕,`OLED_Clear`函数用于清屏,`OLED_ShowChar`函数用于显示单个字符,`OLED_ShowString`函数用于显示字符串。需要注意的是,字符和字符串的显示需要根据字体的大小进行相应的操作。
阅读全文