Sei sulla pagina 1di 27

1.

PIC Microcontroller- in MikroC


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 I suggest you to other compilers. So I
suggest you to use Hi-Tech C compiler by Microchip after you get familiar with
microcontrollers. I-Tech C is a bit difficult compared to MikroC as there is no built in
function.

Getting Started with MikroC Pro


You can buy mikroC form microelectronic or download a trial version here. Trial
Version is limited to 2K of program words and is sufficient for most of our
applications.

Download and Install MikroC Pro.


Create a Folder for this project in any of your drives.
Open microC Pro for PIC.

Fig No. 1 MikroC Pro Welcome Screen


Click on Project>> New Project

Fig No. 2 : Welcome-New Project Wizard


Click Next.

Fig No. 3 Project Settings- New Project Wizard

Enter Project name, path ,clock frequency, microcontroller and click


Next.Clock frequency is the frequency of oscilla
Tor used with microcontroller. Here we use PIC 16f877A Microcontroller with
8MHz crystal.

Fig No. 4 Add Files to Project-New Project Wizard

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.

Fig No. 5 Library Manager-New Project Wizard

Here you can add MikroCs built in libraries such as UART,PWM,LCD


etcyou may include All Libraries or include none. If you select none, then you
can selectively include required libraries later. Then click next.
Finyhuuuyuuiu

Fig No. 6 Finish-New Project Wizard

Click Finish, to complete the New Project Wizard.


Then you will see the editor, where you can enter the MikroC Code.

1.1MikroC Programming
Before going to the programming you should understand the following things.
Output pins of a PIC Microcontroller are divided in to different
PORTS containing a group of GPIO pins.
In 16F PIC Microcontrollers, there are two registers associated with a
port, TRIS and PORT. Eg: TRISB, PORTB, TRISC, PORTC.
TRIS stands for Tri-State, which determines the direction of each
GPIO pin. Logic 1 at a particular bit of TRIS register makes the
corresponding pin input and logic 0 at a particular bit of TRIS register
makes the corresponding pin output. An input pin of PIC
microcontroller have very high input impedance, thus it may said to be
in Hi-impedance state.
PORT register is used to read data from or write data to GPIO
pins.Logic 1 at a particular bit of PORT register makes the
corresponding pin at Logic High(VDD) and Logic low (VSS) if that
pin is an output pin (TRIS bit is 0).
PORT register can be used to read digital data from an input pin.
Logic 1 indicates the pin is at Logic High and Logic 0 indicates that
pins is at Logic low.
Fig No. 7 PORT TRIS Register PIC Microcontroller

You can write to PORT and TRIS register entirely or bit by bit.

2.MikroC Code Blinking an LED


2.1 Program 1:
Void main( )
{
TRISB=0X00;
While(1)
{
PORTB=0X55;
delay_ms(10);
PORTB=0XAA;
delay_ms(10);
}
}
OUTPUT : 01010101 AFTER 10ms delay
10101010
2.2 Program 2:

Void main( )
{
TRISB=0X00;
While(1)
{
PORTB=0X01;
delay_ms(10);
PORTB=0X03;
delay_ms(10);
PORTB=0X07;
delay_ms(10);
PORTB=0X0F;
delay_ms(10);
PORTB=0X1F;
delay_ms(10);
PORTB=0X3F;
delay_ms(10);
PORTB=0X7F;
delay_ms(10);
PORTB=0XFF;
delay_ms(10);
PORTB=0X7F;
delay_ms(10);
PORTB=0X3F;
delay_ms(10);
PORTB=0X1F;
delay_ms(10);
PORTB=0X0F;
delay_ms(10);
PORTB=0X07;
delay_ms(10);
PORTB=0X03;
delay_ms(10);
PORTB=0X01;
delay_ms(10);
}
}
2.3 Program 3:

LED INTERFACING USING for loop.

unsigned int i;
void main( )
{
TRISB=0X00;
While(1)
{
PORTB=0X01;
delay_ms(10);
for (i=0;i<=7;i++)
{
PORTB=PORTB<<i;
delay_ms(10);
}
}
}

OUTPUT: All LEDs glow one by one after 10 ms delay.


3. LCD INTERFACING WITH PIC
3.1 PROGRAM 1:

Void main( )
{
TRISB=0X00;
While(1)
{
PORTB=0X38;
delay_ms(10);
PORTB=0X0E;
delay_ms(10);
PORTB=0X01;
delay_ms(10);
PORTB=0X06;
delay_ms(10);
PORTB=0X84;
delay_ms(10);
}
}

OUTPUT: LCD Glow


3.2 PROGRAM 2:

Void lcd cmd (unsigned char value)


{
PORTB=value;
PORTC=0X01;
delay_ms(100);
PORTC=0X00;
}
Void lcd cmd (unsigned char value)
{
PORTB=value;
PORTC=0X05;
delay_ms(100);
PORTC=0X04;
}
Void main( )
{
TRISB=0X00;
TRISC=0X00;
Lcdcmd(0x38);
Lcdcmd(0x0E);
Lcdcmd(0x01);
Lcdcmd(0x06);
Lcdcmd(0x84);
Lcddata(A);
Lcddata(B);
While(1);
}

OUTPUT: LCD Shows Character A and B.

