Professional Documents
Culture Documents
EXPERIMENT NO 8
Objective: In this lab we will see the introduction to MikroC PRO for PIC.
LAB ASSESSMENT:
Data presentation
Experimental results
Conclusion
Date: Signature:
Lab 08
Introduction to MikroC PRO for PIC
Objectives:
• Install the MikroC Pro 6.4.0
• Create a New Project
• Make Schematic on Proteus and burn your hex file in PIC18F452
Hardware:
• PIC 18f452 microcontroller
• Led Bargraph
• Resistors
Introduction:
MikroC is a Powerful tool for PIC microcontroller. It is designed to provide the costumer with
the easiest possible solution for developing applications for embedded systems without
compromising performance or control.
PIC”.
Launch the application” keygen” and then a black window will appear after waiting for 1 or 2
seconds you are done with it.
Procedure:
Step1: The process of creating a new project is very simple. Select the New Project option from
the Project menu as shown below. The New Project Wizard window appears. It can also be
opened by clicking the New Project icon from the Project toolbar .
Step2: Rename the project then select the directory in which you want to save your project.
Select the device for which you want to write code here I am using “PIC18f452” and then choose
the xtal oscillators value.
Click Next.
Step3: This step allows you to include additional files that you need in your project: some
headers or source files that you already wrote, and that you might need in further development.
Since we are building a simple application, we won’t be adding any files a t this moment. So just
Click Next
Step4: Following step allows you to quickly set whether you want to include all libraries in your
project, or not. Even if all libraries are included, they will not consume any memory unless they
are explicitly used from within your code. The main advantage of including all libraries is that
you will have over 500 functions available for use in your code right away, and visible from
Code Assistant [CTRL+Space]. We will leave this in default configuration:
Make sure to leave “Include All” selected. Click Next.
Step6: In this Step you will write a code for “LED Blinking”.
Write code as shown below and build it
Step8: Debugging, you can add or remove registers or variables in watch window to observe
them step by step to verify your logic (as this step is not necessary)
Make Schematic in Proteus:
Double click on pic18f452 and specify path for program hex file.
Click on play button, if you followed all the steps in correct way then blinking will be started.
If it is not working then you can debug your code to identify the exact problem.
Trisx is used to mark a port input or output to make a port input Trisx=0; and to make a port
output Trisx=0xff;
Writing on LAT or PORT is absolutely the same. While reading LAT reads the value stored by
the last program instruction, while reading PORT reads the actual pin’s status.
Sometimes when you write data at some pins using Portx and a load is attached with these pins
than this load may change the status of these pins and you can get unexpected results while
reading the status of these pins using PORTx because PORTx depends on external circuit and
reads the actual status of the pins So, to write data on pins an internal latch Latx is being used
because Latx will never be dependent on external circuit and reads the value stored by the last
program instruction
Example: Led’s are connected with some I/O pins and you want to turn them on or off. Then
you may write data on Latx or PORTx it doesn’t matter. Led is a Light emitting diode so after
certain voltage light emitting diode will be in forward biased and act like short circuit and logic
1 will be shorted with ground and changes its state to logic 0 and it produces unexpected
results while reading through PORTx because you are writing logic 1 on these pins but getting
logic 0 while reading. But if you write data using Latx while reading through Latx you will get
the same result.
To get more understanding, Use PORTB instead of LATB in the above code then your
equation will be like:
PORTB=~PORTB;
You can see that PORTB on the right side will always be 0 (because if you write 1 then Led’s
starts to glow, gets forward biased, become short circuited with ground and then PORTB
changes its state to 0). ~PORTB always be ‘1’. So PORTB on the left side will always be ‘1’.
So you are always witting ‘1’ on PORTB but always reading ‘0’ from this Port. So, Led’s will
be ‘on’ all the time which leads to inaccurate results. If you want to make this code working
you will have to look at external circuit. Their main problem was short circuit to ground at
logic ‘1’ and you can avoid it by just adding a resistor in series with each led to get the desired
results. it by just adding a resistor in series with each led to get the desired results. it by just
adding a resistor in series with each led to get the desired results.
EXERCISE:
Task 1:
Read data from PortD and write it on PortB. Define Port D an input Port and Port B an
Output Port. Connect led bar graph to observe output and connect switches on Port D. Show
your output in proteus and Hardware.
• Code:
void main () {
• Proteus Simulation:
Task 2:
Write a code to generate the pattern shown below on Led Bar graph. Show your output in
proteus and Hardware.
• Code:
void main () {
unsigned int i;
TRISB=0;
TRISD=0;
while (1) {
LATB.LATB0=1;
LATD.LATD7=1;
for(i=0;i<=2;i++)
delay_ms(500);
PORTB=PORTB<<1;
PORTD=PORTD>>1;
}
delay_ms(500);
LATB=0;
LATD=0;
• Proteus Simulation:
Task 3:
Write a code for the up counter from 00-99 on two SSD’s by not using more than nine pins
of PIC18F452. Show your output in Proteus and on hardware.
• Code:
int x,y;
char DSP_code[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void main() {
int i;
TRISC=0;
PORTC=0;
TRISD=0;
PORTD=0;
x=0xFE;
y=0xFD;
while(1)
for(i=0;i<=99;i++)
delay_ms(10);
LATD=x;
LATC=DSP_code[i%10u];
delay_ms(250);
LATD=y;
LATC=DSP_code[(i/10)%10u];
delay_ms(250); } } }
• Proteus Simulation:
Conclusion:
In this lab we learn about PIC and its uses. We learn about Mickro C for PIC and implement different tasks related to uses
of PIC in C languages using the Mickro-C software.