2 Wire LCD with CD4094 Lib translation

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

2 Wire LCD with CD4094 Lib translation

#1 Post by Soumitrab » 14 Dec 2014 15:34

Hi all
I tried to port this lib to Mikropascal but perhaps i've missed out something coz well, it doesn't work on my setup :-)
The lib is here : http://www.libstock.com/projects/view/6 ... ing-cd4094
And the port is as below. The hardware connections are the same as in the libstock schematic. Any help with this would be hugely appreciated.

Code: Select all

unit TwoWireLCD;

const
  _LCD_FIRST_ROW         = 0x80;         //Move cursor to the 1st row
  _LCD_SECOND_ROW        = 0xC0;         //Move cursor to the 2nd row
  _LCD_THIRD_ROW         = 0x94;         //Move cursor to the 3rd row
  _LCD_FOURTH_ROW        = 0xD4;         //Move cursor to the 4th row
  _LCD_CLEAR             = 0x01;         //Clear display
  _LCD_RETURN_HOME       = 0x02;         //Return cursor to home position, returns a shifted display to
                                                 //its original position. Display data RAM is unaffected.
  _LCD_CURSOR_OFF        = 0x0C;         //Turn off cursor
  _LCD_UNDERLINE_ON      = 0x0E;         //Underline cursor on
  _LCD_BLINK_CURSOR_ON   = 0x0F;          //Blink cursor on
  _LCD_MOVE_CURSOR_LEFT  = 0x10;          //Move cursor left without changing display data RAM
  _LCD_MOVE_CURSOR_RIGHT = 0x14;           //Move cursor right without changing display data RAM
  _LCD_TURN_ON           = 0x0C;         //Turn Lcd display on
  _LCD_TURN_OFF          = 0x08;         //Turn Lcd display off
  _LCD_SHIFT_LEFT        = 0x18;         //Shift display left without changing display data RAM
  _LCD_SHIFT_RIGHT       = 0x1E;         //Shift display right without changing display data RAM

var 
  //LCD_clk: sbit; sfr; external;
  //LCD_data: sbit; sfr; external;
  LCD_clk: sbit at RB0_bit;
  LCD_data: sbit at RB1_bit;


procedure SerialLcd_WriteNibble(d: byte);
procedure Serial_Lcd_Cmd(const Command: byte);
procedure Serial_LCD_Chr(row, col: byte; LCDChar: byte);
procedure Serial_Lcd_Init();
procedure Serial_LCD_Out(row, col: byte; var str: string);

implementation

var 
  rs: byte;

procedure SerialLcd_WriteNibble(d: byte);
var
  i: byte;
begin
  LCD_clk := 0;
  LCD_data := 0;
  for i := 0 to 7 do
  begin
    LCD_clk := 1;
    Delay_Us(10);
    LCD_clk := 0;
    Delay_Us(10);
  end;

  LCD_data := 1;                                        //set Q7= 1 for E
  LCD_clk := 1;
  Delay_Us(10);
  LCD_clk := 0;
  Delay_Us(10);

  LCD_data := rs;                                //set Q6 for RS
  LCD_clk := 1;
  Delay_Us(10);
  LCD_clk := 0;
  Delay_Us(10);

  i := 8;
  while i > 0 do //8;i>0;i=i>>1)
  begin
    i := i shr 1;
    if (d and i)=1 then
      LCD_data := 1
    else
      LCD_data := 0;
    Delay_Us(10);
    LCD_clk := 1;
    Delay_Us(10);
    LCD_clk := 0;

  end;

  LCD_data := 0;                        //shift Q0
  Delay_Us(10);
  LCD_clk := 1;
  Delay_Us(10);
  LCD_clk := 0;

  LCD_data := 1;                //now send enable
  Delay_Us(10);
  LCD_data := 0;
   Delay_Us(10);
end;

procedure Serial_Lcd_Cmd(Command: byte);
begin
  rs := 0; //because sending command

  SerialLcd_WriteNibble(Command shr 4);
  Delay_Us(100);
  SerialLcd_WriteNibble(Command and 0xF);

  if((Command = 0x0C) or (Command = 0x01) or (Command = 0x0E) or (Command = 0x0F) or (Command = 0x10)
                   or (Command = 0x1E) or (Command = 0x18) or (Command = 0x08) or (Command = 0x14)
                   or (Command = 0x02)) then
      Delay_ms(50);

end;

procedure Serial_LCD_Chr(row, col: byte; LCDChar: byte);
begin
  case (row) of
    1: Serial_Lcd_Cmd(0x80 + col-1);
    2: Serial_Lcd_Cmd(0xC0 + col-1);
    3: Serial_Lcd_Cmd(0x94 + col-1);
    4: Serial_Lcd_Cmd(0xD4 + col-1);
  end;

  RS := 1;
  SerialLcd_WriteNibble(LCDChar shr 4);
  Delay_us(10);
  SerialLcd_WriteNibble(LCDChar and 0x0F);
end;

procedure Serial_Lcd_Init();
begin
  LCD_Clk := 0;
  LCD_data := 0;

  rs := 0;
  delay_ms(40);
  Serial_Lcd_Cmd(0x03);
  delay_ms(5);
  Serial_Lcd_Cmd(0x03);
  delay_ms(5);
  Serial_Lcd_Cmd(0x03);
  delay_ms(2);
  //SerialLcd_WriteNibble(0x02);
  Serial_Lcd_Cmd(0x02);


  Serial_Lcd_Cmd(0x28);   //set LCD_data length 4 bit 2 line
  delay_ms(50);
  Serial_Lcd_Cmd(0x0C);   // set display on cursor oFF blink off to on the cursor E to and on the blink F
  delay_ms(50);
  // Serial_Lcd_Cmd(0x01); // clear lcd
  //delay_ms(250);
  Serial_Lcd_Cmd(0x06);  // cursor shift direction
  delay_ms(50);
  Serial_Lcd_Cmd(0x0C);  //set ram address
  delay_ms(50);
