Welcome to the world of PIC Microcontrollers.
You are in the right place if you are a beginner in the filed of microcontrollers. 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 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 follow our Hi-Tech C Tutorials.
You are in the right place if you are a beginner in the filed of microcontrollers. 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 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 follow our Hi-Tech C Tutorials.
MikroC Programming
Before going to the programming you should understand the
following things.
Output pins of a PIC Microcontroller are divided into
different PORTS containing a group of GPIO (General Purpose Input Output Pins)
pins.
In 16F PIC Microcontrollers, there are various registers but
those associated with port register are 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 0 at a particular bit of PORT register makes
the corresponding pin at 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 the
pin is at Logic Low.
You can write to PORT and TRIS register entirely or bit by
bit.
Writing Bit by Bit :
TRISC.F0 = 1; //Makes 0th bit of PORTC Input
TRISC.F5 = 0; //Makes 5th bit of PORTC Output
PORTB.F3 = 1; //Makes 3ed bit of PORTB at Logic High
PORTB.F7 = 0; //Makes 7th bit of PORTB at Logic Low
Writing Entire
Register
Note : You should be familiar with following C Programming
concepts.
A number with a prefix ‘0b’ indicates a binary number.
A number with a prefix ‘0’ indicates an octal number.
A number with a prefix ‘0x’ indicates a hexadecimal number.
A number without prefix is a decimal number.
Having understand the above LED blinking is a work over ,the on and off process is programmed on the chip with the help of the below code using either mplab or mikcro c
Circuit Diagram
MikroC Code –
Blinking an LED
The following program blinks an LED with a delay of 1
second.
void main()
{
TRISC.F0 = 0;
//Makes PORTB0 or RB0 Output Pin
PORTC=0b00000000;//
clears the port
while(1) //If the
condition is true perform the action
{
PORTC.F0 = 1;
//LED ON
Delay_ms(1000); //1
Second Delay ie charged capacitor
PORTC.F0 = 0;
//LED OFF
Delay_ms(1000);
//1 Second Delay ie charged capacitor
}
}
Mplab IDE Code –
Blinking an LED
The following program blinks an LED with a delay of 1
second.
#include <htc.h>
#include <pic.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4e6
#endif
void main(){while(1)
{TRISC=0;
PORTC=0;
While(1){
PORTC=OB00000001;__delay_ms(1000); PORTC=OB00000000;__delay_ms(1000);}}
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.
For any suggestion you are free to mail sltech via sltechnology4all@yahoo,com
No comments:
Post a Comment