3.3 PROGRAM 3:
LCD INTERFACING USING ARRAY
Void lcd cmd (unsigned char value)
{
PORTB=value;
PORTC=0X01;
delay_ms(100);
PORTC=0X00;
}
Void lcd cmd (unsigned char value)
{
PORTB=value;
PORTC=0X05;
delay_ms(100);
PORTC=0X04;
}
Void main( )
{
unsigned ar[ ]={0X38,0X0E,0X01,0X06,0X84}
unsigned ar1[ ]=RUPINDER;
int i,j;
TRISB=0X00;
TRISC=0X00;
for(i=0;i<=4;i++)
{
Lcdcmd(ar[i]);
}
for(i=0;i<=4;i++)
{
Lcdcmd(ar[i]);
}
While(1);
}

OUTPUT: LCD display RUPINDER


4. SEVEN SEGMENT INTERFACING
4.1 PROGRAM 1:

Void main( )
{
TRISB=0X00;
While(1)
{
PORTB=0X3F;
delay_ms(10);
PORTB=0X06;
delay_ms(10);
PORTB=0X5B;
delay_ms(10);
PORTB=0X4F;
delay_ms(10);
PORTB=0X66;
delay_ms(10);
PORTB=0X6D;
delay_ms(10);
PORTB=0X7D;
delay_ms(10);
PORTB=0X07;
delay_ms(10);
PORTB=0X7F;
delay_ms(10);
PORTB=0X6F;
delay_ms(10);
}
}

OUTPUT: Seven Segment display 1,2,3.9 after 10 ms


delay
4.2 PROGRAM 2:
void main ( )
{
int i;
int ar[ ] ]={0X6F,0X7F,0X07,0X6D,0X66,0X4F,0X5B,
0X66,0X3F}
TRISB=0X00,
While(1)
}
for(i=0,i<9;i++)
{
PORTB=ar(i);
Delay _ms(40);
}
}

OUTPUT: seven segment display 9,8,7,1 ..after 40 ms delay.


5. DC MOTOR INTERFACING
Programming of DC MOTOR in Both direction clockwise and anticlockwise.

5.1 PROGRAM 1:
DC MOTOR move forward then after 100ms delay move Reverse direction.
Void main( )
{
TRISB=0X00;
While(1)
{
PORTB=0X05;
delay_ms(100);
PORTB=0X0A;
delay_ms(100);
}
}

PROGRAM 2:
Void main( )
{
TRISB=0X00;
While(1)
{
PORTB=0X01;
delay_ms(200);
PORTB=0X08;
delay_ms(200);
}
}
PROGRAM 3:
Void main( )
{
TRISB=0X00;
While(1)
{
PORTB=0X01;
delay_ms(200);
PORTB=0X08;
delay_ms(200);
}
}

KEIL SOFTWARE USED FOR ARM PROGRAMMING


Keil Compiler offers an evaluation an package that will allow the assembly and
debugging of files 2K or less. This package is freely available at their web site.
1.Open keil from the Start menu.
2.The figure below shows the basic names of the windows referred in this document.

Figure No. 8
STARTING A NEW ASSEMBLER PROJECT

1. Select New Project from the Project Menu.

Figure No. 9
2. Name the ProjectToggle.a51
3. Click on the Save button.

Figure No. 10

4. The device Window will be Displayed.


5. Select the Part you will be using to test with for now we will use the dallas
semiconductor Part DS89C420.
6. Double Click on the Dallas semiconductor.

Figure No.11
Figure No. 12

7. Scroll down and select the DS89C420 part.


8. Click ok.
CREATING SOURCE FILE
1. Click File menu and Select New.

Figure No. 13
Figure No. 14

3. Copy the examples to the right into the NEW window . this File will toggle Ports 1
and 2 with a delay.
Figure No. 15

4. Click on File Menu and SELECT save as.


12. LED INTERFACING WITH ARM

12.1 PROGRAM 1:
# include <lpc21xx.h>
Void delay( );
Int main( )
{
While (1)
{
PINSEL0=0X00000000;
IODIR0=0X000000FF;
IOSET0=0X00000003;
delay( );
IOCLR0=0X00000003;
delay( );
IOSET0=0X0000000C;
delay( );
IOCLR0=0X0000000C;
delay( );
IOSET0=0X00000030;
delay( );
IOCLR0=0X00000030;
delay( );
IOSET0=0X000000CO;
delay( );
IOCLR0=0X000000C0;
delay( );
}
}
Void delay ( )
{
Unsigned int I, j;
for(i=0;i<=1000;i++)
for(j=0;j<=400;j++);
}

OUTPUT: LED glow as 00000011


00001100
00110000
11000000
12.2 PROGRAM 2: LED INTERFACING USING ARRAY

# include<lpc21xx.h>
Unsigned ar[ ]={0X00000003,0X0000000C,0X00000030;
0X000000C0};
Unsigned int k;
Void delay( );
Int main( )
{
While (1)
{
PINSEL0=0X00000000;
IODIR0=0X000000FF;
{
For(k=0;k<4;k++)
{
IOSET0=AR[K];
Delay();
IOCLR0=ar[k];
Delay();
}
}
}
}
Void delay()
{
unsigned int i;
for(i=0;i<1000;i++);
}

OUTPUT: LED glows as 00000011


00001100
00110000
11000000
INDEX

SR. NO. DESCRIPTION PAGE NO.

1 PIC Microcontroller In Mikro-c 1

1.1 Mikro-C Programming 6

2. Mikro C Code Blinking an LED 7

2.1 Program 1 8

2.2 Program 2 9

2.3 Program 3 10

3 LCD Interfacing with Pic 11

3.2 Program 2 12

3.3 Program 3 13

4 Seven segment Interfacing 14

4.2 Program 2 15

5 DC Motor interfacing Program 1& 2 16

Program 3 17

6 Keil Software used for ARM programming 18

6.1 Starting a new assembler project 19-24

LED Interfacing with ARM 25

Program 2 26

Potrebbero piacerti anche