introduction to pic16f84a based lcd programming with mikroc

15
Introduction to PIC16F84A Based LCD Programming with MikroC Page 1 Introduction to PIC16F84A Based LCD Programming with MikroC CONTENT 1) Introduction to Embedded Systems 2) Liquid Crystal Display Basics 3) MikroC Basics 4) First LCD Program 5) Downloading the Program – WinPic800 6) More Programs 7) Appendix 8) Reference

Upload: louie-reyes

Post on 13-Apr-2015

380 views

Category:

Documents


13 download

DESCRIPTION

pic burning

TRANSCRIPT

Page 1: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 1  

Introduction to PIC16F84A Based LCD Programming with MikroC        

     CONTENT 

 1)   Introduction to Embedded Systems   2) Liquid Crystal Display Basics  3) MikroC Basics  4) First LCD Program  5) Downloading the Program – WinPic800  6) More Programs  7) Appendix  8) Reference 

           

 

Page 2: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 2  

1) Introduction  

*What is an embedded system? An embedded system is a special‐purpose computer system designed to perform one or few dedicated functions.(Wijesundara, M.N.,2010). For this we kan use microcontrollers.  

 * µController vs. µProcessor A µController comes with built‐in peripherals [i.e. CPU,Memory(RAM/ROM),Input‐output] whereas a µProcessor needs such peripherals to be connected externally. For an example, in PIC16F84A µController CPU, memory and input/output(i/o) are integrated in a single chip. Conversely, your computers Pentium µProcessor requires i/o devices and memory devices which are external to the CPU. 

 *Well‐known Microcontroller Unit(MCU) Vendors  

MCU Vender  products Microchip  PIC12F, PIC16C, PIC18F, dsPIC34F Motorola  6804, 68HC11  Intel  8051, 8061 Atmel  AT89, AT91, AVR32 Philips  ARM9, ARM Cortex‐M3    For more: http://en.wikipedia.org/wiki/List_of_common_microcontrollers 

   *PIC1684A Architecture 

 

i/o ports –     PORT A  5 pins ‐ PORT B   8 pins 

Page 3: Introduction to PIC16F84A Based LCD Programming With MikroC

Introducti

 How to pA PIC MCUlevel langprogrammPIC prograHowever,the machprocess issoftware)software tsame time

ion to PIC16F8

program PICU kan be proguage such as ming. MikroC,amming using in order to dine code(*.hes called assem, firstly it shothe entire proe.  

•programminMikroC•Compilationproduces *.aassembly file

*.c

84A Based LC

C? grammed usiC or C++ cou, CCS C Compg C language.download(burex) from the hmbling.  If the ould be compiocess happen

ng using 

 asm e

D Programmi

ng a low leveld be used. M

piler, IAR Wor.  rn) the prograhuman readaprogram wasiled to generans automatica

Programm

•automatproducesmachine 

*.

ng with Mikro

el language suMPLAB IDE kakbench, GCC,

am to the chible assemblys written usinate the assemally and produ

ming with MikroC

ically s the *.hex code as well

.asm

oC 

uch as assembn be used for, etc are com

p, the progray source codeng C languagembly code. In uce both *.as

•*.hexcouldto the

bly. Alternativr assembly levmercially use

am must be tr(*.asm). The e (by any of thmost of the csm and *.hex 

x machine coded be downloadee chip 

*.hex

Page

 

vely a high vel ed software fo

ranslated to translation he above commercial files at the 

 

e ed 

e 3

or 

Page 4: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 4  

How to download the *.hex file to the PIC?  Domestically used software for this purpose are ICProg  and WinPIC800. However this needs external downloader circuitry. This would be either serial (Com) or USB based.  To build a JDM programmer:  http://www.semis.demon.co.uk/uJDM/uJDMmain.htm                                         

Page 5: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 5  

2)  LCD Basics In a 16x2 LCD there are 14 or 16 pins. 

PIN No.  PIN  comment1  Vss  0 V to the LCD 2  Vdd  +5 V to the LCD 3  Vee  Contrast (high when 0 V) 4  RS  Register Select (when 0 V, Data bits are taken as commands to the LCD 

and when +5 V, Data bits are taken as characters) 5  R/W  0 V to write to the LCD6  En  Data bits are transferred to the LCD module at the negative edge of this 

bit (from +5 V to 0 V) 7  D0    

 Data bits – These are considered as either commands or characters depending on the status of RS.  LCD requires 8‐bit data. These could be sent as 8 bits directly or 4 bits for two times. If it is the 4‐bit mode, then D4‐D7 pins are used.    

8  D1 9  D2 10  D3 11  D4 12  D5 13  D6 14  D7 15  LED+  Backlit – Generally it is 5 V DC. (Look if there are resistors connected to 

the LEDs!) 16  LED‐  In order to provide a stable 5 V DC supply, the following circuit kan be used. 

 

 5 V Power Regulator 

LCD pins kan be connected to the PIC as follows.

 

Pin configuration  

Page 6: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 6  

3) MikroC Basics  Project‐>New Project‐> 

i) Give a project name ii) Project path ‐  saving location  iii) Select Device as 16F84A iv) Select the clock as 004.000 v) Tick on WDT_OFF(Watch Dog Timer off)  and  XT_OSC (crystal oscillator). 

Make sure all other options are not ticked.  vi) Ok 

 EXCERSICE 1: Follow the above six steps to make a project named “LCD_TEST” in a folder located on the desktop.

  * Write any simple program within the main program opens and closeswith curly brackets.   

void main(){ ....write uer program here... }

 Make sure if you use brackets properly and do not make any case change (i.e. void main should be in simple letters). Also you will notice that all the command lines should be ended with semicolons.  

Page 7: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 7  

 *Simply press Crtl+S  or  project‐>save project.  *Press Crtl+F9 or project‐>Build .  This will produce both *.asm and *.hex files in the project folder. If you have done any mistake (i.e. syntax error), there will be an error message at the bottom of the window as follows indicating the relevant error and the corresponding line where you have done the mistake.  

 If there is no mistake, there would be a message indicating that the building process was completed successfully.  

   *There are a lot of inbuilt functions that kan be used directly. E.g. ‐ If you need a 150 ms delay you kan simply use:      Delay_ms(150);  Functions that are related to a particular purpose are categorised under libraries. If you want refer how to program LCD using inbuilt functions, go Help‐>MikroC Libraries‐>LCD (4‐bit interface) Library            

Page 8: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 8  

4) First LCD Program  

*Firstly you have to initialize the ports (pins). For this let us use the default function for initializing ports.  

Lcd_Init(&PORTB) we use port B to communicate with the LCD. If you use this function, by default the pins would be assigned as 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  Note that this configuration is compatible with the circuit that we have designed.   *The ports kan be used as either input or output as the designer wishes. Here we send data to the LCD and hence let us set port B as output. This kan be done by giving the command TRISB = 0 at the top of the main function.   

*You kan use Lcd_Out(1,3,”Hello!”) command to display want you want to display on the LCD. 

 *Also we kan use Lcd_cmd(Lcd_Clear) to erase the display.  *If you use double slash(i.e. //) wherewher in the program, the rest of the line is taken as a comment. It is only use for the clarity for the liveware and it is not considered in the compilation process by the 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); // Initialize LCD connected to PORTB Lcd_Cmd(Lcd_CLEAR); // Erase the display Lcd_Out(1, 3, “WELCOME”); // Print text to LCD,1st row, 3rd column }

Page 9: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 9  

                                   

 

Page 10: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 10  

5) Downloading the program in to the PIC Let us use WinPic800 software.   *Install the software and open it. Goto Settings  Hardware Select JDM Programmer set to COM1 Apply Edits *Select PIC 16F and 16F84A from the pop down menus in the top right hand coner of the main window, as shown in the figure.  

                                       *File Open  open the file that you want to download to the PIC. *Select Program All button(Download to the PIC). This will first automatically erase the program in the PIC and the new program will be burnt.  

            

Page 11: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 11  

6) More Programs   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

Page 12: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 12  

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!

Program 3 //Ransalu Senanayake_16-May-2010// char sec[7]; char mins[7]; char hrs[7]; unsigned short i,j,k; void main() { TRISB = 0; // PORTB is output i=0; j=0; k=0; 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

Page 13: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 13  

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

Issues with this clock: It  is impossible to adjust the time. If you simply use three switches and set as PORT B change interrupt, time kan be adjusted easily.  Also, there is an issue with the precision of the clock. The problem could be avoided to a certain extent, if TMR0 overflow interrupt is used, rather than using the delay function.  

                      

Page 14: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 14  

7) APPENDIX 

 Complete Cct Diagram 

 

PCB Layout‐1 for the cct 

 PCB Layout‐2 for the cct 

 

Page 15: Introduction to PIC16F84A Based LCD Programming With MikroC

Introduction to PIC16F84A Based LCD Programming with MikroC   Page 15  

8) Reference  [1] Katzen, Sid , 2000. The Quintessential PIC Microcontroller.    [2] SEMIE uJDM a Simple Programmer, online. Available at: http://www.semis.demon.co.uk/uJDM/uJDMmain.htm [Accessed: 19th May 2010]  [3] mikroElektronika, User’s Manual. Online. Available at:       

http://www.mikroe.com/pdf/mikroc/mikroc_manual.pdf[Accessed: 14th May 2010]  [4] Ilett, J. How to use Intelligent L.C.Ds  [5] Microchip, Datasheet: PIC16F84A  

             

Prepared by: (20‐May‐10)Ransalu Senanayake            2nd year Student,{ B.Eng(Hons) Degree in Electronic Engineering – Sheffield Hallam University(UK)}, 

         Department of Electronic and Computer Engineering,           Sri Lanka Institute of Information Technology, Sri Lanka. 

   Other tutorials by Ransalu: 

* Introduction to Matlab and Curve Fitting * Introduction to PCB Designing with orCAD v.9.2+