Anda di halaman 1dari 10

4. PIC example o 4.1. Basic control software 4.1.1. Requirements / features 4.1.2. Global declarations 4.1.2.1.

.1. Re ister declarations 4.1.2.2. !iteral declarations 4.1.2.". Procedure declarations / librar# interface 4.1.". Code 4.1.".1. !C$ initialisation 4.1.".2. Bus# fla 4.1.".". Clear displa# 4.1.".4. Cursor %ome 4.1.".&. 'ntr# mode 4.1.".(. $ispla# mode 4.1.".). *et c%aracter enerator R+, address 4.1.".-. *et displa# data R+, address 4.1."... Get address counter contents 4.1.".1/. 0rite c%aracter 4.1.".11. 0rite command 4.1.".12. $ela# loops o 4.2. +d1anced control software 4.2.1. 2ser defined c%aracters ... o 4.". +1ailabilit# o 4.4. 3ar et %ardware 4.4.1 Controller 4.4.2 Interface o 4.&. $e1elopment en1ironment 4.&.1. *oftware 4.&.2. 4ardware

4. PIC example
35C

4.1. Basic control software


,icroc%ip6s +7&-) was used as a basis for t%is code. WARNING: ,icroc%ip6s +7&-) %as ma8or errors in t%e read from !C$ code sequences. 3%e routines on t%is pa e use t%e correct read from !C$ code sequences.

35C 4.1.1. Requirements features 9 4$44)-/9based :industr#9standard; c%aracter9!C$< all software in t%is c%apter is based on it6s instruction9set. 9 PIC1(C-4 runnin on a 4,4= cr#stal< some code is based on t%is frequenc#. 9 -9bit interface between microcontroller and !C$9module. 35C 4.1.!. Glo"al #eclarations 3o et t%in s wor>in . 35C 4.1.!.1. Re$ister #eclarations
Purpose: - Tells MPASM which ports and registers (files) to use. Code: LCD_DATA LCD_DATA_T$'S LCD_CT$L LCD_T MP D LA, ._D LA, !" !" !" !" !" !" P#$T% T$'S% P#$TA ()(*( ()(*()(*/ & LCD data lines interface & LCD control lines interface & LCD su+routines internal use & "sed in D LA,))) routines & "sed in ._D LA,))) routines

35C 4.1.!.!. %iteral #eclarations


Purpose: - Literal declarations ( 0uates) used in the code. Code: & P#$TA control +its LCD_ !" LCD_$1 !" LCD_$S !"

* 2 (

& LCD na+le control line & LCD $ead31rite control line & LCD $egister-Select control line

35C 4.1.!.&. Proce#ure #eclarations li"rar' interface

*ince ,P!IB and ,P!I7? are not #et a1ailable< no declarations are needed. 35C 4.1.&. Co#e 35C 4.1.&.1. %C( initialisation
Purpose: - LCD initialisiation code to +e e)ecuted after power-up (i.e.4 before an5 other su+routines are used). - Should +e 6odified to 5our needs (i.e. displa5 t5pe7 cursor on3off7 etc.) Code: LCD'8'T CL$: M#;L1 CALL M#;L1 CALL M#;L1 CALL CALL M#;L1 CALL M#;L1 CALL $ T"$8 LCD_CT$L ()(2 ._D LA,<(( ()(-? LCDP"TCMD ()((( LCDDM#D LCDCL A$ ()((/ LCDDM#D ()((* LCD M#D & %us5-flag is not 5et 9alid & ALL P#$T output should output Low. & power-up dela5 & -( = (.<6S > 2<6S & %us5 :lag should +e 9alid fro6 here & ?-+it-interface7 *-lines & disp.off7 curs.off7 no-+lin@ & disp.on7 curs.off & auto-inc (shift-cursor)

35C 4.1.&.!. Bus' fla$


Purpose: - Tests if the LCD is +us5. $eturns when LCD +us5-flag is inacti9e. Code: LCD%"S, %S: M#;L1 M#;1: %C: %C: %S: %S: M#;: %C:

