Author Topic: Complex program to write into a LCD  (Read 3072 times)

0 Members and 1 Guest are viewing this topic.

Offline tahmodTopic starter

  • Contributor
  • Posts: 31
Complex program to write into a LCD
« on: April 19, 2013, 11:16:15 pm »
I want to learn to program a LCD and I am doing a tutorial. This project contain two files one written below and the second file is in the website shown in the end of the post.

-Can you guys tell me if the question I wrote in the code is correct or wrong? I got 18 question
;****************************************************************
;*   LCD driver routines, with a simple menu         *
;*   Assembler version:                *
;*   Filename:                   *
;*      Main.asm (main routine)               *
;*   Dependents:                  *
;*      LCD.asm               *
;*      18F8722.lkr   or 18F87J11.lkr         *
;*   Jan 16 2013   NPS               *
;*    PIC18 Explorer. The following functions are included       *
;*   with this code:                  *
;*      1. Menu Item 1               *
;*         Add own code            *
;*      2. Menu Item 2               *
;*         Add own code            *
;*      3. Menu Item 3               *
;*         Add own code            *   
;****************************************************************
;1. This is the configuration bits changed to allow the different microcontroller to be used?
#ifdef __18F8722
   list p=18F8722
   #include p18F8722.inc
   CONFIG   OSC = HSPLL
   CONFIG   FCMEN = OFF
   CONFIG   IESO = OFF
   CONFIG   PWRT = OFF           
   CONFIG   BOREN = OFF
   CONFIG   WDT = OFF
   CONFIG   MCLRE = ON 
   CONFIG   LVP = OFF 
   CONFIG   XINST = OFF
#endif

#ifdef __18F87J11
   list p=18F87J11
   #include p18F87J11.inc
    CONFIG   FOSC = HSPLL
   CONFIG STVREN = OFF,XINST = OFF,WDTEN = OFF,CP0 = OFF,FCMEN = OFF,IESO = OFF
   CONFIG      MODE = MM
#endif
;2. -This make the porta,5 equal to scroll? -When ever you use scroll this is like you use porta,5?

   #define   scroll_dir   TRISA,5
   #define   scroll      PORTA,5      ;Push-button RA5 on PCB
   #define   select_dir   TRISB,0      
   #define   select      PORTB,0      ;Push-button RB0 on PCB
;3.Dont understand what they doing with theEEPROM memory?
#define   EEPROM_CS      LATA,3       ; EEprom chip select
#define   EEPROM_CS_TRIS   TRISA,3       ; EEprom chip select
4.what is these 2 line code doing?
   EXTERN   LCDInit, temp_wr, d_write, i_write, LCDLine_1, LCDLine_2
   EXTERN    Delay,Check,InitSPI
;5. They giving location for the SFR into the memory location?- but what is the variable UDATE_ACS?
variables   UDATA_ACS
ptr_pos      RES 1
ptr_count   RES 1
temp_1      RES 1
temp_2      RES 1
temp_3      RES 1

;6 I dont understand why they use the NOP instruction here? -we dont need some delay here?
STARTUP CODE 0
   NOP
   goto   start
   NOP
   NOP
   NOP
PROG1    CODE   ;6 what is this CODE  variable doing?

;7. is this the table to store the character we want to put?
stan_table      ;table for standard code
   ;      "XXXXXXXXXXXXXXXX"
   ;            ptr:
   data   "  Menu Item 1   "      ;0
   data   "                "      ;16
   data   "  Menu Item 2   "      ;32
   data   "  Menu Item 3   "      ;48
   data   "RB0=Now RA5=Next"   ;64
   data   "Microchip PICDEM"     ;80
   data   " PIC18 Explorer "   ;96
   data   "LCDMenu Demo NPS"   ;112
   data   "Item 3 Execution"   ;128
   data   "Item 2 Execution"   ;144
   data   "Item 1 Execution"   ;160

start

   bsf        OSCTUNE,PLLEN      ;8. what is Osctune doing? Enable PLL
   call    LCDInit            ;Initialize the LCD
                     ;9i the LCinit just call an instruction in the other file, but what happen in the other file?


