The Godfather talking
You can run, but you can't hide.
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 05, 2016, 05:22:58 05:22


Login with username, password and session length


Pages: [1]
Print
Author Topic: KS0108 Base Library for STM32F4 series  (Read 5071 times)
0 Members and 1 Guest are viewing this topic.
Cynetron
Newbie
*
 Muted
Offline Offline

Posts: 24

Thank You
-Given: 21
-Receive: 30


« on: January 07, 2012, 04:57:48 16:57 »

I'm working on STM32F4 and needed GLCD Library but i couldn't find and wrote one
i try to write adaptable with other tools and system integrations, so you can develop as your wishes
Note: try to use systick for timing, so you can configure easy, thats why i put delay routines seperate
so enjoy...

ks0108.c
Code:
/*
128x64 KS0108 LCD Driver for STM32F4
*/
#include "ks0108.h"
#include "font.h"
#include "delay.h"

/* Global Variables */
volatile unsigned char x_ks0108, y_ks0108;

/* Functions */

void strobe_ks0108(void){
delay_us(1); /* Delay 1us */
KS0108_EN = 1; /* Strobe */
delay_us(1); /* Delay 1us */
KS0108_EN = 0; /* Strobe */
delay_us(1); /* Delay 1us */
}

void wait_busy_ks0108(void){
KS0108_PORT_DIR(KS0108_PORT_INPUT); /* Change Port Direction */
KS0108_DI = 0; /* Instruction Mode */
KS0108_RW = 1; /* Read Mode */
strobe_ks0108(); /* LCD Strobe */
while((KS0108_DATA&0x7F)==0x80); /* Chek Busy Flag */
KS0108_RW = 0; /* Write Mode */
KS0108_PORT_DIR(KS0108_PORT_OUTPUT); /* Change Port Direction */
}

void putcmd_ks0108(char ins){
wait_busy_ks0108(); /* Wait Until LCD not Busy */
KS0108_DI = 0; /* Instruction Mode */
KS0108_LDATA(ins); /* Load Instruction to Port */
strobe_ks0108(); /* LCD Strobe */
}

unsigned char getd_ks0108(void){
char data;
KS0108_PORT_DIR(KS0108_PORT_INPUT); /* Set Port Direction */
KS0108_DI = 1; /* Data Mode */
KS0108_RW = 1; /* Read Mode */
strobe_ks0108(); /* LCD Strobe */
data = KS0108_DATA; /* Read LCD Data */
KS0108_RW = 0; /* Write Mode */
KS0108_PORT_DIR(KS0108_PORT_OUTPUT); /* Set Port Direction */
return data;
}

void putd_ks0108(unsigned char data){
if(x_ks0108==0){
KS0108_L(); /* Select Chip */
putcmd_ks0108(KS0108_Y_ADRESS|(y_ks0108/8));
putcmd_ks0108(KS0108_X_ADRESS);
}else if(x_ks0108==64){
KS0108_R(); /* Select Chip */
putcmd_ks0108(KS0108_Y_ADRESS|(y_ks0108/8));
putcmd_ks0108(KS0108_X_ADRESS);
}
wait_busy_ks0108(); /* Wait Until LCD not Busy */
KS0108_DI = 1; /* Data Mode */
KS0108_LDATA(data); /* Load Data to Port */
strobe_ks0108(); /* LCD Strobe */
x_ks0108++;
if(x_ks0108>127){x_ks0108=0;y_ks0108+=8;}
if(y_ks0108>63){y_ks0108=0;}
}

void cls_ks0108(void){
unsigned int i;
x_ks0108 = 0;y_ks0108 = 0;
for(i=0;i<1024;i++)putd_ks0108(0x00);
x_ks0108 = 0;y_ks0108 = 0;
}

