/
oled.c
215 lines (189 loc) · 4.42 KB
/
oled.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "oled.h"
#include "ASCII.h"
uint8_t oled_data[4][128]={0};
extern void DelayUS(__IO uint32_t nTime);
#define I2C_SDA_LOW() GPIOE->BSRRH = GPIO_Pin_2
#define I2C_SDA_HIGH() GPIOE->BSRRL = GPIO_Pin_2
#define I2C_SCL_LOW() GPIOE->BSRRH = GPIO_Pin_3
#define I2C_SCL_HIGH() GPIOE->BSRRL = GPIO_Pin_3
#define I2C_SDA_Read() (GPIOE->IDR & GPIO_Pin_2)
//#define I2C_Delay() DelayUS(1);//实现延时4us
static void I2C_Delay()
{
uint8_t i=24;
while(i--);
}
static void I2C_Start(void)
{
I2C_SCL_HIGH();
I2C_SDA_HIGH();
I2C_Delay();
I2C_SDA_LOW();
I2C_Delay();
I2C_SCL_LOW();
I2C_Delay();
}
static void I2C_Stop(void)
{
//I2C_SCL_LOW();
I2C_SDA_LOW();
//I2C_Delay();
I2C_SCL_HIGH();
I2C_Delay();
I2C_SDA_HIGH();
I2C_Delay();
}
static uint8_t I2C_WaitAck(void) //0:Ack 1:NoAck
{
I2C_SDA_HIGH();
I2C_SCL_HIGH();
I2C_Delay();
if(I2C_SDA_Read())
{
//I2C_Delay();
//if(I2C_SDA_Read())
return 1;
}
I2C_SCL_LOW();
I2C_Delay();
return 0;
}
static void I2C_SendByte(uint8_t Byte)
{
uint8_t Cnt;
for(Cnt=0;Cnt<8;Cnt++)
{
if(Byte&0x80)
I2C_SDA_HIGH();
else
I2C_SDA_LOW();
Byte <<= 1;
I2C_Delay();
I2C_SCL_HIGH();
I2C_Delay();
I2C_SCL_LOW();
I2C_Delay();
}
}
static void OLED_WriteCmd(uint8_t Cmd)
{
I2C_Start();
I2C_SendByte(0x78);
I2C_WaitAck();
I2C_SendByte(0x00);
I2C_WaitAck();
I2C_SendByte(Cmd);
I2C_WaitAck();
I2C_Stop();
}
static void OLED_WriteData(uint8_t Data)
{
I2C_Start();
I2C_SendByte(0x78);
I2C_WaitAck();
I2C_SendByte(0x40);
I2C_WaitAck();
I2C_SendByte(Data);
I2C_WaitAck();
I2C_Stop();
}
static void OLED_SetAddr(uint8_t page,uint8_t col)
{
OLED_WriteCmd(0xb0|page);//设置页
OLED_WriteCmd(((col>>4)&0x0f)|0x10);//设置列高4位
OLED_WriteCmd(0x0f&col);//设置列低4位
}
void OLED_Clear(void)
{
uint8_t x,y;
for(y=0;y<4;y++)
for(x=0;x<128;x++)
oled_data[y][x] = 0;
for(y=4;y<8;y++)
{
OLED_SetAddr(y,0);
for(x=0;x<128;x++)
{
OLED_WriteData(0x00);
}
}
}
void OLED_Update(uint8_t spage, uint8_t epage, uint8_t scol, uint8_t ecol)
{
uint8_t x,y;
for(y=spage;y<=epage;y++)
{
OLED_SetAddr(y+4,scol);
for(x=scol;x<=ecol;x++)
{
OLED_WriteData(oled_data[y][x]);
}
}
}
void OLED_String(uint8_t page, uint8_t col, uint8_t *str)
{
uint8_t i = 0;
uint8_t j = 0;
uint8_t c = *str;
while(c && (c < 0x80))
{
if(c<0x20)
c = 0x20;
else
c -= 0x20;
if(page<4)
{
if((col+i+5)<128)
{
for(j=0; j<5; j++)
oled_data[page][col+i++] = ASCII0507[5 * c + j];
}
}
c = *++str;
}
OLED_Update(page, page, col, col + i);
}
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_2 | GPIO_Pin_3);
DelayUS(10000);
OLED_WriteCmd(0xae); //关显示
OLED_WriteCmd(0xd5); //晶振频率
OLED_WriteCmd(0x80);
OLED_WriteCmd(0xa8); //duty 设置
OLED_WriteCmd(0x3f); //duty=1/64
OLED_WriteCmd(0xd3); //显示偏移
OLED_WriteCmd(0x00);
OLED_WriteCmd(0xa1); //段重定向设置
OLED_WriteCmd(0xa6);
OLED_WriteCmd(0x40); //起始行
OLED_WriteCmd(0xb0);
OLED_WriteCmd(0xd5);
OLED_WriteCmd(0xf0);
OLED_WriteCmd(0x8d); //升压允许
OLED_WriteCmd(0x14);
OLED_WriteCmd(0x20); //page address mode
OLED_WriteCmd(0x02);
OLED_WriteCmd(0xc8); //行扫描顺序:从上到下
OLED_WriteCmd(0xa1); //列扫描顺序:从左到右
OLED_WriteCmd(0xda); //sequential configuration
OLED_WriteCmd(0x02); // ****************
OLED_WriteCmd(0x81); //微调对比度,本指令的 0x81 不要改动,改下面的值
OLED_WriteCmd(0xcf); //微调对比度的值,可设置范围 0x00~0xff
OLED_WriteCmd(0xd9); //Set Pre-Charge Period
OLED_WriteCmd(0xf1);
OLED_WriteCmd(0xdb); //Set VCOMH Deselect Level
OLED_WriteCmd(0x49);
// OLED_WriteCmd(0x40);
OLED_WriteCmd(0xaf); //开显示
OLED_Clear(); //初始清屏
OLED_Update(1,2,20,100);
}