SPI接线比较多,这次使用的是PB9~5.
软件仿真接口配置可以随意,
#define OLED_GPIO_PORT GPIOB
#define OLED_PERIPH_CLOCK CRM_GPIOB_PERIPH_CLOCK
#define OLED_SCL GPIO_PINS_9
#define OLED_SDA GPIO_PINS_8
#define OLED_RES GPIO_PINS_7
#define OLED_DC GPIO_PINS_6
#define OLED_CS GPIO_PINS_5
#define OLED_SCL_Clr() gpio_bits_reset(OLED_GPIO_PORT,OLED_SCL);//CLK
#define OLED_SCL_Set() gpio_bits_set(OLED_GPIO_PORT,OLED_SCL);
#define OLED_SDA_Clr() gpio_bits_reset(OLED_GPIO_PORT,OLED_SDA);//SDA
#define OLED_SDA_Set() gpio_bits_set(OLED_GPIO_PORT,OLED_SDA);
#define OLED_RES_Clr() gpio_bits_reset(OLED_GPIO_PORT,OLED_RES);//RES
#define OLED_RES_Set() gpio_bits_set(OLED_GPIO_PORT,OLED_RES);
#define OLED_DC_Clr() gpio_bits_reset(OLED_GPIO_PORT,OLED_DC);//DC
#define OLED_DC_Set() gpio_bits_set(OLED_GPIO_PORT,OLED_DC);
#define OLED_CS_Clr() gpio_bits_reset(OLED_GPIO_PORT,OLED_CS);//CS
#define OLED_CS_Set() gpio_bits_set(OLED_GPIO_PORT,OLED_CS);
#define OLED_CMD 0
#define OLED_DATA 1
OLED初始化配置;
void OLED_Init(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_8|GPIO_PINS_9|GPIO_PINS_7|GPIO_PINS_6|GPIO_PINS_5;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOB, &gpio_init_struct);
OLED_RES_Clr();
uint32_t _dcnt = 1000000;
_dcnt = 1000000;
while (_dcnt-- != 0) {__NOP();};
OLED_RES_Set();
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
OLED_WR_Byte(0x81,OLED_CMD); // contract control
OLED_WR_Byte(0xFF,OLED_CMD);//--128
OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
OLED_WR_Byte(0x80,OLED_CMD);//
OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
OLED_WR_Byte(0x30,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
}
那么,SPI_OLED的底层函数与IIC不同
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
u8 i;
if(cmd)
{OLED_DC_Set();}
else
{OLED_DC_Clr();}
OLED_CS_Clr();
for(i=0;i<8;i++)
{
OLED_SCL_Clr();
if(dat&0x80)
{OLED_SDA_Set();}
else
{OLED_SDA_Clr();}
OLED_SCL_Set();
dat<<=1;
}
OLED_CS_Set();
OLED_DC_Set();
}
之后,编译就行
这是实际显示图。
感谢21ic及亚特力,感谢各位观看,希望对大家有所帮助。
SPI_OLED.zip
(9.17 KB)
|