STAT"S7$P( ()(:: LCD_DATA_T$'S STAT"S7 $P( LCD_CT$L7 LCD_$S LCD_CT$L7 LCD_$1 LCD_CT$L7 LCD_ LCD_DATA7 1 LCD_CT$L7 LCD_

& Select $egister page 2 & Set P#$T% for input & & & & & & Select $egister page ( Set LCD for co66and 6ode Setup to read +us5 flag LCD -line Aigh $ead +us5 flag B DDra6 address LCD -line Low

A8DL1 %T:SS D#T# LCD8#T%"S, %C: %S: M#;L1 M#;1: %C: $ T"$8

()?( STAT"S7 C LCD%"S, LCD_CT$L7 LCD_$1 STAT"S7 $P( ()((( LCD_DATA_T$'S STAT"S7 $P(

& Chec@ %us5 flag7 Aigh > %us5

& Select $egister page 2 & Set P#$T% for output & Select $egister page (

35C 4.1.&.&. Clear #ispla'


Purpose: - Clears displa5 and returns cursor to ho6e position (upper-left corner). Code: LCDCL A$ M#;L1 CALL $ T"$8

()((2 LCDP"TCMD

35C 4.1.&.4. Cursor )ome


Purpose: - $eturns cursor to ho6e position. - $eturns displa5 to original position (when shifted). Code: LCDA#M M#;L1 CALL $ T"$8

()((* LCDP"TCMD

35C 4.1.&.*. +ntr' mo#e


Purpose: - Sets entr5 6ode of the LCD - $e0uired entr5 6ode 6ust +e set in 1 +( 4 ( > no displa5 shift7 2 > displa5 shift +2 4 ( > auto-decre6ent7 2 > auto-incre6ent +*-+E 4 donFt care Code: LCD M#D A8DL1

()((-

& Strip upper +its

'#$L1 CALL $ T"$8

()((/ LCDP"TCMD

& :unction set

35C 4.1.&.,. (ispla' mo#e


Purpose: - Sets displa5 control - $e0uired entr5 6ode 6ust +( 4 ( > cursor +lin@ +2 4 ( > cursor off7 +* 4 ( > displa5 off7 $AM) +--+E 4 donFt care Code: LCDDM#D A8DL1 '#$L1 CALL $ T"$8

+e set in 1 off7 2 > cursor +lin@ on (if +2 > 2) 2 > cursor on 2 > displa5 on (displa5 data re6ains in DD-

()((E ()((? LCDP"TCMD

& Strip upper +its & :unction set

35C 4.1.&.-. .et c)aracter $enerator RA/ a##ress


Purpose: - Sets the Character-Denerator-$AM address. CD$AM data is read3written after this setting. - $e0uired CD$AM address 6ust +e set in 1 +(-< 4 re0uired CD$AM address +G-E 4 donFt care Code: LCDSCDA A8DL1 '#$L1 CALL $ T"$8

()(-: ()(/( LCDP"TCMD

& Strip upper +its & :unction set

35C 4.1.&.0. .et #ispla' #ata RA/ a##ress


Purpose: - Sets the Displa5-Data-$AM address. DD$AM data is read3written after this setting. - $e0uired entr5 6ode 6ust +e set in 1 +(-G 4 re0uired DD$AM address +E 4 donFt care

Code: LCDSDDA '#$L1 CALL $ T"$8

()(?( LCDP"TCMD

& :unction set

35C 4.1.&.1. Get a##ress counter contents


Purpose: - $eturns address counter contents7 used for +oth DD$AM and CD$AM. - $AM address is returned in 1 Code: LCDDADD$ %S: M#;L1 M#;1: %C: %C: %S: %S: M#;: %C: A8DL1 %C: %S: M#;L1 M#;1: %C: $ T"$8

STAT"S7$P( ()(:: LCD_DATA_T$'S STAT"S7 $P( LCD_CT$L7 LCD_$S LCD_CT$L7 LCD_$1 LCD_CT$L7 LCD_ LCD_DATA7 1 LCD_CT$L7 LCD_ ()(E: LCD_CT$L7 LCD_$1 STAT"S7 $P( ()((( LCD_DATA_T$'S STAT"S7 $P(

& Select $egister page 2 & Set P#$T% for input & & & & & & & Select $egister page ( Set LCD for co66and 6ode Setup to read +us5 flag LCD -line Aigh $ead +us5 flag B $AM address LCD -line Low Strip upper +it

& Select $egister page 2 & Set P#$T% for output & Select $egister page (

35C 4.1.&.12. Write c)aracter


Purpose: - Sends character to LCD - $e0uired character 6ust +e in 1 Code: LCDP"TCAA$ M#;1: CALL %C: %S: %S: M#;: M#;1: %C: $ T"$8

LCD_T MP LCD%"S, LCD_CT$L7 LCD_CT$L7 LCD_CT$L7 LCD_T MP7 LCD_DATA LCD_CT$L7

LCD_$1 LCD_$S LCD_ 1 LCD_

& & & & &

Character to send is in 1 1ait for LCD to +e read5 Set LCD in read 6ode Set LCD in data 6ode LCD -line Aigh

& Send data to LCD & LCD -line Low

35C 4.1.&.11. Write comman#


Purpose: - Sends co66and to LCD - $e0uired co66and 6ust +e in 1 Code: LCDP"TCMD M#;1: CALL %C: %C: %S: M#;: M#;1: %C: $ T"$8

LCD_T MP LCD%"S, LCD_CT$L7 LCD_CT$L7 LCD_CT$L7 LCD_T MP7 LCD_DATA LCD_CT$L7

LCD_$1 LCD_$S LCD_ 1 LCD_

& & & & &

Co66and to send is in 1 1ait for LCD to +e read5 Set LCD in read 6ode Set LCD in co66and 6ode LCD -line Aigh

& Send data to LCD & LCD -line Low

35C 4.1.&.1!. (ela' loops


Purpose: - "sed in LCD'8'T su+routine - $e0uired dela5 factor 6ust +e in 1 (Could +e coded 6ore efficient7 +ut this approach gi9es 6ore fle)i+ilit5) Code: &=================================== D LA,<(( M#;L1 DF2G<F & M#;1: D LA, & D LA,<((_L##P D C:SC D LA,7 : & D#T# D LA,<((_L##P & D LA,<((_ 8D $ T"$8 & &=================================== ._D LA,<(( M#;1: ._D LA, & ._D LA,<((_L##P CALL D LA,<(( & D C:SC ._D LA,7 : & D#T# ._D LA,<((_L##P & ._D LA,<((_ 8D $ T"$8 & a <((uS dela5 H /MAI .-tal B2 B* step2 step* B2 c5cle 2 c5cle 2 c5cle * c5cles * c5cles

a dela5 of F1F = <((6S B2 step2 step* stepB* 2 c5cle wait <((uSec 2 c5cle * c5cles * c5cles

35C

4.!. A#3ance# control software


35C

4.!.1. 4ser #efine# c)aracters


Purpose: After se9eral re0uests a 0uic@ e)planation on how to i6ple6ent user-defined characters4 :irst 5ouFll need to 6a@e a pi)el definition for the characters 5ou want to use. This is the pi)el definition for an underlined F(F (char code ()-() +ased on a <)E dots character definition4 J +its J +5te row J EG</-*2( J 9alue -----------------------((( J ))) J ()( ((2 J ) ) J ()22 (2( J ) )) J ()2(22 J ) ) ) J ()2< 2(( J )) ) J ()2K 2(2 J ) ) J ()22 22( J ))) J ()( 222 J ))))) J ()2: The +5te 9alues need to +e loaded into CD$AM address ((cccrrr (+inar5)7 where4 - ccc > user-defined character nu6+er ((...E) - rrr > row nu6+er of the user defined character ((...E) #nce thatFs done 5ou can write character codes (...E to the desired LCD character position7 Lust li@e 5ou do with Fnor6alF characters. "ser-defined character definitions 6a5 +e changed Fon-the-fl5F. 1hile defining a <)E dots character4 - Character code +its (DD$AM) *..( correspond to CD$AM address +its <..(i.e. ? possi+le user defined characters). 1hile defining a <)2( dots character4 - Character code +its (DD$AM) *..2 correspond to CD$AM address +its <../ (i.e. / possi+le user defined characters). 'tFs +est to switch off the cursor while writing to CD$AM. Code: (More detailed code 6a5 +e pu+lished so6e da5)

35C 4.&. A3aila"ilit'

!C$9PIC.@IP :1)<).( b#tes;A an example usin some of t%e abo1e subroutines :all subroutines are included;. *ource is coded for a 4B2/ !C$< ad8ust it to #our needsC *%ows t%e followin screen on a 4B2/ !C$A
---------------------JThis is on line 4 (J JThis is on line 4 2J JThis is on line 4 *J JThis is on line 4 -J ----------------------

*%ows t%e followin screen on a 2B4/ !C$A


----------------------------------------JThis is on line 4 (This is on line 4 *J JThis is on line 4 2This is on line 4 -J ------------------------------------------

*%ows t%e followin screen on a 2B2/ !C$A


---------------------JThis is on line 4 (J JThis is on line 4 2J ----------------------

35C

4.4. 4se# )ar#ware


35C 4.4.1 Controller 9 + PIC1(C-4 is used to control t%e !C$. 9 -9bit data interface between controller and !C$. 35C 4.4.! Interface

*%ift9Clic> to download if.

Anda mungkin juga menyukai