stm32上基于SPI接口的OLED数据显示

   日期:2020-12-28     浏览:90    评论:0    
核心提示:stm32f103指南者oled显示1.显示自己的学号和姓名;2.显示AHT20的温度和湿度;3.上下的滑动显示长字符1.显示自己的学号和姓名;2.显示AHT20的温度和湿度;3.上下的滑动显示长字符

stm32f103指南者oled显示

  • 1.显示自己的学号和姓名;
  • 2.显示AHT20的温度和湿度;
  • 3.上下的滑动显示长字符
  • 总结

1.显示自己的学号和姓名;

1、SPI协议接口:
SPI协议,即串行外围设备接口,是一种高速全双工的通信总线。
在OLED官网下载例程: http://www.lcdwiki.com/zh/0.96inch_SPI_OLED_Module
2、打开“0.96inch_OLED_Demo_STM32F103ZET6_Hardware_4-wire_SPI文件夹中的例程”。
3、按照main.c文件中的注释连接OLED和stm32
CS接口不用连接。
4、使用PCtoLCD2002编写字模,
5、将字模代码粘贴到oledfront.h中
6.进入test.c,修改TEST_MainPage函数如下

void TEST_MainPage(void)
{ 	
	
	GUI_ShowString(20,0,"631807030xxx",8,1);
	GUI_ShowCHinese(16,20,16,"张家财",1);
	delay_ms(1500);			
}

7、修改main.c如下

#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "gui.h"
#include "test.h"
int main(void)
{ 	
	delay_init();	    	       //延时函数初始化 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	 //设置NVIc中断分组2:2位抢占优先级,2位响应优先级
	
	OLED_Init();			         //初始化OLED
	OLED_Clear(0);             //清屏,全黑
	while(1) 
	{ 	
		TEST_MainPage();         //主页面显示姓名与学号
		OLED_Clear(0); 
		
	}
}

8、编译并烧录程序到stm32中,可看见OLED显示了姓名和学号。

2.显示AHT20的温度和湿度;

1、在USER目录下加入bsp_i2c.c,bsp_i2c.h,usart.c,usart.h几个文件,代码分别如下。


bsp_i2c.c

#include "bsp_i2c.h"
#include "delay.h"

uint8_t   ack_status=0;
uint8_t   readByte[6];
uint8_t   AHT20_status=0;

uint32_t  H1=0;  //Humility
uint32_t  T1=0;  //Temperature

uint8_t  AHT20_OutData[4];
uint8_t  AHT20sendOutData[10] = { 0xFA, 0x06, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF};

void IIC_Init(void)
{ 					     
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(	RCC_APB2Periph_GPIOB, ENABLE );	
	   
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;   //ÍÆÍìÊä³ö
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	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(4);
 	IIC_SDA=0;//START:when CLK is high,DATA change form high to low 
	delay_us(4);
	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(4);
	IIC_SCL=1; 
	IIC_SDA=1;//·¢ËÍI2C×ÜÏß½áÊøÐźÅ
	delay_us(4);							   	
}
//µÈ´ýÓ¦´ðÐźŵ½À´
//·µ»ØÖµ£º1£¬½ÓÊÕÓ¦´ðʧ°Ü
// 0£¬½ÓÊÕÓ¦´ð³É¹¦
u8 IIC_Wait_Ack(void)
{ 
	u8 ucErrTime=0;
	SDA_IN();      //SDAÉèÖÃΪÊäÈë 
	IIC_SDA=1;delay_us(1);	   
	IIC_SCL=1;delay_us(1);	 
	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(2);
	IIC_SCL=1;
	delay_us(2);
	IIC_SCL=0;
}
//²»²úÉúACKÓ¦´ð 
void IIC_NAck(void)
{ 
	IIC_SCL=0;
	SDA_OUT();
	IIC_SDA=1;
	delay_us(2);
	IIC_SCL=1;
	delay_us(2);
	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(2);   //¶ÔTEA5767ÕâÈý¸öÑÓʱ¶¼ÊDZØÐëµÄ
		IIC_SCL=1;
		delay_us(2); 
		IIC_SCL=0;	
		delay_us(2);
    }	 
} 	    
//¶Á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(2);
		IIC_SCL=1;
    receive<<=1;
    if(READ_SDA)receive++;   
		delay_us(1); 
  }					 
	if (!ack)
			IIC_NAck();//·¢ËÍnACK
	else
			IIC_Ack(); //·¢ËÍACK 
	return receive;
}
 
