OLED_ShowString(3, 1, "Speed3:"); uint8_t RX_Data; while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); RX_Data = USART_ReceiveData(USART1); if(RX_Data == '1') { OLED_ShowString(1, 1, "Speed1:"); } else if(RX_Data == '2') { OLED_ShowString(2, 1, "Speed2:");
时间: 2024-04-04 20:34:50 浏览: 59
这段代码是在嵌入式系统中使用的,主要是通过串口通信的方式接收数据,并根据数据的不同值来控制 OLED 屏幕显示不同的文字。具体来说,该代码首先在 OLED 屏幕上显示 "Speed3:" 这个字符串,然后通过串口接收数据,存储在 RX_Data 变量中。如果 RX_Data 的值为 '1',则 OLED 屏幕上显示 "Speed1:";如果 RX_Data 的值为 '2',则 OLED 屏幕上显示 "Speed2:"。
这段代码可能是用于控制某种设备的速度,根据不同的输入值来改变速度等级,并通过 OLED 屏幕来显示当前的速度等级。
相关问题
2,项目题目 《无线温湿度报警系统》 项目功能: ①获取温湿度数据 ②. 设置温湿度数据阀值,低于或超过阀值则进行系统报警(蜂鸣器、LED) 3. 使用OLED显示屏显示温湿度数据和系统状态 4 通过串口通信方式,将温湿度数据和系统状态发送到用户端(USB线连接PC、蓝牙连接手机) ⑤ 系统报警状态下,通过一定方式进行自调整(通风,加热,加湿) STM32读取DHT11温湿度传感器并显示在数码管上 使用材料:STM32F103、DHT11温湿度传感器、OLED 平台:keil 技术实现:利用单片机读DHT11的温湿度数据,并且显示到OLED上 ,求代码
由于这是一个比较复杂的项目,需要多个模块的代码来实现。以下是基本框架和代码,供参考:
主函数:
```c
#include "stm32f10x.h"
#include "dht11.h"
#include "oled.h"
#include "usart.h"
#include "delay.h"
int main(void) {
uint8_t temp, humi;
int alarm = 0;
int fan = 0, heater = 0, humidifier = 0;
int count = 0;
SystemInit();
OLED_Init();
USART1_Init(115200);
DHT11_Init();
while (1) {
DHT11_Read_Data(&temp, &humi);
OLED_Show_Temperature_Humidity(temp, humi);
if (temp < TEMP_LOW_THRESHOLD || temp > TEMP_HIGH_THRESHOLD ||
humi < HUMI_LOW_THRESHOLD || humi > HUMI_HIGH_THRESHOLD) {
alarm = 1;
GPIO_SetBits(GPIOA, GPIO_Pin_8);
GPIO_SetBits(GPIOC, GPIO_Pin_13);
} else {
alarm = 0;
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
}
if (alarm) {
if (count < 10) {
fan = 1;
heater = 1;
humidifier = 1;
} else {
fan = 0;
heater = 0;
humidifier = 0;
count = 0;
}
count++;
}
USART_SendData(USART1, temp);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, humi);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, alarm);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, fan);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, heater);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, humidifier);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
delay_ms(1000);
}
}
```
DHT11模块:
```c
#include "dht11.h"
#define DHT11_GPIO GPIOB
#define DHT11_Pin GPIO_Pin_2
void DHT11_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = DHT11_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_GPIO, &GPIO_InitStructure);
}
void DHT11_Set_Input(void) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = DHT11_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_GPIO, &GPIO_InitStructure);
}
void DHT11_Set_Output(void) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = DHT11_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_GPIO, &GPIO_InitStructure);
}
uint8_t DHT11_Read_Byte(void) {
uint8_t byte = 0;
for (int i = 0; i < 8; i++) {
byte <<= 1;
while (GPIO_ReadInputDataBit(DHT11_GPIO, DHT11_Pin) == RESET);
delay_us(28);
if (GPIO_ReadInputDataBit(DHT11_GPIO, DHT11_Pin) == SET) {
byte |= 0x01;
}
while (GPIO_ReadInputDataBit(DHT11_GPIO, DHT11_Pin) == SET);
}
return byte;
}
void DHT11_Read_Data(uint8_t* temp, uint8_t* humi) {
uint8_t data[5];
DHT11_Set_Output();
GPIO_ResetBits(DHT11_GPIO, DHT11_Pin);
delay_ms(18);
GPIO_SetBits(DHT11_GPIO, DHT11_Pin);
delay_us(30);
DHT11_Set_Input();
while (GPIO_ReadInputDataBit(DHT11_GPIO, DHT11_Pin) == SET);
while (GPIO_ReadInputDataBit(DHT11_GPIO, DHT11_Pin) == RESET);
while (GPIO_ReadInputDataBit(DHT11_GPIO, DHT11_Pin) == SET);
for (int i = 0; i < 5; i++) {
data[i] = DHT11_Read_Byte();
}
if ((data[0] + data[1] + data[2] + data[3]) == data[4]) {
*humi = data[0];
*temp = data[2];
}
}
```
OLED模块:
```c
#include "oled.h"
#define OLED_GPIO GPIOA
#define OLED_DC GPIO_Pin_2
#define OLED_RST GPIO_Pin_3
#define OLED_CS GPIO_Pin_4
void OLED_Write_Command(uint8_t cmd) {
GPIO_ResetBits(OLED_GPIO, OLED_DC);
GPIO_ResetBits(OLED_GPIO, OLED_CS);
SPI_I2S_SendData(SPI1, cmd);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
GPIO_SetBits(OLED_GPIO, OLED_CS);
}
void OLED_Write_Data(uint8_t data) {
GPIO_SetBits(OLED_GPIO, OLED_DC);
GPIO_ResetBits(OLED_GPIO, OLED_CS);
SPI_I2S_SendData(SPI1, data);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
GPIO_SetBits(OLED_GPIO, OLED_CS);
}
void OLED_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = OLED_DC | OLED_RST | OLED_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(OLED_GPIO, &GPIO_InitStructure);
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
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);
OLED_Write_Command(0xAE); //display off
OLED_Write_Command(0x20); //Set Memory Addressing Mode
OLED_Write_Command(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
OLED_Write_Command(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
OLED_Write_Command(0xc8); //Set COM Output Scan Direction
OLED_Write_Command(0x00); //---set low column address
OLED_Write_Command(0x10); //---set high column address
OLED_Write_Command(0x40); //--set start line address
OLED_Write_Command(0x81); //--set contrast control register
OLED_Write_Command(0xff);
OLED_Write_Command(0xa1); //--set segment re-map 0 to 127
OLED_Write_Command(0xa6); //--set normal display
OLED_Write_Command(0xa8); //--set multiplex ratio(1 to 64)
OLED_Write_Command(0x3f); //
OLED_Write_Command(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
OLED_Write_Command(0xd3); //-set display offset
OLED_Write_Command(0x00); //-not offset
OLED_Write_Command(0xd5); //--set display clock divide ratio/oscillator frequency
OLED_Write_Command(0xf0); //--set divide ratio
OLED_Write_Command(0xd9); //--set pre-charge period
OLED_Write_Command(0x22); //
OLED_Write_Command(0xda); //--set com pins hardware configuration
OLED_Write_Command(0x12);
OLED_Write_Command(0xdb); //--set vcomh
OLED_Write_Command(0x20); //0x20,0.77xVcc
OLED_Write_Command(0x8d); //--set DC-DC enable
OLED_Write_Command(0x14); //
OLED_Write_Command(0xaf); //--turn on oled panel
OLED_Clear();
}
void OLED_Clear(void) {
OLED_Write_Command(0xb0);
OLED_Write_Command(0x00);
OLED_Write_Command(0x10);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 128; j++) {
OLED_Write_Data(0x00);
}
}
}
void OLED_Show_Char(uint8_t x, uint8_t y, uint8_t chr) {
if (x > 127 || y > 7) {
return;
}
uint8_t c = chr - ' ';
OLED_Write_Command(0xb0 + y);
OLED_Write_Command(x & 0x0f);
OLED_Write_Command(0x10 | ((x >> 4) & 0x0f));
for (int i = 0; i < 6; i++) {
OLED_Write_Data(Font6x8[c][i]);
}
}
void OLED_Show_String(uint8_t x, uint8_t y, char* str) {
while (*str) {
OLED_Show_Char(x, y, *str++);
x += 6;
}
}
void OLED_Show_Temperature_Humidity(uint8_t temp, uint8_t humi) {
OLED_Clear();
OLED_Show_String(0, 0, "TEMP:");
OLED_Show_Char(48, 0, temp / 10 + '0');
OLED_Show_Char(54, 0, temp % 10 + '0');
OLED_Show_Char(60, 0, 'C');
OLED_Show_String(0, 1, "HUMI:");
OLED_Show_Char(48, 1, humi / 10 + '0');
OLED_Show_Char(54, 1, humi % 10 + '0');
OLED_Show_Char(60, 1, '%');
}
```
USART模块:
```c
#include "usart.h"
void USART1_Init(uint32_t baud_rate) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
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_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baud_rate;
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
int USART_SendData(USART_TypeDef* usart, uint8_t data) {
while (USART_GetFlagStatus(usart, USART_FLAG_TXE) == RESET);
USART_SendData(usart, data);
return data;
}
uint8_t USART_ReceiveData(USART_TypeDef* usart) {
while (USART_GetFlagStatus(usart, USART_FLAG_RXNE) == RESET);
return USART_ReceiveData(usart);
}
```
其他模块:
delay.h:
```c
#ifndef __DELAY_H
#define __DELAY_H
#include "stm32f10x.h"
void delay_us(uint32_t us);
void delay_ms(uint32_t ms);
#endif
```
delay.c:
```c
#include "delay.h"
static uint32_t us_ticks = 0;
void SysTick_Handler(void) {
if (us_ticks != 0) {
us_ticks--;
}
}
void delay_us(uint32_t us) {
us_ticks = us;
while (us_ticks);
}
void delay_ms(uint32_t ms) {
while (ms--) {
delay_us(1000);
}
}
```
Font6x8.h:
```c
#ifndef __FONT6X8_H
#define __FONT6X8_H
#include "stm32f10x.h"
extern const uint8_t Font6x8[][6];
#endif
```
Font6x8.c:
```c
#include "Font6x8.h"
const uint8_t Font6x8[][6] = {
{0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0x00,0x00,0x5f,0x00,0x00,0x00},/*"!",1*/
{0x00,0x07,0x00,0x07,0x00,0x00},/*""",2*/
{0x14,0x7f,0x14,0x7f,0x14,0x00},/*"#",3*/
{0x24,0x2a,0x7f,0x2a,0x12,0x00},/*"$",4*/
{0x23,0x13,0x08,0x64,0x62,0x00},/*"%",5*/
{0x36,0x49,0x55,0x22,0x50,0x00},/*"&",6*/
{0x00,0x05,0x03,0x00,0x00,0x00},/*"'",7*/
{0x00,0x1c,0x22,0x41,0x00,0x00},/*"(",8*/
{0x00,0x41,0x22,0x1c,0x00,0x00},/*")",9*/
{0x14,0x08,0x3e,0x08,0x14,0x00},/*"*",10*/
{0x08,0x08,0x3e,0x08,0x08,0x00},/*"+",11*/
{0x00,0x50,0x30,0x00,0x00,0x00},/*",",12*/
{0x08,0x08,0x08,0x08,0x08,0x00},/*"-",13*/
{0x00,0x60,0x60,0x00,0x00,0x00},/*".",14*/
{0x20,0x10,0x08,0x04,0x02,0x00},/*"/",15*/
{0x3e,0x51,0x49,0x45,0x3e,0x00},/*"0",16*/
{0x00,0x42,0x7f,0x40,0x00,0x00},/*"1",17*/
{0x42,0x61,0x51,0x49,0x46,0x00},/*"2",18*/
{0x21,0x41,0x45,0x4b,0x31,0x00},/*"3",19*/
{0x18,0x14,0x12,0x7f,0x10,0x00},/*"4",20*/
{0x27,0x45,0x45,0x45,0x39,0x00},/*"5",21*/
{0x3c,0x4a,0x49,0x49,0x30,0x00},/*"6",22*/
{0x01,0x71,0x09,0x05,0x03,0x00},/*"7",23*/
stm32f103c8t6oled显示时间
好的,为了实现这个功能,你需要准备以下材料:
- STM32F103C8T6开发板
- OLED显示屏
- 杜邦线若干
步骤如下:
1. 连接OLED显示屏和STM32F103C8T6开发板,具体连接方式可参考OLED显示屏的数据手册。
2. 在Keil MDK中新建一个工程,并添加STM32F103C8T6的支持包和OLED显示屏的驱动程序。
3. 编写程序,在程序中使用RTC(实时时钟)模块获取当前时间,并将时间信息显示在OLED屏幕上。
4. 通过USART串口通信功能,将当前时间发送给PC端,实现与PC端的时间同步功能。
以下是示例代码,你可以根据自己的实际情况进行修改和优化:
```c
#include "stm32f10x.h"
#include "oled.h"
#include "rtc.h"
#include "usart.h"
// RTC时钟初始化
void RTC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
}
int main(void)
{
// 初始化RTC时钟
RTC_Configuration();
// 初始化串口
USART_Configuration();
// 初始化OLED显示屏
OLED_Init();
while (1)
{
// 获取RTC当前时间
RTC_TimeTypeDef RTC_TimeStruct;
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
// 将时间信息转换为字符串格式
char timeStr[20];
sprintf(timeStr, "%02d:%02d:%02d", RTC_TimeStruct.RTC_Hours, RTC_TimeStruct.RTC_Minutes, RTC_TimeStruct.RTC_Seconds);
// 在OLED屏幕上显示时间信息
OLED_ShowString(0, 0, (uint8_t*)timeStr, 16);
// 将时间信息发送给PC端
USART_SendString(timeStr);
// 延时一定时间,避免频繁更新
delay_ms(1000);
}
}
```
希望这个回答能够对你有所帮助!
阅读全文