目录
OLED屏显、IIC协议、USART协议、中断、取模、DHT11温湿度采集、ADC光照采集、stm32f407vet6开发板等
前言
随着智能化的发展,“智慧”一词常映入我们的眼帘,目前我们的生活中很多会运用嵌入式技术,将实现功能的程序写入单片机中,因环境的改变通过单片机控制做出相应的动作,在本项目中主要实现智慧农业的实现,主要使用STM32进行开发。
提示:以下是本篇文章正文内容,下面案例可供参考
一、项目介绍
智慧农业实现的功能是首先进入开机界面,界面展现的是一个取模后的开机动画,及欢迎文字,经过一定的时间后,变成主界面,再通过按键中断控制切换成数据采集界面,采集对应的农业环境的光照、温度、湿度的数据,当采集的数值超过阈值后会进行蜂鸣器的响应等,在查看完数据后可通过按键中断进入关机的程序,跳入关机界面。
二、项目技术
OLED屏显、IIC协议、USART协议、中断、取模、DHT11温湿度采集、ADC光照采集、stm32f407vet6开发板等
三、项目代码及实现结果
1.项目代码
oled.c (oled数据文字显示,及取模软件取模后的调用)
#include "oled.h" //oled操作函数文件
#include "iic_at24c02.h"
u8 OLED_GRAM[144][8];
//char humi = 0;
//char temp = 0;
//======================================================
// 128X64I液晶底层驱动[8X16]字体库
// 设计者: powerint
// 描 述: [8X16]西文字符的字模数据 (纵向取模,字节倒序)
// !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
//======================================================
int hour;
int min;
int sec;
char secstr[4];
char minstr[4];
char hourstr[4];
unsigned char logo[]=
{
0x00,0x00,0x00,0x00,0x20,0x40,0x80,0xB8,0xF8,0xB8,0x80,0x40,0x20,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x40,0x20,0x10,0x0F,0x10,0x20,0x40,0x00,0x00,0x00,0x00
};
unsigned char logo1[]=
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x04,0x08,0x8B,0x7F,
0x8B,0x08,0x04,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x00,
0x01,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char logo2[]=
{0xFE,0xF8,0x00,0x06,0x3E,0xFC,0xFC,0x38,0x30,0x60,0xE0,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x60,0x30,0x38,0xFC,0xFC,0x7C,
0xDF,0x9F,0xBC,0xFC,0xFE,0xFE,0x11,0x1F,0x40,0xE0,0xF1,0xE3,0x43,0x03,0x03,0x03,
0x06,0x86,0xCE,0xCC,0x8C,0x06,0xE7,0xF3,0x31,0x18,0x0C,0x06,0x03,0x01,0x00,0x00,
0x0F,0x1F,0x1C,0x38,0xF0,0xE3,0x40,0x46,0x86,0x80,0x01,0x38,0xF8,0x78,0x39,0x13,
0x00,0x01,0x33,0x7B,0xB1,0xC0,0xCE,0xC7,0xC4,0x6C,0x3C,0x1C,0x0C,0x04,0x00,0x00,
0x00,0x00,0x40,0x78,0x7F,0x3F,0x1E,0x18,0x38,0x30,0x30,0x20,0x60,0x60,0x60,0x21,
0x31,0x71,0xF9,0xFD,0xEF,0xC3,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char logo3[]=
{0x00,0x00,0x00,0x00,0x00,0xC0,0xF0,0x38,0x1C,0x8C,0xE6,0xFE,0x9F,0x4F,0xDF,0xF9,
0xFB,0xCF,0x8F,0xDF,0xFE,0xC6,0xCC,0xBC,0x78,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x80,0xBF,0xFB,0xFE,0xEF,0x81,0x20,0x61,0xE1,0xC1,0xC7,0xFF,
0xFF,0xC3,0xC1,0xE1,0x60,0x20,0x83,0xFF,0xFE,0xF3,0x3F,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0E,0x1F,0x19,0x3B,0x3F,0x67,0xEF,0xFF,0xFB,0xEA,0xCA,0xCA,0xDE,0xDE,
0xDE,0xDE,0xCE,0xCF,0xEF,0xFF,0xFF,0xEF,0x7F,0x3F,0x19,0x1B,0x1F,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFF,0xFF,0xF3,0xF7,0xEF,0xEF,0xFE,0xFE,
0xFC,0xFE,0xEF,0xEF,0xE7,0xF1,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char logo4[]=
{0x00,0x00,0x00,0x00,0x00,0xC0,0xF0,0x38,0x1C,0x8C,0xE6,0xFE,0x9F,0x4F,0xDF,0xF9,
0xFB,0xCF,0x8F,0xDF,0xFE,0xC6,0xCC,0xBC,0x78,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x80,0xBF,0xFB,0xFE,0xEF,0x81,0x20,0x61,0xE1,0xC1,0xC7,0xFF,
0xFF,0xC3,0xC1,0xE1,0x60,0x20,0x83,0xFF,0xFE,0xF3,0x3F,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0E,0x1F,0x19,0x3B,0x3F,0x67,0xEF,0xFF,0xFB,0xEA,0xCA,0xCA,0xDE,0xDE,
0xDE,0xDE,0xCE,0xCF,0xEF,0xFF,0xFF,0xEF,0x7F,0x3F,0x19,0x1B,0x1F,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFF,0xFF,0xF3,0xF7,0xEF,0xEF,0xFE,0xFE,
0xFC,0xFE,0xEF,0xEF,0xE7,0xF1,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
const unsigned char F8X16[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//!1
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//"2
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//#3
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$4
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//%5
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//&6
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'7
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//(8
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//)9
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//*10
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//,12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//-13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//.14
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,///15
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//016
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//117
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//218
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//319
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//420
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//521
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//622
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//723
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//824
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//925
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//:26
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//;27
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//<28
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//=29
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//>30
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//?31
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@32
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A33
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B34
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C35
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D36
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E37
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F38
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G39
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H40
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I41
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J42
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K43
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L44
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M45
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N46
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O47
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P48
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q49
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R50
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S51
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T52
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U53
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V54
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W55
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X56
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y57
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z58
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[59
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\60
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//]61
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_63
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//`64
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a65
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b66
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c67
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d68
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e69
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f70
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g71
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h72
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i73
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j74
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k75
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l76
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m77
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n78
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o79
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p80
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q81
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r82
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s83
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t84
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u85
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v86
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w87
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x88
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y89
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z90
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{91
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//|92
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//}93
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~94
};
//汉字字模数据 字库字符数组
uint8_t F16X16[]=
{
0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,//温0
0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00,
0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00,//度1
0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00,
0x10,0x60,0x02,0x8C,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,//湿2
0x04,0x04,0x7E,0x01,0x44,0x48,0x50,0x7F,0x40,0x40,0x7F,0x50,0x48,0x44,0x40,0x00,
0x00,0x3E,0x22,0xE2,0x22,0x3E,0x00,0xFE,0x22,0x22,0x22,0x22,0x22,0xE2,0x02,0x00,//距3
0x20,0x3F,0x20,0x1F,0x11,0x11,0x00,0x7F,0x44,0x44,0x44,0x44,0x44,0x47,0x40,0x00,
0x04,0x04,0x04,0xF4,0x84,0xD4,0xA5,0xA6,0xA4,0xD4,0x84,0xF4,0x04,0x04,0x04,0x00,//离4
0x00,0xFE,0x02,0x02,0x12,0x3A,0x16,0x13,0x12,0x1A,0x32,0x42,0x82,0x7E,0x00,0x00,
0x40,0x40,0x42,0x44,0x58,0xC0,0x40,0x7F,0x40,0xC0,0x50,0x48,0x46,0x40,0x40,0x00,//光5
0x80,0x80,0x40,0x20,0x18,0x07,0x00,0x00,0x00,0x3F,0x40,0x40,0x40,0x40,0x78,0x00,
0x00,0xFE,0x42,0x42,0x42,0xFE,0x00,0x42,0xA2,0x9E,0x82,0xA2,0xC2,0xBE,0x00,0x00,//照6
0x80,0x6F,0x08,0x08,0x28,0xCF,0x00,0x00,0x2F,0xC8,0x08,0x08,0x28,0xCF,0x00,0x00,
0x20,0xC2,0x0C,0x80,0x02,0xF2,0x12,0x12,0xFE,0x12,0xFE,0x12,0x12,0xF2,0x02,0x00,//酒7
0x04,0x04,0x7E,0x01,0x00,0xFF,0x4A,0x49,0x48,0x48,0x48,0x49,0x49,0xFF,0x00,0x00,
0x44,0x58,0xC0,0xFF,0x50,0x4C,0x00,0x44,0x54,0x54,0x7F,0x54,0x54,0x54,0x44,0x00,//精8
0x08,0x06,0x01,0xFF,0x01,0x06,0x00,0x00,0xFF,0x15,0x15,0x55,0x95,0x7F,0x00,0x00,
0x10,0x60,0x02,0x8C,0x20,0x18,0x08,0xC8,0x38,0xCF,0x08,0x08,0x28,0x98,0x00,0x00,//浓9
0x04,0x04,0x7E,0x01,0x04,0x02,0x01,0xFF,0x40,0x21,0x06,0x0A,0x11,0x20,0x40,0x00,
0x10,0x10,0x14,0xD4,0x54,0x54,0x54,0xFC,0x52,0x52,0x52,0xD3,0x12,0x10,0x10,0x00,//重10
0x40,0x40,0x50,0x57,0x55,0x55,0x55,0x7F,0x55,0x55,0x55,0x57,0x50,0x40,0x40,0x00,
0x20,0x20,0x20,0xBE,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xBE,0x20,0x20,0x20,0x00,//量11
0x00,0x80,0x80,0xAF,0xAA,0xAA,0xAA,0xFF,0xAA,0xAA,0xAA,0xAF,0x80,0x80,0x00,0x00,
0x00,0x00,0x04,0x04,0x04,0x04,0x04,0xFC,0x04,0x04,0x04,0x04,0x04,0x04,0x00,0x00,//王12
0x00,0x80,0x81,0x81,0x81,0x81,0x81,0xFF,0x81,0x81,0x81,0x81,0x81,0x81,0x80,0x00,
0x10,0x94,0x53,0x32,0x1E,0x32,0x52,0x10,0x00,0x7E,0x42,0x42,0x42,0x7E,0x00,0x00,
0x00,0x00,0x00,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,0x00,/*"智",13*/
0x22,0x2A,0xAA,0xFF,0xAA,0xAA,0xA2,0x80,0xA2,0xAA,0xAA,0xFF,0xAA,0x2A,0x22,0x00,
0x80,0x60,0x08,0x0A,0x6A,0x8A,0x8A,0x9A,0xAA,0x8A,0x8A,0xCA,0x0F,0x20,0xC0,0x00,/*"慧",14*/
0x20,0x18,0x08,0x08,0x08,0xC8,0x38,0xCF,0x08,0x08,0x08,0x08,0xA8,0x18,0x00,0x00,
0x10,0x08,0x04,0x02,0xFF,0x40,0x20,0x00,0x03,0x04,0x0A,0x11,0x20,0x40,0x40,0x00,/*"农",15*/
0x00,0x10,0x60,0x80,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0xC0,0x30,0x00,0x00,
0x40,0x40,0x40,0x43,0x40,0x7F,0x40,0x40,0x40,0x7F,0x42,0x41,0x40,0x40,0x40,0x00,/*"业",16*/
0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00,
0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00,/*"欢",17*/
0x40,0x40,0x42,0xCC,0x00,0x00,0xFC,0x04,0x02,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00,
0x00,0x40,0x20,0x1F,0x20,0x40,0x4F,0x44,0x42,0x40,0x7F,0x42,0x44,0x43,0x40,0x00,/*"迎",18*/
0x20,0x10,0x08,0xFC,0x23,0x10,0x88,0x67,0x04,0xF4,0x04,0x24,0x54,0x8C,0x00,0x00,
0x40,0x30,0x00,0x77,0x80,0x81,0x88,0xB2,0x84,0x83,0x80,0xE0,0x00,0x11,0x60,0x00,/*"您",19*/
0x00,0xFC,0x84,0x84,0x84,0xFC,0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x00,
0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,0x01,0x06,0x40,0x80,0x7F,0x00,0x00,0x00,/*"时",20*/
0x00,0xF8,0x01,0x06,0x00,0xF0,0x12,0x12,0x12,0xF2,0x02,0x02,0x02,0xFE,0x00,0x00,
0x00,0xFF,0x00,0x00,0x00,0x1F,0x11,0x11,0x11,0x1F,0x00,0x40,0x80,0x7F,0x00,0x00,/*"间",21*/
0x06,0x09,0x09,0xE6,0xF8,0x0C,0x04,0x02,0x02,0x02,0x02,0x02,0x04,0x1E,0x00,0x00,
0x00,0x00,0x00,0x07,0x1F,0x30,0x20,0x40,0x40,0x40,0x40,0x40,0x20,0x10,0x00,0x00,/*"℃",22*/
0x90,0x52,0x34,0x10,0xFF,0x10,0x34,0x52,0x80,0x70,0x8F,0x08,0x08,0xF8,0x08,0x00,
0x82,0x9A,0x56,0x63,0x22,0x52,0x8E,0x00,0x80,0x40,0x33,0x0C,0x33,0x40,0x80,0x00,/*"数",23*/
0x10,0x10,0xFF,0x10,0x90,0x00,0xFE,0x92,0x92,0x92,0xF2,0x92,0x92,0x9E,0x80,0x00,
0x42,0x82,0x7F,0x01,0x80,0x60,0x1F,0x00,0xFC,0x44,0x47,0x44,0x44,0xFC,0x00,0x00,/*"据",24*/
0x00,0x00,0x04,0x14,0x64,0x04,0x0C,0xB4,0x02,0x02,0x42,0x33,0x02,0x00,0x00,0x00,
0x40,0x41,0x21,0x11,0x09,0x05,0x03,0xFF,0x03,0x05,0x09,0x11,0x21,0x41,0x40,0x00,/*"采",25*/
0x20,0x10,0x08,0xFC,0x57,0x54,0x54,0x55,0xFE,0x54,0x54,0x54,0x54,0x04,0x00,0x00,
0x44,0x44,0x24,0x27,0x15,0x0D,0x05,0xFF,0x05,0x0D,0x15,0x25,0x25,0x45,0x44,0x00,/*"集",26*/
0x40,0x40,0x42,0x44,0x58,0xC0,0x40,0x7F,0x40,0xC0,0x50,0x48,0x46,0x40,0x40,0x00,
0x80,0x80,0x40,0x20,0x18,0x07,0x00,0x00,0x00,0x3F,0x40,0x40,0x40,0x40,0x78,0x00,/*"光",27*/
0x00,0xF8,0x00,0x00,0xFF,0x40,0x20,0x18,0x0F,0x18,0x68,0x08,0x08,0x08,0x08,0x00,
0x00,0x1F,0x00,0x00,0xFF,0x00,0x00,0x7F,0x21,0x21,0x3F,0x21,0x21,0x7F,0x00,0x00,/*"临",28*/
0x02,0x02,0x02,0x02,0x02,0x02,0xFE,0x02,0x02,0x42,0x82,0x02,0x02,0x02,0x02,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x01,0x06,0x00,0x00,0x00,/*"下",29*/
0x00,0x02,0x0C,0x80,0x60,0x80,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00,
0x02,0x02,0x7E,0x01,0x80,0x40,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00,/*"次",30*/
0x02,0x02,0xF2,0x92,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0x92,0xF2,0x02,0x02,0x00,
0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x07,0x04,0x04,0x44,0x84,0x7F,0x04,0x04,0x00,/*"再",31*/
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0xF2,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,
0x80,0x80,0x40,0x47,0x20,0x18,0x06,0x01,0x7E,0x80,0x80,0x87,0x80,0x80,0xE0,0x00,/*"见",32*/
};
//显示温度 和湿度函数
void OLed_ShowTemp(void)
{
//第1行显示温度
OLed_ShowChina1(16,2,0);
OLed_ShowChina1(32,2,1);
OLed_ShowChina1(82,2,22);
//第2行显示湿度
OLed_ShowChina1(16,4,2);
OLed_ShowChina1(32,4,1);
OLed_ShowASCII(82,4,"%");
}
//显示距离汉字
void OLed_ShowDist(void)
{
//第2行显示距离
OLed_ShowChina1(16,2,3);
OLed_ShowChina1(32,2,4);
}
//显示光照汉字
void OLed_ShowLight(void)
{
//第2行显示光照
OLed_ShowChina1(16,6,5);
OLed_ShowChina1(32,6,6);
}
//显示酒精浓度汉字
void OLed_ShowAlcol(void)
{
//第2行显示酒精浓度
OLed_ShowChina1(0,2,7);
OLed_ShowChina1(16,2,8);
OLed_ShowChina1(32,2,9);
OLed_ShowChina1(48,2,1);
}
//显示重量汉字
void OLed_ShowWeight(void)
{
//第2行显示重量
OLed_ShowChina1(16,2,10);
OLed_ShowChina1(32,2,11);
}
//显示智慧农业
void OLed_Start(void)
{
OLed_ShowChina1(0,0,13);//智
OLed_ShowChina1(16,0,14);//慧
OLed_ShowChina1(32,0,15);//农
OLed_ShowChina1(48,0,16);//业
OLed_ShowChina1(64,0,17);//欢
OLed_ShowChina1(80,0,18);//迎
OLed_ShowChina1(96,0,19);//您
OLed_ShowASCII(112,0,"!");
OLed_ShowASCII(128,0,"");
}
//显示数据采集
void OLed_Showcj(void)
{
OLed_ShowChina1(24,0,23);
OLed_ShowChina1(40,0,24);
OLed_ShowChina1(56,0,25);
OLed_ShowChina1(72,0,26);
}
void OLed_Time(void)
{
OLed_ShowASCII(32,6,"12:00:00");
OLed_ShowASCII(24,4,"2023/02/27");
delay_ms(1000);
}
/*
写OLED命令寄存器的函数
*/
//这里就是根据写函数发送的各种指令而已。
//iic写自己函数
void WriteOLedCmd(uint8_t cmd)
{
uint8_t CtrWord = 0x00;
IIC_Start();
IIC_Send_Byte(OLED_SLAVE_ADDR_WR); //发送从设备地址
IIC_Wait_Ack();
IIC_Send_Byte(CtrWord); //发送命令控制字
IIC_Wait_Ack();
IIC_Send_Byte(cmd);
IIC_Wait_Ack();
IIC_Stop();
}
/*
写OLED数据的函数
*/
//iic写数据函数
void WriteOLedData(uint8_t data)
{
uint8_t CtrWord = 0x00;
CtrWord |= (0x1<<6); //表示发送的是数据
IIC_Start();
IIC_Send_Byte(OLED_SLAVE_ADDR_WR); //发送从设备地址
IIC_Wait_Ack();
IIC_Send_Byte(CtrWord); //发送命令控制字
IIC_Wait_Ack();
IIC_Send_Byte(data);
IIC_Wait_Ack();
IIC_Stop();
}
/*
设置显示位置
y--> page address页地址 ,相当于 行 (0 ~ 7) 1 100
x --> 列地址 (0 ~ 127)
*/
void OLed_SetPos(unsigned char x, unsigned char y)
{
WriteOLedCmd((0xb0+y)); //设置行地址,设置页号
WriteOLedCmd(((x&0xf0)>>4)|0x10); //设置列地址高位
WriteOLedCmd((x&0x0f)|0x00); //设置列地址的低位
}
#define X_WIDTH 128
/*
填充显示数据缓冲区
*/
void OLed_Fill(unsigned char bmp_data)
{
unsigned char y,x;
for(y=0;y<8;y++)
{
//设置PAGE地址
WriteOLedCmd(0xb0+y);
//设置列地址
WriteOLedCmd(0x00);
WriteOLedCmd(0x10);
for(x=0;x<X_WIDTH;x++)
{
WriteOLedData(bmp_data);
}
}
}
//-----------------------------------------------------//
//1 打开oled函数
void InitOLed(void)
{
//oled屏幕里面有一块oled芯片。芯片需要指令来控制。
//让所有知识点内容统一起来。
//全部的内容就是通过iic协议向oled芯片输入各种指令,控制oled芯片:SSD1306;
//通过输入命令,可以控制oled屏幕。
//给OLED发送命令 初始化OLED
//对oled设备进行相关设定
//查询指令表得出的结论
//根据芯片手册SSD1306:向芯片发送指令,驱动芯片做事。
//关闭显示 //1010 1110
WriteOLedCmd(0xAE);//--turn off oled panel//查表。
//设定行地址
WriteOLedCmd(0x00);//---set low column address //行底地址
//设定列地址
WriteOLedCmd(0x10);//---set high column address //行高地址
//设定起始行地址
WriteOLedCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
//设定对比度控制寄存器。
WriteOLedCmd(0x81);//--set contrast control register
WriteOLedCmd(0xCF); // Set SEG Output Current Brightness
//设定段映射
WriteOLedCmd(0xA1);//--Set SEG/Column Mapping 0xa0???? 0xa1??
//设定扫描方向行扫描 // 1100 1000
WriteOLedCmd(0xC8);//Set COM/Row Scan Direction 0xc0???? 0xc8??
//正常显示 高电平驱动
WriteOLedCmd(0xA6);//--set normal display
//复用率
WriteOLedCmd(0xA8);//--set multiplex ratio(1 to 64)
WriteOLedCmd(0x3f);//--1/64 duty
//偏移量设定
WriteOLedCmd(0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
WriteOLedCmd(0x00);//-not offset
//时钟频率
WriteOLedCmd(0xd5);//--set display clock divide ratio/oscillator frequency
WriteOLedCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
//充电周期
WriteOLedCmd(0xD9);//--set pre-charge period
WriteOLedCmd(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
//引脚硬件配置 //iic协议的发送数据函数
WriteOLedCmd(0xDA);//--set com pins hardware configuration
WriteOLedCmd(0x12);
//电压输出
WriteOLedCmd(0xDB);//--set vcomh
//选择线
WriteOLedCmd(0x40);//Set VCOM Deselect Level
//设定页地址
WriteOLedCmd(0x02);//-Set Page Addressing Mode (0x00/0x01/0x02)
//WriteOLedCmd(0x02);//
//充电使能 //对oled驱动芯片的设定
WriteOLedCmd(0x8D);//--set Charge Pump enable/disable
WriteOLedCmd(0x14);//--set(0x10) disable
//全部显示、翻转显示 --- 这就是 oled控制芯片与指令的关系。
WriteOLedCmd(0xA4);// Disable Entire Display On (0xa4/0xa5)
WriteOLedCmd(0xA6);// Disable Inverse Display On (0xa6/a7)
//打开显示器
WriteOLedCmd(0xAF);//--turn on oled panel
//打开显示器
WriteOLedCmd(0xAF); /*display ON*/
OLed_Fill(0x00);//描点;
//WriteOLedCmd(0xAE);
}
//2oled关闭函数
void offInitOLed(void)
{
//给OLED发送命令 初始化OLED
WriteOLedCmd(0xAE);//--turn off oled panel
WriteOLedCmd(0x00);//---set low column address
WriteOLedCmd(0x10);//---set high column address
WriteOLedCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
WriteOLedCmd(0x81);//--set contrast control register
WriteOLedCmd(0xCF); // Set SEG Output Current Brightness
WriteOLedCmd(0xA1);//--Set SEG/Column Mapping 0xa0???? 0xa1??
WriteOLedCmd(0xC8);//Set COM/Row Scan Direction 0xc0???? 0xc8??
WriteOLedCmd(0xA6);//--set normal display
WriteOLedCmd(0xA8);//--set multiplex ratio(1 to 64)
WriteOLedCmd(0x3f);//--1/64 duty
WriteOLedCmd(0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
WriteOLedCmd(0x00);//-not offset
WriteOLedCmd(0xd5);//--set display clock divide ratio/oscillator frequency
WriteOLedCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
WriteOLedCmd(0xD9);//--set pre-charge period
WriteOLedCmd(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
WriteOLedCmd(0xDA);//--set com pins hardware configuration
WriteOLedCmd(0x12);
WriteOLedCmd(0xDB);//--set vcomh
WriteOLedCmd(0x40);//Set VCOM Deselect Level
WriteOLedCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
WriteOLedCmd(0x02);//
WriteOLedCmd(0x8D);//--set Charge Pump enable/disable
WriteOLedCmd(0x14);//--set(0x10) disable
WriteOLedCmd(0xA4);// Disable Entire Display On (0xa4/0xa5)
WriteOLedCmd(0xA6);// Disable Inverse Display On (0xa6/a7)
WriteOLedCmd(0xAF);//--turn on oled panel
}
//发送一个字节
//mode:数据/命令标志 0,表示命令;1,表示数据;
void OLED_WR_Byte(u8 dat,u8 mode)
{
IIC_Start();
IIC_Send_Byte(0x78);
IIC_Wait_Ack();
if(mode){IIC_Send_Byte(0x40);}
else{IIC_Send_Byte(0x00);}
IIC_Wait_Ack();
IIC_Send_Byte(dat);
IIC_Wait_Ack();
IIC_Stop();
}
//更新显存到OLED
void OLED_Refresh(void)
{
u8 i,n;
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); //设置高列起始地址
IIC_Start();
IIC_Send_Byte(0x78);
IIC_Wait_Ack();
IIC_Send_Byte(0x40);
IIC_Wait_Ack();
for(n=0;n<128;n++)
{
IIC_Send_Byte(OLED_GRAM[n][i]);
IIC_Wait_Ack();
}
IIC_Stop();
}
}
//清屏函数
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
for(n=0;n<128;n++)
{
OLED_GRAM[n][i]=0;//清除所有数据
}
}
OLED_Refresh();//更新显示
}
/***********************************显示3个函数*************************************************/
uint8_t fbuf1[]={0x00,0x00,0xF0,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x10,0xF0,0x00,0x00,0x00};
uint8_t fbuf2[]={0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x0F,0x00,0x00,0x00};/*中*/
uint8_t fbuf3[]={0x00,0xFE,0x02,0x12,0x92,0x92,0x92,0xF2,0x92,0x92,0x92,0x12,0x02,0xFE,0x00,0x00};
uint8_t fbuf4[]={0x00,0xFF,0x40,0x48,0x48,0x48,0x48,0x4F,0x48,0x4A,0x4C,0x48,0x40,0xFF,0x00,0x00};/*国*/
/*
功能:在指定位置显示指定ASCII码对应字符串
@x: 显示的行号(页号0~7)
@y:显示的列号(列号0~127)
@str: 要显示的ascii的字符串
*/
//3.显示字母函数
void OLed_ShowASCII(uint8_t x, uint8_t y,char *str)
{
uint8_t i = 0;
char *pstr = str;
while(*pstr)
{
OLed_SetPos(x,y);
for(i=0;i<8;i++)
{
WriteOLedData(F8X16[((*pstr)-32)*16+i]);
}
OLed_SetPos(x,y+1);
for(i=0;i<8;i++)
{
WriteOLedData(F8X16[((*pstr)-32)*16+8+i]);
}
pstr++;
x +=8;
}
}
/*
功能: 在指定位置显示一个汉字, 显示下一个汉字时,X每次递增16
Y递增 2 字模数据 buf
*/
//4.显示汉字函数
void OLed_ShowChina(uint8_t x,uint8_t y,uint8_t *buf)
{
uint8_t i = 0;
OLed_SetPos(x,y); //设置显示位置
for(i=0;i<16;i++)
{
WriteOLedData(buf[i]);
}
OLed_SetPos(x,(y+1));
for(i=0;i<16;i++)
{
WriteOLedData(buf[i+16]);
}
}
//显示图片
//x0:横坐标;y0:纵坐标;x1:图片起点;y1:所占页数;BMP[]:选用的结构体名称
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
unsigned int j=0;
unsigned char x,y;
if(y1%8 == 0)
y = y1/8;
else
y = y1/8 +1;
for(y=y0;y<y1;y++)
{
OLed_SetPos(x0,y);
for(x=x0;x<x1;x++)
{
WriteOLedData(BMP[j++]);
}
}
}
//num 显示汉字的个数
//space 每一遍显示的间隔
//mode:0,反色显示;1,正常显示
void OLED_ScrollDisplay(u8 num,u8 space)//,u8 mode
{
u8 i,n,t=0,m=0,r;
while(1)
{
if(m==0)
{
OPEN_show();
t++;
}
if(t==num)
{
for(r=0;r<16*space;r++) //显示间隔
{
for(i=1;i<144;i++)
{
for(n=0;n<8;n++)
{
OLED_GRAM[i-1][n]=OLED_GRAM[i][n];
}
}
OLED_Refresh();
}
t=0;
}
m++;
if(m==16){m=0;}
for(i=1;i<144;i++) //实现左移
{
for(n=0;n<8;n++)
{
OLED_GRAM[i-1][n]=OLED_GRAM[i][n];
}
}
OLED_Refresh();
break;
}
}
//显示开机动画
void OPEN_show(void)
{
int x= 0;
while(x<48)
{
OLED_Clear();
OLED_DrawBMP(x-48+48,2,x-48+80,6,logo3);//显示图片移动从最左边到中间
delay_ms(100);
x++;
OLED_Clear();
if(x==48)
{
OLED_Clear();
break;
}
}
OLed_ShowChina1(32,2,17);
OLED_DrawBMP(48,2,80,6,logo3);//显示图片
OLed_ShowChina1(80,2,18);
OLed_ShowChina1(32,4,27);
OLed_ShowChina1(80,4,28);
delay_ms(6000);
OLED_Clear();
}
//显示关机动画
void CLOSE_show(void)
{
int x= 0;
while(x<80)
{
OLED_Clear();
OLED_DrawBMP(x+48,2,x+80,6,logo3);//显示图片移动从最左边到中间到移除
delay_ms(100);
x++;
OLED_Clear();
if(x==80)
{
OLED_Clear();
break;
}
}
OLed_ShowChina1(32,2,31);
OLED_DrawBMP(48,2,80,6,logo3);
OLed_ShowChina1(80,2,32);
OLed_ShowChina1(32,4,33);
OLed_ShowChina1(80,4,34);
delay_ms(6000);
OLED_Clear();
offInitOLed();
}
/*
在 x,y 位置显示 中国
*/
void OLed_ShowTest(unsigned char x,unsigned char y)
{
uint8_t i = 0;
OLed_SetPos(x,y);
for(i=0;i<16;i++)
{
WriteOLedData(fbuf1[i]);
}
OLed_SetPos(x,(y+1));
for(i=0;i<16;i++)
{
WriteOLedData(fbuf2[i]);
}
OLed_SetPos((x+16),y);
for(i=0;i<16;i++)
{
WriteOLedData(fbuf3[i]);
}
OLed_SetPos((x+16),(y+1));
for(i=0;i<16;i++)
{
WriteOLedData(fbuf4[i]);
}
}
//取汉字字库输出
void OLed_ShowChina1(uint8_t x,uint8_t y,unsigned char bufstr)
{
unsigned char wm=0;
unsigned int adder=32*bufstr;
OLed_SetPos(x,y);
for(wm = 0;wm < 16;wm++)
{
WriteOLedData(F16X16[adder]);
adder += 1;
}
OLed_SetPos(x,y + 1);
for(wm = 0;wm < 16;wm++)
{
WriteOLedData(F16X16[adder]);
adder += 1;
}
}
//起始页0
void OLed_page0(void)
{
//显示智慧农业
OLed_Start();
OLed_Time();
}
//页1
void OLed_page1(void)
{ OLed_Showcj();
WS_Run();
GM_Av();
}
main.c(主函数)
#include "stm32f4xx.h"
#include "sys.h"
#include "delay.h"
#include "exti.h"
#include "led.h"
#include "key.h"
#include "beep.h"
#include "timer.h"
#include "usart1.h"
#include "dht11.h"
#include "pwm.h"
#include "oled.h"
#include "iic_at24c02.h"
#include "myadc.h"
extern unsigned char logo[];
extern uint8_t F16X16[];
extern uint8_t HZ1[];
extern uint8_t HZ2[];
unsigned int flag = 1;
unsigned int LED_value = 0;
unsigned char data = 0;
extern char wd[30];//外部变量声明 温度
extern char sd[30];
extern u8 humi ;
extern u8 temp ;
extern unsigned short int num;
unsigned int count=1;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
IIC_Init();
LED_Init();
Beep_Init();
InitOLed();
myAdcInit();
Usart1_Init();
Key_Init();
pwminit();
myPrintf("电源已上电\t",0);
OPEN_show();
//main1();
Open_Led(1);
while(1)
{
myPrintf("\t主界面已打开\r\n",0);
while(1)
{
OLed_page0();
delay_ms(1000);
if(KEY1==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY1==0)
{
while(KEY1==0);
myPrintf("\t主界面已关闭\r\n",0);
OLED_Clear();
break;
}
}
if(KEY3==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY3==0)
{
while(KEY3==0);
Open_Led(LEDALL);
Openbeep();
//GPIO_ToggleBits(GPIOE,GPIO_Pin_8);
}
}
if(KEY4==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY4==0)
{
while(KEY4==0);
Closebeep();
Close_Led(LEDALL);
//GPIO_ToggleBits(GPIOE,GPIO_Pin_8);
}
}
}
if(KEY2==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY2==0)
{
while(KEY2==0);
CLOSE_show();
Close_Led(1);
break;
}
}
while(1)
{
DHT11_Read_Data(&temp,&humi);
num = getAdcAverage(0,4);
myPrintf("正在采集...\r\n",0);
Led_Shine();
myPrintf("\t温度:",temp);
delay_ms(500);
myPrintf("\t湿度:",humi);
delay_ms(500);
myPrintf("\t光照:",num);
if(temp>25)
{
myBeep();
myPrintf("\t\n报警!温度过高!\t\n",0);
}
else if(temp<=25)
{
Closebeep();}
OLed_page1();
delay_ms(1000);
if(KEY1==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY1==0)
{
while(KEY1==0);
myPrintf("采集结束...\r\n",0);
OLED_Clear();
break;
}
}
}
if(KEY2==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY2==0)
{
while(KEY2==0);
CLOSE_show();
Close_Led(1);
break;
}
}
}
//防止程序跑飞
}
iic.c
#include "iic_at24c02.h"
#include "delay.h"
#define I2C_DELAY 2
/*
//初始化IIC
I2C_SCL ---> PB6
I2C_SDA ---> PB7
*/
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOB时钟
//GPIOB6,B7初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
IIC_SCL=1;
IIC_SDA=1;
}
//产生IIC起始信号
void IIC_Start(void)
{
SDA_OUT(); //sda线输出
IIC_SDA=1;
IIC_SCL=1;
delay_us(I2C_DELAY);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(I2C_DELAY);
IIC_SCL=0;//钳住I2C总线,准备发送或接收数据
}
//产生IIC停止信号
void IIC_Stop(void)
{
SDA_OUT();//sda线输出
IIC_SCL=0;
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
delay_us(I2C_DELAY);
IIC_SCL=1;
delay_us(I2C_DELAY);
IIC_SDA=1;//发送I2C总线结束信号
delay_us(I2C_DELAY);
}
//等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDA设置为输入
IIC_SDA=1;delay_us(I2C_DELAY);
IIC_SCL=1;delay_us(I2C_DELAY);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL=0;//时钟输出0
return 0;
}
//产生ACK应答
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
delay_us(I2C_DELAY);
IIC_SCL=1;
delay_us(I2C_DELAY);
IIC_SCL=0;
}
//不产生ACK应答
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(I2C_DELAY);
IIC_SCL=1;
delay_us(I2C_DELAY);
IIC_SCL=0;
}
//IIC发送一个字节
//返回从机有无应答
//1,有应答
//0,无应答
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0;//拉低时钟开始数据传输
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7;
txd<<=1;
delay_us(I2C_DELAY); //对TEA5767这三个延时都是必须的
IIC_SCL=1;
delay_us(I2C_DELAY);
IIC_SCL=0;
delay_us(I2C_DELAY);
}
}
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN();//SDA设置为输入
for(i=0;i<8;i++ )
{
IIC_SCL=0;
delay_us(I2C_DELAY);
IIC_SCL=1;
delay_us(I2C_DELAY);
receive<<=1;
if(READ_SDA)receive++;
}
if (!ack)
IIC_NAck();//发送nACK
else
IIC_Ack(); //发送ACK
return receive;
}
gm1.c(光照数据的采集ADC的转换)
#include "stm32f4xx.h"
#include "oled.h"
#include "beep.h"
#include "led.h"
#include "iic_at24c02.h"
#include "delay.h"
#include "dht11.h"
#include "myadc.h"
#include "stdio.h"
char guangzhao[30];
unsigned short int num = 0 ;
/*
使用ADC1
ADC_IN0 --->光照--->PA0
ADC_IN10 --->可变电压输入--->PC0
ADC_IN8--->J15-->PB0
ADC_IN9--->J14-->PB1 --->酒精传感器
*/
//初始化ADC
//三个函数:电路配置 + 采集函数 + 求平均值函数
void myAdcInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
//1使能GPIOA时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//2ADC1电路使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
//3先初始化ADC1通道0 IO口 PA0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
//模拟输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
//不带上下拉
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
//初始化 PA0
GPIO_Init(GPIOA, &GPIO_InitStructure);
// //ADC1复位 //复位结束
// RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);
// RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);
//独立模式
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
//两个采样阶段之间的延迟5个时钟
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
//DMA失能
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
//4初始化
ADC_CommonInit(&ADC_CommonInitStructure);
//12位模式
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
//非扫描模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
//关闭连续转换 一次转换
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
//禁止触发检测,使用软件触发
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
//右对齐
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
//1个转换在规则序列中 也就是只转换规则序列1
ADC_InitStructure.ADC_NbrOfConversion = 1;
//5ADC初始化
ADC_Init(ADC1, &ADC_InitStructure);
//6开启AD转换器
ADC_Cmd(ADC1, ENABLE);
}
//获得ADC值
//ch: @ref ADC_channels
//通道值 0~16取值范围为:ADC_Channel_0~ADC_Channel_16
//返回值:转换结果
unsigned short int getAdc(unsigned char ch)
{
//设置规则通道组
//1ADC1,通道0,一个序列,480个机器周期,提高采样时间可以提高精确度,这是采样时间。
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_480Cycles );
//2开始转换
ADC_SoftwareStartConv(ADC1);
//3等待转换结束
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));
//4获取转换结果
return ADC_GetConversionValue(ADC1);
}
//获取通道ch的转换值,取times次,然后平均
//ch:通道编号
//times:获取次数
//返回值:通道ch的times次转换结果平均值
unsigned short int getAdcAverage(unsigned char ch,unsigned char times)
{
//一个通道采集5次,取和 求平均值。
unsigned short int temp_val=0;
unsigned char t;
for(t=0;t<times;t++)
{
temp_val+=getAdc(ch);
delay_ms(5);
}
return temp_val/times;
}
void GM_Av(void)
{
//
// //各种初始化
// delay_init(168);
// IIC_Init();
// InitOLed();
myAdcInit();
num = getAdcAverage(0,4);
//显示光照汉字
OLed_ShowLight();
//显示光照数值 //一直是通道0 采集四个取平均值。
sprintf(guangzhao,":%d",getAdcAverage(0,4));
OLed_ShowASCII(48,6,guangzhao);
delay_ms(500);
}
dht11.c(温湿度采集)
#include "dht11.h"
char wd[30];
char sd[30];
char buf[5] = "";
u8 humi = 0;
u8 temp = 0;
//复位DHT11
void DHT11_Rst(void)
{
DHT11_IO_OUT(); //SET OUTPUT
DHT11_DQ_OUT=0; //拉低DQ
delay_ms(20); //拉低至少18ms
DHT11_DQ_OUT=1; //DQ=1
delay_us(30); //主机拉高20~40us
}
//等待DHT11的回应
//返回1:未检测到DHT11的存在
//返回0:存在
u8 DHT11_Check(void)
{
u8 retry=0;
DHT11_IO_IN();//SET INPUT
while (DHT11_DQ_IN&&retry<100)//DHT11会拉低40~80us
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
else retry=0;
while (!DHT11_DQ_IN&&retry<100)//DHT11拉低后会再次拉高40~80us
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
return 0;
}
//从DHT11读取一个位
//返回值:1/0
u8 DHT11_Read_Bit(void)
{
u8 retry=0;
while(DHT11_DQ_IN&&retry<100)//等待变为低电平
{
retry++;
delay_us(1);
}
retry=0;
while(!DHT11_DQ_IN&&retry<100)//等待变高电平
{
retry++;
delay_us(1);
}
delay_us(40);//等待40us
if(DHT11_DQ_IN)return 1;
else return 0;
}
//从DHT11读取一个字节
//返回值:读到的数据
u8 DHT11_Read_Byte(void)
{
u8 i,dat;
dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
//从DHT11读取一次数据
//temp:温度值(范围:0~50°)
//humi:湿度值(范围:20%~90%)
//返回值:0,正常;1,读取失败
u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
u8 buf[5];
u8 i;
DHT11_Rst();
if(DHT11_Check()==0)
{
for(i=0;i<5;i++)//读取40位数据
{
buf[i]=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi=buf[0];
*temp=buf[2];
}
}else return 1;
return 0;
}
//初始化DHT11的IO口 DQ 同时检测DHT11的存在
//返回1:不存在 --->PA3
//返回0:存在
u8 DHT11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA时钟
//GPIOF9,F10初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化
DHT11_Rst();
return DHT11_Check();
}
void WS_Run(void)
{
DHT11_Init();
OLed_ShowTemp();
DHT11_Read_Data(&temp,&humi);
sprintf(wd,":%d",temp);
sprintf(sd,":%d",humi);
OLed_ShowASCII(48,2,wd);
OLed_ShowASCII(48,4,sd);
delay_ms(200);
}
usart1.c(usart串口的使用)
#include "usart1.h"
extern void pwminit(void);
char buftest[25]="";
char buftest1[25]="";
u8 pwmx = 0;
void Usart1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10,GPIO_AF_USART1);
GPIO_InitTypeDef usart1struct;
usart1struct.GPIO_Mode =GPIO_Mode_AF ;
usart1struct.GPIO_Pin = GPIO_Pin_9 |GPIO_Pin_10;
usart1struct.GPIO_PuPd = GPIO_PuPd_UP;
usart1struct.GPIO_Speed = GPIO_Low_Speed;
GPIO_Init(GPIOA, &usart1struct);
USART_InitTypeDef Usartstruct;
Usartstruct.USART_BaudRate = 115200;
Usartstruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
Usartstruct.USART_Parity = USART_Parity_No;
Usartstruct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
Usartstruct.USART_StopBits = USART_StopBits_1;
Usartstruct.USART_WordLength = USART_WordLength_8b;
USART_Init( USART1,& Usartstruct);
//总中断NVIC分等级
NVIC_InitTypeDef nvicstruct;
nvicstruct.NVIC_IRQChannel = USART1_IRQn;
nvicstruct.NVIC_IRQChannelPreemptionPriority = 0;
nvicstruct.NVIC_IRQChannelSubPriority =1 ;
nvicstruct.NVIC_IRQChannelCmd = ENABLE ;
NVIC_Init(&nvicstruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void myPrintf(char *buf,int num)
{
unsigned int i;
if(num !=0)
{
strcpy(buftest,buf);
sprintf(buftest1,"%d\t",num);
strcat(buftest,buftest1);
strcat(buftest,"\r\n");
for(i= 0;i<strlen(buftest);i++)
{
USART_SendData(USART1, buftest[i]);
delay_ms(10);
}
}
else
{
strcpy(buftest,buf);
for(i= 0;i<strlen(buftest);i++)
{
USART_SendData(USART1, buftest[i]);
delay_ms(10);
}
}
}
void USART1_IRQHandler(void)
{
//pwminit();
u16 led0pwmval=1000;
unsigned char result = 0;
//
if(USART_GetITStatus(USART1, USART_IT_RXNE))
{
result = USART_ReceiveData(USART1);
if(result == 1)
{
Open_Led(4);
USART_SendData(USART1, result+1);
}
else if(result == 2)
{
Close_Led(4);
USART_SendData(USART1, result +2);
}
else if(result == 3)
{
led0pwmval=0;
//修改比较值,修改占空比
TIM_SetCompare1(TIM1,led0pwmval);
USART_SendData(USART1, result +1);
}
else if(result == 4)
{
led0pwmval=100;
//修改比较值,修改占空比
TIM_SetCompare1(TIM1,led0pwmval);
USART_SendData(USART1, result +2);
}
else if(result == 5)
{
led0pwmval=500;
//修改比较值,修改占空比
TIM_SetCompare1(TIM1,led0pwmval);
USART_SendData(USART1, result +2);
}
}
// Open_Led(1);
Close_Led(2);
USART_ClearITPendingBit(USART1,USART_IT_RXNE );
}
key.c (按键控制)
#include "key.h"
#include "led.h"
#include "beep.h"
#include "delay.h"
void Key_Init()
{
//配置时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
//配置引脚
GPIO_InitTypeDef GPIO_InitStructure;//初始化结构体
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;//设置引脚
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//设置触发方式
//ledstruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
//初始化引脚
GPIO_ResetBits(GPIOE,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6);
//配置时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
//配置引脚
GPIO_InitTypeDef GPIO_InitStructure1;//初始化结构体
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN;//模式
GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;//推挽
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_13;//设置引脚
GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_UP;//设置触发方式
//ledstruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure1);
//初始化引脚
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
}
void Key_Scan()
//u8 Key_Scan(u8 mode)
{
if(KEY1==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY1==0)
{
while(KEY1==0)
{
Led_Flow();
}
}
}
if(KEY2==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY2==0)
{
while(KEY2==0);
Close_Led(LEDALL);
//GPIO_ToggleBits(GPIOE,GPIO_Pin_8);
}
}
if(KEY3==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY3==0)
{
while(KEY3==0);
Openbeep();
//GPIO_ToggleBits(GPIOE,GPIO_Pin_8);
}
}
if(KEY4==0)
{//消除抖动,中间隔一段时间
delay_ms(10);
if(KEY4==0)
{
while(KEY4==0);
Closebeep();
//GPIO_ToggleBits(GPIOE,GPIO_Pin_8);
}
}
}
beep.c(蜂鸣器)
#include "beep.h"
#include "led.h"
void Beep_Init()
{
//配置时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//配置引脚
GPIO_InitTypeDef GPIO_InitStructure;//初始化结构体
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//设置引脚
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//设置触发方式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//初始化引脚
GPIO_ResetBits(GPIOB,GPIO_Pin_10);
}
void Openbeep()
{
GPIO_SetBits(GPIOB,GPIO_Pin_10);
}
void Closebeep()
{
GPIO_ResetBits(GPIOB,GPIO_Pin_10);
}
void myBeep()
{
GPIO_SetBits(GPIOB,GPIO_Pin_10);
delay_ms(100);
GPIO_ResetBits(GPIOB,GPIO_Pin_10);
delay_ms(1000);
}
void Beep_Led()
{
Open_Led(LEDALL);
delay_ms(100);
Openbeep();
delay_ms(1);
Close_Led(LEDALL);
delay_ms(100);
Closebeep();
delay_ms(1000);
}
2.结果
总结
以上就是今天要讲的内容,有什么疑问,或者项目存在问题,欢迎批评指正,仅供参考。