void IIC_WriteByte(uint16_t addr,uint8_t data,uint8_t device_addr)
{ 
	IIC_Start();  
	
	if(device_addr==0xA0) //eepromµØÖ·´óÓÚ1×Ö½Ú
		IIC_Send_Byte(0xA0 + ((addr/256)<<1));//·¢Ë͸ߵØÖ·
	else
		IIC_Send_Byte(device_addr);	    //·¢Æ÷¼þµØÖ·
	IIC_Wait_Ack(); 
	IIC_Send_Byte(addr&0xFF);   //·¢Ë͵͵ØÖ·
	IIC_Wait_Ack(); 
	IIC_Send_Byte(data);     //·¢ËÍ×Ö½Ú 
	IIC_Wait_Ack();  		    	   
  IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ 
	if(device_addr==0xA0) //
		delay_ms(10);
	else
		delay_us(2);
}
 
uint16_t IIC_ReadByte(uint16_t addr,uint8_t device_addr,uint8_t ByteNumToRead)  //¶Á¼Ä´æÆ÷»ò¶ÁÊý¾Ý
{ 	
		uint16_t data;
		IIC_Start();  
		if(device_addr==0xA0)
			IIC_Send_Byte(0xA0 + ((addr/256)<<1));
		else
			IIC_Send_Byte(device_addr);	
		IIC_Wait_Ack();
		IIC_Send_Byte(addr&0xFF);   //·¢Ë͵͵ØÖ·
		IIC_Wait_Ack(); 
 
		IIC_Start();  	
		IIC_Send_Byte(device_addr+1);	    //·¢Æ÷¼þµØÖ·
		IIC_Wait_Ack();
		if(ByteNumToRead == 1)//LM75ζÈÊý¾ÝΪ11bit
		{ 
			data=IIC_Read_Byte(0);
		}
		else
			{ 
				data=IIC_Read_Byte(1);
				data=(data<<8)+IIC_Read_Byte(0);
			}
		IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ 
		return data;
}



void  read_AHT20_once(int *T,int *H)
{ 
	delay_ms(10);

	reset_AHT20();
	delay_ms(10);

	init_AHT20();
	delay_ms(10);

	startMeasure_AHT20();
	delay_ms(80);

	read_AHT20(T,H);
	delay_ms(5);
}


void  reset_AHT20(void)
{ 

	I2C_Start();

	I2C_WriteByte(0x70);
	ack_status = Receive_ACK();
	if(ack_status) printf("1");
	else printf("1-n-");
	I2C_WriteByte(0xBA);
	ack_status = Receive_ACK();
		if(ack_status) printf("2");
	else printf("2-n-");
	I2C_Stop();

	
}



void  init_AHT20(void)
{ 
	I2C_Start();

	I2C_WriteByte(0x70);
	ack_status = Receive_ACK();
	if(ack_status) printf("3");
	else printf("3-n-");	
	I2C_WriteByte(0xE1);
	ack_status = Receive_ACK();
	if(ack_status) printf("4");
	else printf("4-n-");
	I2C_WriteByte(0x08);
	ack_status = Receive_ACK();
	if(ack_status) printf("5");
	else printf("5-n-");
	I2C_WriteByte(0x00);
	ack_status = Receive_ACK();
	if(ack_status) printf("6");
	else printf("6-n-");
	I2C_Stop();
}


void startMeasure_AHT20(void)
{ 
	//------------
	I2C_Start();

	I2C_WriteByte(0x70);
	ack_status = Receive_ACK();
	if(ack_status) printf("7");
	else printf("7-n-");
	I2C_WriteByte(0xAC);
	ack_status = Receive_ACK();
	if(ack_status) printf("8");
	else printf("8-n-");
	I2C_WriteByte(0x33);
	ack_status = Receive_ACK();
	if(ack_status) printf("9");
	else printf("9-n-");
	I2C_WriteByte(0x00);
	ack_status = Receive_ACK();
	if(ack_status) printf("10");
	else printf("10-n-");
	I2C_Stop();
}



