IIC

(转)在转一篇针对u8glib单色屏库+SSD1306 的使用介绍

Using the u8glib on the STM32
The u8glib is a really nice library to use when you don’t want to mess around with different diplay protocols or drawing routines. It is well-conceived and easy to use. Unfortunately there has been no port for STM32 microcontrollers so I tried to do this.

In the following example I will use the library to display some text on a SSD1306 display via I2C. The controller I use is the STM32F101CBT6 and the IDE will be Atollic True Studio for ARM Lite.

You should start by downloading the u8glib for arm. This is the C version of the library which I will use since you can only compile C code with the Lite version of my IDE.

The next step is to implement the entire project into your code. So I created a new folder and just draged in the „src“ folder from the archive.

Now some low level routines have to be implemented to allow the library to talk to the display device. The following routines have to be created:
  • Delay by n microseconds
  • Delay by 10 microseconds
  • Delay by 1 microsecond
  • Doing a Reset on the display
  • Writing a byte to the display
  • Writing a sequence of bytes to the display
  • Switching from data to command mode or vice versa

To do so we create a new source file called „u8g_arm.c“ and a header file „u8g_arm.h„.

In the header file you should include the original u8g.h header file so basically just do:
#ifndef _U8G_ARM_H
#define _U8G_ARM_H

//adjust this path:
#include "u8glib/u8g.h"

//main com function. read on...
uint8_t u8g_com_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr);

#endif
The most important procedure in these files will be the one that is called by the u8g library with data to send to the display device. This communication procedure looks like this. I added comments for every important statement. Add it to the file „u8g_arm.c“.
uint8_t u8g_com_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
{

switch(msg)
{
case U8G_COM_MSG_STOP:
//STOP THE DEVICE
break;

case U8G_COM_MSG_INIT:
//INIT HARDWARE INTERFACES, TIMERS, GPIOS...
break;

case U8G_COM_MSG_ADDRESS:
//SWITCH FROM DATA TO COMMAND MODE (arg_val == 0 for command mode)
break;

case U8G_COM_MSG_RESET:
//TOGGLE THE RESET PIN ON THE DISPLAY BY THE VALUE IN arg_val
break;

case U8G_COM_MSG_WRITE_BYTE:
//WRITE BYTE TO DEVICE
break;

case U8G_COM_MSG_WRITE_SEQ:
case U8G_COM_MSG_WRITE_SEQ_P:
//WRITE A SEQUENCE OF BYTES TO THE DEVICE
break;

}
return 1;
}
Now just add code to every case of the switch statement. For my communication with I2C it looks like this:
uint8_t u8g_com_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
{

switch(msg)
{
case U8G_COM_MSG_STOP:
break; //do nothing...

case U8G_COM_MSG_INIT:
i2c_init(); //inits the i2c hardware, gpios and timers
u8g_MicroDelay();
break;

case U8G_COM_MSG_ADDRESS:
//the control byte switches the mode on the device and is set here
if (arg_val == 0)
{
control = 0;
}
else
{
control = 0x40;
}
u8g_10MicroDelay();
break;

case U8G_COM_MSG_RESET:
//pin 9 is my reset pin
GPIO_WriteBit(GPIOB, GPIO_Pin_9, arg_val);
u8g_10MicroDelay();
break;

case U8G_COM_MSG_WRITE_BYTE:
//simple: just write one byte
i2c_out(arg_val);
u8g_MicroDelay();
break;

case U8G_COM_MSG_WRITE_SEQ:
case U8G_COM_MSG_WRITE_SEQ_P:
{
register uint8_t *ptr = arg_ptr;
//send the control byte (to switch from command to data mode)
I2C_start(SSD1306_I2C_ADDRESS, I2C_Direction_Transmitter);
I2C_SendData(I2C2, control);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

//now send the rest of data
while( arg_val > 0 )
{
I2C_SendData(I2C2, *ptr++);
arg_val--;
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
//done
I2C_stop();
u8g_MicroDelay();
}
break;

}
return 1;
}
My routines to  write and read data via I2C are basically copied from this nice article. If you want the full code please contact me or write a comment since I don’t want to publish it because of bad style.

Ok so now we have the low level hardware communication set up. Now it is time to really enjoy the amazing u8g library.

To tell u8glib to use our communication function we just have to call it like this.
My routines to  write and read data via I2C are basically copied from this nice article. If you want the full code please contact me or write a comment since I don’t want to publish it because of bad style.

Ok so now we have the low level hardware communication set up. Now it is time to really enjoy the amazing u8g library.

To tell u8glib to use our communication function we just have to call it like this.
I hope you noticed that the third parameter to this function is our hardware routine.

That is basically all you have to do. Now you can use all functions of the u8glib. Please remind that you use the C library of the project so you cannot directly copy code from the tutorials.
 
>>引用自:http://blog.bastelhalde.de/?p=759

0 个评论

要回复文章请先登录注册