#ifdef __18F8722
   movlw   b'00001110'         ;RA0 = analog input
   movwf   ADCON1
#endif
#ifdef __18F87J11
   bsf      WDTCON,ADSHR      ;Shared SFR
   setf   ANCON0      ;10. this set the whole bits of ANCON0 or a single bit?
   setf   ANCON1
   bcf      ANCON1,0         ;11. having a zero in the bi one of ANCON1 Make RA0 Analog input?   
   bcf      WDTCON,ADSHR
#endif
12. what is WDTCON, TXSTA and RCSTA?
   movlw   B'10100100'         ;initialize USART
   movwf   TXSTA            ;8-bit, Async, High Speed
   movlw   .255
   movwf   SPBRG            ;9.6kbaud @ 40MHz
   movlw   B'10010000'
   movwf   RCSTA

   bsf   TRISB,0               ;make switch RB0 an Input
   bsf   TRISA,5               ;make switch RA5 an Input

;**************** STANDARD CODE MENU SELECTION *******************
         ;Introduction
   movlw   .80               ;send "Microchip PICDEM" to LCD
   movwf   ptr_pos
   call   stan_char_1

   movlw   .96               ;send " PIC18 Explorer  " to LCD
   movwf   ptr_pos
   call   stan_char_2
   call   delay_1s         ;delay for display

   movlw   .112            ;send "LCDMenu Demo " to LCD
   movwf   ptr_pos
   call   stan_char_1
   call   delay_1s         ;delay for display
   call   delay_1s         ;delay for display

menu
;13. what is the purpose of waiting for A5 and RBo to release?
   btfss   scroll            ;wait for RA5 release
   goto   $-2      
   btfss   select            ;wait for RB0 release
   goto   $-2

;------------------ MENU item 1  ----------------------------
   call delay_100ms
   call delay_100ms
   call delay_100ms
   btfss   scroll            ;YES
   bra   $-2                  ;wait for release
;14 why there is a scroll button here? in the hardwre there is only once they ask when you press it goes to the next one.
;-When you press the  button it goes low, when you dont press it is high?
;-this btfss is just to test if the button is not touched anymore,
   movlw   0x00            ;Display  " Menu Item 1  "
   movwf   ptr_pos
   call   stan_char_1
;15 when you move a number in dot for example into w register from literal, then move to ptr_pos, the ptr_pos shows the data wrtten ino .64 from the table?
;16 what does the stan char_2 does?-the stan char_2 represent the second line? and what the stan_char_1 does?it represent the first line in the LDR?

   movlw   .64               ;Display "RB0=Now RA5=Next"
   movwf   ptr_pos
   call   stan_char_2

;wait for key press
v_wait
   btfss   select            ;execute item1?
   bra   item1               ;YES
   btfsc   scroll            ;Goto Next
   bra   v_wait               ;NO
;16 when you press the key, the scroll goes low and excetute the next instruction which is bra v_wait. For scroll, when you press the button scroll, scroll button goes low and skip the next uinstrution as it is now btfsc instead of btfss. this statement is correct?
;------------------ MENU item 2  ----------------------------
menu_2
   call delay_100ms
   call delay_100ms
   call delay_100ms
   btfss   scroll            ;wait for RA5 release
   bra   $-2   

   movlw   .32               ;Display "Menu Item 2"
   movwf   ptr_pos
   call   stan_char_1

   movlw   .64               ;Display "RB0=Now RA5=Next"
   movwf   ptr_pos
   call   stan_char_2

;wait for key press
t_wait
   btfss   select            ;Execute Item 2 code?
   bra   item2               ;YES
   btfsc   scroll            ;Goto Next
   bra   t_wait               ;NO

;------------------ MENU item 3  ----------------------------
menu_3
   call   delay_100ms
   call delay_100ms
   call delay_100ms
   btfss   select;            ;wait for RB0 release
   bra   $-2;         

   movlw   .48               ;Display "Menu Item 3     "
   movwf   ptr_pos
   call   stan_char_1

   movlw   .64               ;Display "RB0=Now RA5=Next"
   movwf   ptr_pos
   call   stan_char_2