void read_AHT20(int *T,int *H)
{ 
	uint8_t   i;

	for(i=0; i<6; i++)
	{ 
		readByte[i]=0;
	}

	//-------------
	I2C_Start();

	I2C_WriteByte(0x71);
	ack_status = Receive_ACK();
	readByte[0]= I2C_ReadByte();
	Send_ACK();

	readByte[1]= I2C_ReadByte();
	Send_ACK();

	readByte[2]= I2C_ReadByte();
	Send_ACK();

	readByte[3]= I2C_ReadByte();
	Send_ACK();

	readByte[4]= I2C_ReadByte();
	Send_ACK();

	readByte[5]= I2C_ReadByte();
	SendNot_Ack();
	//Send_ACK();

	I2C_Stop();

	//--------------
	if( (readByte[0] & 0x68) == 0x08 )
	{ 
		H1 = readByte[1];
		H1 = (H1<<8) | readByte[2];
		H1 = (H1<<8) | readByte[3];
		H1 = H1>>4;

		H1 = (H1*1000)/1024/1024;

		T1 = readByte[3];
		T1 = T1 & 0x0000000F;
		T1 = (T1<<8) | readByte[4];
		T1 = (T1<<8) | readByte[5];

		T1 = (T1*2000)/1024/1024 - 500;

		AHT20_OutData[0] = (H1>>8) & 0x000000FF;
		AHT20_OutData[1] = H1 & 0x000000FF;

		AHT20_OutData[2] = (T1>>8) & 0x000000FF;
		AHT20_OutData[3] = T1 & 0x000000FF;
	}
	else
	{ 
		AHT20_OutData[0] = 0xFF;
		AHT20_OutData[1] = 0xFF;

		AHT20_OutData[2] = 0xFF;
		AHT20_OutData[3] = 0xFF;
		printf("ʧ°ÜÁË");

	}
	printf("\r\n");
	printf("ζÈ:%d%d.%d",T1/100,(T1/10)%10,T1%10);
	printf("ʪ¶È:%d%d.%d",H1/100,(H1/10)%10,H1%10);
	printf("\r\n");
	*T=T1;
	*H=H1;
}




uint8_t  Receive_ACK(void)
{ 
	uint8_t result=0;
	uint8_t cnt=0;

	IIC_SCL = 0;
	SDA_IN(); 
	delay_us(4);

	IIC_SCL = 1;
	delay_us(4);

	while(READ_SDA && (cnt<100))
	{ 
		cnt++;
	}

	IIC_SCL = 0;
	delay_us(4);

	if(cnt<100)
	{ 
		result=1;
	}
	return result;
}



void  Send_ACK(void)
{ 
	SDA_OUT();
	IIC_SCL = 0;
	delay_us(4);

	IIC_SDA = 0;
	delay_us(4);

	IIC_SCL = 1;
	delay_us(4);
	IIC_SCL = 0;
	delay_us(4);

	SDA_IN();
}



void  SendNot_Ack(void)
{ 
	SDA_OUT();
	IIC_SCL = 0;
	delay_us(4);

	IIC_SDA = 1;
	delay_us(4);

	IIC_SCL = 1;
	delay_us(4);

	IIC_SCL = 0;
	delay_us(4);

	IIC_SDA = 0;
	delay_us(4);
}


void I2C_WriteByte(uint8_t  input)
{ 
	uint8_t  i;
	SDA_OUT();
	for(i=0; i<8; i++)
	{ 
		IIC_SCL = 0;
		delay_ms(5);

		if(input & 0x80)
		{ 
			IIC_SDA = 1;
			//delaymm(10);
		}
		else
		{ 
			IIC_SDA = 0;
			//delaymm(10);
		}

		IIC_SCL = 1;
		delay_ms(5);

		input = (input<<1);
	}

	IIC_SCL = 0;
	delay_us(4);

	SDA_IN();
	delay_us(4);
}	


