完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
OLED.c #include "OLED.h" #include "CodeTable.h" #include "STM32f10x_gpio.h" void Delay(int x) { int i,j; for(i=0;i<2000;i++) for(j=0;j void OLED_WrDat(unsigned char dat)//写数据 { unsigned char i; OLED_WR_D ; // Delay(20); //读写数据命令 for(i=0;i<8;i++) { if((dat << i) & 0x80) { OLED_DA_H; //先送高位 最后送低位 完成一个字节的发送 } else { OLED_DA_L; } OLED_SCL_L; //给上升沿 // Delay(20); OLED_SCL_H; } } void OLED_WrCmd(unsigned char cmd)//写命令 { unsigned char i; OLED_WR_C ; // Delay(20); for(i=0;i<8;i++) //发送一个八位数据 { if((cmd << i) & 0x80) { OLED_DA_H; } else { OLED_DA_L; } OLED_SCL_L; //给上升沿 // Delay(20); OLED_SCL_H; } } 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 OLED_WrDat(bmp_dat); } } } void OLED_CLS(void)//清屏 { OLED_Fill(0x00); } void OLED_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); //使能GPIOB C端口时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15|GPIO_Pin_12|GPIO_Pin_13; //PB13,PB12,PB15推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;//速度2MHz GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOD3,6 GPIO_SetBits(GPIOB,GPIO_Pin_15|GPIO_Pin_12|GPIO_Pin_13); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PC6推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;//速度2MHz GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_SetBits(GPIOC,GPIO_Pin_6); OLED_CS_L; OLED_WrCmd(0xae); //参照官方文档设置 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(0x00); //清屏 OLED_SetPos(0,0); } void OLED_6x8Str(unsigned char x, unsigned char y, unsigned char ch[]) //写6*8点阵字符 { // x----表示从纵向第几列开始写 纵向共有128列 一行最多可以写128/6行 unsigned char c=0,i=0,j=0; // y-----表示从纵向第几行开始写 横向共有64行 最多可以写64/8列 while (ch[j]!=' |