-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patholed_i2c.c
311 lines (258 loc) · 8.83 KB
/
oled_i2c.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// oled_i2c.c
#include "oled_i2c.h"
#include "tm4c123gh6pm.h"
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
int result=0;
void OLED_Write_Float(float f)
{
char* buf11;
// int status;
sprintf( buf11, "%f", f );
OLED_Write_String(buf11);
OLED_Write_String( " " );
}
//int main(void)
//{
/* int count = 0;
// float dec = 0.0;
I2C3_Init();
OLED_Init();
OLED_Clear();
// variables for counting
while ( 1 ) {
/////////////////////
// Strings
///////////////////
OLED_YX( 0, 0 );
OLED_Write_String( "OLED SSD1306" );
Delay_ms(1000);
OLED_YX(1, 0);
OLED_Write_String ("TM4C123");
Delay_ms(1000);
/////////////////////
// Integer Count
////////////////////
for (count = 0; count <= 100; count++){
OLED_YX(2, 0 );
OLED_Write_String( "Count:" );
OLED_YX(3, 2);
OLED_Write_Integer(count);
Delay_ms(100);
}
OLED_Clear();
Delay_ms(100);
}*/
// }
// I2C intialization and GPIO alternate function configuration
void I2C3_Init ( void )
{
SYSCTL_RCGCGPIO_R |= 0x00000008 ; // Enable the clock for port D
SYSCTL_RCGCI2C_R |= 0x00000008 ; // Enable the clock for I2C 3
GPIO_PORTD_DEN_R |= 0x03; // Assert DEN for port D
// Configure Port D pins 0 and 1 as I2C 3
GPIO_PORTD_AFSEL_R |= 0x00000003 ;
GPIO_PORTD_PCTL_R|= 0x00000033 ;
GPIO_PORTD_ODR_R |= 0x00000002 ; // SDA (PD1 ) pin as open darin
I2C3_MCR_R = 0x0010 ; // Enable I2C 3 master function
/* Configure I2C 3 clock frequency
(1 + TIME_PERIOD ) = SYS_CLK /(2*
( SCL_LP + SCL_HP ) * I2C_CLK_Freq )
TIME_PERIOD = 16 ,000 ,000/(2(6+4) *100000) - 1 = 7 */
I2C3_MTPR_R = 0x07 ;
}
/* wait untill I2C Master module is busy */
/* and if not busy and no error return 0 */
static int I2C_wait_till_done(void)
{
while(I2C3_MCS_R & 1); /* wait until I2C master is not busy */
return I2C3_MCS_R & 0xE; /* return I2C error code, 0 if no error*/
}
char I2C3_Wr(int slaveAddr, char memAddr, uint8_t data)
{
char error;
/* send slave address and starting address */
I2C3_MSA_R = slaveAddr << 1;
I2C3_MDR_R = memAddr;
I2C3_MCS_R = 3; /* S-(saddr+w)-ACK-maddr-ACK */
error = I2C_wait_till_done(); /* wait until write is complete */
if (error) return error;
/* send data */
I2C3_MDR_R = data;
I2C3_MCS_R = 5; /* -data-ACK-P */
error = I2C_wait_till_done(); /* wait until write is complete */
while(I2C3_MCS_R & 0x40); /* wait until bus is not busy */
error = I2C3_MCS_R & 0xE;
if (error) return error;
return 0; /* no error */
}
// Receive one byte of data from I2C slave device
char I2C3_Write_Multiple(int slave_address, char slave_memory_address, int bytes_count, uint8_t* data)
{
char error;
if (bytes_count <= 0)
return -1; /* no write was performed */
/* send slave address and starting address */
I2C3_MSA_R = slave_address << 1;
I2C3_MDR_R = slave_memory_address;
I2C3_MCS_R = 3; /* S-(saddr+w)-ACK-maddr-ACK */
error = I2C_wait_till_done(); /* wait until write is complete */
if (error) return error;
/* send data one byte at a time */
while (bytes_count > 1)
{
I2C3_MDR_R = *data++; /* write the next byte */
I2C3_MCS_R = 1; /* -data-ACK- */
error = I2C_wait_till_done();
if (error) return error;
bytes_count--;
}
/* send last byte and a STOP */
I2C3_MDR_R = *data++; /* write the last byte */
I2C3_MCS_R = 5; /* -data-ACK-P */
error = I2C_wait_till_done();
while(I2C3_MCS_R & 0x40); /* wait until bus is not busy */
if (error) return error;
return 0; /* no error */
}
//OLED
void OLED_Command( uint8_t temp){
I2C3_Wr(0x3C,0x00,temp);
}
/*******************************************************************************
* Function: void OLED_Data ( uint8_t temp)
*
* Returns: Nothing
*
* Description: sends data to the OLED
*
******************************************************************************/
void OLED_Data( uint8_t temp){
I2C3_Wr(0x3C,0x40,temp);
}
/*******************************************************************************
* Function: void OLED_Init ()
*
* Returns: Nothing
*
* Description: Initializes OLED
*
******************************************************************************/
void OLED_Init() {
OLED_Command(OLED_DISPLAYOFF); // 0xAE
OLED_Command(OLED_SETDISPLAYCLOCKDIV); // 0xD5
OLED_Command(0x80); // the suggested ratio 0x80
OLED_Command(OLED_SETMULTIPLEX); // 0xA8
OLED_Command(0x1F);
OLED_Command(OLED_SETDISPLAYOFFSET); // 0xD3
OLED_Command(0x0); // no offset
OLED_Command(OLED_SETSTARTLINE | 0x0); // line #0
OLED_Command(OLED_CHARGEPUMP); // 0x8D
OLED_Command(0xAF);
OLED_Command(OLED_MEMORYMODE); // 0x20
OLED_Command(0x00); // 0x0 act like ks0108
OLED_Command(OLED_SEGREMAP | 0x1);
OLED_Command(OLED_COMSCANDEC);
OLED_Command(OLED_SETCOMPINS); // 0xDA
OLED_Command(0x02);
OLED_Command(OLED_SETCONTRAST); // 0x81
OLED_Command(0x8F);
OLED_Command(OLED_SETPRECHARGE); // 0xd9
OLED_Command(0xF1);
OLED_Command(OLED_SETVCOMDETECT); // 0xDB
OLED_Command(0x40);
OLED_Command(OLED_DISPLAYALLON_RESUME);// 0xA4
OLED_Command(OLED_NORMALDISPLAY); // 0xA6
OLED_Command(OLED_DISPLAYON); //--turn on oled panel
}
/*******************************************************************************
* Function: void OLED_YX(unsigned char Row, unsigned char Column)
*
* Returns: Nothing
*
* Description: Sets the X and Y coordinates
*
******************************************************************************/
void OLED_YX(unsigned char Row, unsigned char Column)
{
OLED_Command( 0xB0 + Row);
OLED_Command( 0x00 + (8*Column & 0x0F) );
OLED_Command( 0x10 + ((8*Column>>4)&0x0F) );
}
/*******************************************************************************
* Function: void OLED_PutChar(char ch)
*
* Returns: Nothing
*
* Description: Writes a character to the OLED
*
******************************************************************************/
void OLED_PutChar(char ch )
{
uint8_t bytes[9];
const uint8_t *base = &OledFont[ch - 32][0];
if ( ( ch < 32 ) || ( ch > 127 ) ){
ch = ' ';
}
//bytes[0] = 0x40;
memmove( bytes + 1, base, 8 );
I2C3_Write_Multiple(0x3C,0x40,9,bytes);
}
/*******************************************************************************
* Function: void OLED_Clear()
*
* Returns: Nothing
*
* Description: Clears the OLED
*
******************************************************************************/
void OLED_Clear()
{
uint16_t row;
uint16_t col ;
for ( row = 0; row < 8; row++ ) {
for ( col = 0; col < 16; col++ ) {
OLED_YX( row, col );
OLED_PutChar(' ');
}
}
}
/*******************************************************************************
* Function: void OLED_Write_String( char *s )
*
* Returns: Nothing
*
* Description: Writes a string to the OLED
*
******************************************************************************/
void OLED_Write_String( char *s )
{
while (*s) OLED_PutChar( *s++);
}
/*******************************************************************************
* Function: void OLED_Write_Integer ( uint8_t i )
*
* Returns: Nothing
*
* Description: Writes an integer to the OLED
*
******************************************************************************/
void OLED_Write_Integer(uint8_t i)
{
char s[20];
sprintf( s, "%d", i );
OLED_Write_String( s );
OLED_Write_String( " " );
}
/*******************************************************************************
* Function: void OLED_Write_Float( float f )
*
* Returns: Nothing
*
* Description: Writes a float to the OLED
*
******************************************************************************/
int x;