uint8_t I2C_ReadByte(void)
{ 
	uint8_t  resultByte=0;
	uint8_t  i=0, a=0;

	IIC_SCL = 0;
	SDA_IN();
	delay_ms(4);

	for(i=0; i<8; i++)
	{ 
		IIC_SCL = 1;
		delay_ms(3);

		a=0;
		if(READ_SDA)
		{ 
			a=1;
		}
		else
		{ 
			a=0;
		}

		//resultByte = resultByte | a;
		resultByte = (resultByte << 1) | a;

		IIC_SCL = 0;
		delay_ms(3);
	}

	SDA_IN();
	delay_ms(10);

	return   resultByte;
}


void  set_AHT20sendOutData(void)
{ 
	
	AHT20sendOutData[3] = AHT20_OutData[0];
	AHT20sendOutData[4] = AHT20_OutData[1];
	AHT20sendOutData[5] = AHT20_OutData[2];
	AHT20sendOutData[6] = AHT20_OutData[3];

// AHT20sendOutData[7] = (drf1609.shortAddress >> 8) & 0x00FF;
// AHT20sendOutData[8] = drf1609.shortAddress & 0x00FF;

// AHT20sendOutData[9] = getXY(AHT20sendOutData,10);
}


void  I2C_Start(void)
{ 
	SDA_OUT();
	IIC_SCL = 1;
	delay_ms(4);

	IIC_SDA = 1;
	delay_ms(4);
	IIC_SDA = 0;
	delay_ms(4);

	IIC_SCL = 0;
	delay_ms(4);
}



void  I2C_Stop(void)
{ 
	SDA_OUT();
	IIC_SDA = 0;
	delay_ms(4);

	IIC_SCL = 1;
	delay_ms(4);

	IIC_SDA = 1;
	delay_ms(4);
}

bsp_i2c.h

#ifndef __BSP_I2C_H
#define __BSP_I2C_H

#include "sys.h"
#include "delay.h"
#include "usart.h"
//ʹÓÃIIC1 ¹ÒÔØM24C02,OLED,LM75AD,HT1382 PB6,PB7
 
#define SDA_IN() {GPIOB->CRL&=0x0FFFFFFF;GPIOB->CRL|=(u32)8<<28;}
#define SDA_OUT() {GPIOB->CRL&=0x0FFFFFFF;GPIOB->CRL|=(u32)3<<28;}
 
//IO²Ù×÷º¯Êý 
#define IIC_SCL PBout(6) //SCL
#define IIC_SDA PBout(7) //SDA 
#define READ_SDA PBin(7) //ÊäÈëSDA 


//IICËùÓвÙ×÷º¯Êý
void IIC_Init(void);                //³õʼ»¯IICµÄIO¿Ú 
void IIC_Start(void);				//·¢ËÍIIC¿ªÊ¼ÐźÅ
void IIC_Stop(void);	  			//·¢ËÍIICÍ£Ö¹ÐźÅ
void IIC_Send_Byte(u8 txd);			//IIC·¢ËÍÒ»¸ö×Ö½Ú
u8 IIC_Read_Byte(unsigned char ack);//IIC¶ÁÈ¡Ò»¸ö×Ö½Ú
u8 IIC_Wait_Ack(void); 				//IICµÈ´ýACKÐźÅ
void IIC_Ack(void);					//IIC·¢ËÍACKÐźÅ
void IIC_NAck(void);				//IIC²»·¢ËÍACKÐźÅ
 
void IIC_WriteByte(uint16_t addr,uint8_t data,uint8_t device_addr);
uint16_t IIC_ReadByte(uint16_t addr,uint8_t device_addr,uint8_t ByteNumToRead);//¼Ä´æÆ÷µØÖ·£¬Æ÷¼þµØÖ·£¬Òª¶ÁµÄ×Ö½ÚÊý 


void  read_AHT20_once(int *T,int *H);
void  reset_AHT20(void);
void  init_AHT20(void);	
void  startMeasure_AHT20(void);
void  read_AHT20(int *T,int *H);
uint8_t  Receive_ACK(void);
void  Send_ACK(void);
void  SendNot_Ack(void);
void I2C_WriteByte(uint8_t  input);
uint8_t I2C_ReadByte(void);	
void  set_AHT20sendOutData(void);
void  I2C_Start(void);
void  I2C_Stop(void);
#endif

usart.c

#include "sys.h"
#include "usart.h"