c_wait
   btfss   select            ;execute item3 code?
   bra   item3               ;YES
   btfsc   scroll            ;Go back to Main menu
   bra   c_wait               ;NO

   bra   menu               ;begining of menu

;*******************************************************************
;*************   USER CODE     *************************************

;-------------   Menu Item 1   -------------------------------------
item1
   movlw   .160               ;Display "Item 1 Execution"
   movwf   ptr_pos
   call   stan_char_2

   bra menu
;-------------   Menu Item 2   -------------------------------------
item2
   movlw   .144               ;Display "Item 2 Execution"
   movwf   ptr_pos
   call   stan_char_2

   bra menu_2
;-------------   Menu Item 3   -------------------------------------
item3
   movlw   .128               ;Display "Item 3 Execution"
   movwf   ptr_pos
   call   stan_char_2

   bra menu_3

;   bra menu               ; go back to menu

;************************** ROUTINES ******************************
;******************************************************************
;******************************************************************
;17. how this code of stan_char_1 work?
;----Standard code, Place characters on line-1--------------------------
stan_char_1
   call   LCDLine_1            ;mvoe cursor to line 1
   movlw   .16                  ;1-full line of LCD
   movwf   ptr_count
   movlw   UPPER stan_table
   movwf   TBLPTRU
   movlw   HIGH stan_table
   movwf   TBLPTRH
   movlw   LOW stan_table
   movwf   TBLPTRL
   movf   ptr_pos,W
   addwf   TBLPTRL,F
   clrf   WREG
   addwfc   TBLPTRH,F
   addwfc   TBLPTRU,F

stan_next_char_1
   tblrd   *+
   movff   TABLAT,temp_wr         
   call   d_write               ;send character to LCD

   decfsz   ptr_count,F            ;move pointer to next char
   bra   stan_next_char_1

   movlw   "\n"               ;move data into TXREG
   movwf   TXREG               ;next line
   btfss   TXSTA,TRMT            ;wait for data TX
   goto   $-2
   movlw   "\r"               ;move data into TXREG
   movwf   TXREG               ;carriage return
   btfss   TXSTA,TRMT            ;wait for data TX
   goto   $-2

   return

;----Standard code, Place characters on line-2--------------------------
stan_char_2   
   call   LCDLine_2            ;move cursor to line 2
   movlw   .16                  ;1-full line of LCD
   movwf   ptr_count
   movlw   UPPER stan_table
   movwf   TBLPTRU
   movlw   HIGH stan_table
   movwf   TBLPTRH
   movlw   LOW stan_table
   movwf   TBLPTRL
   movf   ptr_pos,W
   addwf   TBLPTRL,F
   clrf   WREG
   addwfc   TBLPTRH,F
   addwfc   TBLPTRU,F

stan_next_char_2
   tblrd   *+
   movff   TABLAT,temp_wr
   call   d_write               ;send character to LCD

   decfsz   ptr_count,F            ;move pointer to next char
   bra   stan_next_char_2

   movlw   "\n"               ;move data into TXREG
   movwf   TXREG               ;next line
   btfss   TXSTA,TRMT            ;wait for data TX
   goto   $-2
   movlw   "\r"               ;move data into TXREG
   movwf   TXREG               ;carriage return
   btfss   TXSTA,TRMT            ;wait for data TX
   goto   $-2

   return
;----------------------------------------------------------------------
;------------------ 100ms Delay --------------------------------
;18 -is the rest of the following line are delays codes? -why there are so many delay labels and code,? because we are only using delay 100ms
delay_100ms
   call Delaysmall
   call Delaysmall
   call Delaysmall
   call Delaysmall
   call Delaysmall
   call Delaysmall
   call Delaysmall
   call Delaysmall
   call Delaysmall
   call Delaysmall
   return

Delaysmall ;100ms at 4Mhz
   movlw   0xFF
   movwf   temp_1
   movlw   0x83
   movwf   temp_2

