Shop OBEX P1 Docs P2 Docs Learn Events
anyone can tell me how to make a automatic newline on the 16*2LCD display ? — Parallax Forums

anyone can tell me how to make a automatic newline on the 16*2LCD display ?

daniel dingdaniel ding Posts: 52
edited 2011-11-12 05:01 in Propeller 1
Hi everyone, I am going to use a ps2 keybard to input words or characters to the 16*2LCD.
But I don't know how to make a automatic newline.
who can give me some adivices or help, thanks!

Comments

  • JonnyMacJonnyMac Posts: 9,198
    edited 2011-10-01 05:23
    You need to keep track of your cursor position and when you get to the end of a line you a cursor position command to move to the next line.
  • daniel dingdaniel ding Posts: 52
    edited 2011-10-01 06:29
    Jonny Mac,
    I know it, but how to track the cursor. You know I should read the value of the AC, but how to do it?
    en----, could you give me a example~thanks a lot!
  • LeonLeon Posts: 7,620
    edited 2011-10-01 06:48
    You know where the cursor is when the program starts (at position zero), just increment a variable every time you display a character.
  • JonnyMacJonnyMac Posts: 9,198
    edited 2011-10-01 09:27
    And if you lose track, you can always read the cursor position from the display. You need to be using the RW pin to do this, so you can put the buss into read mode. If you look at my LCD object

    http://obex.parallax.com/objects/474/

    ...you'll see that the waitbusy method returns the current cursor position.
  • daniel dingdaniel ding Posts: 52
    edited 2011-10-02 03:46
    thanks a lot,
  • daniel dingdaniel ding Posts: 52
    edited 2011-10-02 05:10
    Leon,


    con
    _clkmode=xtal1+pll8x
    _xinfreq=5_000_000
    obj
    key:"keyboard"
    lcd:"lcdroutines4"

    pub main |temp,counter,index ,x
    lcd.initialize_lcd
    key.start(16,17)
    repeat

    case key.getkey


    13:lcd. position(2,1)
    42:lcd.print(string("*"))
    43:lcd.print(string("+"))
    45:lcd.print(string("-"))
    46:lcd.print(string("."))
    47:lcd.print(string("/"))
    48:lcd.print_dec(0)
    49:lcd.print_dec(1)
    50:lcd.print_dec(2)
    51:lcd.print_dec(3)
    52:lcd.print_dec(4)
    53:lcd.print_dec(5)
    54:lcd.print_dec(6)
    55:lcd.print_dec(7)
    56:lcd.print_dec(8)
    57:lcd.print_dec(9)
    200:lcd.shiftleft
    lcd.space(1)
    lcd.shiftleft
    192:lcd.shiftleft
    193:lcd.shiftright
    194:lcd.home
    waitcnt(clkfreq/100+cnt)

    It is my program, I use a keyboard to input numbers and characters, can you tell me how and where add a parameter for a cursor add?
    Thanks!
  • Mike GMike G Posts: 2,702
    edited 2011-10-02 06:37
    What about adding a counter to your case logic that increments when a character is printed.
    case key.getkey
    
      13:lcd. position(2,1)
      42:lcd.print(string("*"))
           count++
    
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-10-02 06:41
    Hi daniel, the "case" instruction can be expanded and additional statements can be added.

    Knowing where the cursor should be after each expression you can add, subtract or pre define a value.
     192:lcd.shiftleft
          if cursor>0
            cursor-=1
     193:lcd.shiftright
          if cursor<32
            cursor+=1
     194:lcd.home 
          cursor:=0
    

    Jeff T.
  • daniel dingdaniel ding Posts: 52
    edited 2011-10-02 07:30
    Mike G,

    Thanks for your reply. But do you mean that I should add a "counter++" every "lcd.print(......)".
    just like

    42:lcd.print(string("*"))
    count++
    43:lcd.print(string("+"))
    count++
    45:lcd.print(string("-"))
    count++
    46:lcd.print(string("."))
    count++
    47:lcd.print(string("/"))
    count++

    additions can you tell me how to make a code display just like yours reply.

    I am really a new guy to this forum, if you can tell me how to make a photo for me just like your guitar I will appreciate it very much.

    Thanks;
  • Mike GMike G Posts: 2,702
    edited 2011-10-02 07:47
    Surround your code block with code tags.... [ code ] your code [ / code ]. See the BB code list for more fun with tags. Add an avatar by editing your forum profile.
    Thanks for your reply. But do you mean that I should add a "counter++" every "lcd.print(......)".
    just like
    Yes, I'm sure there is a more elegant method but that's the idea. The count provides line and position with simple math. (count // 16) // 2 = line 0 or 1; (count // 16) = character position in a line. I believe that's right.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2011-10-02 07:49
    Daniel, why are you using single characters as strings? Why wouldn't you just say:

    lcd.print(string("*+-./"))

    Then you would have print also increment the count although it is better to have a count for the X or character position and another for the Y or line position. For every printing character you would increment the X count and compare it with the maximum length of the display. If it reaches the end of the line you would implement the equivalent of a CR LF sequence and depending upon you LCD it may need to have the internal cursor position translated from you X and Y.
    I have just written a full blown 20x4 LCD driver complete with big digits and emulation for a popular serial display as well so I could give you lot's of tips but perhaps you should think more about the task before asking questions as you will learn a lot more.

    If you need more help then it's fairly customary (and necessary) for you to post your code plus some overview of what you are trying to achieve by this exercise.
  • daniel dingdaniel ding Posts: 52
    edited 2011-10-02 19:20
    con
      _clkmode=xtal1+pll8x
      _xinfreq=5_000_000
    obj
      key:"keyboard"
      lcd:"lcdroutines4"
    
    pub main |temp,counter,index ,x
      lcd.initialize_lcd
           
       
    
      key.start(16,17)
    
      
           
    repeat
               
              
               case  key.newkey
               
                  
                 13:lcd. position(2,1) 
                                   
                 32:lcd.space(1)
                                      
                 42:lcd.print(string("*"))
                    counter++
                 43:lcd.print(string("+"))
                 45:lcd.print(string("-"))
                 46:lcd.print(string("."))
                 47:lcd.print(string("/"))
                 102:lcd.print(string("F"))
                 103:lcd.print(string("G"))  
                 120:lcd.print(string("X"))  
                 121:lcd.print(string("Y"))  
                 48:lcd.print_dec(0)
                 49:lcd.print_dec(1)
                 50:lcd.print_dec(2)
                 51:lcd.print_dec(3)
                 52:lcd.print_dec(4)
                 53:lcd.print_dec(5)
                 54:lcd.print_dec(6)
                 55:lcd.print_dec(7)
                 56:lcd.print_dec(8)
                 57:lcd.print_dec(9)
                 200:lcd.shiftleft
                      lcd.space(1)
                      lcd.shiftleft
                 192:lcd.shiftleft
                 193:lcd.shiftright
                 194:lcd.home              
                                   
                     
                                
            
          waitcnt(clkfreq/100+cnt)
    
    these are my codes, the numbers are the key's decimal number. For one "+" who's dec-number is 43, so I set that when we get a 43 lcd will display a "+".
    Your advice of using "lcd.print(string("_,+,*,."))" is good, but how to distinguish the dec-number. Can you give me some help.
  • daniel dingdaniel ding Posts: 52
    edited 2011-10-02 19:37
    :smile::smile:,hahah !
    Thanks very very very much.

    Though your method is not elegant enough, but it is very effective. I am very glad to it.

    As a Chinese student, it is really a hard work to read these English words. But I am adapting myself to it. Thanks again.

    additions, I got another problem here http://forums.parallax.com/showthread.php?134888-anyone-who-can-tell-me-how-to-achieve-this-function-using-a-16*2lcd-and-a-keyboard%26%2365311%3B&p=1040539#post1040539.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-10-02 19:37
    Daniel,

    Jon's object includes an "out" method.

    If you're only displaying one character you can use "lcd.out("+")" (or "lcd.out(43)") instead of using "lcd.print(string("+"))". Your Propeller will thank you (or at least run faster).

    An even better idea is to use "lcd.out(key.newkey)".

    Duane
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2011-10-02 19:54
    Hi Daniel,

    Sorry, now that I see your complete code I see what you are trying to do with lcd.string("*)) etc is normally done just as a single character using the equivalent of lcd.out("*"). Strings can be used for single characters but it's very inefficient and a round-about way of doing things.

    The code looks a bit puzzling as to why you are interpreting keyboard codes this way. If the keyboard codes are printable ASCII then you can just echo them to the LCD and handle the control codes separately. For instance 48 through to 57 are just plain ASCII for 0 to 9. The value of 200 looks like it should just be a standard backspace ($08) character. If the keyboard is spitting out standard and non-standard codes you could employ a lookup table to translate these to standard ASCII and then process them.

    Any standard ASCII code below $20 is a control character and you can have a CASE statement to handle these otherwise handle all the rest as standard printable characters that you send straight to the display. Every character causes the X count to be advanced and a check is done to see if X has advanced past the end of line in which case you perform your defined functions for CR and LF. The CR function should only reset the X count and update the LCD cursor according to X and Y so that the cursor (visible or not) returns to the left side. The LF function increments the Y count and checks for wrap-around after which it updates the cursor according to X and Y count. HOME will reset X and Y count and update the LCD cursor.

    Here's a small snapshot of my character processing statement and what the CR and LF functions look like. Just use these as a guide as they are specific to my implementation.
    pri lcdctrl(c)
          case c
               $00 : '^@    Null
               $01 : '^A    Home
                 LCDHOME
               $02 : '^B    Start BIG
                  size := 1
               $03 : '^C    End BIG
                  size~
               $04 : '^D    Hide cursor
                 NOCUR
               $05 : '^E    underline cursor
                 UNDERLINE
               $06 : '^F    block cursor
                 BLINK
               $07 : '^G    Pulse bell output (default = 100ms)
                 BELL
               $08 : '^H    backspace
                 LCDBS
               $09 : '^I    tab
                 LCDTAB
               $0A : '^J    Line Feed
                 LCDLF
               $0B : '^K    Cursor up
                 LCDUP
               $0C : '^L    Clear screen
                 LCDCLS  'LcdCtl($01)
               $0D : '^M    Carriage return
                 LCDCR   ' LcdCtl($80+40)
               $0E : '^N    Backlight on
                 backlite($7E)
               $0F : '^O    Backlight off
                 backlite(0)
               $10 : '^P    Accept cursor-position entry
                 SETXY
               $11 : '^Q    Clear column and advance to next column
               $12 : '^R    Format right-aligned text (2..9)
                     '      backup from cursor and store text for display
                     '      until control character or decimal point
               $13 : '^S
               $14 : '^T    Set backlight brightness (0..$7F)
                  shift := "T"
               $15 : '^U    Display on
                 DPY_ON
               $16 : '^V    Display off
                 DPY_OFF
               $17 : '^W    Set contrast (00..1F)
                 shift := "W"
               $18 : '^X Set bell duration (00..FF)
               $19 : '^Y
    
               $1A : '^Z    RESET
                 InitLcd
               $1B : '^[ ESC Begin special instruction
                     '(D n B0 B1 B2 B3 B4 B5 B6 B7)     Define custom character
                     ' E 1    Restore custom character
               $1C : '^\ Write next byte direct to lcd as a character
                 shift := 2
               $1D : '^] Write next byte direct to lcd as a control
                 shift := 1
               $1E : '^^    Write next byte as the EEPROM pointer
               $1F : '^_ Write next byte to the EEPROM and increment the pointer
    
               other:
                  LcdData(c)
                  if xpos > chars
                    LCDCR
                    LCDLF
    
    pri LCDCR
      xpos~
      XYCUR
    
    pri LCDLF
      ypos++
      if ypos => lines or size
        ypos~
      XYCUR
    
    
  • daniel dingdaniel ding Posts: 52
    edited 2011-10-03 03:23
    Thanks very much Peter,

    but I use a "case--other" method, why when I input the first time "other" number, the lcd display nothing and the second time when I press the key it display the number?
  • daniel dingdaniel ding Posts: 52
    edited 2011-10-03 03:24
    Thanks, Duane

    I am going to study the object's out mehod now!
  • daniel dingdaniel ding Posts: 52
    edited 2011-10-03 04:14
    Duane,
    Thanks,
    You gave me a big help, I got it now. Thank you very much !
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2011-10-03 04:55
    Thanks very much Peter,

    but I use a "case--other" method, why when I input the first time "other" number, the lcd display nothing and the second time when I press the key it display the number?

    I'm not trying to teach you how to program in Spin, that's for you to learn. Your question was very specific about how to make an automatic newline and my snippet of code is just to show you how I handle part of it. Would you like me to write all the code for you? I hope you don't because you will miss out on the joy and benefits of learning. There are plenty of examples in the OBEX and throughout the forum so what have you found?

    What I have difficulty with is understanding your objective and without that it is a cumbersome piecemeal process of providing help. Perhaps you could provide an overview of why you are going through this exercise and what you hope to achieve.
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-11 19:11
    Duane,
    Are u here? Do u remember u tell me that use a lcd.out(key.newkey) to display the keyboard? I got it , but now I want to let the propeller distinguish what the lcd is displaying.
    For one, if I press the "g" key on keyboard and lcd display a "g" charactor, and the chip know taht I input a "g" charactor ,and if I press enter key next the chip will light a led.

    thanks
    daniel
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-12 05:01
    Please don't do double postings! Find the answer in the thread you started for this question.
Sign In or Register to comment.