//STM32F103ºËÐÄ°åÀý³Ì
//¿âº¯Êý°æ±¾Àý³Ì



// 
//Èç¹ûʹÓÃucos,Ôò°üÀ¨ÏÂÃæµÄÍ·Îļþ¼´¿É.
#if SYSTEM_SUPPORT_UCOS
#include "includes.h" //ucos ʹÓà 
#endif
// 
//STM32¿ª·¢°å
//´®¿Ú1³õʼ»¯ 

// 
 

//
//¼ÓÈëÒÔÏ´úÂë,Ö§³Öprintfº¯Êý,¶ø²»ÐèҪѡÔñuse MicroLIB 
#if 1
#pragma import(__use_no_semihosting) 
//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý 
struct __FILE 
{  
	int handle; 

}; 

FILE __stdout;       
//¶¨Òå_sys_exit()ÒÔ±ÜÃâʹÓðëÖ÷»úģʽ 
void _sys_exit(int x) 
{  
	x = x; 
} 
//Öض¨Òåfputcº¯Êý 
int fputc(int ch, FILE *f)
{       
	while((USART1->SR&0x40)==0);//Ñ­»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï 
    USART1->DR = (u8) ch;      
	return ch;
}
#endif 


 
 
#if EN_USART1_RX //Èç¹ûʹÄÜÁ˽ÓÊÕ
//´®¿Ú1ÖжϷþÎñ³ÌÐò
//×¢Òâ,¶ÁÈ¡USARTx->SRÄܱÜÃâĪÃûÆäÃîµÄ´íÎó 
u8 USART_RX_BUF[USART_REC_LEN];     //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.
//½ÓÊÕ״̬
//bit15£¬ ½ÓÊÕÍê³É±êÖ¾
//bit14£¬ ½ÓÊÕµ½0x0d
//bit13~0£¬ ½ÓÊÕµ½µÄÓÐЧ×Ö½ÚÊýÄ¿
u16 USART_RX_STA=0;       //½ÓÊÕ״̬±ê¼Ç 
  
void uart_init(u32 bound){ 
    //GPIO¶Ë¿ÚÉèÖÃ
  GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	//ʹÄÜUSART1£¬GPIOAʱÖÓ
     //USART1_TX PA.9
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//¸´ÓÃÍÆÍìÊä³ö
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   
    //USART1_RX PA.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
    GPIO_Init(GPIOA, &GPIO_InitStructure);  

   //Usart1 NVIC ÅäÖÃ

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇÀÕ¼ÓÅÏȼ¶3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//×ÓÓÅÏȼ¶3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQͨµÀʹÄÜ
	NVIC_Init(&NVIC_InitStructure);	//¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
  
   //USART ³õʼ»¯ÉèÖÃ

	USART_InitStructure.USART_BaudRate = bound;//Ò»°ãÉèÖÃΪ9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
	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_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆôÖжÏ
    USART_Cmd(USART1, ENABLE);                    //ʹÄÜ´®¿Ú 

}



void USART1_IRQHandler(void)                	//´®¿Ú1ÖжϷþÎñ³ÌÐò
	{ 
	u8 Res;
#ifdef OS_TICKS_PER_SEC //Èç¹ûʱÖÓ½ÚÅÄÊý¶¨ÒåÁË,˵Ã÷ҪʹÓÃucosIIÁË.
	OSIntEnter();    
#endif
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ)
		{ 
		Res =USART_ReceiveData(USART1);//(USART1->DR); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
		
		if((USART_RX_STA&0x8000)==0)//½ÓÊÕδÍê³É
			{ 
			if(USART_RX_STA&0x4000)//½ÓÊÕµ½ÁË0x0d
				{ 
				if(Res!=0x0a)USART_RX_STA=0;//½ÓÊÕ´íÎó,ÖØпªÊ¼
				else USART_RX_STA|=0x8000;	//½ÓÊÕÍê³ÉÁË 
				}
			else //»¹Ã»ÊÕµ½0x0D
				{ 	
				if(Res==0x0d)USART_RX_STA|=0x4000;
				else
					{ 
					USART_RX_BUF[USART_RX_STA&0x3FFF]=Res ;
					USART_RX_STA++;
					if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½ÓÊÕÊý¾Ý´íÎó,ÖØпªÊ¼½ÓÊÕ 
					}		 
				}
			}   		 
     } 
