Introduction To PIC16F84A Based LCD Programming With MikroC
Introduction To PIC16F84A Based LCD Programming With MikroC
CONTENT
1) Introduction *Whatisanembeddedsystem?
Anembeddedsystemisaspecialpurposecomputersystemdesignedtoperformoneorfew dedicatedfunctions.(Wijesundara,M.N.,2010).Forthiswekanusemicrocontrollers.
*Controllervs.Processor
AControllercomeswithbuiltinperipherals[i.e.CPU,Memory(RAM/ROM),Inputoutput]whereasa Processorneedssuchperipheralstobeconnectedexternally.Foranexample,inPIC16F84A ControllerCPU,memoryandinput/output(i/o)areintegratedinasinglechip.Conversely,your computersPentiumProcessorrequiresi/odevicesandmemorydeviceswhichareexternaltothe CPU.
*WellknownMicrocontrollerUnit(MCU)Vendors MCUVender products Microchip PIC12F,PIC16C,PIC18F,dsPIC34F Motorola 6804,68HC11 Intel 8051,8061 Atmel AT89,AT91,AVR32 Philips ARM9,ARMCortexM3
Formore:http://en.wikipedia.org/wiki/List_of_common_microcontrollers
*PIC1684AArchitecture
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC
Page2
Howtop programPIC C?
APICMCU Ukanbeprog grammedusingalowleve ellanguagesu uchasassemb bly.Alternativ velyahigh levellanguagesuchasCorC++couldbeused.M MPLABIDEkanbeusedfor rassemblylev vel programm ming.MikroC, ,CCSCComp piler,IARWorkbench,GCC, ,etcarecommerciallyuse edsoftwarefo or PICprogra ammingusing gClanguage. . However,inordertod download(bur rn)theprogra amtothechip,theprogra ammustbetr ranslatedto themachinecode(*.he ex)fromtheh humanreadableassembly ysourcecode(*.asm).Thetranslation processis scalledassem mbling.Iftheprogramwas swrittenusin ngClanguage e(byanyofth heabove software),firstlyitsho ouldbecompi iledtogenera atetheassem mblycode.Inmostofthec commercial theentirepro ocesshappen nsautomatica allyandprodu uceboth*.as smand*.hexfilesatthe softwaret sametime e.
*. .asm
automatically sthe*.hex produces machinecodeaswell
*.c
Programm mingwithMikroC C
*.hex
Page e3
2) LCDBasics
Ina16x2LCDthereare14or16pins. PINNo. PIN comment 1 Vss 0VtotheLCD 2 Vdd +5VtotheLCD 3 Vee Contrast(highwhen0V) 4 RS RegisterSelect(when0V,DatabitsaretakenascommandstotheLCD andwhen+5V,Databitsaretakenascharacters) 5 R/W 0VtowritetotheLCD 6 En DatabitsaretransferredtotheLCDmoduleatthenegativeedgeofthis bit(from+5Vto0V) 7 D0 8 D1 DatabitsTheseareconsideredaseithercommandsorcharacters 9 D2 dependingonthestatusofRS. 10 D3 LCDrequires8bitdata.Thesecouldbesentas8bitsdirectlyor4bitsfor 11 D4 twotimes.Ifitisthe4bitmode,thenD4D7pinsareused. 12 D5 13 D6 14 D7 15 LED+ BacklitGenerallyitis5VDC.(Lookifthereareresistorsconnectedto 16 LED theLEDs!) Inordertoprovideastable5VDCsupply,thefollowingcircuitkanbeused.
5VPowerRegulator
LCDpinskanbeconnectedtothePICasfollows.
Pinconfiguration
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC
Page5
3) MikroCBasics
Project>NewProject> i) Giveaprojectname ii) Projectpathsavinglocation iii) SelectDeviceas16F84A iv) Selecttheclockas004.000 v) TickonWDT_OFF(WatchDogTimeroff)andXT_OSC(crystaloscillator). Makesureallotheroptionsarenotticked. vi) Ok
EXCERSICE 1: Follow the above six steps to make a project named LCD_TEST in a folder located on the desktop.
*Writeanysimpleprogramwithinthemainprogramopensandcloseswithcurlybrackets. void main(){ ....write uer program here... } Makesureifyouusebracketsproperlyanddonotmakeanycasechange(i.e.void mainshould beinsimpleletters).Alsoyouwillnoticethatallthecommandlinesshouldbeendedwith semicolons.
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC Page6
Ifthereisnomistake,therewouldbeamessageindicatingthatthebuildingprocesswascompleted successfully.
4) FirstLCDProgram
*Firstlyyouhavetoinitializetheports(pins).Forthisletususethedefaultfunctionforinitializing ports. Lcd_Init(&PORTB) weuseportBtocommunicatewiththeLCD.Ifyouusethisfunction,by defaultthepinswouldbeassignedas D7 of LCD RB7 pin of the PIC D6 of LCD RB6 pin of the PIC D5 of LCD RB5 pin of the PIC D4 of LCD RB4 pin of the PIC E of LCD RB3 pin of the PIC RS of LCD RB2 pin of the PIC RW of LCD RB0 pin of the PIC Notethatthisconfigurationiscompatiblewiththecircuitthatwehavedesigned. *Theportskanbeusedaseitherinputoroutputasthedesignerwishes.Herewesenddatatothe LCDandhenceletussetportBasoutput.ThiskanbedonebygivingthecommandTRISB = 0 at thetopofthemainfunction. *YoukanuseLcd_Out(1,3,Hello!)commandtodisplaywantyouwanttodisplayonthe LCD.
*AlsowekanuseLcd_cmd(Lcd_Clear) toerasethedisplay. *Ifyouusedoubleslash(i.e.//)wherewherintheprogram,therestofthelineistakenasacomment. Itisonlyusefortheclarityforthelivewareanditisnotconsideredinthecompilationprocessbythe computer. E.g.Delay_ms(1000); //Pause for 1 second EXCERSICE 1:Type the following program on the workspace and save. Then build it. Do not miss semocolons/brackets or do not confuse the case!! void main() { TRISB = 0; // PORTB is output Lcd_Init(&PORTB); Lcd_Cmd(Lcd_CLEAR); // Initialize LCD connected to PORTB // Erase the display
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC
Page8
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC Page9
5) DownloadingtheprogramintothePIC LetususeWinPic800software. *Installthesoftwareandopenit.GotoSettings Hardware SelectJDMProgrammer set toCOM1 ApplyEdits *SelectPIC16Fand16F84Afromthepopdownmenusinthetoprighthandconerofthe mainwindow,asshowninthefigure.
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC Page10
6) MorePrograms Program1:
void main() { TRISB = 0; // PORTB is output Lcd_Init(&PORTB); Lcd_Cmd(Lcd_CURSOR_OFF); //Turn off the cursor while(1) { //Run the following for infinite times Lcd_Cmd(Lcd_CLEAR); //Erase the display Lcd_Out(1,2,"Nalin de Silva"); // 1st row Delay_ms(1500); Lcd_Cmd(Lcd_CURSOR_OFF); Lcd_Cmd(Lcd_CLEAR); //erase the display Lcd_Out(1,1,"25/1,Weeramon Rd"); //1st row Lcd_Out(2,1,"Panadura"); //2nd row Delay_ms(2000); } // End of the while loop-Run upto this point and then repeat from while }//End of the main
Program2:
char *text = "WELCOME"; //text as a pointer char *text2 = "('_')"; //smiley void main() { Lcd_Init(&PORTB); LCD_Cmd(LCD_CLEAR); Lcd_Cmd(LCD_BLINK_CURSOR_ON); //Blinking cursor ON Lcd_Out(1,1,text2); //Display the smiley- left left Lcd_Out(1,12,text2); //Display the smiley- right side Delay_ms(2000); //Wait for 2 seconds LCD_Cmd(LCD_CLEAR); //Erase the display Lcd_Out(1,6,text); //Display WELCOME Delay_ms(2000); //Wait for 2 seconds LCD_Chr(2,2,'N'); //Display N Delay_ms(1000); //wait for a second LCD_Chr(2,3,'A'); //Display N Delay_ms(1000); //wait for a second LCD_Chr(2,4,'L'); //Display N
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC Page11
Delay_ms(1000); //wait for a second LCD_Chr(2,5,'I'); //Display N Delay_ms(1000); //wait for a second LCD_Chr(2,6,'N'); //Display N Delay_ms(1000); //wait for a second while(1){ //infinite loop LCD_Cmd(LCD_TURN_OFF); //Turn off the Display Delay_ms(2000); //wait for 2 seconds LCD_Cmd(LCD_TURN_ON); //Turn off the Display Delay_ms(2000); //wait for 2 seconds } } Think about another way to display the same thing!
Program3
//Ransalu Senanayake_16-May-2010// char sec[7]; char mins[7]; char hrs[7]; unsigned short i,j,k; void main() { TRISB = 0; i=0; j=0; k=0;
// PORTB is output
Lcd_Init(&PORTB);// WR,*,RS,EN,D4,D5,D6,D7 <==PORTB 0-7 LCD_Cmd(LCD_CLEAR); LCD_Cmd(LCD_CURSOR_OFF); Lcd_Out(1,2,"Hrs:"); Lcd_Out(1,7,"Min:"); Lcd_Out(1,12,"Sec:"); while(1){ while(k<24){ //Hours ShortToStr(k,hrs); Lcd_Out(2,1,hrs); while(j<60){ // Minutes ShortToStr(j,mins); Lcd_Out(2,6,mins); while(i<60) { //Seconds
Page12
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC
ShortToStr(i,sec); Lcd_Out(2,11,sec); Delay_ms(990);//yet to be adjusted for precision i++; } i=0; j++; } j=0; k++; } k=0; } } //End Main Issueswiththisclock: Itisimpossibletoadjustthetime.IfyousimplyusethreeswitchesandsetasPORTBchange interrupt,timekanbeadjustedeasily. Also,thereisanissuewiththeprecisionoftheclock.Theproblemcouldbeavoidedtoacertain extent,ifTMR0overflowinterruptisused,ratherthanusingthedelayfunction.
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC Page13
7) APPENDIX
CompleteCctDiagram
PCBLayout1forthecct
PCBLayout2forthecct
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC Page14
8) Reference
[1]Katzen,Sid,2000.TheQuintessentialPICMicrocontroller. [2]SEMIEuJDMaSimpleProgrammer,online.Availableat: http://www.semis.demon.co.uk/uJDM/uJDMmain.htm[Accessed:19thMay2010] [3]mikroElektronika,UsersManual.Online.Availableat: http://www.mikroe.com/pdf/mikroc/mikroc_manual.pdf[Accessed:14thMay2010] [4]Ilett,J.HowtouseIntelligentL.C.Ds [5]Microchip,Datasheet:PIC16F84A
Preparedby:(20May10)RansaluSenanayake 2ndyearStudent,{B.Eng(Hons)DegreeinElectronicEngineeringSheffieldHallam University(UK)}, DepartmentofElectronicandComputerEngineering, SriLankaInstituteofInformationTechnology,SriLanka.
OthertutorialsbyRansalu:
*IntroductiontoMatlabandCurveFitting *IntroductiontoPCBDesigningwithorCADv.9.2+
IntroductiontoPIC16F84ABasedLCDProgrammingwithMikroC
Page15