几年前用蓝牙芯片开发一个项目,买了SmartRF开发板,上面带一个12864的OLED屏,最近在做一个IIC的OLED屏的项目,把这套板子找出来打算先把显示的目录系统做了。简单撇了一眼资料,写的IIC的驱动例程,可看程序不对呀,驱动方式明明是SPI的,怎么就IIC的例程呢?于是我找原理图看了一下果然没有开漏的两个上拉电阻。找找原来用过的配套软件,没找到,只好拿一块前阵子调试GDE230的板子往上怼了。 好了,万事俱备了,老铁,上吧!把程序移植到GD32E上,没仿真,直接上,结果啥也没有显示。我坐在电脑前想了半天,想这个仿SIP的程序很简单呀,51时我就写个,做PIC690和917时,LPC2000时,包括当初做STM32时都写过,不可能不好用呀!于是我搬出好久未用的用示波器测了测波形,两个板子对比了一下,结果发现GD32E230的做CLK的那个引脚没有输出。血的教训告诉我,用过的板子一定做好记录,要不就直接扔了,太耽误事了!
好了,这个程序是直接移植过来的,能实现数字,英文,汉字和图片的显示,显示字符的程序我修改了一下,加上了反显,上程序!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
单片机源程序如下:
- #ifndef __SSD1306_SPI_H
- #define __SSD1306_SPI_H
- #include "gd32e230.h"
- /*******************************宏定义****************************************/
- #define XLevelL 0x00
- #define XLevelH 0x10
- #define XLevel ((XLevelH&0x0F)*16+XLevelL)
- #define Max_Column 128
- #define Max_Row 64
- #define Brightness 0xCF
- #define X_WIDTH 128
- #define Y_WIDTH 64
- //-----------------OLED端口定义----------------
- #define OLED_CS_Clr() GPIO_BC(GPIOB) = GPIO_PIN_0
- #define OLED_CS_Set() GPIO_BOP(GPIOB) = GPIO_PIN_0 //接OLED模块CS B0
- #define OLED_DC_Clr() GPIO_BC(GPIOB) = GPIO_PIN_1
- #define OLED_DC_Set() GPIO_BOP(GPIOB) = GPIO_PIN_1 //接OLED模块D/C B1
- #define OLED_SDA_Clr() GPIO_BC(GPIOB) = GPIO_PIN_8
- #define OLED_SDA_Set() GPIO_BOP(GPIOB) = GPIO_PIN_8 //接OLED模块MOSI B8
- #define OLED_SCL_Clr() GPIO_BC(GPIOA) = GPIO_PIN_7
- #define OLED_SCL_Set() GPIO_BOP(GPIOA) = GPIO_PIN_7 //接OLED模块的CLK B9
- //#define OLED_CMD 0 //写命令
- //#define OLED_DATA 1 //写数据
- //OLED控制用函数
- void OLED_WrDat(unsigned char dat);//写数据
- void OLED_WrCmd(unsigned char cmd);//写命令
- void OLED_SetPos(unsigned char x, unsigned char y);//设置起始点坐标
- void OLED_Fill(unsigned char bmp_dat);//全屏填充
- void OLED_CLS(void);//清屏
- void OLED_Init(void);//初始化
- void OLED_6x8Str(unsigned char x, unsigned char y, unsigned char ch[]);
- //void OLED_8x16Str(unsigned char x, unsigned char y, unsigned char ch[]);
- void OLED_8x16Str(unsigned char x, unsigned char y, unsigned char FB,unsigned char ch[]);
- void OLED_16x16CN(unsigned char x, unsigned char y, unsigned char N);
- void OLED_BMP(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned char BMP[]);
- #endif
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #include "SSD1306_SPI.h"
- #include "systick.h"
- #include "codetab.h"
- void delay(uint32_t nCount)
- {
- unsigned int a;
- while(nCount)
- {
- a=1800;
- while(a--);
- nCount--;
- }
- return;
- }
- void OLED_WrDat(unsigned char dat)//写数据
- {
- unsigned char i, temp=0;
- OLED_DC_Set();
- for(i=0;i<8;i++) //发送一个八位数据
- {
- OLED_SCL_Clr();
- temp = dat&0x80;
- if (temp == 0)
- {
- OLED_SDA_Clr();
- }
- else
- {
- OLED_SDA_Set();
- }
- //delay_1ms(1U);
- OLED_SCL_Set();
- dat<<=1;
- //delay_1ms(1U);
- }
- }
- void OLED_WrCmd(unsigned char cmd)//写命令
- {
- unsigned char i, temp=0;
- OLED_DC_Clr();
- for(i=0;i<8;i++) //发送一个八位数据
- {
- OLED_SCL_Clr();
- temp = cmd&0x80;
- if (temp == 0)
- {
- OLED_SDA_Clr();
- }
- else
- {
- OLED_SDA_Set();
- }
- OLED_SCL_Set();
- cmd<<=1;;
- }
- }
- void OLED_SetPos(unsigned char x, unsigned char y)//设置起始点坐标
- {
- OLED_WrCmd(0xb0 + y);
- OLED_WrCmd(((x&0xf0)>>4)|0x10);
- OLED_WrCmd((x&0x0f)|0x01);
- }
- void OLED_Fill(unsigned char bmp_dat)//全屏填充
- {
- unsigned char y,x;
- for(y=0;y<8;y++)
- {
- OLED_WrCmd(0xb0+y);
- OLED_WrCmd(0x01);
- OLED_WrCmd(0x10);
- for(x=0;x<X_WIDTH;x++)
- {
- OLED_WrDat(bmp_dat);
- }
- }
- }
- void OLED_CLS(void)//清屏
- {
- OLED_Fill(0x00);
- }
- void OLED_Init(void)
- {
- rcu_periph_clock_enable(RCU_GPIOB);
- gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,GPIO_PIN_0);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_0);
- gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,GPIO_PIN_1);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
- gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,GPIO_PIN_8);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
- rcu_periph_clock_enable(RCU_GPIOA);
- gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,GPIO_PIN_7);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
-
- OLED_SCL_Set();
- OLED_CS_Clr();
- delay(100);
- OLED_CS_Set();
-
- OLED_WrCmd(0xae);//--turn off oled panel
- OLED_WrCmd(0x00);//---set low column address
- OLED_WrCmd(0x10);//---set high column address
- OLED_WrCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
- OLED_WrCmd(0x81);//--set contrast control register
- OLED_WrCmd(0xcf); // Set SEG Output Current Brightness
- OLED_WrCmd(0xa1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
- OLED_WrCmd(0xc8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
- OLED_WrCmd(0xa6);//--set normal display
- OLED_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
- OLED_WrCmd(0x3f);//--1/64 duty
- OLED_WrCmd(0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
- OLED_WrCmd(0x00);//-not offset
- OLED_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
- OLED_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
- OLED_WrCmd(0xd9);//--set pre-charge period
- OLED_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- OLED_WrCmd(0xda);//--set com pins hardware configuration
- OLED_WrCmd(0x12);
- OLED_WrCmd(0xdb);//--set vcomh
- OLED_WrCmd(0x40);//Set VCOM Deselect Level
- OLED_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
- OLED_WrCmd(0x02);//
- OLED_WrCmd(0x8d);//--set Charge Pump enable/disable
- OLED_WrCmd(0x14);//--set(0x10) disable
- OLED_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
- OLED_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
- OLED_WrCmd(0xaf);//--turn on oled panel
- OLED_Fill(0xff); //初始清屏
- delay(50);
- OLED_CLS();
- OLED_SetPos(0,0);
- }
- void OLED_6x8Str(unsigned char x, unsigned char y, unsigned char ch[])
- {
- unsigned char c=0,i=0,j=0;
- while (ch[j]!='\0')
- {
- c = ch[j]-32;
- if(x>126)
- {
- x=0;y++;
- }
- OLED_SetPos(x,y);
- for(i=0;i<6;i++)
- {
- OLED_WrDat(F6x8[c][i]);
- }
- x+=6;
- j++;
- }
- }
- void OLED_8x16Str(unsigned char x, unsigned char y, unsigned char FB,unsigned char ch[])
- {
- unsigned char c=0,i=0,j=0;
- while (ch[j]!='\0')
- {
- c =ch[j]-32;
- if(x>120)
- {
- x=0;y++;
- }
- OLED_SetPos(x,y);
- for(i=0;i<8;i++)
- {
- if(FB == 1) OLED_WrDat(~(F8X16[c*16+i]));
- else OLED_WrDat(F8X16[c*16+i]);
- }
- OLED_SetPos(x,y+1);
- for(i=0;i<8;i++)
- {
- if(FB == 1) OLED_WrDat(~(F8X16[c*16+i+8]));
- else OLED_WrDat(F8X16[c*16+i+8]);
- }
- x+=8;
- j++;
- }
- }
- void OLED_16x16CN(unsigned char x, unsigned char y, unsigned char N)
- {
- unsigned char wm=0;
- unsigned int adder=32*N;
- OLED_SetPos(x , y);
- for(wm = 0;wm < 16;wm++)
- {
- OLED_WrDat(F16x16[adder]);
- adder += 1;
- }
- OLED_SetPos(x,y + 1);
- for(wm = 0;wm < 16;wm++)
- {
- OLED_WrDat(F16x16[adder]);
- adder += 1;
- }
- }
- void OLED_BMP(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++)
- {
- OLED_WrDat(BMP[j++]);
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int main(void)
- {
- extern const unsigned char BMP1[];
- extern const unsigned char BMP_SETTING[];
- extern const unsigned char BMP_POWEROFF[];
- int32_t i = 0;
- uint8_t key = 0;
- /* system clocks configuration */
- rcu_config();
- /* systick configuration */
- systick_config();
- /* GPIO configuration */
- gpio_config();
- /* UART configuration */
- COM_init(EVAL_COM);
- /* OLED configuration */
- OLED_Init();
- /*******************************************************************/
- OLED_6x8Str(0,0,(uint8_t *)"123456789");
- OLED_6x8Str(0,1,(uint8_t *)"abcdefgh");
- delay_1ms(800U);
- OLED_BMP(0,0,128,8,(unsigned char *)BMP1);
- while (1)
- {
- delay_1ms(100U);
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
- MCU:GD32E230C8T6,72MHz
- OLED SPI MODE
- OLED: SmartRF V2.6的OLED屏
- 1 2 3 4 5 6
- GND VCC D0 D1 RST DC
- 0V 3V3 PB9 PA7 PB0 PB1
- GD的库: version 2018-06-19, V1.0.0, firmware for GD32E230
- KEIL MDK 5.27.1.0
复制代码 全部程序51hei下载地址:
OLED_Display - 模拟SPI.zip
(220.23 KB, 下载次数: 64)
|