Complex program to write into a LCD - Page 1
Forum section: electronics / general

author: tahmod
author_rank: Contributor
timestamp: April 19, 2013, 11:16:15 pm
content: 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 __18F8722list p=18F8722#include p18F8722.incCONFIG OSC = HSPLLCONFIG FCMEN = OFFCONFIG IESO = OFFCONFIG PWRT = OFFCONFIG BOREN = OFFCONFIG WDT = OFFCONFIG MCLRE = ONCONFIG LVP = OFFCONFIG XINST = OFF#endif#ifdef __18F87J11list p=18F87J11#include p18F87J11.incCONFIG FOSC = HSPLLCONFIG STVREN = OFF,XINST = OFF,WDTEN = OFF,CP0 = OFF,FCMEN = OFF,IESO = OFFCONFIG 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 select4.what is these 2 line code doing?EXTERN LCDInit, temp_wr, d_write, i_write, LCDLine_1, LCDLine_2EXTERN Delay,Check,InitSPI;5. They giving location for the SFR into the memory location?- but what is the variable UDATE_ACS?variables UDATA_ACSptr_pos RES 1ptr_count RES 1temp_1 RES 1temp_2 RES 1temp_3 RES 1;6 I dont understand why they use the NOP instruction here? -we dont need some delay here?STARTUP CODE 0NOPgoto startNOPNOPNOPPROG1 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 " ;0data " " ;16data " Menu Item 2 " ;32data " Menu Item 3 " ;48data "RB0=Now RA5=Next" ;64data "Microchip PICDEM" ;80data " PIC18 Explorer " ;96data "LCDMenu Demo NPS" ;112data "Item 3 Execution" ;128data "Item 2 Execution" ;144data "Item 1 Execution" ;160startbsf OSCTUNE,PLLEN ;8. what is Osctune doing? Enable PLLcall LCDInit ;Initialize the LCD;9i the LCinit just call an instruction in the other file, but what happen in the other file?#ifdef __18F8722movlw b'00001110' ;RA0 = analog inputmovwf ADCON1#endif#ifdef __18F87J11bsf WDTCON,ADSHR ;Shared SFRsetf ANCON0 ;10. this set the whole bits of ANCON0 or a single bit?setf ANCON1bcf ANCON1,0 ;11. having a zero in the bi one of ANCON1 Make RA0 Analog input?bcf WDTCON,ADSHR#endif12. what is WDTCON, TXSTA and RCSTA?movlw B'10100100' ;initialize USARTmovwf TXSTA ;8-bit, Async, High Speedmovlw .255movwf SPBRG ;9.6kbaud @ 40MHzmovlw B'10010000'movwf RCSTAbsf TRISB,0 ;make switch RB0 an Inputbsf TRISA,5 ;make switch RA5 an Input;**************** STANDARD CODE MENU SELECTION *******************;Introductionmovlw .80 ;send "Microchip PICDEM" to LCDmovwf ptr_poscall stan_char_1movlw .96 ;send " PIC18 Explorer " to LCDmovwf ptr_poscall stan_char_2call delay_1s ;delay for displaymovlw .112 ;send "LCDMenu Demo " to LCDmovwf ptr_poscall stan_char_1call delay_1s ;delay for displaycall delay_1s ;delay for displaymenu;13. what is the purpose of waiting for A5 and RBo to release?btfss scroll ;wait for RA5 releasegoto $-2btfss select ;wait for RB0 releasegoto $-2;------------------ MENU item 1 ----------------------------call delay_100mscall delay_100mscall delay_100msbtfss scroll ;YESbra $-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_poscall 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_poscall stan_char_2;wait for key pressv_waitbtfss select ;execute item1?bra item1 ;YESbtfsc scroll ;Goto Nextbra 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_2call delay_100mscall delay_100mscall delay_100msbtfss scroll ;wait for RA5 releasebra $-2movlw .32 ;Display "Menu Item 2"movwf ptr_poscall stan_char_1movlw .64 ;Display "RB0=Now RA5=Next"movwf ptr_poscall stan_char_2;wait for key presst_waitbtfss select ;Execute Item 2 code?bra item2 ;YESbtfsc scroll ;Goto Nextbra t_wait ;NO;------------------ MENU item 3 ----------------------------menu_3call delay_100mscall delay_100mscall delay_100msbtfss select; ;wait for RB0 releasebra $-2;movlw .48 ;Display "Menu Item 3 "movwf ptr_poscall stan_char_1movlw .64 ;Display "RB0=Now RA5=Next"movwf ptr_poscall stan_char_2c_waitbtfss select ;execute item3 code?bra item3 ;YESbtfsc scroll ;Go back to Main menubra c_wait ;NObra menu ;begining of menu;*******************************************************************;************* USER CODE *************************************;------------- Menu Item 1 -------------------------------------item1movlw .160 ;Display "Item 1 Execution"movwf ptr_poscall stan_char_2bra menu;------------- Menu Item 2 -------------------------------------item2movlw .144 ;Display "Item 2 Execution"movwf ptr_poscall stan_char_2bra menu_2;------------- Menu Item 3 -------------------------------------item3movlw .128 ;Display "Item 3 Execution"movwf ptr_poscall stan_char_2bra 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_1call LCDLine_1 ;mvoe cursor to line 1movlw .16 ;1-full line of LCDmovwf ptr_countmovlw UPPER stan_tablemovwf TBLPTRUmovlw HIGH stan_tablemovwf TBLPTRHmovlw LOW stan_tablemovwf TBLPTRLmovf ptr_pos,Waddwf TBLPTRL,Fclrf WREGaddwfc TBLPTRH,Faddwfc TBLPTRU,Fstan_next_char_1tblrd *+movff TABLAT,temp_wrcall d_write ;send character to LCDdecfsz ptr_count,F ;move pointer to next charbra stan_next_char_1movlw "\n" ;move data into TXREGmovwf TXREG ;next linebtfss TXSTA,TRMT ;wait for data TXgoto $-2movlw "\r" ;move data into TXREGmovwf TXREG ;carriage returnbtfss TXSTA,TRMT ;wait for data TXgoto $-2return;----Standard code, Place characters on line-2--------------------------stan_char_2call LCDLine_2 ;move cursor to line 2movlw .16 ;1-full line of LCDmovwf ptr_countmovlw UPPER stan_tablemovwf TBLPTRUmovlw HIGH stan_tablemovwf TBLPTRHmovlw LOW stan_tablemovwf TBLPTRLmovf ptr_pos,Waddwf TBLPTRL,Fclrf WREGaddwfc TBLPTRH,Faddwfc TBLPTRU,Fstan_next_char_2tblrd *+movff TABLAT,temp_wrcall d_write ;send character to LCDdecfsz ptr_count,F ;move pointer to next charbra stan_next_char_2movlw "\n" ;move data into TXREGmovwf TXREG ;next linebtfss TXSTA,TRMT ;wait for data TXgoto $-2movlw "\r" ;move data into TXREGmovwf TXREG ;carriage returnbtfss TXSTA,TRMT ;wait for data TXgoto $-2return;----------------------------------------------------------------------;------------------ 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 100msdelay_100mscall Delaysmallcall Delaysmallcall Delaysmallcall Delaysmallcall Delaysmallcall Delaysmallcall Delaysmallcall Delaysmallcall Delaysmallcall DelaysmallreturnDelaysmall ;100ms at 4Mhzmovlw 0xFFmovwf temp_1movlw 0x83movwf temp_2d100l1decfsz temp_1,Fbra d100l1decfsz temp_2,Fbra d100l1return;---------------- 1s Delay -----------------------------------delay_1scall Ldelaycall Ldelaycall Ldelaycall Ldelaycall Ldelaycall Ldelaycall Ldelaycall Ldelaycall Ldelaycall LdelayreturnLdelay ;1S at f Mhxmovlw 0xFFmovwf temp_1movwf temp_2movlw 0x05movwf temp_3d1l1decfsz temp_1,Fbra d1l1decfsz temp_2,Fbra d1l1decfsz temp_3,Fbra d1l1return;*********************************************************************endThe code for the LCD file is here.https://www.eevblog.com/forum/microcontrollers/how-can-you-understand-a-code-s/?topicseen

author: mariush
author_rank: Super Contributor
timestamp: April 20, 2013, 01:38:13 am
content: 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.pdfThis 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 , 3The 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 languageSeehttp://ww1.microchip.com/downloads/en/DeviceDoc/33014K.pdfhttp://ww1.microchip.com/downloads/en/DeviceDoc/MPASM_Quick_Ref_Card_30400g.pdfRead 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 codeIt'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 PLLREAD THE MANUAL for that PIC18 chip ... it says there what OSCTUNE means.http://ww1.microchip.com/downloads/en/devicedoc/39646c.pdfpage 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 do17 .. 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 4Mhzmovlw 0xFFmovwf temp_1movlw 0x83movwf temp_2d100l1decfsz temp_1,Fbra d100l1decfsz temp_2,Fbra d100l1returnThis basically puts 255 and 131 in two variables and then the code translates to thistemp_1 = 255temp_2 = 131decfsz 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 offor ( 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