Professional Documents
Culture Documents
Introduction To Mikroc PRO For PIC12
Introduction To Mikroc PRO For PIC12
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.
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.
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:
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);
}
}
}
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);
}
}
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);
}
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);
}
}
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);
}
}
Figure No. 8
STARTING A NEW ASSEMBLER PROJECT
Figure No. 9
2. Name the ProjectToggle.a51
3. Click on the Save button.
Figure No. 10
Figure No.11
Figure No. 12
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
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++);
}
# 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++);
}
2.1 Program 1 8
2.2 Program 2 9
2.3 Program 3 10
3.2 Program 2 12
3.3 Program 3 13
4.2 Program 2 15
Program 3 17
Program 2 26