Listing 1—I used this coding technique to meet the strict timing requirements of the project.
The routine sends a configurable number of pulses to the LCD clock input with less than three
PIC instructions per pulse on average! Try to do it with a classic loop.
;Send W pulses to the XSCK line (W = 0 to 60). Execution duration:100
;ns × (2xW + 20) for W < 60. Average with W = 20 (worst case) giving
;three instructions/pulse (300 ns).
**************************************************************************
send_upto60_pulses ;Limited to 60 because of page boundary
input in tmp_send_w_pulses
movf tmp_send_w_pulses,W
sublw .60 ;Calculate 2 × (60 – w)
rlncf WREG
rlncf WREG
movwf tmp_send_w_pulses
goto pulsesaligned
pulsesnotaligned
org (1 + high pulsesnotaligned)*.256
;Must start on a page boundary
pulsesaligned
movlw high pulsesaligned
movwf PCLATH ;High byte of new PC should be defined
movf tmp_send_w_pulses,W
addwf PCL,F ;Jump to next instruction if W = 0 (60 pulses)
bsf PORTB,RB_LCDXSCL_BIT ;pulse 60
bcf PORTB,RB_LCDXSCL_BIT
bsf PORTB,RB_LCDXSCL_BIT ;pulse 59
bcf PORTB,RB_LCDXSCL_BIT
bsf PORTB,RB_LCDXSCL_BIT ;pulse 58
bcf PORTB,RB_LCDXSCL_BIT
;etc…
bsf PORTB,RB_LCDXSCL_BIT ;pulse 02
bcf PORTB,RB_LCDXSCL_BIT
bsf PORTB,RB_LCDXSCL_BIT ;pulse 01
bcf PORTB,RB_LCDXSCL_BIT
retlw 0 ;Must be in the same page as the first one