Getting Started With PIC 18F Microcontroller - MikroC PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

7/30/2016

GettingStartedwithPIC18FMicrocontrollerMikroC

(https://electrosome.com/)

Home (https://electrosome.com) /
Tutorials (https://electrosome.com/category/tutorials/), PIC Microcontroller (https://electrosome.com/category/tutorials/pic-microcontroller/),
MikroC (https://electrosome.com/category/tutorials/pic-microcontroller/mikroc/) /
Getting Started with PIC 18F Microcontroller MikroC

Getting Started with PIC 18F Microcontroller MikroC

RECEN

(https:/
amplie
23
Jun

Dieren
Transist
(https://
amplie

Getting Started with PIC 18F Microcontroller MikroC


By Ligo George (https://electrosome.com/author/lijoppans/)
MikroC (https://electrosome.com/category/tutorials/pic-microcontroller/mikroc/), PIC Microcontroller
(https://electrosome.com/category/tutorials/pic-microcontroller/), Tutorials (https://electrosome.com/category/tutorials/)

July 24, 2

18F (https://electrosome.com/tag/18f/), Microcontroller (https://electrosome.com/tag/microcontroller/), MikroC


(https://electrosome.com/tag/mikroc/), PIC (https://electrosome.com/tag/pic/), PIC 18F (https://electrosome.com/tag/pic-18f/),
Proteus (https://electrosome.com/tag/proteus/), Tutorials (https://electrosome.com/tag/tutorials/)
2 Comments (https://electrosome.com/getting-started-pic-18f-microcontroller/#disqus_thread)

In this tutorial we will learn how to program PIC 18F Microcontrollers using
MikroC Pro compiler. I hope that you already know the basics of PIC
Programming using MikroC compiler. If you are a beginner to this eld, please
read our rst tutorial Blinking LED using PIC Microcontroller MikroC
(https://electrosome.com/led-blinking-pic/). There are a lot of advantages for
PIC 18F microcontrollers compared to 16F. Some of them are given below.
PIC 18F is optimized for C while PIC 16F is not.
PIC 18F has interrupt priority (high and low priority) while PIC 16F doesnt.
PIC 18F has internal PLL (Phase Locked Loop) to multiply clock frequency
and by thus increases the operating speed.
PIC 18F has more memory.
PIC 18F has hardware multiplier while PIC 16F doesnt.
PIC 18F has more instructions than PIC 16F which makes assembly
language programming simpler and it also reduces the size of HEX File
generated in other compilers.

(https:/
alarm-5
Contents

1 MikroC Programming
1.1 Writing Bit by Bit :

June 26,

1.2 Writing Entire Register


1.3 MikroC Code Control LED
using Switch
2 Circuit Diagram
3 Buy Here
3.0.1 Crystal Oscillator

(https:/
display-

Microcontroller

LCD Disp
(https://
display-f

3.0.3 USB PIC Programmer

May 9, 20

3.0.2 PIC 16F877A

PICkit 2

PIC 18F eliminates Read Modify Write Problems with Mid-Rang PIC Microcontrollers
(https://electrosome.com/read-modify-write-problem-pic/).
In PIC 18F, we neednt bother about bank switching but in PIC 16F it is a big problem if we are using assembly
language programming.

https://electrosome.com/gettingstartedpic18fmicrocontroller/

Rain Ala
(https://
alarm-55

(https:/
cloud-p

EasyEDA
Design S
(https://
cloud-pc
1/9

7/30/2016

GettingStartedwithPIC18FMicrocontrollerMikroC

cloud-pc
In 16F PIC Microcontrollers we have two registers to control IO pins, which are TRIS and PORT. TRIS is used to control April 28,
direction (Input or Output) of each IO pin while PORT is used to Write or Read data from IO pins. Whereas in 18F PIC
Microcontrollers there is an additional register, LAT. LAT stands for Port Latch. As in 16F, 18F also uses TRIS register to
control direction of each IO pin. LAT register is used to write Output and PORT register is used read Inputs. I

recommend you to read the article Read Modify Write Problems with PIC Microcontrollers
(https:/
(https://electrosome.com/read-modify-write-problem-pic/) for understanding the internal working of LAT register.
pic-mic
xc8/)

IC Com
Microco
(https://
microco
April 11,

(https:/
RECEN

night-la

Automa
(https://
night-lam
(https:/
(https://electrosome.com/wp-content/uploads/2014/06/TRIS-PORT-LAT-Registers.jpg)

February
amplie

Dieren
Transist
CATEG
Working of TRIS, PORT and LAT registers can be understand from the above diagram. Logic High (1) at TRIS register (https://
makes corresponding pin of PIC microcontroller Input while Logic Low (0) at TRIS register makes corresponding pin amplie
July 24, 2
Output. When a pin is congured as Input, it will be in High Impedance state and its physical state (voltage level) can be
TRIS PORT and LAT Registers

read using PORT register. When a pin is congured as Output, its output logic state will be determined by LAT register. 1
at LAT register makes corresponding pin Logic High (VDD) and 0 a t LAT register makes corresponding pin Logic Low

(VSS). LAT register bits corresponding to Input pins have no eect on Output or Input, thus they are Dont Care.
Following points should be noted.
(https:/
alarm-5
LAT write operation writes LAT register.
Rain Ala
LAT read operation reads LAT register.
(https://
PORT write operation writes LAT register.
alarm-55
PORT read operation reads the physical state (voltage level) of all the pins.
June 26,
Thus you can write outputs using PORT write or LAT write operation but using PORT write operation is not
recommended as it may causeRMW Problem (https://electrosome.com/read-modify-write-problem-pic/).

MikroC Programming
You can write PORT and TRIS registers entirely or bit by bit.

(https:/
display-

LCD Disp
(https://
display-f

May 9, 20

Writing Bit by Bit :


TRISC.F0=1;//Makes0thbitofPORTCInput
TRISC.F5=0;//Makes5thbitofPORTCOutput
LATC.F3=1;//Makes3rdbitofPORTBatLogicHigh
LATC.F7=0;//Makes7thbitofPORTBatLogicLow

Writing Entire Register

https://electrosome.com/gettingstartedpic18fmicrocontroller/

(https:/
cloud-p

EasyEDA
Design S
(https://
cloud-pc
2/9

7/30/2016

GettingStartedwithPIC18FMicrocontrollerMikroC

Writing Entire Register

cloud-pc
April 28,

A number with a prex 0b indicates a binary number.


A number with a prex 0 indicates an octal number.

(https:/
pic-mic
xc8/)

A number with a prex 0x indicates a hexadecimal number.


A number without prex is a decimal number.
Lets see some examples
Decimal

Binary

Octal

Hexadecimal

0b00000000

00

0x00

0b00000001

01

0x01

128

0b10000000

0200

0x80

255

0b11111111

0377

0xFF

TRISB=0;//MakesallpinsofPORTBOutput
LATB=0xFF;//MakesallpinsofPORTBLogicHigh
TRISC=255;//MakesallpinsofTRISCInput
TRISD=0200;//Makes7thpinofPORTDOutput
LATD=0b10000000;//Makes7thpinofPORTDLogicHigh

IC Com
Microco
(https://
microco
April 11,

(https:/
RECEN

night-la

Automa
(https://
night-lam
(https:/

In this tutorial we will learn How to Read an Input pin and How to Write Output. For demonstration purpose we will use February
amplie
PIC 18F4550, a very common PIC Microcontroller. If you want to use any other 18F microcontroller, just select it while
Dieren
creating the project or edit it through MikroC Project Settings (Project >> Edit Project). I will recommend 18F452 and
Transist
18F4520 as they are also very common and their pins are compatible with PIC 16F877A. Here an LED is connected to CATEG
(https://
15th pin (RC0) of 18F4550 via a current limiting resistor. Hence the 15th pin should be congured as an Output Pin. amplie
Similarly Push Button Switch is connected to 19th (RD0) pin which should be congured as Input Pin.
July 24, 2
If you havent yet started with MikroC, please read the tutorial Blinking LED using PIC Microcontroller
(https://electrosome.com/led-blinking-pic/).

(https:/
alarm-5

MikroC Code Control LED using Switch


voidmain()
{
TRISC.F0=0;//PORTC0Output
TRISD.F0=1;//PORTD0Input
LATC.F0=0;//LEDOFF
while(1)
{
if(PORTD.F0==0)//IFSwitchPressed
{
LATC.F0=1;//LEDOn
Delay_ms(2000);//2SecondDelay
LATC.F0=0;//LEDOff
}
}
}

Rain Ala
(https://
alarm-55
June 26,

(https:/
display-

LCD Disp
(https://
display-f

May 9, 20

Delay_ms() is a predened function in MikroC Pro which will provide delay in milliseconds.

Circuit Diagram
https://electrosome.com/gettingstartedpic18fmicrocontroller/

(https:/
cloud-p

EasyEDA
Design S
(https://
cloud-pc
3/9

7/30/2016

GettingStartedwithPIC18FMicrocontrollerMikroC

cloud-pc
April 28,

(https:/
pic-mic
xc8/)

IC Com
Microco
(https://
microco
April 11,

TAGS
3D

(https:/
(https://e
RECEN

night-la

(https://electrosome.com/wp-content/uploads/2014/01/Getting-Started-with-PIC-18FMicrocontroller.jpg)
LED connected to PIC 18F4550

VDD and VSS of PIC Microcontroller is connected to +5V and GND respectively to provide necessary power for the
operation of the microcontroller. 20MHz crystal is used to provide necessary clock for the microcontroller. 22pF
capacitors stabilizes the oscillations of the crystal. LED is connected to the 0thbit of PORTC (15th pin) and a 470 resistor

16F877A
Automa
(https://e
(https://
night-lam
555
(https:/

(https://e
February
amplie

Dieren
8051 Mic
(https://e
Transist
th
th
is connected in series to limit the current through the LED. Switch is connected to 0 bit of PORTD (19 pin) and a 10K CATEG
microcon
(https://
resistor is used as PULL UP. It ensures that 19th pin will be in HIGH state when the switch is not pressed.
amplie
android

July
24, 2
(https://e

You can download MikroC and Proteus les here.

Arduino

Getting Started with PIC 18F Microcontrollers (https://electrosome.com/wp-content/uploads/2014/06/Getting-Started- (https://e


with-PIC-18F-Microcontrollers.zip)
Arduino

(https:/
(https://e
alarm-5
uno/)

Rain Ala
AT89C51
(https://
(https://e
alarm-55

Buy Here

June
26,
Atmega3

(https://e

SALE

-31%

Atmel
(https://e

(https:/
AVR
(https://e
display-

LCD Disp
DC Moto
(https://
(https://e
motor/)
display-f

May 9, 20

electron
(https://e
2/)

Embedd

(https://electrosome.com/shop/pic-16f877amicrocontroller/)
PIC 16F877A Microcontroller
https://electrosome.com/gettingstartedpic18fmicrocontroller/

(https://e
(https:/
cloud-p
google

(https://e
EasyEDA
Design S
Graphen
(https://
(https://e
cloud-pc

4/9

7/30/2016

GettingStartedwithPIC18FMicrocontrollerMikroC

(https://electrosome.com/shop/pic-16f877amicrocontroller/)

200.00

139.00

cloud-pc

April
28, C
Hi-Tech

(https://e
tech-c/)

Intel
(https://e

(https:/
Add to cart (/getting-started-pic-18f-microcontroller/?add-to-car
pic-mic
LCD
(https://e
xc8/)

IC Com
LED
Microco
(https://e
(https://
MATLAB
microco

(https://e

April 11,

Microcon
(https://e

TAGS

MikroC
3D

(https://e
(https:/
(https://e
RECEN
night-la

(https://electrosome.com/shop/crystal-oscillator/)
Crystal Oscillator

(https://e

(https://electrosome.com/shop/crystal-oscillator/)

10.00

mobile

16F877A
(https://e
Automa
(https://e
(https://
Motor
night-lam
555
(https:/

8.00

Select options (https://electrosome.com/shop/crystal-oscillator/)

(https://e
February
amplie

Motorola
Dieren
8051
Mic
(https://e
(https://e
Transist
CATEG
microcon
(https://
MPLAB
amplie
(https://e
android

July
24, 2
(https://e

MPLAB X
(https://e
Arduino
xc8/)
(https://e

-21%

Multivibr
Arduino

(https://e
(https:/
(https://e
alarm-5
uno/)
pcb

Rain
Ala
(https://e
AT89C51
(https://
(https://e
PIC
alarm-55

(https://e

June
26,
Atmega3
OUT OF STOCK

(https://e
processo
(https://e
Atmel
2/)
(https://e

Proteus
(https:/
AVR
(https://e
(https://e
display-

PWMDisp
LCD
DC
Moto
(https://e
(https://
(https://e
motor/)
display-f

(https://electrosome.com/shop/usb-pic-programmerpickit-2/)
USB PIC Programmer PICkit 2
(https://electrosome.com/shop/usb-pic-programmerpickit-2/)

1,170.00

929.00

https://electrosome.com/gettingstartedpic18fmicrocontroller/

Python

May
9, 20
(https://e

electron
(https://e
Raspber
2/)
(https://e
pi/)
Embedd
(https://e
(https:/
RFID
cloud-p
(https://e
google
(https://e
EasyEDA
samsung
Design
S
(https://e
Graphen
(https://
(https://e
cloud-pc
smartph
5/9

7/30/2016

GettingStartedwithPIC18FMicrocontrollerMikroC

cloud-pc
smartph

(https://e
April
28, C
Hi-Tech

Read More (https://electrosome.com/shop/usb-pic-programmer-pickit-2/)

(https://e
tablet
tech-c/)
(https://e

electroSome
Follow

+1

+ 744

Related Posts:

Blinking LED using

Blinking LED using

Getting Started with

PIC Microcontroller

PIC Microcontroller

MikroC

CCS C

Read Modify Write

MPLAB XC8

with Hi-Tech C

Problem with Mid-

Compiler LED

(https://electrosome.com/blinkingRange PIC
led-pic-

(https://electrosome.com/led(https://electrosome.com/gettingmicrocontroller-hiblinking-pic/)

Getting Started with

PIC Microcontroller

started-pic-ccs-c/)

tech-c/)

IC Communication
Using Internal

with PIC

Interfacing HC-SR04

EEPROM of PIC

Microcontroller

Ultrasonic Sensor

Microcontroller

MPLAB

with PIC

(https://electrosome.com/read(https://electrosome.com/lednight-la
TOP
RA
mobile
modify-writepic-microcontroller16F877A
(https://e
Automa
(https://e
problem-pic/)
mplab-xc8/)
(https://
Motor
night-lam
555
(https:/
(https://e
(https://e
February
amplie

(https://electrosome.com/internal(https://electrosome.com/i2c(https://electrosome.com/hceeprom-pic-

pic-microcontroller-

sr04-ultrasonic-

microcontroller/)

mplab-xc8/)

sensor-pic/)

Share this post

Blinking

Intel
tablets
(https://e
(https://e
(https:/
2/)
pic-mic
LCD
(https://e
xc8/)
Transisto
(https://e
IC
Com
LED
Microco
(https://e
transisto
(https://
(https://e
MATLAB
microco
(https://e
April
11,
Tutorials
(https://e
Microcon
(https://e
TAGS
XC8
(https://e
MikroC
3D
(https://e
(https:/
(https://e
RECEN

Motorola
Dieren
8051
Mic
(https://
(https://e
(https://e
Transist
car-meta
CATEG
microcon
(https://
chassisMPLAB
amplie
(https://e
android

July
24, 2
(https://e

MPLAB X
(https://e
Arduino
xc8/)
(https://e

Multivibr
(https://
Arduino
(https://e
(https:/
wheel-b
(https://e

alarm-5
uno/)
pcb

Rain
Ala
(https://e
AT89C51
(https://
(https://e
PIC
alarm-55

Author

(https://e

Ligo George (https://electrosome.com/author/lijoppans/)

June
26,
Atmega3
(https://e
(https://
processo
screw(https://e
Atmel
mount2/)
(https://e
wheel-7

Proteus
(https:/
AVR
(https://e
(https://e
display-

juan peres
can you help me with this program but in the 18f2550 please ?

Python
(https://
May
9, 20
(https://e
chassiselectron

Ligo George (http://www.electrosome.com/)


Yes, you can try the same with 18F2550.

RELATED POSTS

https://electrosome.com/gettingstartedpic18fmicrocontroller/

PWMDisp
LCD
DC
Moto
(https://e
(https://
(https://e
motor/)
display-f

Interfacing Relay with 8051

(https://e
Raspber
2/)
(https://e
pi/)
Embedd
(https://e
(https:/
RFID
cloud-p
(https://e
google
(https://
(https://e
EasyEDA
screwsamsung
Design
S
mount(https://e
Graphen
(https://
wheel(https://e
cloud-pc
10x4/)
smartph
6/9

7/30/2016

GettingStartedwithPIC18FMicrocontrollerMikroC

Interfacing Relay with 8051


using Keil C AT89C51
(https://electrosome.com/interfacing-relay-8051-keilc/)
May 10, 2013 | Ebin George
(https://electrosome.com/author/ebin-g/) | 8051
Microcontroller
(https://electrosome.com/category/tutorials/8051-

cloud-pc
10x4/)
smartph

(https://e
April
28, C
Hi-Tech

(https://e
tablet
tech-c/)
(https://e

Intel
tablets
(https://e
(https://e
(https:/
2/)
pic-mic
LCD
(https://e
xc8/)
Transisto
(https://e
IC
Com
LED
Microco
(https://e
transisto
(https://
(https://e
MATLAB
microco
(https://e
April
11,
Tutorials
(https://e
Microcon
(https://e
TAGS
XC8
(https://e
MikroC
3D
(https://e
(https:/
(https://e
RECEN

night-la
TOP
RA
mobile

16F877A
(https://e
Automa
(https://e
(https://
Motor
night-lam
555
(https:/

(https://e

(https://e
February
amplie

Motorola
Dieren
8051
Mic
(https://
(https://e
(https://e
Transist
car-meta
microcon
CATEG
(https://
chassisMPLAB
(https://e
amplie
android

July
24, 2
(https://e

MPLAB X
(https://e
Arduino
xc8/)
(https://e

Multivibr
(https://
Arduino
(https://e
(https:/
wheel-b
(https://e

alarm-5
uno/)
pcb

Rain
Ala
(https://e
AT89C51
(https://
(https://e
PIC
alarm-55

(https://e

June
26,
Atmega3
(https://e
(https://
processo
screw(https://e
Atmel
mount2/)
(https://e
wheel-7

Proteus
(https:/
AVR
(https://e
(https://e
display-

PWMDisp
LCD
DC
Moto
(https://e
(https://
(https://e
motor/)
display-f

Python
(https://
May
9, 20
(https://e
chassiselectron

https://electrosome.com/gettingstartedpic18fmicrocontroller/

(https://e
Raspber
2/)
(https://e
pi/)
Embedd
(https://e
(https:/
RFID
(https://e
cloud-p
google
(https://
(https://e
EasyEDA
screwsamsung
Design
S
mount(https://e
Graphen
(https://
wheel(https://e
cloud-pc
smartph
10x4/)
7/9

7/30/2016

GettingStartedwithPIC18FMicrocontrollerMikroC

cloud-pc
smartph
10x4/)

(https://e
April
28, C
Hi-Tech

(https://e
tablet
tech-c/)
(https://e

Intel
tablets
(https://e
(https://e
(https:/
2/)
pic-mic
LCD
(https://e
xc8/)
Transisto
(https://e
IC
Com
LED
Microco
(https://e
transisto
(https://
(https://e
MATLAB
microco
(https://e
April
11,
Tutorials
(https://e
Microcon
(https://e
XC8
TAGS
(https://e
MikroC
3D
(https://e
(https:/
(https://e
RECEN

night-la
TOP
RA
mobile

16F877A
(https://e
Automa
(https://e
(https://
Motor
night-lam
(https:/
555

(https://e

(https://e
February
amplie

Motorola
Dieren
(https://
8051
Mic
(https://e
Transist
(https://e
car-meta
microcon
CATEG
(https://
chassisMPLAB
amplie
(https://e
android

July
24, 2
(https://e

MPLAB X
(https://e
Arduino
xc8/)
(https://e

Multivibr
(https://
Arduino
(https://e
(https:/
wheel-b
(https://e

alarm-5
uno/)
pcb

Rain
Ala
(https://e
AT89C51
(https://
(https://e
PIC
alarm-55

(https://e

June
26,
Atmega3
(https://e
(https://
processo
screw(https://e
Atmel
mount2/)
(https://e
wheel-7

Proteus
(https:/
AVR
(https://e
(https://e
display-

PWMDisp
LCD
DC
Moto
(https://e
(https://
(https://e
motor/)
display-f

Python
(https://
May
9, 20
(https://e
chassiselectron

https://electrosome.com/gettingstartedpic18fmicrocontroller/

(https://e
Raspber
2/)
(https://e
pi/)
Embedd
(https://e
(https:/
RFID
cloud-p
(https://e
google
(https://
EasyEDA
(https://e
screwsamsung
Design
S
mount(https://e
Graphen
(https://
wheel(https://e
cloud-pc
10x4/)
smartph
8/9

7/30/2016

GettingStartedwithPIC18FMicrocontrollerMikroC

cloud-pc
10x4/)
smartph

(https://e
April
28, C
Hi-Tech

(https://e
tablet
tech-c/)
(https://e

Intel
tablets
(https://e
(https://e
(https:/
2/)
pic-mic
LCD
(https://e
xc8/)
Transisto
(https://e
IC
Com
LED
Microco
(https://e
transisto
(https://
(https://e
MATLAB
microco
(https://e
April
11,
Tutorials
(https://e
Microcon
(https://e
XC8
TAGS
(https://e
MikroC
3D
(https://e
(https:/
(https://e
RECEN

night-la
TOP
RA
mobile

16F877A
(https://e
Automa
(https://e
(https://
Motor
night-lam
(https:/
555

(https://e

(https://e
February
amplie

Motorola
Dieren
(https://
8051
Mic
(https://e
Transist
(https://e
car-meta
CATEG
microcon
(https://
chassisMPLAB
amplie
(https://e
android

July
24, 2
(https://e

MPLAB X
(https://e
Arduino
xc8/)
(https://e

Multivibr
(https://
Arduino
(https://e
(https:/
wheel-b
(https://e

alarm-5
uno/)
pcb

Rain
Ala
(https://e
AT89C51
(https://
(https://e
PIC
alarm-55

(https://e

June
26,
Atmega3
(https://e
(https://
processo
screw(https://e
Atmel
mount2/)
(https://e
wheel-7

Copyright 2016. All Rights Reserved.

Proteus
(https:/
AVR
(https://e
(https://e
display-

PWMDisp
LCD
DC
Moto
(https://e
(https://
(https://e
motor/)
display-f

Python
(https://
May
9, 20
(https://e
chassiselectron

https://electrosome.com/gettingstartedpic18fmicrocontroller/

(https://e
Raspber
2/)
(https://e
pi/)
Embedd
(https://e
(https:/
RFID
cloud-p
(https://e
google
(https://
EasyEDA
(https://e
screwsamsung
Design
S
mount(https://e
Graphen
(https://
wheel(https://e
cloud-pc
10x4/)
smartph
9/9

You might also like