Shop OBEX P1 Docs P2 Docs Learn Events
PinRead() question for lcd busy flag routine — Parallax Forums

PinRead() question for lcd busy flag routine

Capt. QuirkCapt. Quirk Posts: 872
edited 2020-12-26 07:34 in Propeller 2
Pinwrite, pinh, pinl automatically handle the DIRx register,
but does PinRead() automatically switch an output pin into
an Input pin, and back to an Output pin?

For example, how does Spin2 handle reading a busy flag for
an HD44780 LCD?


BIll M.
check_busy_flag_v01.spin2  Snipet from Chris Gadds "Parallel LCD driver, 4-bit mode"
'' ========================= P1 to P2 conversion  ======================================
PRI check_busy_flag() | busy   

 '' Partial completed P2 snipet

'' Pinwrite, pinh, pinl automatically handle the DIRx register,
'' but does PinRead() automatically switch an output pin into
'' an Input pin, and back to an Output pin?

'' For example, how does Spin2 handle reading a busy flag for
'' an HD44780 LCD?

'' ================== Partial completed P2 snipet ======================================
  busy := 1
  dira[D7]~
  pinlow(rs)                                    ' outa[RS]~
  pinhigh(rw)                                   ' outa[RW]~~
  repeat while busy
    pinhigh(e)                                  ' outa[E]~~
    busy := pinread(d7)'????                    ' ina[D7]     ' Check busy flag while E is high
    pinlow(e)                                   ' outa[E]~
    clock()
  dira[D7]~~
  pinlow(rw)                                    ' outa[RW]~


'' ========================= Original P1 snipet ======================================
PRI check_busy_flag | busy

  busy := 1
  dira[D7]~
  outa[RS]~
  outa[RW]~~
  repeat while busy
    outa[E]~~
    busy := ina[D7]                                                             ' Check busy flag while E is high
    outa[E]~
    clock
  dira[D7]~~
  outa[RW]~

Comments

  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-12-26 07:48
    does PinRead() automatically switch an output pin into an Input pin
    No. This allows the input bit to read the current state of an output.

    If you have downloaded PNut you can find the source code for the Spin2 interpreter in the examples. Here's is the code for pinread() -- you can see that it does not make any changes to the dirx register.
    ' PINR(pins)                            (7 longs)
    '
    pinr_           testb   x,#5    wc      'read ina or inb
            if_nc   mov     y,ina
            if_c    mov     y,inb
    
                    ror     y,x             'lsb-justify
    
                    shr     x,#6            'trim
                    zerox   y,x
    
            _ret_   mov     x,y             'result in stack top
    

    Not that you asked, but here's how I would translate your check_busy_flag() routine.
    pri check_busy_flag() | busy
    
      busy := 1
    
      pinfloat(D7)
      pinlow(RS)
      pinhigh(RW)
      repeat while (busy)
        pinhigh(E)
        busy := pinread(D7)
        pinlow(E)
        clock()
      pinlow(RW)     
      pinlow(D7)
    
    JonnyMac's Rule: NEVER user inx, outx, or dirx registers in Spin2 -- this creates portability problems even when the syntax is corrected from the P1.
  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2020-12-26 08:10
    JonnyMac wrote: »
    No. This allows the input bit to read the current state of an output.

    If that is true, why add pinfloat()?

    Isn't this enough?

    Bill M.
    '' Revised Ck BF
      busy := 1
      pinlow(rs)                                    
      pinhigh(rw)                                   
      repeat while busy
        pinhigh(e)                                 
        busy := pinread(D7)                   
        pinlow(e)                                  
        clock()
      pinlow(rw) 
    
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-12-26 08:57
    If that is true, why add pinfloat()?
    Maybe I misread your question. You need to use pinfloat() to set a pin or pins group to inputs so that it can see external signals. If the pin is set to output mode, there could be a conflict with the outside world.
    Isn't this enough?
    No, because the last output state of D7 is still on the pin, and unless you disconnect the output driver you cannot read the busy flag.

    Looking through my library I found that I had written an LCD object that could read from the display. I translated it to P2. Want to give it a try?
  • ozpropdevozpropdev Posts: 2,792
    edited 2020-12-26 08:49
    The reason pinread doesn't automatically switch to input is there are scenarios where the internal pullups/pulldowns are being driven by the output states.
    Changing the pin direction would disable these pullups/downs giving unpredicatble input states.
  • Whoops... I found that I missed a couple outa[] translations -- fixed now.
  • ozpropdev wrote: »
    The reason pinread doesn't automatically switch to input is there are scenarios where the internal pullups/pulldowns are being driven by the output states.
    Changing the pin direction would disable these pullups/downs giving unpredicatble input states.

    Another reason to keep the direction bit high is to maintain smartpin mode for status updates.
Sign In or Register to comment.