16F819 PIC driving LCD

Discussion in 'Embedded Systems and Microcontrollers' started by ham_man, Jan 10, 2014.

  1. ham_man

    Thread Starter New Member

    Jan 9, 2014
    Hi I am relatively new to PIC programming and am just using it as a hobby.
    At present I am familliar with assembly language but only in the very early stages of using C language.
    I have had great fun with programming PIC for using LCD display, using 4 bit transfer and two analogue inputs, but I would now like to use Busy check on the LCD so that I don't have to worry about delays and altering osc frequency(within reasonable limits. etc.
    To concentrate on getting the B/Flag going properly I have used another program and cut it down to a very simple LCD output (It just writes a name on the bottom line).
    If i just use a delay in place of the B/Flag check routine--it works fine, but replacing the delay with the B/Flag routine gives 2 lines and nothing else.
    I have checked everywhere I can think of on the net---unfortunately most information is in C language!
    If anyone would be kind enough to help (In ASSEMBLY language if possible) I would be most grateful.
    I list my B/flag routine ---called Busywait below:-

    Code ( (Unknown Language)):
    2. BUSYWAIT        BSF    STATUS,5    ;GOTO BANK 1
    3.             MOVLW    0FFH        ;PORTB ALL INPUTS
    4.             MOVWF    TRISB
    5.             BSF    OPTION_R,7    ;DISABLE PULLUPS
    6.             BCF    STATUS,5    ;RETURN TO BANK 0
    8.             BSF    PORTA,2        ;R/W LINE SET HIGH TO READ LCD DATA
    9.             BCF    PORTA,1        ;R/D CLEAR---SET TO COMMAND
    10.             NOP            ;SMALL DELAY TO ENSURE LCD DATA STABLE
    11.             NOP            ;SMALL DELAY TO ENSURE LCD DATA STABLE
    12.             BCF    PORTA,3        ;CLOCK LOW TO SET LCD DATA INTO PORTB 1ST NIBBLE
    13.             MOVF    PORTB,W        ;MOVE PORTB DATA INTO W REG
    14.             MOVWF    TEMP1        ;MOVE LCD DATA 1ST NIBBLE INTO TEMP1 FILE
    15.             BSF    PORTA,3        ;CLOCK HIGH ENABLE 2ND NIBBLE FROM LCD
    16.             NOP            ;SMALL DELAY TO ENSURE LCD DATA STABLE
    17.             NOP            ;SMALL DELAY TO ENSURE LCD DATA STABLE
    18.             BCF    PORTA,3        ;CLOCK LOW TO READ 2ND NIBBLE (NOT NEEDED FOR B/F CHECK??)
    19.             BTFSC    TEMP1,7        ;TEST 1ST NIBBLE (MSB) BUSY FLAG  TEMP1,7
    20.             GOTO    STILLBUSY    ;LOOP IF B/F STILL SET
    21.             BCF    PORTA,2        ;BACK TO WRITE TO LCD MODE
    22.             BSF    STATUS,5    ;GO TO BANK1
    23.             MOVLW    00H        ;SET ALL PORTB O/P
    24.             MOVWF    TRISB
    25.             BCF    OPTION_R,7    ;RE-ENABLE PORTB PULLUPS
    26.             BCF    STATUS,5    ;RETURN TO BANK 0
    27.             RETLW    0        ;RETURN W REG CLEARED
    Any help will be appreciated --many thanks
    Last edited by a moderator: Jan 10, 2014
  2. MaxHeadRoom


    Jul 18, 2013
    Look at the tutorials 3.x here
    There are three examples of routines to do time delay wait or checking the busy signals.
    These can be used and saved as individual routines as needed.
    They work as I have used them, also on a 18F at 20Mhz.
  3. ham_man

    Thread Starter New Member

    Jan 9, 2014
    Hi many thanks for the quick reply I will certainly check that out, this problem has been a big slow down for me so far, like about 3 days messing about with the software and searching the net--I hope the tutorials use assembly ---thanks again best wishes
  4. MaxHeadRoom


    Jul 18, 2013
    Thats all it uses.
  5. ham_man

    Thread Starter New Member

    Jan 9, 2014
    Thanks I check it out
  6. ham_man

    Thread Starter New Member

    Jan 9, 2014
    Hi again MaxHeadRoom, I have found the Busy Flag read routine in the tutorials --it looks surprisingly similar to my effort, just a couple of lines different, doing the same function, but that may make all the difference! I'll try it out with my hardware.
    Many thanks again for all your interest.
  7. nigelwright7557

    Distinguished Member

    May 10, 2008
    Using this code isn't very professional

    It should be
    BSF R_W
    BCF R_D

    This makes it less likely to have a bug and is alos a lot better.
    ham_man likes this.
  8. MMcLaren

    Well-Known Member

    Feb 14, 2010
    Hi ham_man:

    Are the RB4..RB7 pins on the PIC connected to the D4..D7 lines on the LCD?
  9. ham_man

    Thread Starter New Member

    Jan 9, 2014
    Hi MMcLaren, thanks for your interest --yes pins D4--D7 LCD connected to RB4...RB7
  10. ham_man

    Thread Starter New Member

    Jan 9, 2014
    Hi bertus, many thanks for editing (sorting out) my posted code using code tags. I'm a newby and will use tags in future thanks again
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    You also must have in mind that the buzzy flag on the LCD. Do not function in the early stages of the configuration procedure. I do not remember exact then it become active. But should be noted in the datasheet
  12. ham_man

    Thread Starter New Member

    Jan 9, 2014
    Hi Thanks--yes you are correct the B Flag cannot be polled until initation has completed ----after about 3 inputs of (say) 0x30, with long delays 4/5 msecs between each one. Then after that you can change to 4 bit tansfer (or whatever) and poll the B Flag.
    With help from several members I now have the routine working well--it saves messing about with delays in the program.
    So many thanks all round. :)
  13. takao21203

    AAC Fanatic!

    Apr 28, 2012
    I have used assembler for some years on the 16f.

    The problem when you write the bitnumber like that is you can not reuse it, without making a lot of changes.

    And for larger programs you also need to do the banking.

    Why not let the C compiler generate the assembler for you?

    using .h include files, #defines and automatic banking as well FSR handling at the end of the day let you write 10x much as code.

    It is OK to use assembler for a while but it is important to make the transition some day.

    If you want to use USB, it is not worth it by the assembler way.

    People mostly use C for good reasons, such as exchangeability and portability, and to make it easy for others to read code.

    2 As for the busy flag, yes it can be tested, but you need to change the tristate each time.

    There are other ways than a delay, you can program flags together with a timer interrupt, and a small LCD command parser.

    I have done this in assembler some while ago. Learning memory addressing in C is not all that easy, but it can be done. It is absolutely worth it.

    Still have all my assembler sources on the harddrive, but they are embarassing to show off. And lengthy, even if just for LCD. For instance reading from internal EE tables, and even alternatively from external I2C EE.
  14. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    Most of the LCD commands only need a handful of uS "delay" which is often used up by code execution time without even needing a deliberate delay.

    It saves messing about with tristating pins and the busy flag. ;)