Jump to content

Lcd Control And Replacement Of Sprintf Command

Recommended Posts



I'm quit new to programming with C, I've done some basic stuff at school couple of years ago but it never was my thing.

Well after a few years it's still whispering in my ears that i should learn how to program.


For school I;ve bought the Sourceboost Boostc Pro 6.xx license. And still own this license.

Found out that a lot has changed since the version I had (6.40) so updated to 6.97 latest version of 6.xx


At the moment i;m just playing around to learn how to program. Only walking in to two problems at this moment.

I'm using the lcd driver from lika.be http://lika.be/wp/2005/08/boostc-compiler-libraries/


Well I can send commands to the LCD. that all worked fine.

But what I can't figure out is how to move the cursor to a specific place at the display. gotoxy doesnt seem to work.


The next problem I walked in to was that i could not place the interrupt tmr0 result at the display.

Well I;ve found a way to convert it so it shows the value at the lcd now. But it's using way to much rom of my PIC16F627A about 35% just for the conversion.

Are there any replacement commands for the sprintf conversion to show the value on the display?


This is the code I;ve made so far:


#pragma DATA 0x2007, 0x2118    
#pragma CLOCK_FREQ 4000000

#include <system.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd_test.h"
#include "lcd.h"

unsigned int rx_out;

void init(void)             // Configureren van de settings
    trisa = 00100000b;        // Stel de in / uitgangen op port A in.
    trisb = 00000001b;        // Stel de in / uitgangen op port B in.
    porta = 00000000b;        // Alle pennen poort a zijn laag
    portb = 00000000b;        // Alle pennen poort b zijn laag
    //rx_out = 0;
    //Configuratie Timer0
    option_reg.T0CS = 0;     // 0 = Gebruik systeemklok, 1 = Gebruik klok op pin T0CKI
    option_reg.T0SE = 0;      // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low
    option_reg.PSA = 0;        // 0 = Prescaler voor Timer0, 1 = Prescaler voor WatchDogTimer WDT
    option_reg.PS2 = 0;        // PS0 - PS2 Prescaler waarde zie datasheet
    option_reg.PS1 = 1;
    option_reg.PS0 = 0;
    option_reg.INTEDG = 1;    // Interrupt als portb.0 ? wordt.
    set_bit(intcon,GIE);    // Interrupts activeren.
    set_bit(intcon,INTE);    // Interrupt PortB.0 is actief = 1, Uitgeschakeld = 0.
    // LCD interface
    lcd_init();                            // LCD initialiseren lcd.c / lcd.h / lcd_test.h

    lcd_send_cmd(LINE1);                // Stuur LCD naar regel 1
    lcd_printf("Test 1");                // Plaats de volgende tekst op het lcd
    lcd_send_cmd(LINE2);                // Stuur LCD naar regel 2
    lcd_printf("Regel 2");                // Plaats de volgende tekst op het lcd
    delay_s(2);                            // Wacht 3 seconden (start op scherm)    

void main()
    init();                                    // Eerst alle settings goed zetten
    char buffer[5];                            // Buffer aanmaken voor conversie naar LCD
    lcd_send_cmd(DISP_CLR);                    // Display leegmaken
    while(1)                                // Start van de programma Loop
            sprintf(buffer, "%5d", rx_out); // Conversie int naar string
            lcd_send_cmd(LINE1);            // Stuur LCD naar regel 1
            lcd_printf(buffer);                // Plaats de volgende tekst op het lcd
            delay_ms(10);                    // wacht 0,1 seconde voor refresh

void interrupt( void )                        // Interrupt routine
    if(test_bit(intcon,INTF))                // Controleer algemene interrupt vlag
        if(option_reg.INTEDG == 1)            // Controleer status portb.0 interrupt
                tmr0 = 0;                    // Reset tmr0.
                intcon.T0IE = 1;            //
                option_reg.INTEDG = 0;        // Zet portb.0 interrupt op laag
                intcon.INTF = 0;            // Reset interrupt vlag
                rx_out = tmr0;                // Schrijf waarde tmr0 weg naar rx_out
                intcon.T0IE = 0;            //
                option_reg.INTEDG = 1;        // Zet portb.0 interrupt op hoog
                intcon.INTF = 0;            // Reset interrupt vlag

I've tried the dutch forum www.circuitsonline.nl for help and also have tried a lot of things myself but i seem pretty stuck for now.

Link to post
Share on other sites


The "sprintf" function is very heavy, as it must deal with a very large number of formating options.
Its one of those things to be "used only at gun point" :) I do avoid it like the plague.

What you need is simple binary to ASCII convertion.

This can be achieved using the "itoa()" or "ltoa()" functions that are way smaller than sprintf.



I also use, depending on project specific needs, the shift - add 3 its a quite simple and easy to implement algorithm.

And if I need ASCII I just have to add the "3" to the high nibble in the end to convert the BCD digits to ASCII so they can be displayed on an LCD.


EDIT: BoostC ships with a special set of even smaller (lighter) functions like "uitoa_dec()" and others, see "Lightweight Conversion Functions" at page 81 of the BoostC help.






Best regards
PS: I have some sample code to the "shift - add 3" algorithm but it was written for a PIC24 device using Microchip XC16 that supports "long long" integers, so it can't be directly used with boostC.

Edited by JorgeF
Link to post
Share on other sites




hello jorgeF,


thank you for your answer, I tried to use the uitoa function but couldn't get it to work on the lcd.


So if you have any example code maybe i could find what i've did wrong.


This statement ...

uitoa_dec(buffer, (unsigned int)tmr0, 3);

... will use less than 150 code instructions to give you a nice clean ASCII string in buffer representing the value of TIMER0..

It is much lighter than "sprintf", after that you only need to send it to the LCD.


As for the LCD it looks like you are using a ready made set of functions, so its not easy to give specific support.

As a generic information I can tell you that, at least for HD44780 compatible LCDs, the cursor is positioned using the memory address of the display buffer.

This memory addresses are sequencial in one line but not from the end of line 1 to the beginning of line 2, you have to double check the specs for your specific LCD.


Have you tried the BoostC "lcd functions" (page 94 of BoostC reference manual).


I never used, neither the default BoostC libs nor others for writing to an LCD.

I allways do it the hard way, write my own code against the PIC pins for the first time so I can understand how things work.

After that, I use a lib or my own code depending on wich yields better size/speed results on a project by project basis.



For the moment my main working PC is inop, but if you get really stuck I will try to find some code scraps that you can use.



Best regards


Edited by JorgeF
Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...