Professional Documents
Culture Documents
1+2
Blinking LED using PIC Microcontroller
MikroC is the best compiler for beginners as it contains built in functions for most of the
commonly used tasks. But MikroC is less efficient and the hex file generated will be large size
compared to other compilers. So it is suggested to use Hi-Tech C compiler by Microchip after
you get familiar with microcontrollers. Note that, Hi-Tech C is a bit difficult compared to
MikroC as there is no built in functions.
You can download a trial version of mikroC form mikroElectronika . Trial Version is limited to
2k of program words and is sufficient for most of our applications.
Click Next
Project Settings – New Project Wizard
Enter Project name, path (created folder path), clock frequency, microcontroller and
click Next. Clock Frequency is the frequency of oscillator used with microcontroller.
Here we use PIC 18F45K22 microcontroller with 8MHz crystal.
Here you can add your subprogram files or user defined header files in large projects.
Hence we are dealing with simple LED Blinking in this tutorial, ignore it and click Next.
Here you can add MikroC’s built in libraries such as UART, PWM, LCD etc… You may
include All Libraries or include None. If you select None, then you can selectively
include required libraries later. Then click Next.
Click Finish, to complete the New Project Wizard.
Then you will see the editor, where you can enter the MikroC Code.
MikroC Programming
Before going to the programming you should understand the following things.
You can write to PORT and TRIS register entirely or bit by bit.
0 0b00000000 00 0x00
1 0b00000001 01 0x01
void main()
PORTB.F0 = 1; //LED ON
Note : Delay_ms(const unsigned long a) is a built in function of MikroC Pro which provides a
delay of ‘a’ milliseconds. The variable ‘a’ must be a constant of type unsigned long integer.
Save it
Then Compile it. Click Build >> Build (or Ctrl+F9)
A hex file will be generated in your Project Folder. You need to write this file to microcontroller
using a programmer.
Circuit Diagram:
VDD and VSS of PIC Microcontroller is connected to +5V and GND respectively to provide necessary
power for the operation of the microcontroller. 8MHz crystal is used to provide necessary clock for the
microcontroller. 22pF capacitors stabilizes the oscillations of the crystal. LED is connected to the 0th bit of
PORTB and a 470Ω resistor is connected in series to limit the current through the LED.
Input Using Push Button:
Suppose push button switch is connected to the first bit of PORT D (RD0) which is configured
as an input pin. Which is connected to a pull up resistor such that this pin is at Vcc potential
when the switch is not pressed. When the switch is pressed this pin RD0 will be grounded. The
LED is connected to the first bit of PORT B (RB0) and a resistor is connected in series with it to
limit the current.
MikroC program:
void main()
ANSELD=0;
ANSELB=0;
While(1)
PORTB.F0 = 1; //LED ON
}}}
LAB. Tasks:
2- Modify the code and connection to perform 8bits up counter with 1-sec
delay between each count.
- Each time you press the button the LEDs must count up by one.
4- Apply all previous projects on Easy PIC board. See User guide page 13
programming software “how to download the hex-file to the PIC”
LAB.3
Multiplexing of Seven Segment Displays with PIC Microcontroller
When a Seven Segment Display is interface with PIC Microcontroller it needs minimum 7 pins
to display a value. But real time applications like Digital Clock, Calculator, Digital Watch
requires 3-6 seven segment displays. Lets assume that we need 6 digit display, ie we need 7
segment * 6 Display = 42 pins. Thus we actually need Microcontroller with 42 output pins. This
is waste and not economical to use lot of pins of a Microcontroller just for display.
The simplest way to drive Seven Segment Display is by using a driver or decoder and are
available for up to 4 displays. Alternatively we can drive more than one Seven Segment Display
by using a technique called ‘Multiplexing’. This technique is based on the principle of
Persistence of Vision of our eyes. If the frames change at a rate of 25 ( or more) frames per
second, human eye can’t detect that visual change. Each display is turned on above this rate and
our eyes will think that the display is turned on for whole the time.
void main() {
trisa=0;
trisd=0;
ansela=0;
anseld=0;
while(1)
{
porta=1;
portd=0b00111111;
delay_ms(10);
porta=2;
portd=0b00000110;
delay_ms(10);
}}
Counter code using push button
void main() {
int i=0;
unsigned char x[]={0b00111111,0b00000110,0b01011011,…….};
// Complete numbers using Mikroc seven segment editor. From Tools menu
trisa=0;
trisd=0;
porta=1;
trisc=1;
anselc=0;
portd=x[0];
while(1)
{
if(portc.f0==1)
{i++;
portd=x[i];
delay_ms(30);
while(portc.f0==1){}
delay_ms(30);}
}}
LAB. Tasks:
3- Using two push buttons and one seven-segment display write the needed
code to do the following:
If the first push button is pressed once then the seven-segment display will
count up, else if the second push button is pressed once then the seven-
segment display will count down
LAB. 4
LCD interfacing with PIC Microcontroller
A PIC Microcontroller can be easily made to communicate with LCD by using the built in
Libraries of MikroC. Interfacing between PIC and LCD can be 4-bit or 8-bit. The difference
between 4-bit and 8-bit is how data are send to the LCD. In the 8-bit mode to write an 8-
bit character to the LCD module, ASCII data is send through the data lines DB0- DB7 and data
strobe is given through the E line.
But 4-bit mode uses only 4 data lines. In this mode the 8-bit ASCII data is divided into 2 parts
which are send sequentially through data lines DB4 – DB7 with its own data strobe through the
E line. The idea of 4-bit communication is to save as much pins that used to interface with LCD.
The 4-bit communication is a bit slower when compared to 8-bit. The speed difference is only
minimal, as LCDs are slow speed devices the tiny speed difference between these two modes is
not significant. Thus the 4-bit mode data transmission is most commonly used.
MikroC Pro provides built in libraries for interfacing LCDs with HD44780 compliant controllers
using 4 bit mode data transmission.
For the proper functioning of the LCD library, you must define, how the pins of LCD are
connected to pic microcontroller as given next.
// LCD module connections
The above definitions tell the compiler, how LCD is connected to the microcontroller. The two set of
definitions are used to provide Data (PORT) and Direction (TRIS) registers.
Library Functions
1- Lcd_Init
This function initializes the LCD module connected to the above defined pins of the PIC
Microcontroller.
2- Lcd_Out
This functions prints the text (string) in a particular row and column.
3- Lcd_Out_Cp
This function prints the text (string) in the current cursor position. When we write data to LCD
Screen, it automatically increments the cursor position.
4- Lcd_Chr
It prints the character (out_char) in the specified row and column of the LCD Screen.
5- Lcd_Chr_Cp
6- Lcd_Cmd
// Copy the first lines from: help index write LCD at the end of page copy from the written program"
void main()
{ ANSELB=0;
}
LAB. Tasks:
- Each time the push button is pressed the LCD must count up by one.
Piezo Buzzer
Using buzzer to generate sounds and music. User guide page 32 shows hardware connection details.
Assume sound as square wave which you can modify its frequency and duty cycle to change the
sound. Next code shows how to generate beep sound each time a push button is pressed:
void main() {
portc=0;
anselc=0;
trisb=1;
anselb=0;
while(1)
{if(portb.f0==1)
{portc=0;
delay_us(1916);
portc=255;
delay_us(1916);}}
}
LAB. Tasks:
1- Implement the previous code and connection
2- In the previous code calculate the sound frequency and duty cycle.
3- Modify the code using the same frequency with duty cycle 75%. Note the
difference.
4- Write the needed code using eight push buttons as piano.
5- Rewrite the previous codes using MikroC function “Sound”. See Mikroc
help.
Sound_Init(&PORTC, 2);
Sound_Play(880, 1000); // Play sound at 880Hz for 1 second
LAB . 6
The solution to above problems is to use H-bridge circuit. It is a special circuit, by using the 4
switches we can control the direction of DC Motor. Depending upon our power requirements we
can make our own H-bridge using Transistors/MOSFETs as switches. It is better to use ready
made ICs, instead of making our own H-bridge.
We can drive two DC Motors with one L293D, in this example we are using only the first pair of
drivers to drive one DC Motor. First pair of drivers are enabled by connecting EN1 to Logic
HIGH. IN1 and IN2 are connected to RB0 and RB1 of PIC Microcontroller respectively which
are used to provide control signal to the DC Motor. DC Motor is connected to OUT1 and OUT2
of the L293D.
MikroC Source Code
void main()
{ANSELB=0;
While(1)
PORTB.F0 = 1;
PORTB = 0; // or PORTB = 3
PORTB.F1 = 1;
LAB. Tasks:
2- Write the code and draw the circuit needed to connect two push buttons
and one DC-motor to a PIC. If the first push button is pressed then the motor
will rotate clockwise for 3 second. If the second push button is pressed then
the motor will rotate counterclockwise for 3 second.