void init_ks0108(void){

KS0108_RST_DIR(KS0108_PIN_OUTPUT);
KS0108_RW_DIR(KS0108_PIN_OUTPUT);
KS0108_DI_DIR(KS0108_PIN_OUTPUT);
KS0108_EN_DIR(KS0108_PIN_OUTPUT);
KS0108_CS1_DIR(KS0108_PIN_OUTPUT);
KS0108_CS2_DIR(KS0108_PIN_OUTPUT);
KS0108_PORT_DIR(KS0108_PORT_OUTPUT); /* Set Port Direction */

KS0108_DI = 0; /* Instruction Mode */
KS0108_RW = 0; /* Write Mode */
KS0108_N(); /* Deselect Chips */
KS0108_LDATA(0x00); /* Load Data to Port */
KS0108_EN = 0;

KS0108_RST = 0;
delay_ms(2); /* Delay 2ms */
KS0108_RST = 1;
delay_ms(1); /* Delay 1ms */

KS0108_L(); /* Select Chip */
putcmd_ks0108(KS0108_DISPLAY_OFF);
putcmd_ks0108(KS0108_START_LINE);
putcmd_ks0108(KS0108_X_ADRESS);
putcmd_ks0108(KS0108_Y_ADRESS);
putcmd_ks0108(KS0108_DISPLAY_ON);

KS0108_R(); /* Select Chip */
putcmd_ks0108(KS0108_DISPLAY_OFF);
putcmd_ks0108(KS0108_START_LINE);
putcmd_ks0108(KS0108_X_ADRESS);
putcmd_ks0108(KS0108_Y_ADRESS);
putcmd_ks0108(KS0108_DISPLAY_ON);

cls_ks0108();
}

void gotoxy_ks0108(unsigned char x, unsigned char y){
x_ks0108 = x;
y_ks0108 = y;
if(x_ks0108<64){
KS0108_L(); /* Select Chip */
putcmd_ks0108(KS0108_Y_ADRESS|(y_ks0108/8));
putcmd_ks0108(KS0108_X_ADRESS|x_ks0108);
}else{
KS0108_R(); /* Select Chip */
putcmd_ks0108(KS0108_Y_ADRESS|(y_ks0108/8));
putcmd_ks0108(KS0108_X_ADRESS|(x_ks0108-64));
}
}

void putc_ks0108(unsigned char c){
int i;
switch(c){
case '\f':cls_ks0108();break;
case '\n':gotoxy_ks0108(0,y_ks0108+8);break;
case '\r':gotoxy_ks0108(0,y_ks0108);break;
default:if(x_ks0108>122)gotoxy_ks0108(0,y_ks0108+8);
for(i = 0; i < 5; i++)putd_ks0108(Font[(((c-0x20)*5) + i)]);
putd_ks0108(0x00);
break;
}

}

void puts_ks0108(unsigned char *s){
do{
putc_ks0108(*s);
}while(*++s);
}

ks0108.h
Code:
/*
128x64 KS0108 LCD Driver for STM32F4
*/

#include <stm32f4xx.h>

/* LCD Connection Descriptions */
#define KS0108_RST_PIN 0
#define KS0108_RST_PORT GPIOB_BASE

#define KS0108_RW_PIN 1
#define KS0108_RW_PORT GPIOB_BASE

#define KS0108_DI_PIN 2
#define KS0108_DI_PORT GPIOB_BASE

#define KS0108_EN_PIN 11
#define KS0108_EN_PORT GPIOB_BASE

#define KS0108_CS1_PIN 4
#define KS0108_CS1_PORT GPIOC_BASE

#define KS0108_CS2_PIN 5
#define KS0108_CS2_PORT GPIOC_BASE

#define KS0108_DT_PIN 8
#define KS0108_DT_PORT GPIOE_BASE

/* LCD Definitions */
#define KS0108_Y_ADRESS 0xB8 /* Swapped with X address for real coordinate system */
#define KS0108_X_ADRESS 0x40 /* Swapped with Y address for real coordinate system */
#define KS0108_START_LINE 0xC0
#define KS0108_DISPLAY_ON 0x3F
#define KS0108_DISPLAY_OFF 0x3E

