void OLED_display_on(void) { oled_write_byte(0x8d, OLED_CMD); oled_write_byte(0x14, OLED_CMD); oled_write_byte(0xaf, OLED_CMD); } void OLED_display_off(void) { oled_write_byte(0x8d, OLED_CMD); oled_write_byte(0x10, OLED_CMD); oled_write_byte(0xae, OLED_CMD);
时间: 2023-06-16 08:02:09 浏览: 244
这段代码是用于 OLED 显示屏控制的。其中,OLED_display_on() 函数用于打开 OLED 显示屏,而 OLED_display_off() 函数则用于关闭 OLED 显示屏。
具体来说,OLED_display_on() 函数通过向 OLED 显示屏发送三个命令字节,分别为 0x8d、0x14 和 0xaf,来打开 OLED 显示屏。其中,第一个命令字节为设置 DC-DC 电源模式/开关电源模式,第二个命令字节为设置 DC-DC 电源输出电压,第三个命令字节为打开 OLED 显示屏。
而 OLED_display_off() 函数则通过向 OLED 显示屏发送三个命令字节,分别为 0x8d、0x10 和 0xae,来关闭 OLED 显示屏。其中,第一个命令字节和第二个命令字节与 OLED_display_on() 函数相同,第三个命令字节为关闭 OLED 显示屏。
相关问题
使用stm32f103c8t6和ZE08甲醛传感器建立联系,同时使用SPI通信的OLED模块显示甲醛浓度,传感器接PB10和11,OLED接PA5和7传输数据,程序设计
以下是使用stm32f103c8t6和ZE08甲醛传感器建立联系,同时使用SPI通信的OLED模块显示甲醛浓度的程序设计:
```c
#include "stm32f10x.h"
#define OLED_DC_Pin GPIO_Pin_5
#define OLED_DC_Port GPIOA
#define OLED_CS_Pin GPIO_Pin_7
#define OLED_CS_Port GPIOA
void OLED_GPIO_init(void);
void OLED_write_cmd(unsigned char command);
void OLED_write_data(unsigned char data);
void OLED_init(void);
void OLED_show_str(unsigned char x, unsigned char y, unsigned char *str);
void SPI1_init(void);
void SPI1_send_byte(unsigned char byte);
unsigned char SPI1_receive_byte(void);
void USART2_init(void);
void USART2_send_byte(unsigned char byte);
void delay_ms(unsigned int ms);
int main(void)
{
unsigned char buf[10];
float concentration;
unsigned char str[10];
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
OLED_GPIO_init();
OLED_init();
SPI1_init();
USART2_init();
while(1)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_10);
delay_ms(1000);
SPI1_send_byte(0xFF);
buf[0] = SPI1_receive_byte();
buf[1] = SPI1_receive_byte();
buf[2] = SPI1_receive_byte();
buf[3] = SPI1_receive_byte();
GPIO_SetBits(GPIOB, GPIO_Pin_10);
concentration = ((buf[0] * 256) + buf[1]) / 1000.0;
sprintf((char *)str, "%.2f mg/m3", concentration);
OLED_show_str(0, 1, str);
USART2_send_byte(buf[0]);
USART2_send_byte(buf[1]);
USART2_send_byte(buf[2]);
USART2_send_byte(buf[3]);
USART2_send_byte('\n');
delay_ms(5000);
}
}
void OLED_GPIO_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = OLED_DC_Pin | OLED_CS_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void OLED_write_cmd(unsigned char command)
{
GPIO_ResetBits(OLED_DC_Port, OLED_DC_Pin);
GPIO_ResetBits(OLED_CS_Port, OLED_CS_Pin);
SPI1_send_byte(command);
GPIO_SetBits(OLED_CS_Port, OLED_CS_Pin);
}
void OLED_write_data(unsigned char data)
{
GPIO_SetBits(OLED_DC_Port, OLED_DC_Pin);
GPIO_ResetBits(OLED_CS_Port, OLED_CS_Pin);
SPI1_send_byte(data);
GPIO_SetBits(OLED_CS_Port, OLED_CS_Pin);
}
void OLED_init(void)
{
OLED_write_cmd(0xAE); // display off
OLED_write_cmd(0x20); // addressing mode
OLED_write_cmd(0x10); // horizontal addressing mode
OLED_write_cmd(0xb0); // set page start address
OLED_write_cmd(0xc8); // set COM output scan direction
OLED_write_cmd(0x00); // set low column address
OLED_write_cmd(0x10); // set high column address
OLED_write_cmd(0x40); // set start line address
OLED_write_cmd(0x81); // set contrast control register
OLED_write_cmd(0xff); // set contrast to maximum
OLED_write_cmd(0xa1); // set segment re-map 95 to 0
OLED_write_cmd(0xa6); // set normal display
OLED_write_cmd(0xa8); // set multiplex ratio
OLED_write_cmd(0x3f); // 1/64 duty
OLED_write_cmd(0xa4); // display all on/resume to display
OLED_write_cmd(0xd3); // set display offset
OLED_write_cmd(0x00); // not offset
OLED_write_cmd(0xd5); // set display clock divide ratio/oscillator frequency
OLED_write_cmd(0xf0); // set divide ratio
OLED_write_cmd(0xd9); // set pre-charge period
OLED_write_cmd(0x22); // set pre-charge period
OLED_write_cmd(0xda); // set COM pins hardware configuration
OLED_write_cmd(0x12);
OLED_write_cmd(0xdb); // set vcomh
OLED_write_cmd(0x20); // 0.77xVcc
OLED_write_cmd(0x8d); // set charge pump enable/disable
OLED_write_cmd(0x14); // enable charge pump
OLED_write_cmd(0xaf); // turn on oled panel
}
void OLED_show_str(unsigned char x, unsigned char y, unsigned char *str)
{
unsigned char i, j;
for(i = 0; str[i] != '\0'; i++)
{
for(j = 0; j < 6; j++)
{
OLED_write_data(F6x8[(str[i] - 32) * 6 + j]);
}
}
}
void SPI1_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void SPI1_send_byte(unsigned char byte)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, byte);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
unsigned char SPI1_receive_byte(void)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, 0xFF);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
void USART2_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
void USART2_send_byte(unsigned char byte)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
USART_SendData(USART2, byte);
}
void delay_ms(unsigned int ms)
{
unsigned int i, j;
for(i = 0; i < ms; i++)
{
for(j = 0; j < 1000; j++);
}
}
```
注意事项:
1. 在使用 SPI 通信时,需要先拉低从机的片选引脚(本例中为 PB10),并在传输完数据后再拉高。
2. OLED 模块使用的是 I2C 通信,本例中未使用 I2C,而是采用了自己定义的一组字符库(F6x8),因此 OLED 模块只能显示英文字母和数字,并且是单色的。如果需要显示中文或多种颜色,需要使用 I2C 通信,并且设置不同的字符库和颜色值。
树莓派使用wiringpi驱动oled屏幕
好的,树莓派使用wiringpi驱动oled屏幕的具体步骤如下:
1.安装wiringpi库
在终端中输入以下命令进行安装:
```
sudo apt-get update
sudo apt-get install wiringpi
```
2.连接oled屏幕
将oled屏幕的SDA和SCL引脚分别连接到树莓派的GPIO2和GPIO3引脚上。
3.编写代码
使用wiringpi库编写代码来控制oled屏幕的显示。以下是一个简单的示例:
```
#include <wiringPi.h>
#include <wiringPiI2C.h>
#define OLED_ADDR 0x3c
int fd;
void write_cmd(unsigned char cmd)
{
wiringPiI2CWriteReg8(fd, 0x00, cmd);
}
void write_data(unsigned char data)
{
wiringPiI2CWriteReg8(fd, 0x40, data);
}
void OLED_Init()
{
write_cmd(0xAE); // display off
write_cmd(0x20); // set memory addressing mode
write_cmd(0x00); // horizontal addressing mode
write_cmd(0x21); // set column address
write_cmd(0x00); // start column
write_cmd(0x7F); // end column
write_cmd(0x22); // set page address
write_cmd(0x00); // start page
write_cmd(0x07); // end page
write_cmd(0xB0); // set page start address
write_cmd(0xC8); // flip vertically
write_cmd(0x00); // set low column address
write_cmd(0x10); // set high column address
write_cmd(0x40); // set display start line
write_cmd(0x81); // set contrast control
write_cmd(0xFF); // set maximum contrast
write_cmd(0xA1); // set segment remap
write_cmd(0xA6); // set normal display
write_cmd(0xA8); // set multiplex ratio
write_cmd(0x3F); // set maximum multiplex ratio
write_cmd(0xA4); // set display mode
write_cmd(0xD3); // set display offset
write_cmd(0x00); // set offset to 0
write_cmd(0xD5); // set display clock divide ratio/oscillator frequency
write_cmd(0xF0); // set divide ratio and oscillator frequency
write_cmd(0xD9); // set pre-charge period
write_cmd(0x22); // set pre-charge period to 2 DCLKs
write_cmd(0xDA); // set COM pins hardware configuration
write_cmd(0x12); // set alternate COM pins
write_cmd(0xDB); // set VCOMH
write_cmd(0x40); // set VCOMH to 0.83 VCC
write_cmd(0x8D); // set charge pump
write_cmd(0x14); // enable charge pump
write_cmd(0xAF); // display on
}
void OLED_Clear()
{
int i, j;
for (i = 0; i < 8; i++)
{
write_cmd(0xB0 + i);
write_cmd(0x00);
write_cmd(0x10);
for (j = 0; j < 128; j++)
{
write_data(0x00);
}
}
}
void OLED_ShowString(unsigned char x, unsigned char y, unsigned char *str)
{
unsigned char c = 0, i = 0, j = 0;
while (str[j] != '\0')
{
c = str[j] - 32;
if (x > 120)
{
x = 0;
y++;
}
write_cmd(0xB0 + y);
write_cmd((x & 0x0F) | 0x10);
write_cmd((x >> 4) | 0x00);
for (i = 0; i < 8; i++)
{
write_data(pgm_read_byte(font8x16 + c * 16 + i));
}
write_cmd(0xB0 + y);
write_cmd((x & 0x0F) | 0x10);
write_cmd((x >> 4) | 0x00);
for (i = 8; i < 16; i++)
{
write_data(pgm_read_byte(font8x16 + c * 16 + i));
}
x += 8;
j++;
}
}
int main(int argc, char **argv)
{
if (wiringPiSetup() < 0)
{
fprintf(stderr, "Failed to setup wiringPi\n");
return 1;
}
if ((fd = wiringPiI2CSetup(OLED_ADDR)) < 0)
{
fprintf(stderr, "Failed to setup I2C device: %s\n", strerror(errno));
return 1;
}
OLED_Init();
OLED_Clear();
OLED_ShowString(0, 0, "Hello, world!");
return 0;
}
```
这段代码实现了oled屏幕的初始化、清屏和显示字符串等功能。
4.编译运行
在终端中输入以下命令进行编译:
```
gcc -o oled oled.c -lwiringPi
```
然后运行程序:
```
sudo ./oled
```
此时,oled屏幕应该可以显示出“Hello, world!”这个字符串了。
阅读全文