d100l1
   decfsz   temp_1,F
   bra   d100l1
   decfsz   temp_2,F
   bra   d100l1
   return

;---------------- 1s Delay -----------------------------------
delay_1s
   call   Ldelay
   call   Ldelay
   call   Ldelay
   call    Ldelay
   call   Ldelay
   call    Ldelay
   call    Ldelay
   call    Ldelay
   call   Ldelay
   call    Ldelay
   return

Ldelay      ;1S at f Mhx
   movlw   0xFF
   movwf   temp_1
   movwf   temp_2
   movlw   0x05
   movwf   temp_3
d1l1
   decfsz   temp_1,F
   bra   d1l1
   decfsz   temp_2,F
   bra   d1l1
   decfsz   temp_3,F
   bra   d1l1
   return   

;*********************************************************************

        end



The code for the LCD file is here.
https://www.eevblog.com/forum/microcontrollers/how-can-you-understand-a-code-s/?topicseen
 

Online mariush

  • Super Contributor
  • ***
  • Posts: 5029
  • Country: ro
  • .
Re: Complex program to write into a LCD
« Reply #1 on: April 20, 2013, 01:38:13 am »
If you don't know programming and you're a beginner in microcontrollers, going with a program written in Assembler is not the right idea.

Best idea would be to get any PIC chip out there, put it on a breadboard and connect the wires from the programmer to it, then open the XC8 manual and the PIC chip datasheet and one of the tutorials that come with the programmers and follow the tutorials to get the chip light up a led or something simple.

Once you get the basics , making a pin output or input, making it digital or analogue, writing a function, the basics of C language (if, while, for, function etc)..you will be able to write code to print something on the lcd display easily.

There are also good books out there that teach you how to program microcontrollers in C, Mikroe has for example a book available online: http://www.mikroe.com/chapters/view/74/pic-basic-book-chapter-1-world-of-microcontrollers/

The book uses BASIC examples, but you can easily convert those to C if you want.

The PicKit3 programmer also comes with some manuals with lessons on it that make it really easy to understand C and learn.. it's even available online:

http://ww1.microchip.com/downloads/en/DeviceDoc/41370C.pdf

This one is for pic 18 but can be easily adapted to PIC16, there's nothing complicated about it and the lessons are explained well.

anyway answering some of your questions

;1. This is the configuration bits changed to allow the different microcontroller to be used?

anything in an ifdev is ran only if that condition is true... the chunk in ifdef 18f8722 is included only if you use that chip (you are supposed to tell the compiler the chip or write that in the make file)
yes, you can in theory add other chips there and set the proper settings for that chip you want to use

;2. -This make the porta,5 equal to scroll? -When ever you use scroll this is like you use porta,5?

define  just gives some names to pins, it's an "alias", so it doesn't have to use every time trisa,5 .. it just uses from that point scroll_dir

;3.Dont understand what they doing with theEEPROM memory?

it's just some aliases for some registers. it doesn't do anything up to this point, just says  "somewhere in the code I may use the word "eeprom_cs" you should know that when i use the word, consider it's actually the address in memory that holds LATA , 3

The memory addresses assigned to those keywords LATA, TRISA, PORTA etc are in those files with the extension .lkr probably.

4.what is these 2 line code doing?

extern tells the compiler that those segments/functions/whatever are written in other files so it should look for them in other places. LCDInit for example and other lcd related functions are in that other file you linked to in the other thread.

;5. They giving location for the SFR into the memory location?- but what is the variable UDATE_ACS?

those are just definitions for variables.  READ THE Assembler language manual that should come with that assembler compiler, it should explain you what all the terms like "Variables", "define", 'extern" , labels, sections etc mean

;6 I dont understand why they use the NOP instruction here? -we dont need some delay here?

read the pic 18 documentation, my guess is maybe those nops are there to give some room for a built in firmware or something to inject itself there and work properly. I don't know

;6 what is this CODE  variable doing?

Again, these are some standard keywords for the programming language, they're related to assembly language

