New ST7920 128x64 LCD library - need help to improve

General discussion on mikroC PRO for PIC.
Author
Message
sparks
Posts: 40
Joined: 06 Apr 2011 18:50
Location: Varna, Bulgaria

New ST7920 128x64 LCD library - need help to improve

#1 Post by sparks » 09 Dec 2012 18:58

After around 2 weeks of winning fights with the poor ST7920 hardware design and bugs, I am happy to announce that the library is complete and I am that '.' close to post it on libstock. The following function are implemented:

Code: Select all


/*************************************************************************
* Function:       ST_char8x8
* Input:            character, row, column, mask (color)
*                      row 0..7; column 0..15
*                      if mask == 0, draws normal character (white on blue background),
*                      if mask == 0xff draws an inverted character
* Overview:      Draws a 8x8 dots character on the screen
*                      Font set: CP1251 - 0x20..0x7F ASCII
*                                                   & 0xC0..0xFF Cyrillic characters
**************************************************************************/

Code: Select all

/*************************************************************************
* Function:       ST_txt8x8
* Input:            text_array, row, column, mask (color)
*                      row 0..7; column 0..15
*                      if mask == 0, draws normal text,
*                      if mask == 0xff draws an inverted text
* Overview:      Puts the LCD in graphic mode to draw 8x8 dots characters.
*                      Font set: CP1251 - 0x20..0x7F ASCII
*                                                  & 0xC0..0xFF Cyrillic characters
*                      Optimized for speed - draws all characters in the text
*                      array together, much faster than calling ST_char8x8()
*                      in a loop.
**************************************************************************/

Code: Select all

/*************************************************************************
* Function:       ST_txt8x16
* Input:          text_array, row, column
*                 row 0..1; column 0..15
* Overview:       Drives the LCD in text mode, using the internal 8x16
*                 character ROM (HCGROM) to type text. Check the ST7920
*                 datasheet for the charset.
**************************************************************************/

Code: Select all


/*************************************************************************
* Function:       ST_chr16x32
* Precondition:   Put ST7920 in EXTENDED Instruction Set before calling
* Input:          character, row, column, mask (color)
*                 row 0..1; column 0..7
*                 if mask == 0, draws normal character,
*                 if mask == 0xff draws an inverted character
* Output:         ~
* Overview:       Draws a 16x32 dots character on the screen
*                 Font set: ASCII 0x2B..0x3E (digits and some signs)
*                               & 0x3F..0x46 animated hourglass
**************************************************************************/

Code: Select all

/*************************************************************************
* Function:       ST_txt16x32
* Precondition:   ~
* Input:          character, row, column, mask (color)
*                 row 0..1; column 0..7
*                 if mask == 0, draws normal character,
*                 if mask == 0xff draws an inverted character
* Overview:       Draws 16x32 dots character on the screen
*                 Font set: ASCII 0x2B..0x3E (digits and some signs)
*                               & 0x3F..0x46 animated hourglass
**************************************************************************/

Code: Select all

/*************************************************************************
* Function:       ST_rect
* Input:          row, column, length, height, color
*                 row 0..63; column 0..15
*                 length 1..16; height 1..64
*                 if mask == 0, draws empty (black) rectangle
*                 if mask == 0xff draws filled rectangle
*                 if mask == 0xaa, 0x55, 0x0f... draws rectangle filled
*                 with stripes
* Overview:       Draws a rectangle at specified coordinates. Each column
*                 is 8 dots wide (limitation of the ST7920 design)
**************************************************************************/

Code: Select all

/*************************************************************************
* Function:       ST_dot
* Input:          x, y, color
*                 x 0..127; y 0..63
*                 if color == 1, draws a normal (white) dot
*                 if color == 0, draws an "empty" (black) dot
*                 if color == 2..255, inverts the dot at the specified
*                 coordinates
* Overview:       Draws a dot at specified coordinates.
**************************************************************************/

To make the library more flexible and simple to use, I need to know how some MikroC libraries are written. For example the Sound_Init(char *snd_port, char snd_pin) library.
As the example in the help file states Sound_Init(&PORTC, 3) will Initialize the pin RC3 for playing sound. The library somehow manages to find the corresponding TRIS (TRISC3_bit) and sets it to output. This is what I am willing to do in my library, so instead of having

Code: Select all

sbit ST_RS                 at RB0_bit;
sbit ST_RS_TRIS            at TRISB0_bit;
// and so on
i will set only

Code: Select all

sbit ST_RS                 at RB0_bit;
and the library will recognize the corresponding TRISC0_bit and set it accordingly. One solution is to check for the corresponding TRIS with a lot of if() statements, but this will make the code huge. Another way is to use

Code: Select all

#if ST_RS == RB0
    #define ST_RS_TRIS TRISB0
#endif
// and so on
but then all the pins have to be declared in .pld file or in the library itself. I don't think this is a smart solution.
Any ideas to solve this problem in a third way are very welcome.

I am also looking for people who have ST7920 128x64 LCD and are willing to test the library before release.

Greetings,
--sparks--

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: New ST7920 128x64 LCD library - need help to improve

#2 Post by janni » 09 Dec 2012 22:54

sparks wrote:To make the library more flexible and simple to use, I need to know how some MikroC libraries are written. For example the Sound_Init(char *snd_port, char snd_pin) library.
As the example in the help file states Sound_Init(&PORTC, 3) will Initialize the pin RC3 for playing sound. The library somehow manages to find the corresponding TRIS (TRISC3_bit) and sets it to output.
Because the first parameter of Sound_Init is address of PORTx, it's easy to obtain address of TRISx. Addresses of these registers differ by a constant value for a given PIC family.
From declaration of sbit one cannot so easily extract the PORTx address, or the pin number. Furthermore, when declared as external, it will be known only while linking the code file to the one containing declaration of sbit.

sparks
Posts: 40
Joined: 06 Apr 2011 18:50
Location: Varna, Bulgaria

Re: New ST7920 128x64 LCD library - need help to improve

#3 Post by sparks » 10 Dec 2012 02:18

janni wrote:Because the first parameter of Sound_Init is address of PORTx, it's easy to obtain address of TRISx. Addresses of these registers differ by a constant value for a given PIC family.
Thanks for the tip, janni! Actually I thought about it too, but the LATx, PORTx and TRISx are in a same bank at equal offsets only in PIC18 family, while in PIC16 they are in different banks.
If I could only check for a declared constant inside an #if statement, or if the compiler could optimize the code so, that when it finds

Code: Select all

const char c = 5;

if(c == 10){ 
    // do something
}
to omit all the code between {}.

Greetings,
--sparks--

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: New ST7920 128x64 LCD library - need help to improve

#4 Post by janni » 10 Dec 2012 02:54

sparks wrote:Actually I thought about it too, but the LATx, PORTx and TRISx are in a same bank at equal offsets only in PIC18 family, while in PIC16 they are in different banks.
And still equidistant :) .
If I could only check for a declared constant inside an #if statement, or if the compiler could optimize the code so, that when it finds

Code: Select all

const char c = 5;

if(c == 10){ 
    // do something
}
to omit all the code between {}.
Compiler does remove dead code. The whole if statement will produce zero code.

sparks
Posts: 40
Joined: 06 Apr 2011 18:50
Location: Varna, Bulgaria

Re: New ST7920 128x64 LCD library - need help to improve

#5 Post by sparks » 10 Dec 2012 03:45

janni wrote:Compiler does remove dead code. The whole if statement will produce zero code.
It does not remove dead code if a constant is declared externally and it's being checked inside extern function. Please check the attached example project and tell me how it compiles.
Attachments
passing constant.zip
(7.24 KiB) Downloaded 376 times

Greetings,
--sparks--

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: New ST7920 128x64 LCD library - need help to improve

#6 Post by janni » 10 Dec 2012 13:58

Yes, with external constant declaration the code stays. Linker does not remove the dead code, only compiler does and, while compiling the file, external constant is undetermined.

sparks
Posts: 40
Joined: 06 Apr 2011 18:50
Location: Varna, Bulgaria

Re: New ST7920 128x64 LCD library - need help to improve

#7 Post by sparks » 10 Dec 2012 21:15

Thanks for the comments, janni!

Greetings,
--sparks--

kniffeldi
Posts: 1
Joined: 13 Jan 2013 13:09

Re: New ST7920 128x64 LCD library - need help to improve

#8 Post by kniffeldi » 13 Jan 2013 13:48

Hello sparks,

this library looks very intresting.
Where can i download this library?
I will port this library to the stm32 and gcc compiler.

Thanks kniffeldi

sparks
Posts: 40
Joined: 06 Apr 2011 18:50
Location: Varna, Bulgaria

Re: New ST7920 128x64 LCD library - need help to improve

#9 Post by sparks » 15 Jan 2013 16:36

kniffeldi wrote:Hello sparks,

this library looks very intresting.
Where can i download this library?
I will port this library to the stm32 and gcc compiler.

Thanks kniffeldi
I'll try to find some free time next week to tidy it up and post it on libstock.

Greetings,
--sparks--

frikino
Posts: 7
Joined: 05 Mar 2013 16:45

Re: New ST7920 128x64 LCD library - need help to improve

#10 Post by frikino » 05 Mar 2013 17:09

excuse me, Have you posted the library yet?

sparks
Posts: 40
Joined: 06 Apr 2011 18:50
Location: Varna, Bulgaria

Re: New ST7920 128x64 LCD library - need help to improve

#11 Post by sparks » 07 Mar 2013 10:42

frikino wrote:excuse me, Have you posted the library yet?
Not yet, but it's complete. Need some time to shoot a small video, presenting it.
If you need it, leave a p.m. with your e-mail and I'll send it to you.

Greetings,
--sparks--

frikino
Posts: 7
Joined: 05 Mar 2013 16:45

Re: New ST7920 128x64 LCD library - need help to improve

#12 Post by frikino » 07 Mar 2013 15:43

ok!:)

kristian
Posts: 69
Joined: 17 Mar 2013 15:48

Re: New ST7920 128x64 LCD library - need help to improve

#13 Post by kristian » 18 Mar 2013 13:48

I also need the library. I'll leave you a p.m. thanks

zzzlep
Posts: 1
Joined: 21 Apr 2013 17:45

Re: New ST7920 128x64 LCD library - need help to improve

#14 Post by zzzlep » 21 Apr 2013 17:51

Hye- i also need that library~ how can i get it
? Thnks

kostas08
Posts: 1
Joined: 20 May 2013 22:42

Re: New ST7920 128x64 LCD library - need help to improve

#15 Post by kostas08 » 20 May 2013 22:54

can you PM me your email? I really need this library too :)

Thanks!

Post Reply

Return to “mikroC PRO for PIC General”