end;

procedure Serial_LCD_Out(row, col: byte; var str: string);
var
 p, q, c: byte;
begin
 q := strlen(str);
 c := col;
 for p := 0 to q-1 do
 begin

   Serial_LCD_Chr(row,c,str[p]);
   c := c+1;
 end;
 row := 1;
 col := 1;
end;
end.

jayanthd
Posts: 630
Joined: 08 May 2013 18:31
Location: Bangalore

Re: 2 Wire LCD with CD4094 Lib translation

#2 Post by jayanthd » 18 Dec 2014 16:38

Hi

I am the one who made this http://www.libstock.com/projects/view/6 ... ing-cd4094

I am Jayanth Devarayanadurga.

Please zip and post your mikro Pascal project here. I will fix the code.
[HW:] EasyPIC v7, mikroProg PIC, AVR, STM32, PSoC, Tiva, 8051
[SW:] mikroC PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroBasic PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroPascal PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
Visual GLCD, Visual TFT

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: 2 Wire LCD with CD4094 Lib translation

#3 Post by Soumitrab » 18 Dec 2014 17:41

Hi Jayanth
Thanks for dropping in and helping out. I've attached the code as asked.
Would be good to get this going.

Thanks and regards
Attachments
ThreeWire16x2.zip
(46.42 KiB) Downloaded 187 times

jayanthd
Posts: 630
Joined: 08 May 2013 18:31
Location: Bangalore

Re: 2 Wire LCD with CD4094 Lib translation

#4 Post by jayanthd » 19 Dec 2014 05:42

Hi Soumitrab,

I have fixed the code and it is working in Proteus. I have not tested the Pascal version in hardware. Test it and reply.
Attachments
Two Wire LCD.rar
(101.54 KiB) Downloaded 202 times
2-Wire LCD using CD4094.png
2-Wire LCD using CD4094.png (54.22 KiB) Viewed 5993 times
[HW:] EasyPIC v7, mikroProg PIC, AVR, STM32, PSoC, Tiva, 8051
[SW:] mikroC PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroBasic PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroPascal PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
Visual GLCD, Visual TFT

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: 2 Wire LCD with CD4094 Lib translation

#5 Post by Soumitrab » 19 Dec 2014 10:03

Thanks Jayanth, working perfectly now ! :)

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: 2 Wire LCD with CD4094 Lib translation

#6 Post by Soumitrab » 19 Dec 2014 12:11

Hi Jayanth
Btw, there is a small hitch with how this works. Apparently it does not quite advance beyond 2 lines and prints gibberish when after the delay. Not sure why...
In this:

Code: Select all

    Serial_LCD_Out(1,1,'Serial LCD');         // Write text in first row
      Serial_LCD_Out(2,1,'2 Wire');             // Write text in second row
//uptil here things print fine
      delay_ms(2000);
      Serial_LCD_Cmd(_LCD_CLEAR);
      Serial_LCD_Out(1,1,'Using');         // Write text in first row
      Serial_LCD_Out(2,1,'CD4094');
//this does not print as expected but prints gibberish

jayanthd
Posts: 630
Joined: 08 May 2013 18:31
Location: Bangalore

Re: 2 Wire LCD with CD4094 Lib translation

#7 Post by jayanthd » 20 Dec 2014 12:26

What LCD are you using > 16X2, 16X4, 20X4 ? Is it HD44780 Compatible. Are you saying in 20X4 LCD lines 3 and 4 doesn't display properly but display gibberish ?
[HW:] EasyPIC v7, mikroProg PIC, AVR, STM32, PSoC, Tiva, 8051
[SW:] mikroC PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroBasic PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroPascal PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
Visual GLCD, Visual TFT

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: 2 Wire LCD with CD4094 Lib translation

#8 Post by Soumitrab » 20 Dec 2014 16:13

Im using a 16x2 LCD. On startup the first 2 lines appear fine but i cannot subsequently update the lines with new text after sending the _LCD_CLEAR command. No matter what text i send the LCD, li get gibberish...in the sense the alphanumeric characters are visible but not pertaining to the text that was sent.

jayanthd
Posts: 630
Joined: 08 May 2013 18:31
Location: Bangalore

Re: 2 Wire LCD with CD4094 Lib translation

#9 Post by jayanthd » 20 Dec 2014 16:34

I tested in Proteus and it works fine. Increase delays to 20 us or 50 us in Write_Nibble() function and try. Zip and post your project file which is giving error. Compile and attach the complete project zipped. I will see if your compiler is generating wrong .hex file or not. Maybe compiler files are corrupted.
[HW:] EasyPIC v7, mikroProg PIC, AVR, STM32, PSoC, Tiva, 8051
[SW:] mikroC PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroBasic PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroPascal PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
Visual GLCD, Visual TFT

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: 2 Wire LCD with CD4094 Lib translation

#10 Post by Soumitrab » 25 Dec 2014 18:22

Yes increasing the delays to 20us makes it work just great. Everything AOK with the setup now !
Thanks a lot ! :)

Post Reply

Return to “User Projects”