I know there are tons of comments and sites around with people looking for some code for the STM8. Lets thank Nick DeRoller for contributing this code to us all.
// Run LCD from STM8L sample code
// Runs 2x16 charicter display in 4-bit mode
// Utilizes waits
// Created by: Jason Lopez
// Edited for STM8L by: Nick DeRoller
// Hardware Connections:
// E -> PB0
// R/W -> PB1
// DB4 -> PB4
// DB5 -> PB5
// DB6 -> PB6
// DB7 -> PB7
// Requires 'stm8l15x_gpio.c'
// All other drivers may be excluded from the build to
// speed build time.
// Compiled using Raisonance
#include "stm8l15x.h"
#define LCD_PORT GPIOB
#define LCD_RS GPIO_Pin_0
#define LCD_E GPIO_Pin_1
#define GPIO_HIGH(a,b) a->ODR|=b
#define GPIO_LOW(a,b) a->ODR&=~b
void DelayMS(int delay);
void initLCD(void);
void LCD_DATA(unsigned char varData,unsigned char varType);
void LCD_NYB(unsigned char nyb, char varType);
void LCD_LINE(char line);
void LCD_STR(const unsigned char *text);
void main(void) {
GPIO_DeInit(LCD_PORT);
GPIO_Init(LCD_PORT, GPIO_Pin_All, GPIO_Mode_Out_PP_Low_Fast);
initLCD();
LCD_LINE(1);
LCD_STR(" AtomSoftTech ");
DelayMS(100);
LCD_LINE(2);
LCD_STR(" Jason Lopez ");
DelayMS(100);
while(1);
}
void LCD_STR(const unsigned char *text){
while(*text){
LCD_DATA(*text++,1);
}
}
void initLCD(void){
GPIO_LOW(LCD_PORT,LCD_E); //clear enable
GPIO_LOW(LCD_PORT,LCD_RS); //going to write command
DelayMS(30); //delay for LCD to initialise.
LCD_NYB(0x03,0); //Required for initialisation
DelayMS(5); //required delay
LCD_NYB(0x03,0); //Required for initialisation
DelayMS(1); //required delay
LCD_DATA(0x02,0); //set to 4 bit interface, 1 line and 5*7 font
LCD_DATA(0x28,0); //set to 4 bit interface, 2 line and 5*10 font
LCD_DATA(0x0c,0); //set to 4 bit interface, 2 line and 5*7 font
LCD_DATA(0x01,0); //clear display
LCD_DATA(0x06,0); //move cursor right after write
}
void LCD_DATA(unsigned char varData,unsigned char varType)
{
DelayMS(10); //TEST LCD FOR BUSY
LCD_NYB(varData >> 4,varType); //WRITE THE UPPER NIBBLE
LCD_NYB(varData,varType); //WRITE THE LOWER NIBBLE
}
void LCD_NYB(unsigned char nyb, char varType){
unsigned char temp;
temp = (nyb<<4) & 0xF0;
GPIO_Write(LCD_PORT,temp);
if(varType == 0){
GPIO_LOW(LCD_PORT,LCD_RS); //COMMAND MODE
} else {
GPIO_HIGH(LCD_PORT,LCD_RS); //CHARACTER/DATA MODE
}
GPIO_HIGH(LCD_PORT,LCD_E); //ENABLE LCD DATA LINE
DelayMS(1); //SMALL DELAY
GPIO_LOW(LCD_PORT,LCD_E); //DISABLE LCD DATA LINE
}
void LCD_LINE(char line){
switch(line){
case 0:
case 1:
LCD_DATA(0x80,0);
break;
case 2:
LCD_DATA(0xC0,0);
break;
}
}
void DelayMS(int delay){
int nCount = 0;
while (delay != 0){
nCount = 100;
while (nCount != 0){
nCount--;
}
delay--;
}
}