/* Port Definitions */
#define KS0108_PIN_INPUT 0x00
#define KS0108_PIN_OUTPUT 0x01
#define KS0108_PORT_INPUT 0x00000000
#define KS0108_PORT_OUTPUT 0x00005555

#define KS0108_RST (*(volatile unsigned short*)( 0x42000000U | ((KS0108_RST_PORT+0x14) << 5) | ((KS0108_RST_PIN << 2)))) /* ODR */
#define KS0108_RW (*(volatile unsigned short*)( 0x42000000U | ((KS0108_RW_PORT+0x14) << 5) | ((KS0108_RW_PIN << 2)))) /* ODR */
#define KS0108_DI (*(volatile unsigned short*)( 0x42000000U | ((KS0108_DI_PORT+0x14) << 5) | ((KS0108_DI_PIN << 2)))) /* ODR */
#define KS0108_EN (*(volatile unsigned short*)( 0x42000000U | ((KS0108_EN_PORT+0x14) << 5) | ((KS0108_EN_PIN << 2)))) /* ODR */
#define KS0108_CS1 (*(volatile unsigned short*)( 0x42000000U | ((KS0108_CS1_PORT+0x14) << 5) | ((KS0108_CS1_PIN << 2)))) /* ODR */
#define KS0108_CS2 (*(volatile unsigned short*)( 0x42000000U | ((KS0108_CS2_PORT+0x14) << 5) | ((KS0108_CS2_PIN << 2)))) /* ODR */

#define KS0108_RST_DIR(x) ((*(volatile unsigned int*)KS0108_RST_PORT)=((*(volatile unsigned int*)KS0108_RST_PORT)&~(0x03U<<(KS0108_RST_PIN*2)))|(x<<(KS0108_RST_PIN*2))) /* MODER */
#define KS0108_RW_DIR(x) ((*(volatile unsigned int*)KS0108_RW_PORT)=((*(volatile unsigned int*)KS0108_RW_PORT)&~(0x03U<<(KS0108_RW_PIN*2)))|(x<<(KS0108_RW_PIN*2))) /* MODER */
#define KS0108_DI_DIR(x) ((*(volatile unsigned int*)KS0108_DI_PORT)=((*(volatile unsigned int*)KS0108_DI_PORT)&~(0x03U<<(KS0108_DI_PIN*2)))|(x<<(KS0108_DI_PIN*2))) /* MODER */
#define KS0108_EN_DIR(x) ((*(volatile unsigned int*)KS0108_EN_PORT)=((*(volatile unsigned int*)KS0108_EN_PORT)&~(0x03U<<(KS0108_EN_PIN*2)))|(x<<(KS0108_EN_PIN*2))) /* MODER */
#define KS0108_CS1_DIR(x) ((*(volatile unsigned int*)KS0108_CS1_PORT)=((*(volatile unsigned int*)KS0108_CS1_PORT)&~(0x03U<<(KS0108_CS1_PIN*2)))|(x<<(KS0108_CS1_PIN*2))) /* MODER */
#define KS0108_CS2_DIR(x) ((*(volatile unsigned int*)KS0108_CS2_PORT)=((*(volatile unsigned int*)KS0108_CS2_PORT)&~(0x03U<<(KS0108_CS2_PIN*2)))|(x<<(KS0108_CS2_PIN*2))) /* MODER */
#define KS0108_PORT_DIR(x) ((*(volatile unsigned int*)KS0108_DT_PORT)= \
((*(volatile unsigned int*)KS0108_DT_PORT)&~(0x0000FFFFU<<(KS0108_DT_PIN*2)))|(x<<(KS0108_DT_PIN*2))) /* MODER */
#define KS0108_L() (KS0108_CS1=0, KS0108_CS2=1)
#define KS0108_R() (KS0108_CS1=1, KS0108_CS2=0)
#define KS0108_N() (KS0108_CS1=0, KS0108_CS2=0)
#define KS0108_DATA ((unsigned char)(((*((volatile unsigned short*)KS0108_DT_PORT+0x10)&(0x00FFU<<KS0108_DT_PIN)))>>KS0108_DT_PIN)) /* IDR */
#define KS0108_LDATA(x) ((*(volatile unsigned short*)(KS0108_DT_PORT+0x14)) = \
((*(volatile unsigned short*)(KS0108_DT_PORT+0x14))&~(0x00FFU<<KS0108_DT_PIN))|(x<<KS0108_DT_PIN)) /* ODR */

/* Public Functions */
extern void init_ks0108(void);
extern void gotoxy_ks0108(unsigned char, unsigned char);
extern void putc_ks0108(unsigned char);
extern void puts_ks0108(unsigned char*);
extern void cls_ks0108(void);

font.h
Code:
static const unsigned char Font[]= {
0x00, 0x00, 0x00, 0x00, 0x00,// (space)
0x00, 0x00, 0x5F, 0x00, 0x00,// !
0x00, 0x07, 0x00, 0x07, 0x00,// "
0x14, 0x7F, 0x14, 0x7F, 0x14,// #
0x24, 0x2A, 0x7F, 0x2A, 0x12,// $
0x23, 0x13, 0x08, 0x64, 0x62,// %
0x36, 0x49, 0x55, 0x22, 0x50,// &
0x00, 0x05, 0x03, 0x00, 0x00,// '
0x00, 0x1C, 0x22, 0x41, 0x00,// (
0x00, 0x41, 0x22, 0x1C, 0x00,// )
0x08, 0x2A, 0x1C, 0x2A, 0x08,// *
0x08, 0x08, 0x3E, 0x08, 0x08,// +
0x00, 0x50, 0x30, 0x00, 0x00,// ,
0x08, 0x08, 0x08, 0x08, 0x08,// -
0x00, 0x30, 0x30, 0x00, 0x00,// .
0x20, 0x10, 0x08, 0x04, 0x02,// /
0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
0x00, 0x42, 0x7F, 0x40, 0x00,// 1
0x42, 0x61, 0x51, 0x49, 0x46,// 2
0x21, 0x41, 0x45, 0x4B, 0x31,// 3
0x18, 0x14, 0x12, 0x7F, 0x10,// 4
0x27, 0x45, 0x45, 0x45, 0x39,// 5
0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
0x01, 0x71, 0x09, 0x05, 0x03,// 7
0x36, 0x49, 0x49, 0x49, 0x36,// 8
0x06, 0x49, 0x49, 0x29, 0x1E,// 9
0x00, 0x36, 0x36, 0x00, 0x00,// :
0x00, 0x56, 0x36, 0x00, 0x00,// ;
0x00, 0x08, 0x14, 0x22, 0x41,// <
0x14, 0x14, 0x14, 0x14, 0x14,// =
0x41, 0x22, 0x14, 0x08, 0x00,// >
0x02, 0x01, 0x51, 0x09, 0x06,// ?
0x32, 0x49, 0x79, 0x41, 0x3E,// @
0x7E, 0x11, 0x11, 0x11, 0x7E,// A
0x7F, 0x49, 0x49, 0x49, 0x36,// B
0x3E, 0x41, 0x41, 0x41, 0x22,// C
0x7F, 0x41, 0x41, 0x22, 0x1C,// D
0x7F, 0x49, 0x49, 0x49, 0x41,// E
0x7F, 0x09, 0x09, 0x01, 0x01,// F
0x3E, 0x41, 0x41, 0x51, 0x32,// G
0x7F, 0x08, 0x08, 0x08, 0x7F,// H
0x00, 0x41, 0x7F, 0x41, 0x00,// I
0x20, 0x40, 0x41, 0x3F, 0x01,// J
0x7F, 0x08, 0x14, 0x22, 0x41,// K
0x7F, 0x40, 0x40, 0x40, 0x40,// L
0x7F, 0x02, 0x04, 0x02, 0x7F,// M
0x7F, 0x04, 0x08, 0x10, 0x7F,// N
0x3E, 0x41, 0x41, 0x41, 0x3E,// O
0x7F, 0x09, 0x09, 0x09, 0x06,// P
0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
0x7F, 0x09, 0x19, 0x29, 0x46,// R
0x46, 0x49, 0x49, 0x49, 0x31,// S
0x01, 0x01, 0x7F, 0x01, 0x01,// T
0x3F, 0x40, 0x40, 0x40, 0x3F,// U
0x1F, 0x20, 0x40, 0x20, 0x1F,// V
0x7F, 0x20, 0x18, 0x20, 0x7F,// W
0x63, 0x14, 0x08, 0x14, 0x63,// X
0x03, 0x04, 0x78, 0x04, 0x03,// Y
0x61, 0x51, 0x49, 0x45, 0x43,// Z
0x00, 0x00, 0x7F, 0x41, 0x41,// [
0x02, 0x04, 0x08, 0x10, 0x20,// "\"
0x41, 0x41, 0x7F, 0x00, 0x00,// ]
0x04, 0x02, 0x01, 0x02, 0x04,// ^
0x40, 0x40, 0x40, 0x40, 0x40,// _
0x00, 0x01, 0x02, 0x04, 0x00,// `
0x20, 0x54, 0x54, 0x54, 0x78,// a
0x7F, 0x48, 0x44, 0x44, 0x38,// b
0x38, 0x44, 0x44, 0x44, 0x20,// c
0x38, 0x44, 0x44, 0x48, 0x7F,// d
0x38, 0x54, 0x54, 0x54, 0x18,// e
0x08, 0x7E, 0x09, 0x01, 0x02,// f
0x08, 0x14, 0x54, 0x54, 0x3C,// g
0x7F, 0x08, 0x04, 0x04, 0x78,// h
0x00, 0x44, 0x7D, 0x40, 0x00,// i
0x20, 0x40, 0x44, 0x3D, 0x00,// j
0x00, 0x7F, 0x10, 0x28, 0x44,// k
0x00, 0x41, 0x7F, 0x40, 0x00,// l
0x7C, 0x04, 0x18, 0x04, 0x78,// m
0x7C, 0x08, 0x04, 0x04, 0x78,// n
0x38, 0x44, 0x44, 0x44, 0x38,// o
0x7C, 0x14, 0x14, 0x14, 0x08,// p
0x08, 0x14, 0x14, 0x18, 0x7C,// q
0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x48, 0x54, 0x54, 0x54, 0x20,// s
0x04, 0x3F, 0x44, 0x40, 0x20,// t
0x3C, 0x40, 0x40, 0x20, 0x7C,// u
0x1C, 0x20, 0x40, 0x20, 0x1C,// v
0x3C, 0x40, 0x30, 0x40, 0x3C,// w
0x44, 0x28, 0x10, 0x28, 0x44,// x
0x0C, 0x50, 0x50, 0x50, 0x3C,// y
0x44, 0x64, 0x54, 0x4C, 0x44,// z
0x00, 0x08, 0x36, 0x41, 0x00,// {
0x00, 0x00, 0x7F, 0x00, 0x00,// |
0x00, 0x41, 0x36, 0x08, 0x00,// }
0x08, 0x08, 0x2A, 0x1C, 0x08,// ->
0x08, 0x1C, 0x2A, 0x08, 0x08 // <-
};

delay.c
Code:
#include "delay.h"

void delay_ns(unsigned int d){ /* Not exact value, for 168 MHz @ Level3 Opt */
unsigned int i;
i=d/25;while(i)i--;
}

void delay_us(unsigned int d){ /* for 168 MHz @ Level3 Opt */
unsigned int i,j;
i=d;while(i){i--;j=55;while(j)j--;}
}

void delay_ms(unsigned int d){ /* for 168 MHz @ Level3 Opt */
unsigned int i;
i=d;while(i){i--;delay_us(998);}
}

delay.h
Code:
#include <stm32f4xx.h>

extern void delay_ns(unsigned int);
extern void delay_us(unsigned int);
extern void delay_ms(unsigned int);
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC