STM32 软件IIC OLED温湿度显示
Posted 一只小阿大:)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32 软件IIC OLED温湿度显示相关的知识,希望对你有一定的参考价值。
用的板子是STM32最小系统板STM32F103C8T6
原理之前再硬件IIC那篇文章中写过,就不写了,直接附上代码了
oled.c
#include "stm32f10x.h"
#include "oled.h"
#include "oledfont.h"
#include "delay.h"
//static void delay_u(unsigned char num)
//
// uint8_t i=10;
// while(num--)
//
//
// while(i--);
//
//
//
//static void delay_ms(unsigned int ms)
//
// unsigned int x,y;
// for(x = ms; x > 0 ; x--)
//
// for(y = 12000;y>0;y--);
//
//
//
static void OLED_GPIO_Init(void)
GPIO_InitTypeDef GPIO_InitStructure; //¶¨ÒåÒ»¸öGPIO_InitTypeDefÀàÐ͵ĽṹÌå
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); //´ò¿ªGPIOCµÄÍâÉèʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; //Ñ¡Ôñ¿ØÖƵÄÒý½Å
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //ÉèÖÃΪͨÓÿªÂ©Êä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ÉèÖÃÊä³öËÙÂÊΪ50MHz
GPIO_Init(GPIOB,&GPIO_InitStructure); //µ÷Óÿ⺯Êý³õʼ»¯GPIOA
OLED_SCLK_Set(); //ÉèPC2£¨SCL£©Îª¸ßµçƽ
OLED_SDIN_Set(); //ÉèPC3£¨SDA£©Îª¸ßµçƽ
static void OLED_IIC_Start(void)
OLED_SCLK_Set(); //ʱÖÓÏßÖøß
OLED_SDIN_Set(); //ÐźÅÏßÖøß
delay_us(1);
OLED_SDIN_CLr(); //ÐźÅÏßÖõÍ
delay_us(1);
OLED_SCLK_CLr(); //ʱÖÓÏßÖõÍ
delay_us(1);
static void OLED_IIC_Stop(void)
OLED_SDIN_CLr(); //ÐźÅÏßÖõÍ
delay_us(1);
OLED_SCLK_Set(); //ʱÖÓÏßÖøß
delay_us(1);
OLED_SDIN_Set(); //ÐźÅÏßÖøß
delay_us(1);
static unsigned char IIC_Wait_Ack(void)
unsigned char ack;
OLED_SCLK_CLr(); //ʱÖÓÏßÖõÍ
delay_us(1);
OLED_SDIN_Set(); //ÐźÅÏßÖøß
delay_us(1);
OLED_SCLK_Set(); //ʱÖÓÏßÖøß
delay_us(1);
if(OLED_READ_SDIN()) //¶ÁÈ¡SDAµÄµçƽ
ack = IIC_NO_ACK; //Èç¹ûΪ1£¬Ôò´Ó»úûÓÐÓ¦´ð
else
ack = IIC_ACK; //Èç¹ûΪ0£¬Ôò´Ó»úÓ¦´ð
OLED_SCLK_CLr();//ʱÖÓÏßÖõÍ
delay_us(1);
return ack; //·µ»Ø¶ÁÈ¡µ½µÄÓ¦´ðÐÅÏ¢
static void Write_IIC_Byte(unsigned char IIC_Byte)
unsigned char i; //¶¨Òå±äÁ¿
for(i=0;i<8;i++) //forÑ»·8´Î
OLED_SCLK_CLr(); //ʱÖÓÏßÖõͣ¬Îª´«ÊäÊý¾Ý×ö×¼±¸
delay_us(1);
if(IIC_Byte & 0x80) //¶ÁÈ¡×î¸ßλ
OLED_SDIN_Set();//×î¸ßλΪ1
else
OLED_SDIN_CLr(); //×î¸ßλΪ0
IIC_Byte <<= 1; //Êý¾Ý×óÒÆ1λ
delay_us(1);
OLED_SCLK_Set(); //ʱÖÓÏßÖøߣ¬²úÉúÉÏÉýÑØ£¬°ÑÊý¾Ý·¢ËͳöÈ¥
delay_us(1);
OLED_SCLK_CLr(); //ʱÖÓÏßÖõÍ
delay_us(1);
while(IIC_Wait_Ack()); //´Ó»úÓ¦´ð
static void Write_IIC_Command(unsigned char IIC_Command)
OLED_IIC_Start();
Write_IIC_Byte(0x78);//дÈë´Ó»úµØÖ·£¬SD0 = 0
Write_IIC_Byte(0x00);//дÈëÃüÁî
Write_IIC_Byte(IIC_Command);//Êý¾Ý
OLED_IIC_Stop(); //·¢ËÍÍ£Ö¹ÐźÅ
/*
@brief IICдÈëÊý¾Ý
@param IIC_Data£ºÊý¾Ý
@retval ÎÞ
*/
static void Write_IIC_Data(unsigned char IIC_Data)
OLED_IIC_Start();
Write_IIC_Byte(0x78); //дÈë´Ó»úµØÖ·£¬SD0 = 0
Write_IIC_Byte(0x40); //дÈëÊý¾Ý
Write_IIC_Byte(IIC_Data);//Êý¾Ý
OLED_IIC_Stop(); //·¢ËÍÍ£Ö¹ÐźÅ
//¶ÔOLEDдÈëÒ»¸ö×Ö½Ú
void OLED_WR_Byte(unsigned char dat,unsigned char cmd)
if(cmd)
Write_IIC_Data(dat); //дÈëÊý¾Ý
else
Write_IIC_Command(dat); //дÈëÃüÁî
//ÉèÖÃÊý¾ÝдÈëµÄÆðʼÐУ¬ÁÐ
//x: ÁеÄÆðʼµÍµØÖ·ÓÚÆðʼ¸ßµØÖ·
//y: Ò³ ÆðʼҳµÄµØÖ· 0-7
void OLED_Set_Pos(unsigned char x, unsigned char y)
OLED_WR_Byte(0xb0+y,OLED_CMD);//дÈëÒ³µØÖ·
OLED_WR_Byte((x&0x0f),OLED_CMD); //дÈëÁеĵØÖ· µÍ°ë×Ö½Ú
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);//дÈëÁеĵØÖ· ¸ß°ë×Ö½Ú
/*
@brief ¿ªÏÔʾ
@param ÎÞ
@retval ÎÞ
*/
void OLED_Display_On(void)
OLED_WR_Byte(0X8D,OLED_CMD); //ÉèÖÃOLEDµçºÉ±Ã
OLED_WR_Byte(0X14,OLED_CMD); //ʹÄÜ£¬¿ª
OLED_WR_Byte(0XAF,OLED_CMD); //¿ªÏÔʾ
/*
@brief ¹ØÏÔʾ
@param ÎÞ
@retval ÎÞ
*/
void OLED_Display_Off(void)
OLED_WR_Byte(0XAE,OLED_CMD); //¹ØÏÔʾ
OLED_WR_Byte(0X8D,OLED_CMD); //ÉèÖÃOLEDµçºÉ±Ã
OLED_WR_Byte(0X10,OLED_CMD); //ʧÄÜ£¬¹Ø
//ÇåÆÁÄ»
void OLED_Clear(void)
unsigned char 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); //дÈë 0 ÇåÆÁ
void OLED_ShouChar(unsigned char x,unsigned char y,unsigned char chr)
unsigned char c = 0,i=0;
c =chr - ' '; //»ñÈ¡×Ö·ûµÄÆ«ÒÆÁ¿
if(x>Max_Column)
x = 0; //Èç¹ûÁгöÁË·¶Î§£¬¾Í´ÓÏÂÁ½Ò³µÄµÚ0ÁпªÊ¼
y=y+2;
if(SIZE == 16) //×Ö·û´óСÈç¹ûΪ16 =8 *16
OLED_Set_Pos(x,y); //´Óx,y¿ªÊ¼»µã
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);//ÕÒ³ö×Ö·ûcµÄÊý×éλÊý£¬ÏȰѵÚÒ»ÁлÍê
OLED_Set_Pos(x,y+1); //´Óx,y¿ªÊ¼»µã
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);//ÕÒ³ö×Ö·ûcµÄÊý×éλÊý£¬ÏȰѵÚÒ»ÁлÍê
else // 6 =6*8
OLED_Set_Pos(x,y); //´Óx,y¿ªÊ¼»µã
for(i=0;i<6;i++)
OLED_WR_Byte(F6x8[c][i],OLED_DATA);//ÕÒ³ö×Ö·ûcµÄÊý×éλÊý£¬ÏȰѵÚÒ»ÁлÍê
void OLED_Init(void)
OLED_GPIO_Init(); //GPIO¿Ú³õʼ»¯
delay_ms(200); //ÑÓ³Ù£¬ÓÉÓÚµ¥Æ¬»úÉϵç³õʼ»¯±ÈOLED¿ì£¬ËùÒÔ±ØÐë¼ÓÉÏÑÓ³Ù£¬µÈ´ýOLEDÉϸ´Î»Íê³É
OLED_WR_Byte(0xAE,OLED_CMD); //¹Ø±ÕÏÔʾ
OLED_WR_Byte(0x00,OLED_CMD); //ÉèÖõÍÁеØÖ·
OLED_WR_Byte(0x10,OLED_CMD); //ÉèÖøßÁеØÖ·
OLED_WR_Byte(0x40,OLED_CMD); //ÉèÖÃÆðʼÐеØÖ·
OLED_WR_Byte(0xB0,OLED_CMD); //ÉèÖÃÒ³µØÖ·
OLED_WR_Byte(0x81,OLED_CMD); // ¶Ô±È¶ÈÉèÖ㬿ÉÉèÖÃÁÁ¶È
OLED_WR_Byte(0xFF,OLED_CMD); // 265
OLED_WR_Byte(0xA1,OLED_CMD); //ÉèÖöΣ¨SEG£©µÄÆðʼӳÉäµØÖ·£»columnµÄ127µØÖ·ÊÇSEG0µÄµØÖ·
OLED_WR_Byte(0xA6,OLED_CMD); //Õý³£ÏÔʾ£»0xa7ÄæÏÔʾ
OLED_WR_Byte(0xA8,OLED_CMD); //ÉèÖÃÇý¶¯Â·Êý£¨16~64£©
OLED_WR_Byte(0x3F,OLED_CMD); //64duty
OLED_WR_Byte(0xC8,OLED_CMD); //ÖØÓ³Éäģʽ£¬COM[N-1]~COM0ɨÃè
OLED_WR_Byte(0xD3,OLED_CMD); //ÉèÖÃÏÔʾƫÒÆ
OLED_WR_Byte(0x00,OLED_CMD); //ÎÞÆ«ÒÆ
OLED_WR_Byte(0xD5,OLED_CMD); //ÉèÖÃÕðµ´Æ÷·ÖƵ
OLED_WR_Byte(0x80,OLED_CMD); //ʹÓÃĬÈÏÖµ
OLED_WR_Byte(0xD9,OLED_CMD); //ÉèÖÃ Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD); //ʹÓùٷ½ÍƼöÖµ
OLED_WR_Byte(0xDA,OLED_CMD); //ÉèÖÃ com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD); //ʹÓÃĬÈÏÖµ
OLED_WR_Byte(0xDB,OLED_CMD); //ÉèÖà Vcomh£¬¿Éµ÷½ÚÁÁ¶È£¨Ä¬ÈÏ£©
OLED_WR_Byte(0x40,OLED_CMD); ʹÓùٷ½ÍƼöÖµ
OLED_WR_Byte(0x8D,OLED_CMD); //ÉèÖÃOLEDµçºÉ±Ã
OLED_WR_Byte(0x14,OLED_CMD); //¿ªÏÔʾ
OLED_WR_Byte(0xAF,OLED_CMD); //¿ªÆôOLEDÃæ°åÏÔʾ
OLED_Clear(); //ÇåÆÁ
OLED_Set_Pos(0,0); //ÉèÖÃÊý¾ÝдÈëµÄÆðʼÐС¢ÁÐ
//ÏÔʾ×Ö·û´®
void OLED_ShowString(unsigned char x, unsigned char y,unsigned char *chr)
unsigned char j=0;
while(chr[j] != '\\0') //ÅжÏÊDz»ÊÇ×îºóÒ»¸ö×Ö·û
OLED_ShouChar(x,y,chr[j]);//ÏÔʾ×Ö·û
x+=8; //ÁÐÊý+8£¬Ò»¸ö×Ö·ûÕ¼ 8
if(x>=128)
x= 0 ;
y +=2;
j++;
//¼ÆËãm^n´Î·½£¬result Ò»¸öÊýµÄn´Î·½
unsigned int oled_pow(unsigned char m,unsigned char n)
unsigned int result=1;
while(n--)result*=m;
return result;
void OLED_ShowNum(unsigned char x,unsigned char y,unsigned int num,unsigned char len,unsigned char size)
unsigned char t,temp; //¶¨Òå±äÁ¿
unsigned char enshow=0; //¶¨Òå±äÁ¿
for(t=0;t<len;t++)
temp=(num/oled_pow(10,len-t-1))%10;//È¡³öÊäÈëÊýµÄÿ¸ö룬Óɸߵ½µÍ
if(enshow==0&&t<(len-1)) //enshow£ºÊÇ·ñΪµÚÒ»¸öÊý£»t<(len-1)£ºÅжÏÊÇ·ñΪ×îºóÒ»¸öÊý
if(temp==0) //Èç¹û¸ÃÊýΪ0
OLED_ShouChar(x+(size/2)*t,y,' ');//ÏÔʾ 0 £»x+(size2/2)*t¸ù¾Ý×ÖÌå´óСƫÒƵÄÁÐÊý£¨8£©
continue; //Ìø¹ýÊ£ÏÂÓï¾ä£¬¼ÌÐøÖظ´Ñ»·£¨±ÜÃâÖظ´ÏÔʾ£©
else enshow=1;
OLED_ShouChar(x+(size/2)*t,y,temp+'0'); //ÏÔʾһ¸öλ£»x+(size2/2)*t¸ù¾Ý×ÖÌå´óСƫÒƵÄÁÐÊý£¨8£©
void OLED_ShowCHinese(unsigned char x,unsigned char y,unsigned char no)
unsigned char t,adder = 0;
OLED_Set_Pos(x,y);
for(t=0;t<16;t++)
OLED_WR_Byte( Hzk[2*no][t],OLED_DATA);//» noÔÚÊý×éλÖõĵÚÒ»Ò³16Áеĵã
adder+=1; //Êý×éµØÖ·+1
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
OLED_WR_Byte( Hzk[2*no+1][t],OLED_DATA);//» noÔÚÊý×éλÖõĵÚ2Ò³16Áеĵã
adder+=1; //Êý×éµØÖ·+1
oled.h
#ifndef _OLED_H_
#define _OLED_H_
#include "stm32f10x.h"
#define OLED_SCLK_Set() GPIO_SetBits(GPIOB,GPIO_Pin_0) //PB0(SCL)Êä³ö¸ß
#define OLED_SCLK_CLr() GPIO_ResetBits(GPIOB,GPIO_Pin_0) //PB0(SCL)Êä³öµÍ
#define OLED_SDIN_Set() GPIO_SetBits(GPIOB,GPIO_Pin_1) //PB1(SDA)Êä³ö¸ß
#define OLED_SDIN_CLr() GPIO_ResetBits(GPIOB,GPIO_Pin_1) //PB1(SDA)Êä³öµÍ
#define OLED_READ_SDIN() GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) //¶ÁÈ¡PB1(SDA)µçƽ
#define IIC_ACK 0 //Ó¦´ð
#define IIC_NO_ACK 1 //²»Ó¦´ð
#define OLED_CMD 0 //дÃüÁî
#define OLED_DATA 1 //дÊý¾Ý
#define SIZE 16 //ÏÔʾ×Ö·ûµÄ´óС
#define Max_Column 128 //×î´óÁÐÊý
//static void delay_u(unsigned char num);
//static void delay_ms(unsigned int ms);
static void OLED_IIC_Start(void);
static void OLED_IIC_Stop(void);
static unsigned char IIC_Wait_Ack(void);
static void Write_IIC_Byte(unsigned char IIC_Byte);
static void Write_IIC_Command(unsigned char IIC_Command);
static void Write_IIC_Data(unsigned char IIC_Data);
void OLED_WR_Byte(unsigned char dat,unsigned char cmd);
void OLED_Set_Pos(unsigned char x,unsigned char y);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Clear(void);
void OLED_ShouChar(unsigned char x,unsigned char y,unsigned char chr);
void OLED_Init(void);
void OLED_ShowString(unsigned char x, unsigned char y,unsigned char *chr);
void OLED_ShowNum(unsigned char x,unsigned char y,unsigned int num,unsigned char len ,unsigned char size);
void OLED_ShowCHinese(unsigned char x,unsigned char y,unsigned char no);
#endif
delay.c
#include "delay.h"
#include "stm32f10x.h"
void initSysTick(void)
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //ÉèÖÃʱÖÓÔ´8·ÖƵ
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; STM32 硬件IIC OLED
STM32使用IIC总线通讯协议在OLED屏幕上显示字符串汉字图像(硬件IIC)
STM32实现0.96寸OLED显示模拟IIC和IIC四种实现(标准库和HAL库)