See 

http://ww1.microchip.com/downloads/en/DeviceDoc/33014K.pdf
http://ww1.microchip.com/downloads/en/DeviceDoc/MPASM_Quick_Ref_Card_30400g.pdf

Read the manuals, there's no magic instant programmer mode. But it's really not a great idea to complicate your life with Assembly language when you can work easier in C.. See the lessons manual I linked above

;7. is this the table to store the character we want to put?
stan_table      ;table for standard code

It's keywords for assembly language, tells it those strings are constant and won't change. if the stuff is constant, it's treated differently than variables and so on.

 ;8. what is Osctune doing? Enable PLL

READ THE MANUAL for that PIC18 chip ... it says there what OSCTUNE means.

http://ww1.microchip.com/downloads/en/devicedoc/39646c.pdf

page 34 and forward

 ;9i the LCinit just call an instruction in the other file, but what happen in the other file?

When you said a few lines above EXTERN the compiler read that file and loaded it in memory so it knows all functions from that file.  when you say call LCDInit, the compiler looks for the LCDInit label in that other file and runs all the lines until another label it sees that "Return" keyword ...  a label like "LCDInit" and a return form a function named LCDInit which can be called from other parts using "call LCDInit"

the rest are silly questions, something you'd know if you'd bother reading the assembly manual, the pic18 datasheet and learn what those pic18 instructions do

17 .. routines... everything from a label to a return is like a block of code that can be called from various parts of the code.. when it says "call x" somewhere, the cpu jumps to the label does everything up to the "return keyword" then returns back to the line after the "call x" line.

18...

basically, a delay is just another word for making the microcontroller execute an instruction doing nothing, therefore wasting time.
NOP is an instruction for example which just says "nothing, just go on to the next instruction in the program"

Every instruction the cpu understands takes a certain number of cycles, and within a second the PIC18 can execute a number of cycles that depends on the frequency of the oscillator.  Each cycle is a 4 oscillations of the oscillator, so for example if you use a 4 Mhz oscillator, the PIC18 can do 1.000.000 (1 mega) of cycles.
So if you want the PIC to do nothing for 1 ms, at 4 Mhz that means you have to make it waste 1000 cycles.

You could either write 1000 NOP commands one after another but that just wastes bytes in the flash memory of the microcontroller, so you resort to some tricks with other instructions the PIC supports.

Delaysmall ;100ms at 4Mhz
   movlw   0xFF
   movwf   temp_1
   movlw   0x83
   movwf   temp_2

d100l1
   decfsz   temp_1,F
   bra   d100l1
   decfsz   temp_2,F
   bra   d100l1
   return

This basically puts 255 and 131 in two variables and then the code translates to this
temp_1 = 255
temp_2 = 131

decfsz is an instruction which basically decreases the value of that variable. If the variable decreases down to 0, then the instruction right after the decfsz is skipped, otherwise it's executed.
bra is branch, it says to jump back to that label.

decfsz takes 1 or 2 cycles (if the variable becomes 0) , bra takes 2 cycles, return takes two cycles , the whole code is a sort of

for ( i = 1 to 255 )  {
  for ( j = 1 to 131) {
   nothing
  }
}
but the simple fact that those instructions decrease the variables temp_1 and temp_2 to 0 make the microcontroller waste cycles.  Those numbers 255 and 131  (0xFF and 0x83) are so chosen that if you add up how many cycles those instructions waste, you'll find out the value comes very close to 100.000 cycles (which is equal to 100 ms at 4 Mhz)

Since the PIC can't use big values, you can't just say there temp_1 = 100000 and then decfsz and go back, but you can make a small routing that waits 100 ms and in another routine that's supposed to wait 1 second, you can call the delay 100 ms routine ten times.

It's all about minimizing the bytes needed to tell the microcontroller to waste time.. that routine that makes the pic wait 100 ms probably translates to about 15-30 bytes of flash memory... pics don't have megabytes of storage so it matters, and it's certainly much better than 100.000 NOPs, you couldn't even write 100K in a pic.

the whole


 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf