- 学习目标
- 学习内容
- ifndef __OLED_H
- define __OLED_H
- include “stm32f4xx.h”
- include “systick.h”
- ifndef u8
- define u8 uint8_t
- endif
- ifndef u16
- define u16 uint16_t
- endif
- ifndef u32
- define u32 uint32_t
- endif
- define CLK_RCU RCC_AHB1Periph_GPIOB
- define CLK_PORT GPIOB
- define CLK_PIN GPIO_Pin_3
- define MOSI_RCU RCC_AHB1Periph_GPIOB
- define MOSI_PORT GPIOB
- define MOSI_PIN GPIO_Pin_5
- define DC_RCU RCC_AHB1Periph_GPIOB
- define DC_PORT GPIOB
- define DC_PIN GPIO_Pin_8
- define CS1_RCU RCC_AHB1Periph_GPIOA
- define CS1_PORT GPIOA
- define CS1_PIN GPIO_Pin_15
- define MISO_RCU RCC_AHB1Periph_GPIOB
- define MISO_PORT GPIOB
- define MISO_PIN GPIO_Pin_4
- define CS2_RCU RCC_AHB1Periph_GPIOA
- define CS2_PORT GPIOA
- define CS2_PIN GPIO_Pin_4
- define OLED_SCL_Clr() GPIO_ResetBits(CLK_PORT, CLK_PIN)//SCL
- define OLED_SCL_Set() GPIO_SetBits(CLK_PORT, CLK_PIN)
- define OLED_SDA_Clr() GPIO_ResetBits(MOSI_PORT,MOSI_PIN)//SDA
- define OLED_SDA_Set() GPIO_SetBits(MOSI_PORT,MOSI_PIN)
- define OLED_DC_Clr() GPIO_ResetBits(DC_PORT,DC_PIN)//DC
- define OLED_DC_Set() GPIO_SetBits(DC_PORT,DC_PIN)
- define OLED_CS_Clr() GPIO_ResetBits(CS1_PORT,CS1_PIN)//CS1
- define OLED_CS_Set() GPIO_SetBits(CS1_PORT,CS1_PIN)
- define OLED_READ_FS0() GPIO_ReadInputDataBit(MISO_PORT,MISO_PIN)//FS0
- define OLED_ROM_CS_Clr() GPIO_ResetBits(CS2_PORT,CS2_PIN)//CS2
- define OLED_ROM_CS_Set() GPIO_SetBits(CS2_PORT,CS2_PIN)
- define OLED_CMD 0 //写命令
- define OLED_DATA 1 //写数据
- endif
- ifndef __OLED_H
- define __OLED_H
- include “gd32f4xx.h”
- include “systick.h”
- include
- ifndef u8
- define u8 uint8_t
- endif
- ifndef u16
- define u16 uint16_t
- endif
- ifndef u32
- define u32 uint32_t
- endif
- define CLK_RCU RCU_GPIOB
- define CLK_PORT GPIOB
- define CLK_PIN GPIO_PIN_3
- define MOSI_RCU RCU_GPIOB
- define MOSI_PORT GPIOB
- define MOSI_PIN GPIO_PIN_5
- define DC_RCU RCU_GPIOB
- define DC_PORT GPIOB
- define DC_PIN GPIO_PIN_8
- define CS1_RCU RCU_GPIOA
- define CS1_PORT GPIOA
- define CS1_PIN GPIO_PIN_15
- define MISO_RCU RCU_GPIOB
- define MISO_PORT GPIOB
- define MISO_PIN GPIO_PIN_4
- define CS2_RCU RCU_GPIOA
- define CS2_PORT GPIOA
- define CS2_PIN GPIO_PIN_4
- define OLED_SCL_Clr() gpio_bit_write(CLK_PORT, CLK_PIN, 0)
- define OLED_SCL_Set() gpio_bit_write(CLK_PORT, CLK_PIN, 1)
- define OLED_SDA_Clr() gpio_bit_write(MOSI_PORT, MOSI_PIN, 0)
- define OLED_SDA_Set() gpio_bit_write(MOSI_PORT, MOSI_PIN, 1)
- define OLED_DC_Clr() gpio_bit_write(DC_PORT, DC_PIN, 0)
- define OLED_DC_Set() gpio_bit_write(DC_PORT, DC_PIN, 1)
- define OLED_CS_Clr() gpio_bit_write(CS1_PORT, CS1_PIN, 0)
- define OLED_CS_Set() gpio_bit_write(CS1_PORT, CS1_PIN, 1)
- define OLED_READ_FS0() gpio_input_bit_get(MISO_PORT, MISO_PIN)
- define OLED_ROM_CS_Clr() gpio_bit_write(CS2_PORT, CS2_PIN, 0)
- define OLED_ROM_CS_Set() gpio_bit_write(CS2_PORT, CS2_PIN, 1)
- define OLED_CMD 0 //写命令
- define OLED_DATA 1 //写数据
- endif
- 练习题
学习目标
- 掌握移植方法
- 掌握调试方式
学习内容
需求
官方测试示例
选择对应的平台
测试示例中,找到芯片对应平台,我们选择的是STM32F407
修改例程
已知错误修改:
#ifndef u8#define u8 uint8_t#endif#ifndef u16#define u16 uint16_t#endif#ifndef u32#define u32 uint32_t#endif
引脚定义修改:
#define CLK_RCU RCC_AHB1Periph_GPIOB#define CLK_PORT GPIOB#define CLK_PIN GPIO_Pin_3#define MOSI_RCU RCC_AHB1Periph_GPIOB#define MOSI_PORT GPIOB#define MOSI_PIN GPIO_Pin_5#define DC_RCU RCC_AHB1Periph_GPIOB#define DC_PORT GPIOB#define DC_PIN GPIO_Pin_8#define CS1_RCU RCC_AHB1Periph_GPIOA#define CS1_PORT GPIOA#define CS1_PIN GPIO_Pin_15#define MISO_RCU RCC_AHB1Periph_GPIOB#define MISO_PORT GPIOB#define MISO_PIN GPIO_Pin_4#define CS2_RCU RCC_AHB1Periph_GPIOA#define CS2_PORT GPIOA#define CS2_PIN GPIO_Pin_4#define OLED_SCL_Clr() GPIO_ResetBits(CLK_PORT, CLK_PIN)//SCL#define OLED_SCL_Set() GPIO_SetBits(CLK_PORT, CLK_PIN)#define OLED_SDA_Clr() GPIO_ResetBits(MOSI_PORT,MOSI_PIN)//SDA#define OLED_SDA_Set() GPIO_SetBits(MOSI_PORT,MOSI_PIN)#define OLED_DC_Clr() GPIO_ResetBits(DC_PORT,DC_PIN)//DC#define OLED_DC_Set() GPIO_SetBits(DC_PORT,DC_PIN)#define OLED_CS_Clr() GPIO_ResetBits(CS1_PORT,CS1_PIN)//CS1#define OLED_CS_Set() GPIO_SetBits(CS1_PORT,CS1_PIN)#define OLED_READ_FS0() GPIO_ReadInputDataBit(MISO_PORT,MISO_PIN)//FS0#define OLED_ROM_CS_Clr() GPIO_ResetBits(CS2_PORT,CS2_PIN)//CS2#define OLED_ROM_CS_Set() GPIO_SetBits(CS2_PORT,CS2_PIN)
初始化逻辑修改:
GPIO_InitTypeDef GPIO_InitStructure;RCC_AHB1PeriphClockCmd(CLK_RCU,ENABLE);//使能A端口时钟GPIO_InitStructure.GPIO_Pin = CLK_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(CLK_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(CLK_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = MOSI_PIN;GPIO_Init(MOSI_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(DC_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = DC_PIN;GPIO_Init(DC_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(CS1_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = CS1_PIN;GPIO_Init(CS1_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(CS2_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = CS2_PIN;GPIO_Init(CS2_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(MISO_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = MISO_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//输入模式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(MISO_PORT, &GPIO_InitStructure); //初始化GPIOA
烧录测试
无法通过
STM32自己环境
官方示例无法通过,切换自己的环境,进行测试。
include “stm32f4xx.h”
include “systick.h”
//SCL=SCLK //SDA=MOSI //DC=DC //CS=CS1 //FS0=MOSI //CS2=CS2
ifndef u8
define u8 uint8_t
endif
ifndef u16
define u16 uint16_t
endif
ifndef u32
define u32 uint32_t
endif
//————————-OLED端口定义————————
define CLK_RCU RCC_AHB1Periph_GPIOB
define CLK_PORT GPIOB
define CLK_PIN GPIO_Pin_3
define MOSI_RCU RCC_AHB1Periph_GPIOB
define MOSI_PORT GPIOB
define MOSI_PIN GPIO_Pin_5
define DC_RCU RCC_AHB1Periph_GPIOB
define DC_PORT GPIOB
define DC_PIN GPIO_Pin_8
define CS1_RCU RCC_AHB1Periph_GPIOA
define CS1_PORT GPIOA
define CS1_PIN GPIO_Pin_15
define MISO_RCU RCC_AHB1Periph_GPIOB
define MISO_PORT GPIOB
define MISO_PIN GPIO_Pin_4
define CS2_RCU RCC_AHB1Periph_GPIOA
define CS2_PORT GPIOA
define CS2_PIN GPIO_Pin_4
define OLED_SCL_Clr() GPIO_ResetBits(CLK_PORT, CLK_PIN)//SCL
define OLED_SCL_Set() GPIO_SetBits(CLK_PORT, CLK_PIN)
define OLED_SDA_Clr() GPIO_ResetBits(MOSI_PORT,MOSI_PIN)//SDA
define OLED_SDA_Set() GPIO_SetBits(MOSI_PORT,MOSI_PIN)
define OLED_DC_Clr() GPIO_ResetBits(DC_PORT,DC_PIN)//DC
define OLED_DC_Set() GPIO_SetBits(DC_PORT,DC_PIN)
define OLED_CS_Clr() GPIO_ResetBits(CS1_PORT,CS1_PIN)//CS1
define OLED_CS_Set() GPIO_SetBits(CS1_PORT,CS1_PIN)
define OLED_READ_FS0() GPIO_ReadInputDataBit(MISO_PORT,MISO_PIN)//FS0
define OLED_ROM_CS_Clr() GPIO_ResetBits(CS2_PORT,CS2_PIN)//CS2
define OLED_ROM_CS_Set() GPIO_SetBits(CS2_PORT,CS2_PIN)
define OLED_CMD 0 //写命令
define OLED_DATA 1 //写数据
void OLED_ColorTurn(u8 i); void OLED_DisplayTurn(u8 i); void OLED_WR_Byte(u8 dat,u8 cmd); void OLED_Clear(void); void OLED_address(u8 x,u8 y); void OLED_Display_128x64(u8 dp); void OLED_Display_16x16(u8 x,u8 y,u8 dp); void OLED_Display_8x16(u8 x,u8 y,u8 dp); void OLED_Display_5x7(u8 x,u8 y,u8 dp); void Send_Command_to_ROM(u8 dat); u8 Get_data_from_ROM(void); void OLED_get_data_from_ROM(u8 addrHigh,u8 addrMid,u8 addrLow,u8 pbuff,u8 DataLen); void OLED_Display_GB2312_string(u8 x,u8 y,u8 text); void OLED_Display_string_5x7(u8 x,u8 y,u8 *text); void OLED_ShowNum(u8 x,u8 y,float num,u8 len); void OLED_Init(void);
endif
```c#include "oled.h"#include "stdlib.h"//反显函数void OLED_ColorTurn(u8 i){if(i==0){OLED_WR_Byte(0xA6,OLED_CMD);//正常显示}if(i==1){OLED_WR_Byte(0xA7,OLED_CMD);//反色显示}}//屏幕旋转180度void OLED_DisplayTurn(u8 i){if(i==0){OLED_WR_Byte(0xC8,OLED_CMD);//正常显示OLED_WR_Byte(0xA1,OLED_CMD);}if(i==1){OLED_WR_Byte(0xC0,OLED_CMD);//反转显示OLED_WR_Byte(0xA0,OLED_CMD);}}//开启OLED显示void OLED_DisPlay_On(void){OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能OLED_WR_Byte(0x14,OLED_CMD);//开启电荷泵OLED_WR_Byte(0xAF,OLED_CMD);//点亮屏幕}//关闭OLED显示void OLED_DisPlay_Off(void){OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能OLED_WR_Byte(0x10,OLED_CMD);//关闭电荷泵OLED_WR_Byte(0xAE,OLED_CMD);//关闭屏幕}//向SSD1306写入一个字节。//mode:数据/命令标志 0,表示命令;1,表示数据;void OLED_WR_Byte(u8 dat,u8 cmd){u8 i;if(cmd)OLED_DC_Set();elseOLED_DC_Clr();OLED_CS_Clr();for(i=0;i<8;i++){OLED_SCL_Clr();if(dat&0x80)OLED_SDA_Set();elseOLED_SDA_Clr();OLED_SCL_Set();dat<<=1;}OLED_CS_Set();OLED_DC_Set();}//清屏函数void OLED_Clear(void){u8 i,n;for(i=0;i<8;i++){OLED_WR_Byte(0xb0+i,OLED_CMD);//设置页地址OLED_WR_Byte(0x10,OLED_CMD); //设置列地址的高4位OLED_WR_Byte(0x00,OLED_CMD); //设置列地址的低4位for(n=0;n<128;n++){OLED_WR_Byte(0x00,OLED_DATA);//清除所有数据}}}//设置起始地址void OLED_address(u8 x,u8 y){OLED_WR_Byte(0xb0+y,OLED_CMD); //设置页地址OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); //设置列地址的高4位OLED_WR_Byte((x&0x0f),OLED_CMD); //设置列地址的低4位}//显示128x64点阵图像void OLED_Display_128x64(u8 *dp){u8 i,j;for(i=0;i<8;i++){OLED_address(0,i);for(j=0;j<128;j++){OLED_WR_Byte(*dp,OLED_DATA); //写数据到OLED,每写完一个8位的数据后列地址自动加1dp++;}}}//显示16x16点阵图像、汉字、生僻字或16x16点阵的其他图标void OLED_Display_16x16(u8 x,u8 y,u8 *dp){u8 i,j;for(j=0;j<2;j++){OLED_address(x,y);for(i=0;i<16;i++){OLED_WR_Byte(*dp,OLED_DATA); //写数据到OLED,每写完一个8位的数据后列地址自动加1dp++;}y++;}}//显示8x16点阵图像、ASCII, 或8x16点阵的自造字符、其他图标void OLED_Display_8x16(u8 x,u8 y,u8 *dp){u8 i,j;for(j=0;j<2;j++){OLED_address(x,y);for(i=0;i<8;i++){OLED_WR_Byte(*dp,OLED_DATA); //写数据到LCD,每写完一个8位的数据后列地址自动加1dp++;}y++;}}//显示5*7点阵图像、ASCII, 或5x7点阵的自造字符、其他图标void OLED_Display_5x7(u8 x,u8 y,u8 *dp){u8 i;OLED_address(x,y);for(i=0;i<6;i++){OLED_WR_Byte(*dp,OLED_DATA);dp++;}}//送指令到晶联讯字库ICvoid Send_Command_to_ROM(u8 dat){u8 i;for(i=0;i<8;i++){OLED_SCL_Clr();if(dat&0x80){OLED_SDA_Set();}else{OLED_SDA_Clr();}dat<<=1;OLED_SCL_Set();}}//从晶联讯字库IC中取汉字或字符数据(1个字节)u8 Get_data_from_ROM(void){u8 i,read=0;for(i=0;i<8;i++){OLED_SCL_Clr();read<<=1;if(OLED_READ_FS0()){read++;}OLED_SCL_Set();}return read;}//从相关地址(addrHigh:地址高字节,addrMid:地址中字节,addrLow:地址低字节)中连续读出DataLen个字节的数据到 pbuff的地址//连续读取void OLED_get_data_from_ROM(u8 addrHigh,u8 addrMid,u8 addrLow,u8 *pbuff,u8 DataLen){u8 i;OLED_ROM_CS_Clr();Send_Command_to_ROM(0x03);Send_Command_to_ROM(addrHigh);Send_Command_to_ROM(addrMid);Send_Command_to_ROM(addrLow);for(i=0;i<DataLen;i++){*(pbuff+i)=Get_data_from_ROM();}OLED_ROM_CS_Set();}u32 fontaddr=0;void OLED_Display_GB2312_string(u8 x,u8 y,u8 *text){u8 i=0;u8 addrHigh,addrMid,addrLow;u8 fontbuf[32];while(text[i]>0x00){if((text[i]>=0xb0)&&(text[i]<=0xf7)&&(text[i+1]>=0xa1)){//国标简体(GB2312)汉字在晶联讯字库IC中的地址由以下公式来计算://Address = ((MSB - 0xB0) * 94 + (LSB - 0xA1)+ 846)*32+ BaseAdd;BaseAdd=0//由于担心8位单片机有乘法溢出问题,所以分三部取地址fontaddr=(text[i]-0xb0)*94;fontaddr+=(text[i+1]-0xa1)+846;fontaddr=fontaddr*32;addrHigh=(fontaddr&0xff0000)>>16; //地址的高8位,共24位addrMid=(fontaddr&0xff00)>>8; //地址的中8位,共24位addrLow=(fontaddr&0xff); //地址的低8位,共24位OLED_get_data_from_ROM(addrHigh,addrMid,addrLow,fontbuf,32);//取32个字节的数据,存到"fontbuf[32]"OLED_Display_16x16(x,y,fontbuf);//显示汉字到LCD上,y为页地址,x为列地址,fontbuf[]为数据x+=16;i+=2;}else if((text[i]>=0xa1)&&(text[i]<=0xa3)&&(text[i+1]>=0xa1)){fontaddr=(text[i]-0xa1)*94;fontaddr+=(text[i+1]-0xa1);fontaddr=fontaddr*32;addrHigh=(fontaddr&0xff0000)>>16;addrMid=(fontaddr&0xff00)>>8;addrLow=(fontaddr&0xff);OLED_get_data_from_ROM(addrHigh,addrMid,addrLow,fontbuf,32);OLED_Display_16x16(x,y,fontbuf);x+=16;i+=2;}else if((text[i]>=0x20)&&(text[i]<=0x7e)){unsigned char fontbuf[16];fontaddr=(text[i]-0x20);fontaddr=(unsigned long)(fontaddr*16);fontaddr=(unsigned long)(fontaddr+0x3cf80);addrHigh=(fontaddr&0xff0000)>>16;addrMid=(fontaddr&0xff00)>>8;addrLow=fontaddr&0xff;OLED_get_data_from_ROM(addrHigh,addrMid,addrLow,fontbuf,16);OLED_Display_8x16(x,y,fontbuf);x+=8;i+=1;}elsei++;}}void OLED_Display_string_5x7(u8 x,u8 y,u8 *text){u8 i=0;u8 addrHigh,addrMid,addrLow;while(text[i]>0x00){if((text[i]>=0x20)&&(text[i]<=0x7e)){u8 fontbuf[8];fontaddr=(text[i]-0x20);fontaddr=(unsigned long)(fontaddr*8);fontaddr=(unsigned long)(fontaddr+0x3bfc0);addrHigh=(fontaddr&0xff0000)>>16;addrMid=(fontaddr&0xff00)>>8;addrLow=fontaddr&0xff;OLED_get_data_from_ROM(addrHigh,addrMid,addrLow,fontbuf,8);OLED_Display_5x7(x,y,fontbuf);x+=6;i+=1;}elsei++;}}//显示2个数字//x,y :起点坐标//num1:要显示的小数//len :数字的位数void OLED_ShowNum(u8 x,u8 y,float num1,u8 len){u8 i;u32 t,num;x=x+len*8+8;//要显示的小数最低位的横坐标num=num1*100;//将小数左移两位并转化为整数OLED_Display_GB2312_string(x-24,y,".");//显示小数点for(i=0;i<len;i++){t=num%10;//取个位数的数值num=num/10;//将整数右移一位x-=8;if(i==2){x-=8;}//当显示出来两个小数之后,空出小数点的位置switch(t){case 0 :OLED_Display_GB2312_string(x,y,"0");break;case 1 :OLED_Display_GB2312_string(x,y,"1");break;case 2 :OLED_Display_GB2312_string(x,y,"2");break;case 3 :OLED_Display_GB2312_string(x,y,"3");break;case 4 :OLED_Display_GB2312_string(x,y,"4");break;case 5 :OLED_Display_GB2312_string(x,y,"5");break;case 6 :OLED_Display_GB2312_string(x,y,"6");break;case 7 :OLED_Display_GB2312_string(x,y,"7");break;case 8 :OLED_Display_GB2312_string(x,y,"8");break;case 9 :OLED_Display_GB2312_string(x,y,"9");break;}}}//OLED的初始化void OLED_Init(void){GPIO_InitTypeDef GPIO_InitStructure;RCC_AHB1PeriphClockCmd(CLK_RCU,ENABLE);//使能A端口时钟GPIO_InitStructure.GPIO_Pin = CLK_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(CLK_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(CLK_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = MOSI_PIN;GPIO_Init(MOSI_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(DC_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = DC_PIN;GPIO_Init(DC_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(CS1_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = CS1_PIN;GPIO_Init(CS1_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(CS2_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = CS2_PIN;GPIO_Init(CS2_PORT, &GPIO_InitStructure);RCC_AHB1PeriphClockCmd(MISO_RCU,ENABLE);GPIO_InitStructure.GPIO_Pin = MISO_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//输入模式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(MISO_PORT, &GPIO_InitStructure); //初始化GPIOAGPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);OLED_SCL_Set();OLED_SDA_Set();OLED_DC_Set();OLED_CS_Set();OLED_ROM_CS_Set();delay_1ms(200);OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panelOLED_WR_Byte(0x00,OLED_CMD);//---set low column addressOLED_WR_Byte(0x10,OLED_CMD);//---set high column addressOLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control registerOLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current BrightnessOLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常OLED_WR_Byte(0xA6,OLED_CMD);//--set normal displayOLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 dutyOLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)OLED_WR_Byte(0x00,OLED_CMD);//-not offsetOLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequencyOLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/SecOLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge periodOLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 ClockOLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configurationOLED_WR_Byte(0x12,OLED_CMD);OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomhOLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect LevelOLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)OLED_WR_Byte(0x02,OLED_CMD);//OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disableOLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disableOLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)OLED_Clear();OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/}
#include "main.h"#include "systick.h"#include <stdio.h>#include "Usart0.h"#include "oled.h"#include "bmp.h"void Usart0_recv(uint8_t* data, uint32_t len) {printf("recv: %s\r\n", data);}int main(void){// 系统时钟初始化systick_config();Usart0_init();u8 t=0;OLED_Init(); //初始化OLEDt=' ';OLED_ColorTurn(0);//0正常显示,1 反色显示OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示while(1){//// t+=0.01;//// OLED_ShowNum(0,0,t,5);//显示小数OLED_Display_128x64(bmp1);delay_1ms(500);OLED_Display_GB2312_string(0,0,"12864,带中文字库"); /*在第1页,第1列,显示一串16x16点阵汉字或8x16的ASCII字*/OLED_Display_GB2312_string(0,2,"16X16简体汉字库,"); /*显示一串16x16点阵汉字或8x16的ASCII字.以下雷同*/OLED_Display_GB2312_string(0,4,"或8X16点阵ASCII,");OLED_Display_GB2312_string(0,6,"或5X7点阵ASCII码");delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(24,0,"中景园电子");OLED_Display_GB2312_string(0,2,"主要生产OLED模块");OLED_Display_GB2312_string(0,4,"顾客至上真诚服务");OLED_Display_GB2312_string(0,6,"诚信与质量第一!");delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(0,0,"GB2312简体字库及");OLED_Display_GB2312_string(0,2,"有图型功能,可自");OLED_Display_GB2312_string(0,4,"编大字或图像或生");OLED_Display_GB2312_string(0,6,"僻字,例如:");OLED_Display_16x16(97,6,jiong1); /*在第7页,第81列显示单个自编生僻汉字“囧”*/OLED_Display_16x16(113,6,lei1);delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(0,0,"<!@#$%^&*()_-+]/"); /*在第1页,第1列,显示一串16x16点阵汉字或8*16的ASCII字*/OLED_Display_string_5x7(0,2,"<!@#$%^&*()_-+]/;.,?[");/*在第3页,第1列,显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,3,"XY electronics Co., ");/*显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,4,"Ltd. established at ");/*显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,5,"year 2010.Focus OLED ");/*显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,6,"Mobile:13265585975");/*显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,7,"Tel:0755-32910715");/*显示一串5x7点阵的ASCII字*/delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(0,0,"啊阿埃挨哎唉哀皑"); /*在第1页,第1列,显示一串16x16点阵汉字或8x16的ASCII字*/OLED_Display_GB2312_string(0,2,"癌蔼矮艾碍爱隘鞍"); /*显示一串16x16点阵汉字或8x16的ASCII字.以下雷同*/OLED_Display_GB2312_string(0,4,"氨安俺按暗岸胺案");OLED_Display_GB2312_string(0,6,"肮昂盎凹敖熬翱袄");delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(0,0,"鬟鬣麽麾縻麂麇麈");OLED_Display_GB2312_string(0,2,"麋麒鏖麝麟黛黜黝");OLED_Display_GB2312_string(0,4,"黠黟黢黩黧黥黪黯");OLED_Display_GB2312_string(0,6,"齄鼬鼹鼷鼽鼾鼢鼯");delay_1ms(500);OLED_Clear();}}
GD32环境
移植操作
只需要将API进行切换即可
include “gd32f4xx.h”
include “systick.h”
include
ifndef u8
define u8 uint8_t
endif
ifndef u16
define u16 uint16_t
endif
ifndef u32
define u32 uint32_t
endif
//————————-OLED端口定义————————
define CLK_RCU RCU_GPIOB
define CLK_PORT GPIOB
define CLK_PIN GPIO_PIN_3
define MOSI_RCU RCU_GPIOB
define MOSI_PORT GPIOB
define MOSI_PIN GPIO_PIN_5
define DC_RCU RCU_GPIOB
define DC_PORT GPIOB
define DC_PIN GPIO_PIN_8
define CS1_RCU RCU_GPIOA
define CS1_PORT GPIOA
define CS1_PIN GPIO_PIN_15
define MISO_RCU RCU_GPIOB
define MISO_PORT GPIOB
define MISO_PIN GPIO_PIN_4
define CS2_RCU RCU_GPIOA
define CS2_PORT GPIOA
define CS2_PIN GPIO_PIN_4
// clk
define OLED_SCL_Clr() gpio_bit_write(CLK_PORT, CLK_PIN, 0)
define OLED_SCL_Set() gpio_bit_write(CLK_PORT, CLK_PIN, 1)
// mosi
define OLED_SDA_Clr() gpio_bit_write(MOSI_PORT, MOSI_PIN, 0)
define OLED_SDA_Set() gpio_bit_write(MOSI_PORT, MOSI_PIN, 1)
// dc
define OLED_DC_Clr() gpio_bit_write(DC_PORT, DC_PIN, 0)
define OLED_DC_Set() gpio_bit_write(DC_PORT, DC_PIN, 1)
// CS1
define OLED_CS_Clr() gpio_bit_write(CS1_PORT, CS1_PIN, 0)
define OLED_CS_Set() gpio_bit_write(CS1_PORT, CS1_PIN, 1)
// miso
define OLED_READ_FS0() gpio_input_bit_get(MISO_PORT, MISO_PIN)
// cs2
define OLED_ROM_CS_Clr() gpio_bit_write(CS2_PORT, CS2_PIN, 0)
define OLED_ROM_CS_Set() gpio_bit_write(CS2_PORT, CS2_PIN, 1)
define OLED_CMD 0 //写命令
define OLED_DATA 1 //写数据
void OLED_ColorTurn(u8 i); void OLED_DisplayTurn(u8 i); void OLED_WR_Byte(u8 dat,u8 cmd); void OLED_Clear(void); void OLED_address(u8 x,u8 y); void OLED_Display_128x64(u8 dp); void OLED_Display_16x16(u8 x,u8 y,u8 dp); void OLED_Display_8x16(u8 x,u8 y,u8 dp); void OLED_Display_5x7(u8 x,u8 y,u8 dp); void Send_Command_to_ROM(u8 dat); u8 Get_data_from_ROM(void); void OLED_get_data_from_ROM(u8 addrHigh,u8 addrMid,u8 addrLow,u8 pbuff,u8 DataLen); void OLED_Display_GB2312_string(u8 x,u8 y,u8 text); void OLED_Display_string_5x7(u8 x,u8 y,u8 *text); void OLED_ShowNum(u8 x,u8 y,float num,u8 len); void OLED_Init(void);
endif
```c#include "oled.h"#include "stdlib.h"//反显函数void OLED_ColorTurn(u8 i){if(i==0){OLED_WR_Byte(0xA6,OLED_CMD);//正常显示}if(i==1){OLED_WR_Byte(0xA7,OLED_CMD);//反色显示}}//屏幕旋转180度void OLED_DisplayTurn(u8 i){if(i==0){OLED_WR_Byte(0xC8,OLED_CMD);//正常显示OLED_WR_Byte(0xA1,OLED_CMD);}if(i==1){OLED_WR_Byte(0xC0,OLED_CMD);//反转显示OLED_WR_Byte(0xA0,OLED_CMD);}}//开启OLED显示void OLED_DisPlay_On(void){OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能OLED_WR_Byte(0x14,OLED_CMD);//开启电荷泵OLED_WR_Byte(0xAF,OLED_CMD);//点亮屏幕}//关闭OLED显示void OLED_DisPlay_Off(void){OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能OLED_WR_Byte(0x10,OLED_CMD);//关闭电荷泵OLED_WR_Byte(0xAE,OLED_CMD);//关闭屏幕}//向SSD1306写入一个字节。//mode:数据/命令标志 0,表示命令;1,表示数据;void OLED_WR_Byte(u8 dat,u8 cmd){u8 i;if(cmd)OLED_DC_Set();elseOLED_DC_Clr();OLED_CS_Clr();for(i=0;i<8;i++){OLED_SCL_Clr();if(dat&0x80)OLED_SDA_Set();elseOLED_SDA_Clr();OLED_SCL_Set();dat<<=1;}OLED_CS_Set();OLED_DC_Set();}//清屏函数void OLED_Clear(void){u8 i,n;for(i=0;i<8;i++){OLED_WR_Byte(0xb0+i,OLED_CMD);//设置页地址OLED_WR_Byte(0x10,OLED_CMD); //设置列地址的高4位OLED_WR_Byte(0x00,OLED_CMD); //设置列地址的低4位for(n=0;n<128;n++){OLED_WR_Byte(0x00,OLED_DATA);//清除所有数据}}}//设置起始地址void OLED_address(u8 x,u8 y){OLED_WR_Byte(0xb0+y,OLED_CMD); //设置页地址OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); //设置列地址的高4位OLED_WR_Byte((x&0x0f),OLED_CMD); //设置列地址的低4位}//显示128x64点阵图像void OLED_Display_128x64(u8 *dp){u8 i,j;for(i=0;i<8;i++){OLED_address(0,i);for(j=0;j<128;j++){OLED_WR_Byte(*dp,OLED_DATA); //写数据到OLED,每写完一个8位的数据后列地址自动加1dp++;}}}//显示16x16点阵图像、汉字、生僻字或16x16点阵的其他图标void OLED_Display_16x16(u8 x,u8 y,u8 *dp){u8 i,j;for(j=0;j<2;j++){OLED_address(x,y);for(i=0;i<16;i++){OLED_WR_Byte(*dp,OLED_DATA); //写数据到OLED,每写完一个8位的数据后列地址自动加1dp++;}y++;}}//显示8x16点阵图像、ASCII, 或8x16点阵的自造字符、其他图标void OLED_Display_8x16(u8 x,u8 y,u8 *dp){u8 i,j;for(j=0;j<2;j++){OLED_address(x,y);for(i=0;i<8;i++){OLED_WR_Byte(*dp,OLED_DATA); //写数据到LCD,每写完一个8位的数据后列地址自动加1dp++;}y++;}}//显示5*7点阵图像、ASCII, 或5x7点阵的自造字符、其他图标void OLED_Display_5x7(u8 x,u8 y,u8 *dp){u8 i;OLED_address(x,y);for(i=0;i<6;i++){OLED_WR_Byte(*dp,OLED_DATA);dp++;}}//送指令到晶联讯字库ICvoid Send_Command_to_ROM(u8 dat){u8 i;for(i=0;i<8;i++){OLED_SCL_Clr();if(dat&0x80){OLED_SDA_Set();}else{OLED_SDA_Clr();}dat<<=1;OLED_SCL_Set();}}//从晶联讯字库IC中取汉字或字符数据(1个字节)u8 Get_data_from_ROM(void){u8 i,read=0;for(i=0;i<8;i++){OLED_SCL_Clr();read<<=1;if(OLED_READ_FS0()){read++;}OLED_SCL_Set();}return read;}//从相关地址(addrHigh:地址高字节,addrMid:地址中字节,addrLow:地址低字节)中连续读出DataLen个字节的数据到 pbuff的地址//连续读取void OLED_get_data_from_ROM(u8 addrHigh,u8 addrMid,u8 addrLow,u8 *pbuff,u8 DataLen){u8 i;OLED_ROM_CS_Clr();Send_Command_to_ROM(0x03);Send_Command_to_ROM(addrHigh);Send_Command_to_ROM(addrMid);Send_Command_to_ROM(addrLow);for(i=0;i<DataLen;i++){*(pbuff+i)=Get_data_from_ROM();}OLED_ROM_CS_Set();}u32 fontaddr=0;void OLED_Display_GB2312_string(u8 x,u8 y,u8 *text){u8 i=0;u8 addrHigh,addrMid,addrLow;u8 fontbuf[32];while(text[i]>0x00){if((text[i]>=0xb0)&&(text[i]<=0xf7)&&(text[i+1]>=0xa1)){//国标简体(GB2312)汉字在晶联讯字库IC中的地址由以下公式来计算://Address = ((MSB - 0xB0) * 94 + (LSB - 0xA1)+ 846)*32+ BaseAdd;BaseAdd=0//由于担心8位单片机有乘法溢出问题,所以分三部取地址fontaddr=(text[i]-0xb0)*94;fontaddr+=(text[i+1]-0xa1)+846;fontaddr=fontaddr*32;addrHigh=(fontaddr&0xff0000)>>16; //地址的高8位,共24位addrMid=(fontaddr&0xff00)>>8; //地址的中8位,共24位addrLow=(fontaddr&0xff); //地址的低8位,共24位OLED_get_data_from_ROM(addrHigh,addrMid,addrLow,fontbuf,32);//取32个字节的数据,存到"fontbuf[32]"OLED_Display_16x16(x,y,fontbuf);//显示汉字到LCD上,y为页地址,x为列地址,fontbuf[]为数据x+=16;i+=2;}else if((text[i]>=0xa1)&&(text[i]<=0xa3)&&(text[i+1]>=0xa1)){fontaddr=(text[i]-0xa1)*94;fontaddr+=(text[i+1]-0xa1);fontaddr=fontaddr*32;addrHigh=(fontaddr&0xff0000)>>16;addrMid=(fontaddr&0xff00)>>8;addrLow=(fontaddr&0xff);OLED_get_data_from_ROM(addrHigh,addrMid,addrLow,fontbuf,32);OLED_Display_16x16(x,y,fontbuf);x+=16;i+=2;}else if((text[i]>=0x20)&&(text[i]<=0x7e)){unsigned char fontbuf[16];fontaddr=(text[i]-0x20);fontaddr=(unsigned long)(fontaddr*16);fontaddr=(unsigned long)(fontaddr+0x3cf80);addrHigh=(fontaddr&0xff0000)>>16;addrMid=(fontaddr&0xff00)>>8;addrLow=fontaddr&0xff;OLED_get_data_from_ROM(addrHigh,addrMid,addrLow,fontbuf,16);OLED_Display_8x16(x,y,fontbuf);x+=8;i+=1;}elsei++;}}void OLED_Display_string_5x7(u8 x,u8 y,u8 *text){u8 i=0;u8 addrHigh,addrMid,addrLow;while(text[i]>0x00){if((text[i]>=0x20)&&(text[i]<=0x7e)){u8 fontbuf[8];fontaddr=(text[i]-0x20);fontaddr=(unsigned long)(fontaddr*8);fontaddr=(unsigned long)(fontaddr+0x3bfc0);addrHigh=(fontaddr&0xff0000)>>16;addrMid=(fontaddr&0xff00)>>8;addrLow=fontaddr&0xff;OLED_get_data_from_ROM(addrHigh,addrMid,addrLow,fontbuf,8);OLED_Display_5x7(x,y,fontbuf);x+=6;i+=1;}elsei++;}}//显示2个数字//x,y :起点坐标//num1:要显示的小数//len :数字的位数void OLED_ShowNum(u8 x,u8 y,float num1,u8 len){u8 i;u32 t,num;x=x+len*8+8;//要显示的小数最低位的横坐标num=num1*100;//将小数左移两位并转化为整数OLED_Display_GB2312_string(x-24,y,".");//显示小数点for(i=0;i<len;i++){t=num%10;//取个位数的数值num=num/10;//将整数右移一位x-=8;if(i==2){x-=8;}//当显示出来两个小数之后,空出小数点的位置switch(t){case 0 :OLED_Display_GB2312_string(x,y,"0");break;case 1 :OLED_Display_GB2312_string(x,y,"1");break;case 2 :OLED_Display_GB2312_string(x,y,"2");break;case 3 :OLED_Display_GB2312_string(x,y,"3");break;case 4 :OLED_Display_GB2312_string(x,y,"4");break;case 5 :OLED_Display_GB2312_string(x,y,"5");break;case 6 :OLED_Display_GB2312_string(x,y,"6");break;case 7 :OLED_Display_GB2312_string(x,y,"7");break;case 8 :OLED_Display_GB2312_string(x,y,"8");break;case 9 :OLED_Display_GB2312_string(x,y,"9");break;}}}//OLED的初始化void OLED_Init(void){rcu_periph_clock_enable(CLK_RCU);gpio_mode_set(CLK_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, CLK_PIN);gpio_output_options_set(CLK_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, CLK_PIN);rcu_periph_clock_enable(MOSI_RCU);gpio_mode_set(MOSI_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MOSI_PIN);gpio_output_options_set(MOSI_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, MOSI_PIN);rcu_periph_clock_enable(DC_RCU);gpio_mode_set(DC_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, DC_PIN);gpio_output_options_set(DC_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, DC_PIN);rcu_periph_clock_enable(CS1_RCU);gpio_mode_set(CS1_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, CS1_PIN);gpio_output_options_set(CS1_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, CS1_PIN);rcu_periph_clock_enable(MISO_RCU);gpio_mode_set(MISO_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, MISO_PIN);//gpio_output_options_set(MISO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, MISO_PIN);rcu_periph_clock_enable(CS2_RCU);gpio_mode_set(CS2_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, CS2_PIN);gpio_output_options_set(CS2_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, CS2_PIN);delay_1ms(200);OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panelOLED_WR_Byte(0x00,OLED_CMD);//---set low column addressOLED_WR_Byte(0x10,OLED_CMD);//---set high column addressOLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control registerOLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current BrightnessOLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常OLED_WR_Byte(0xA6,OLED_CMD);//--set normal displayOLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 dutyOLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)OLED_WR_Byte(0x00,OLED_CMD);//-not offsetOLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequencyOLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/SecOLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge periodOLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 ClockOLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configurationOLED_WR_Byte(0x12,OLED_CMD);OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomhOLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect LevelOLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)OLED_WR_Byte(0x02,OLED_CMD);//OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disableOLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disableOLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)OLED_Clear();OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/}
#include "gd32f4xx.h"#include "systick.h"#include <stdio.h>#include "main.h"#include "oled.h"#include "bmp.h"#include "Usart.h"void Usart0_recv(uint8_t *data, uint32_t len) {}int main(void){systick_config();Usart0_init();OLED_Init(); //初始化OLEDOLED_ColorTurn(0);//0正常显示,1 反色显示OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示while(1){OLED_Display_128x64(bmp1);delay_1ms(500);OLED_Display_GB2312_string(0,0,"12864,带中文字库"); /*在第1页,第1列,显示一串16x16点阵汉字或8x16的ASCII字*/OLED_Display_GB2312_string(0,2,"16X16简体汉字库,"); /*显示一串16x16点阵汉字或8x16的ASCII字.以下雷同*/OLED_Display_GB2312_string(0,4,"或8X16点阵ASCII,");OLED_Display_GB2312_string(0,6,"或5X7点阵ASCII码");delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(24,0,"中景园电子");OLED_Display_GB2312_string(0,2,"主要生产OLED模块");OLED_Display_GB2312_string(0,4,"顾客至上真诚服务");OLED_Display_GB2312_string(0,6,"诚信与质量第一!");delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(0,0,"GB2312简体字库及");OLED_Display_GB2312_string(0,2,"有图型功能,可自");OLED_Display_GB2312_string(0,4,"编大字或图像或生");OLED_Display_GB2312_string(0,6,"僻字,例如:");OLED_Display_16x16(97,6,jiong1); /*在第7页,第81列显示单个自编生僻汉字“囧”*/OLED_Display_16x16(113,6,lei1);delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(0,0,"<!@#$%^&*()_-+]/"); /*在第1页,第1列,显示一串16x16点阵汉字或8*16的ASCII字*/OLED_Display_string_5x7(0,2,"<!@#$%^&*()_-+]/;.,?[");/*在第3页,第1列,显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,3,"XY electronics Co., ");/*显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,4,"Ltd. established at ");/*显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,5,"year 2010.Focus OLED ");/*显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,6,"Mobile:13265585975");/*显示一串5x7点阵的ASCII字*/OLED_Display_string_5x7(0,7,"Tel:0755-32910715");/*显示一串5x7点阵的ASCII字*/delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(0,0,"啊阿埃挨哎唉哀皑"); /*在第1页,第1列,显示一串16x16点阵汉字或8x16的ASCII字*/OLED_Display_GB2312_string(0,2,"癌蔼矮艾碍爱隘鞍"); /*显示一串16x16点阵汉字或8x16的ASCII字.以下雷同*/OLED_Display_GB2312_string(0,4,"氨安俺按暗岸胺案");OLED_Display_GB2312_string(0,6,"肮昂盎凹敖熬翱袄");delay_1ms(500);OLED_Clear();OLED_Display_GB2312_string(0,0,(uint8_t *)"鬟鬣麽麾縻麂麇麈");OLED_Display_GB2312_string(0,2,(uint8_t *)"麋麒鏖麝麟黛黜黝");OLED_Display_GB2312_string(0,4,(uint8_t *)"黠黟黢黩黧黥黪黯");OLED_Display_GB2312_string(0,6,(uint8_t *)"齄鼬鼹鼷鼽鼾鼢鼯");delay_1ms(500);OLED_Clear();}}
软件和硬件SPI
SPI是主设备和从设备通讯的协议和桥梁,spi通常管理时钟,输入,输出。设备的切换,交给具体的设备,也就是片选交给具体的设备。
完整代码
#ifndef __SOFT_SPI0_H__#define __SOFT_SPI0_H__#include "gd32f4xx.h"#include "systick.h"/*************** pin config *****************/#define CLK_RCU RCU_GPIOB#define CLK_PORT GPIOB#define CLK_PIN GPIO_PIN_3#define MOSI_RCU RCU_GPIOB#define MOSI_PORT GPIOB#define MOSI_PIN GPIO_PIN_5#define MISO_RCU RCU_GPIOB#define MISO_PORT GPIOB#define MISO_PIN GPIO_PIN_4/************** spi config *******************/// clock polarity: hight(0) or low(1)#define SPI0_CPOL 0// clock phase: leading(0) or trailing(1)#define SPIO_CPHA 0/*************** gpio opt ********************/#define CLK(bit) gpio_bit_write(CLK_PORT, CLK_PIN, bit ? SET : RESET)#define MOSI(bit) gpio_bit_write(MOSI_PORT, MOSI_PIN, bit ? SET : RESET)#define MISO_STATE() gpio_input_bit_get(MISO_PORT, MISO_PIN)void SoftSPI0_init();void SoftSPI0_write_byte(uint8_t data);uint8_t SoftSPI0_read_byte();#endif
#include "Soft_SPI0.h"void SoftSPI0_init() {/******************* GPIO **********************/// clkrcu_periph_clock_enable(CLK_RCU);gpio_mode_set(CLK_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, CLK_PIN);gpio_output_options_set(CLK_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, CLK_PIN);// mosircu_periph_clock_enable(MOSI_RCU);gpio_mode_set(MOSI_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MOSI_PIN);gpio_output_options_set(MOSI_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, MOSI_PIN);// misorcu_periph_clock_enable(MISO_RCU);gpio_mode_set(MISO_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, MISO_PIN);/******************* io state ****************/#if SPI0_CPOLCLK(1);#elseCLK(0);#endif}static void CLK_START() {#if SPI0_CPOLCLK(1);#elseCLK(0);#endif}static void CLK_END() {#if SPI0_CPOLCLK(0);#elseCLK(1);#endif}void SoftSPI0_write_byte(uint8_t data) {uint8_t i;for(i = 0; i < 8; i++){CLK_START();if(data&0x80)MOSI(1);elseMOSI(0);CLK_END();data <<= 1;}CLK_START();}uint8_t SoftSPI0_read_byte() {uint8_t i,read=0;for(i = 0; i < 8; i++){CLK_START();read<<=1;if(MISO_STATE()){read++;}CLK_END();}return read;}
#ifndef __SPI0_H__#define __SPI0_H__#include "gd32f4xx.h"#include "systick.h"/*************** pin config *****************/#define CLK_RCU RCU_GPIOB#define CLK_PORT GPIOB#define CLK_PIN GPIO_PIN_3#define CLK_AF GPIO_AF_5#define MOSI_RCU RCU_GPIOB#define MOSI_PORT GPIOB#define MOSI_PIN GPIO_PIN_5#define MOSI_AF GPIO_AF_5#define MISO_RCU RCU_GPIOB#define MISO_PORT GPIOB#define MISO_PIN GPIO_PIN_4#define MISO_AF GPIO_AF_5void SPI0_init();void SPI0_write_byte(uint8_t data);uint8_t SPI0_read_byte();#endif
#include "SPI0.h"#include <stdio.h>void SPI0_init() {uint32_t spix = SPI0;uint32_t spix_rcu = RCU_SPI0;/************************** GPIO **********************/// sclkrcu_periph_clock_enable(CLK_RCU);gpio_mode_set(CLK_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, CLK_PIN);gpio_output_options_set(CLK_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, CLK_PIN);gpio_af_set(CLK_PORT, CLK_AF, CLK_PIN);// mosircu_periph_clock_enable(MOSI_RCU);gpio_mode_set(MOSI_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, MOSI_PIN);gpio_output_options_set(MOSI_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, MOSI_PIN);gpio_af_set(MOSI_PORT, MOSI_AF, MOSI_PIN);// misorcu_periph_clock_enable(MISO_RCU);gpio_mode_set(MISO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, MISO_PIN);gpio_output_options_set(MISO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, MISO_PIN);gpio_af_set(MISO_PORT, MISO_AF, MISO_PIN);gpio_bit_set(MISO_PORT, MISO_PIN);/************************** SPI **********************/rcu_periph_clock_enable(spix_rcu);spi_i2s_deinit(spix);spi_parameter_struct sps;spi_struct_para_init(&sps);sps.device_mode = SPI_MASTER; //设备模式sps.frame_size = SPI_FRAMESIZE_8BIT;sps.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;sps.nss = SPI_NSS_SOFT;sps.prescale = SPI_PSC_32;sps.endian = SPI_ENDIAN_MSB;spi_init(spix, &sps);//spi_nss_internal_high(spix);spi_enable(spix);}void SPI0_write_byte(uint8_t data) {while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_TBE)) {};spi_i2s_data_transmit(SPI0, data);while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_RBNE)) {};spi_i2s_data_receive(SPI0);}uint8_t SPI0_read_byte() {while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_TBE)) {};spi_i2s_data_transmit(SPI0, 0x00);while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_RBNE)) {};return spi_i2s_data_receive(SPI0);}
忽略指定警告
要在Keil中配置忽略-Winvalid-source-encoding警告,您可以按照以下步骤进行操作:
- 打开Keil软件并打开您的项目。
- 在Keil的菜单栏中,选择 “Project” > “Options for Target”。
- 在弹出的对话框中,选择 “C/C++” 选项卡。
在 “Misc Controls” 部分的 “Other Compiler Options” 字段中添加以下内容:
-Wno-invalid-source-encoding
这将告诉编译器忽略
-Winvalid-source-encoding警告。单击 “OK” 保存更改并关闭对话框。
- 重新编译您的项目,编译器将不再显示
-Winvalid-source-encoding警告。
请注意,这只是忽略该特定警告的方法。确保在忽略警告之前了解其含义,并确保您的代码没有潜在的问题。
练习题
- 实现stm32的spi屏幕移植
- 实现gd32的spi屏幕移植

