STM8L Char-LCD Code: Contributed by Nick DeRoller

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--;
    }
}

Advertisements

Tags: , ,

About atomsoft

Starting a small company. Trying to build it from ground up. Only 2 employees so far. Nothing serious yet but soon!

3 responses to “STM8L Char-LCD Code: Contributed by Nick DeRoller”

  1. Ida says :

    A magazine theme would make ur blog look nicer 🙂

  2. xorkrus (@xorkrus) says :

    Hi!

    #error clnk Debug\newlcd.lkf:1 symbol _GPIO_DeInit not defined (Debug\main.o )
    #error clnk Debug\newlcd.lkf:1 symbol _GPIO_Init not defined (Debug\main.o )
    #error clnk Debug\newlcd.lkf:1 symbol _GPIO_Write not defined (Debug\main.o )

    wtf?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: