Author Topic: LCD program structure :S  (Read 4580 times)

0 Members and 1 Guest are viewing this topic.

Offline tahmodTopic starter

  • Contributor
  • Posts: 31
LCD program structure :S
« on: April 12, 2013, 07:24:09 pm »
I want to learn to program a LCD and I am doing a tutorial.

-How can I understand this code below?
-what is the main structure to understand for a LCD?





;************************************************************************
;*   Microchip Technology Inc. 2007                              *
;*   Assembler version: x.x                                    *
;*   Filename:                                              *
;*      lcd.asm (main routine)                                 *
;*   Dependents:                                             *
;*      Main.asm                                          *
;*   Modified Jan 2013                                        *
;* PICDEM Explorer DEMO code.                                  *
;*                                                      *
;************************************************************************
#ifdef __18F8722
   list p=18F8722
   #include p18F8722.inc
#endif
#ifdef __18F87J11
   list p=18F87J11
   #include p18F87J11.inc
#endif


#define   LCD_E         temp_reg, 6   ; LCD E clock
#define   LCD_RS         temp_reg, 7   ; LCD register select line

#define   LCD_CS         LATA,2       ; LCD chip select
#define   LCD_CS_TRIS      TRISA,2       ; LCD chip select
#define   LCD_RST         LATF,6       ; LCD chip select
#define   LCD_RST_TRIS   TRISF,6       ; LCD chip select

D_LCD_DATA   UDATA_ACS
delay      res   1
temp_wr      res   1
temp_rd      res   1
Dreg1       res 1
Dreg2       res 1
temp_reg    res 1
                                               
   GLOBAL   temp_wr
PROG1   CODE
;---------------------------------------------------------------
;LCD Initialization routine
;---------------------------------------------------------------
   global LCDInit
LCDInit   
   bcf    LCD_CS_TRIS
   bsf      LCD_CS
   clrf   temp_reg
   call   Delay
   call   Delay
   call   Delay

   bcf      LCD_RST_TRIS
   bcf      LCD_RST
   call   Delay
   bsf      LCD_RST

   call   InitSPI
   call    InitPortA_SPI
   call   InitPortB_SPI


   bcf      LCD_E         
   bcf      LCD_RS   
   call   WritePortA

;Function Set
   call   Delay
   movlw   b'00111100' ;0011NFxx
   movwf   temp_wr
   call   InitWrite



   call   Delay
   movlw   b'00001100' ;Display off
   movwf   temp_wr
   call   InitWrite


   call   Delay
   movlw   b'00000001' ;Display Clear
   movwf   temp_wr
   call   InitWrite


   call   Delay
   movlw   b'00000110' ;Entry mode
   movwf   temp_wr
   call   InitWrite

   return
;-------------------------------------------------------------
;Function to write to the PORT
;-------------------------------------------------------------
InitWrite
   bcf      LCD_E      
   bcf      LCD_RS   
   call   WritePortA
   call   WritePortB
   nop
   nop
   nop

   bsf      LCD_E
   call   WritePortA   
   nop
   nop
   nop
   nop
   nop
   nop
   nop
   nop
   nop
   bcf      LCD_E
   call   WritePortA
   return

;---------------------------------------------------
;LCD busy delay
;---------------------------------------------------
   global LCDBusy
LCDBusy
   call   SDelay
   call   SDelay
   return

   global ClearLCD
ClearLCD
   movlw   0x01
   movwf   temp_wr
   rcall   i_write
   return

;---------------------------------------------------
;Write to Line1
;---------------------------------------------------
   global   LCDLine_1   
LCDLine_1
   movlw   0x80
   movwf   temp_wr
   rcall   i_write
   return
;---------------------------------------------------
;Write to Line2
;---------------------------------------------------
   global LCDLine_2
LCDLine_2
   movlw   0xC0
   movwf   temp_wr
   rcall   i_write
   return
;---------------------------------------------------
;Write data
;---------------------------------------------------
   global d_write
d_write
   rcall   LCDBusy
   bcf      LCD_E            
   bsf      LCD_RS
   call   WritePortA
   call   WritePortB
   nop
   nop
   nop
   nop
   bsf      LCD_E
   call   WritePortA   
   nop
   nop
   nop
   nop
   nop
   nop
   bcf      LCD_E      
   bcf      LCD_RS
   call   WritePortA

   movf   temp_wr,w
   movwf   TXREG         ;carriage return
   btfss   TXSTA,TRMT      ;wait for data TX
   bra   $-2
   return
;---------------------------------------------------
;Write instrution
;---------------------------------------------------
   global i_write
i_write
   bcf      LCD_E            
   bcf      LCD_RS
   call   WritePortA   

   rcall   LCDBusy
   call   WritePortB
   nop
   nop
   nop
   nop
   bsf      LCD_E
   call   WritePortA   
   nop
   nop
   nop
   nop
   nop
   nop
   bcf      LCD_E
   bcf      LCD_RS
   call   WritePortA
   return

;---------------------------------------------------
;Initialize MCP923S17 Port
;---------------------------------------------------
InitPortA_SPI

   bcf      LCD_CS
   movlw   0x40
   movwf   SSPBUF
   call   Check
   movlw   0x00
   movwf   SSPBUF
   call   Check
   movlw   0x00
   movwf   SSPBUF
   call   Check
   bsf      LCD_CS
   return
;---------------------------------------------------
;Initialize MCP923S17 Port
;---------------------------------------------------
InitPortB_SPI
   bcf      LCD_CS
   movlw   0x40
   movwf   SSPBUF
   call   Check
   movlw   0x01
   movwf   SSPBUF
   call   Check
   movlw   0x00
   movwf   SSPBUF
   call   Check
   bsf      LCD_CS
   return

;---------------------------------------------------
;Initialize SPI
;---------------------------------------------------
    global InitSPI
InitSPI
   bcf      TRISC,5
   bcf      TRISC,3
   movlw   0x22
   movwf   SSP1CON1
   bsf      SSP1STAT,CKE
   bcf      PIR1,SSPIF
   return

    global Check
Check
   btfss   PIR1,SSPIF
   goto   $-2   
   bcf      PIR1,SSPIF
   movf   SSPBUF,w
   return

;---------------------------------------------------
;Write to  MCP923S17 Port
;---------------------------------------------------
WritePortB
   bcf      LCD_CS
   movlw   0x40
   movwf   SSPBUF
   call   Check
   movlw   0x13
   movwf   SSPBUF
   call   Check



   movf   temp_wr,w   
;   movlw   0xFF
   movwf   SSPBUF
   call   Check      

   bsf      LCD_CS

   return

;---------------------------------------------------
;Write to  MCP923S17 Port
;---------------------------------------------------
WritePortA
   bcf      LCD_CS
   movlw   0x40
   movwf   SSPBUF
   call   Check
   movlw   0x12
   movwf   SSPBUF
   call   Check
;   movlw   0x80
;   movlw   0x40
;   movlw   0xc0
   movf   temp_reg,w
   movwf   SSPBUF
   call   Check      

   bsf      LCD_CS
   return


   GLOBAL Delay
Delay
   nop
   nop
   nop
   nop
   decfsz   Dreg1
   goto Delay
   decfsz   Dreg2
   goto Delay
   return

SDelay
   decfsz   Dreg1
   goto SDelay
   decfsz   Dreg1
   goto   $-2
   decfsz   Dreg1
   goto   $-2
   decfsz   Dreg1
   goto   $-2
   decfsz   Dreg1
   goto   $-2
   decfsz   Dreg1
   goto   $-2
   decfsz   Dreg1
   goto   $-2
   decfsz   Dreg1
   goto   $-2
   decfsz   Dreg1
   goto   $-2

   return

      goto   $



   END
 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: LCD program structure :S
« Reply #1 on: April 12, 2013, 08:20:39 pm »
Quote
-How can I understand this code below

By learning the particular assembler language, the architecture of the MCU, and some basic algorithms and data structures.

Quote
-what is the main structure to understand for a LCD?

Its hardware interface and the protocol used to drive and communicate with it.
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 

Online mariush

  • Super Contributor
  • ***
  • Posts: 5358
  • Country: ro
  • .
Re: LCD program structure :S
« Reply #2 on: April 12, 2013, 08:44:17 pm »
That code you pasted is in assembler language.  It's kind of hard to learn if you have no programming experience.

You don't have to learn Assembler these days, although it wouldn't hurt. You can write programs directly in C and "upload" them in the microcontroller.

LCD screens communicate with the microcontroller through several wires, and you send and receive digital ones and zeroes through the wires with the microcontroller.

The language the LCD screen understands is explained in its datasheet - the screen has on its back a chip that has the brains to light up dots on the screen and form digits, so the microcontroller has to talk to that chip and tell it what letters to display.

There's various chips on the backs of those screens but the most popular chips on the cheap displays all copy an old classic chip called  HD44780.
Your microcontroller sends commands to the chip on the back of the screen by sending 1s and 0s through the wires and the commands are there in the datasheet for the chip that's behind the screen or in the datasheet for the screen.

Here's for example a datasheet for a 2x16 lcd screen, from page 11 it explains what each wire in the connector is and shows the commands to talk to the chip and further below there's code in C which shows how to initialize the screen and write characters on it:

http://savedonthe.net/download/480/lcd_2x16.html

In order to understand that code or the C code, you have to understand how that microcontroller works, and for that you have to read the datasheet of that microcontroller, to understand the concepts (input output ports and so on).

With Arduino it's a bit easier, because there's a library already built for you , but that basically hides things from you and you won't learn quite as much
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: LCD program structure :S
« Reply #3 on: April 13, 2013, 02:04:50 am »
I want to learn to program a LCD and I am doing a tutorial.
Then find a different tutorial. Learning how to do it in assembly is ridiculous.

Try this.
 

Online mariush

  • Super Contributor
  • ***
  • Posts: 5358
  • Country: ro
  • .
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->