Как и обещал выкладываю простой пример.
Ранее писал о нестабильности работы, оказалось что при чтении регистра статуса в 4-ех битном режиме работы необходимо обязательно его полностью, я читал только старшие 4 бита для проверки флага занятости (бит 7).
Описание индикатора брал тут , исполнение отличается от моего, но логика работы аналогична.
Так как питание индикатора 5 вольт проверял его работу с платой STM8S-Discovery на основе шаблона описанного ранее .
Подключение линий ввода-вывода:
#define OLED_2X16_E E, 7, HIGH, OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ
#define OLED_2X16_RW E, 3, HIGH, OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ
#define OLED_2X16_RS D, 0, HIGH, OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ
#define OLED_2X16_DB7 B, 7, HIGH, OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ
#define OLED_2X16_DB6 B, 5, HIGH, OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ
#define OLED_2X16_DB5 B, 3, HIGH, OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ
#define OLED_2X16_DB4 B, 1, HIGH, OUTPUT_PUSH_PULL_SPEED_LIMIT_10MHZ
#define OLED_2X16_DB7_IN B, 7, HIGH, INPUT_FLOATING_WITHOUT_ITERRUPT
#define OLED_2X16_DB6_IN B, 5, HIGH, INPUT_FLOATING_WITHOUT_ITERRUPT
#define OLED_2X16_DB5_IN B, 3, HIGH, INPUT_FLOATING_WITHOUT_ITERRUPT
#define OLED_2X16_DB4_IN B, 1, HIGH, INPUT_FLOATING_WITHOUT_ITERRUPT
Инициализируем, устанавливаем курсор (можно было и не делать этого) и отображаем символ А.
int main ( void )
{
oled_2x16_init ();
oled_2x16_set_cursor_position ( 0 , 0 );
oled_2x16_print_symbol ( 'A' );
while ( 1 )
{
}
return 0 ;
}
Копи-паст кода:
/*
* File: oled_2x16.c
* Date: 28.08.2011
* Denis Zheleznjakov @ ZiBlog.ru
*/
#include "main.h"
//------------------------------------------------------------------------------
static void write_4bit ( uint8_t data )
{
PIN_OFF ( OLED_2X16_DB7 );
PIN_OFF ( OLED_2X16_DB6 );
PIN_OFF ( OLED_2X16_DB5 );
PIN_OFF ( OLED_2X16_DB4 );
PIN_ON ( OLED_2X16_E );
if ( data & 0x80 )
PIN_ON ( OLED_2X16_DB7 );
if ( data & 0x40 )
PIN_ON ( OLED_2X16_DB6 );
if ( data & 0x20 )
PIN_ON ( OLED_2X16_DB5 );
if ( data & 0x10 )
PIN_ON ( OLED_2X16_DB4 );
PIN_OFF ( OLED_2X16_E );
}
//------------------------------------------------------------------------------
static uint8_t read_4bit ( void )
{
uint8_t result = 0 ;
PIN_ON ( OLED_2X16_E );
if ( PIN_SIGNAL ( OLED_2X16_DB7_IN ))
result = 0x80 ;
if ( PIN_SIGNAL ( OLED_2X16_DB6_IN ))
result |= 0x40 ;
if ( PIN_SIGNAL ( OLED_2X16_DB5_IN ))
result |= 0x20 ;
if ( PIN_SIGNAL ( OLED_2X16_DB4_IN ))
result |= 0x10 ;
PIN_OFF ( OLED_2X16_E );
return result ;
}
//-----------------------------------------------------------------------------
static void wait_ready ( void )
{
PIN_OFF ( OLED_2X16_RS );
PIN_CONFIGURATION ( OLED_2X16_DB7_IN );
PIN_CONFIGURATION ( OLED_2X16_DB6_IN );
PIN_CONFIGURATION ( OLED_2X16_DB5_IN );
PIN_CONFIGURATION ( OLED_2X16_DB4_IN );
PIN_ON ( OLED_2X16_RW );
while (( read_4bit () & 0x80 ) != 0x00 )
{
read_4bit ();
}
read_4bit ();
PIN_OFF ( OLED_2X16_RW );
PIN_CONFIGURATION ( OLED_2X16_DB7 );
PIN_CONFIGURATION ( OLED_2X16_DB6 );
PIN_CONFIGURATION ( OLED_2X16_DB5 );
PIN_CONFIGURATION ( OLED_2X16_DB4 );
}
//------------------------------------------------------------------------------
static void write_instruction ( uint8_t instruction )
{
PIN_OFF ( OLED_2X16_RS );
write_4bit ( instruction );
write_4bit (( uint8_t ) ( instruction >> 4 ));
}
//------------------------------------------------------------------------------
static void write_data ( uint8_t data )
{
PIN_ON ( OLED_2X16_RS );
write_4bit ( data );
write_4bit (( uint8_t ) ( data >> 4 ));
}
//-----------------------------------------------------------------------------
void oled_2x16_set_cursor_position ( uint8_t row , uint8_t colum )
{
write_instruction (( uint8_t ) ( 0x80 | ( row > 0 ? 0x40 : 0 ) | colum ));
}
//-----------------------------------------------------------------------------
void oled_2x16_print_symbol ( uint8_t symbol )
{
write_data ( symbol );
}
//-----------------------------------------------------------------------------
void oled_2x16_print_string ( uint8_t * string )
{
while ( * string != 0x00 )
{
write_data ( * string ++ );
}
}
//------------------------------------------------------------------------------
void oled_2x16_init ( void )
{
PIN_CONFIGURATION ( OLED_2X16_E );
PIN_CONFIGURATION ( OLED_2X16_RW );
PIN_CONFIGURATION ( OLED_2X16_RS );
PIN_OFF ( OLED_2X16_E );
PIN_OFF ( OLED_2X16_RW );
PIN_OFF ( OLED_2X16_RS );
PIN_CONFIGURATION ( OLED_2X16_DB7 );
PIN_CONFIGURATION ( OLED_2X16_DB6 );
PIN_CONFIGURATION ( OLED_2X16_DB5 );
PIN_CONFIGURATION ( OLED_2X16_DB4 );
write_4bit ( 0x20 );
write_4bit ( 0x20 );
write_4bit ( 0xA0 );
wait_ready ();
write_4bit ( 0x00 );
write_4bit ( 0xC0 );
wait_ready ();
write_4bit ( 0x00 );
write_4bit ( 0x10 );
wait_ready ();
write_4bit ( 0x00 );
write_4bit ( 0x60 );
wait_ready ();
}