Hi,
I am still developing a code to run the GLCD by using two PCF8574AN IO Expanders.
The main problem i have at the moment is that the programm 'freezes' at a random point after being started.
Is there someone who might want to help me to sort some of the problems out? The code is based on the original KS0108 code in the Playground.
ks0108.h
/*
ks0108.h - Arduino library support for ks0108 and compatable graphic LCDs
Copyright (c)2008 Michael Margolis All right reserved
mailto:memargolis@hotmail.com?subject=KS0108_Library
This library is based on version 1.1 of the excellent ks0108 graphics routines written and
copyright by Fabian Maximilian Thiele. His sitelink is
dead but you can obtain a copy of his original work here:
http://www.scienceprog.com/wp-content/uploads/2007/07/glcd_ks0108.zip
Code changes include conversion to an Arduino C++ library, adding more
flexibility in port addressing and improvements in I/O speed. The interface
has been made more Arduino friendly and some convenience functions added.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Version: 1.0 - May 8 2008 - initial release
Version: 1.0a - Sept 1 2008 - simplified command pin defines
Version: 1.0b - Sept 18 2008 - replaced <wiring.h> with boolean typedef for rel 0012
*/
#include <inttypes.h>
//#include <wiring.h> // for boolean
typedef uint8_t boolean;
typedef uint8_t byte;
#include <avr/pgmspace.h>
#ifndef KS0108_H
#define KS0108_H
/*****************************************************************/
/* Configuration for assigning LCD pins to the PCF8574 */
/*****************************************************************/
//Control Chip
#define TWI1_ADDRESS B0111000 //0x38 //(LLL)
//Data Chip
#define TWI2_ADDRESS B0111001 //0x39 //(LLH)
//The defined bits correspond to the digital pins on the PCF8574. Check the Datasheet for further information.
#define CSEL1 0 // CS1 Bit // swap pin assignments with CSEL2 if left/right image is reversed
#define CSEL2 1 // CS2 Bit
#define R_W 2 // R/W Bit
#define D_I 3 // D/I Bit
#define EN 4 // EN Bit
#define RES 5 // check if needed with your controller, otherwise uncomment
//The Databits of the LCD (db0 to db7) correspond to bits 0 to 7 on the second PCF8574. You only have to provide the address
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*****************************************************************/
/* Panel Configuration */
/*****************************************************************/
// Chips
#define CHIP1 0x00
#define CHIP2 0x01
#ifdef HD44102
#define CHIP_WIDTH 50 // pixels per chip
#else
#define CHIP_WIDTH 64
#endif
// Commands
#ifdef HD44102
#define LCD_ON 0x39
#define LCD_OFF 0x38
#define LCD_DISP_START 0x3E // Display start page 0
#else
#define LCD_ON 0x3F
#define LCD_OFF 0x3E
#define LCD_DISP_START 0xC0
#endif
#define LCD_SET_ADD 0x40
#define LCD_SET_PAGE 0xB8
// Colors
#define BLACK 0xFF
#define WHITE 0x00
// useful user contants
#define NON_INVERTED false
#define INVERTED true
// Font Indices
#define FONT_LENGTH 0
#define FONT_FIXED_WIDTH 2
#define FONT_HEIGHT 3
#define FONT_FIRST_CHAR 4
#define FONT_CHAR_COUNT 5
#define FONT_WIDTH_TABLE 6
#ifdef HD44102
#define DISPLAY_WIDTH 100
#define DISPLAY_HEIGHT 32
#else
#define DISPLAY_WIDTH 128
#define DISPLAY_HEIGHT 64
#endif
// Uncomment for slow drawing
// #define DEBUG
typedef struct {
uint8_t x;
uint8_t y;
uint8_t page;
} lcdCoord;
typedef uint8_t (*FontCallback)(const uint8_t*);
uint8_t ReadFontData(const uint8_t* ptr); //Standard Read Callback
#define DrawVertLine(x, y, length, color) FillRect(x, y, 0, length, color)
#define DrawHoriLine(x, y, length, color) FillRect(x, y, length, 0, color)
#define DrawCircle(xCenter, yCenter, radius, color) DrawRoundRect(xCenter-radius, yCenter-radius, 2*radius, 2*radius, radius, color)
#define ClearScreen() FillRect(0, 0, (DISPLAY_WIDTH-1), (DISPLAY_HEIGHT-1), WHITE)
class ks0108 // shell class for ks0108 glcd code
{
private:
lcdCoord Coord;
boolean Inverted; // changed type to boolean
FontCallback FontRead;
uint8_t FontColor;
const uint8_t* Font;
uint8_t ReadData(void); // TODO this was inline !!!
uint8_t DoReadData(uint8_t first);
void WriteData(uint8_t data);
void WriteCommand(uint8_t cmd, uint8_t chip);
inline void Enable(void);
public:
ks0108();
// Control functions
void Init(boolean invert);
void GotoXY(uint8_t x, uint8_t y);
// Graphic Functions
void DrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t color);
void DrawRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color);
void DrawRoundRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t radius, uint8_t color);
void FillRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color);
void InvertRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height);
void SetInverted(boolean invert);
void SetDot(uint8_t x, uint8_t y, uint8_t color);
// Font Functions
void SelectFont(const uint8_t* font, uint8_t color=BLACK, FontCallback callback=ReadFontData); // defualt arguments added, callback now last arg
int PutChar(char c);
void Puts(char* str);
void Puts_P(PGM_P str);
void PrintNumber(long n);
uint8_t CharWidth(char c);
uint16_t StringWidth(char* str);
uint16_t StringWidth_P(PGM_P str);
//Functions to make TWI work
void writeCommandTWI(int pin, int val);
byte getTWIDATA(byte address);
void setCommandTWI(uint8_t cmd);
void setDataTWI(uint8_t data);
};
extern ks0108 GLCD;
#endif