#ifdef OS_TICKS_PER_SEC //Èç¹ûʱÖÓ½ÚÅÄÊý¶¨ÒåÁË,˵Ã÷ҪʹÓÃucosIIÁË.
	OSIntExit();  											 
#endif
} 
#endif 

usart.h

#ifndef __USART_H
#define __USART_H
#include "stdio.h" 
#include "sys.h" 

//STM32F103ºËÐÄ°åÀý³Ì
//¿âº¯Êý°æ±¾Àý³Ì


// 
//STM32¿ª·¢°å
//´®¿Ú1³õʼ»¯ 

#define USART_REC_LEN 200 //¶¨Òå×î´ó½ÓÊÕ×Ö½ÚÊý 200
#define EN_USART1_RX 1 //ʹÄÜ£¨1£©/½ûÖ¹£¨0£©´®¿Ú1½ÓÊÕ
	  	
extern u8  USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû 
extern u16 USART_RX_STA;         		//½ÓÊÕ״̬±ê¼Ç 
//Èç¹ûÏë´®¿ÚÖжϽÓÊÕ£¬Ç벻ҪעÊÍÒÔϺ궨Òå
void uart_init(u32 bound);
#endif

2、在HARDWARE文件夹中添加stm32f10x_usart.c,该文件位于STM32F10x_FWLib\src中。
3.按照上面所述的方法添加“温度”和“湿度”的字模,修改main.c如下

#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "gui.h"
#include "test.h"
#include "usart.h"
#include "bsp_i2c.h"
int main(void)
{ 	
	int *H,*T;
	int H1=0,T1=0;
	int a,b,c,d;
	H=&H1;
	T=&T1;
	delay_init();	    	       
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);		
	OLED_Init();			         
	OLED_Clear(0);           
	uart_init(115200);	
	IIC_Init();
	while(1) 
	{ 	
		read_AHT20_once(T,H);
		GUI_ShowCHinese(16,10,16,"温度",1);
		a=*T/10;
		b=*T%10;
		GUI_ShowNum(60,10,a,2,16,1);
    GUI_ShowString(80,10,".",16,1);
		GUI_ShowNum(82,10,b,2,16,1);
		GUI_ShowCHinese(16,30,16,"湿度",1);
		c=*H/10;
		d=*H%10;
		GUI_ShowNum(60,30,c,2,16,1);
    GUI_ShowString(80,30,".",16,1);
		GUI_ShowNum(82,30,d,2,16,1);
	}
}

4.编译并烧录程序到stm32中,可见OLED显示了温湿度。
对芯片吹口气数据显示发生变化:

3.上下的滑动显示长字符

1.按上述方式添加“信息科学与工程学院”的字模,并修改main.c如下。
代码如下:

#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "gui.h"
#include "test.h"
int main(void)
{ 	
	delay_init();	    	       //?óê±oˉêy3?ê??ˉ 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	 //éè??NVIC?D??·?×é2:2???à??ó??è??£?2???ìó|ó??è?? 
	OLED_Init();			         //3?ê??ˉOLED 
	OLED_Clear(0);             //???ᣨè?oú£?
	while(1) 
	{ 	
		GUI_ShowCHinese(0,25,16,"信息科学与工程学院",1);
			delay_ms(50);
		  roll();
	}
}

2.在oled.c中添加如下滚动代码。

void roll(void)
{ 
        OLED_WR_Byte(0x2e,OLED_CMD);       
        OLED_WR_Byte(0x29,OLED_CMD);       
        OLED_WR_Byte(0x00,OLED_CMD);       
        OLED_WR_Byte(0x00,OLED_CMD);        
        OLED_WR_Byte(0x07,OLED_CMD);        
        OLED_WR_Byte(0x07,OLED_CMD);        
        OLED_WR_Byte(0x01,OLED_CMD);       
        OLED_WR_Byte(0x2F,OLED_CMD); 
}       

3.编译并烧录程序到stm32中,可看见字段向上滚动。

总结

这次的实验让我学会了用stm32的SPI接口实现对OLED的显示控制。加强了用stm32温度模块的练习。

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服