Você está na página 1de 33

PIC Microcontroller Programing

using MikroC
Lecturer 02-IRC

What is a
microcontroller?
A microcontroller is a

compact standalone computer.


Entire processor.
Memory and the I/O interfaces are located on

a single piece of silicon .


It takes less time to read and write to external

Computer Architecture

PIC16f877A Architecture

Why PIC is preferred??..


Simplicity.
Easy to manipulate.
Compared to other microcontrollers cost is

low .
Lots of recourses and references are there.
Power consumption is low.
Lot of Choices.

Key things need to


consider in
programming..
What are the things we need to concentrate

while doing any programming?

Key 4 things in Microcontroller


programming..
Programm
er
Microcont
roller

IDE and
Compiler

PIC Programming

Programmer

Compiler

Packages of PIC Microcontrollers

Packages of PIC Microcontrollers

PIC 16F877A (DIP


Package)

PIC16F8xxx

Basic I/O Architecture


PIC16F877A Has PORT A,B,C,D,E .
TRIS x Registers are used to select

Input / Output directions.


1 for Input.
0 for Output.

Binary / Decimal/ HexaDecimal Number


Systems

Basic C Program to drive


LEDs using PORTD
void main()
{

TRISD = 0x00;
PORTD = 0x00;
while(1)

// PORTD is output
//initialize PORTD

PORTD = 1;
Delay_ms(100);
PORTD = 2;
Delay_ms(100);
PORTD = 4;
Delay_ms(100);
PORTD = 8;
Delay_ms(100);
PORTD = 16;
Delay_ms(100);
PORTD = 32;
Delay_ms(100);
PORTD = 64;
Delay_ms(100);
PORTD = 128;
Delay_ms(100);
}
}

//move 1 to port D
//wait for few seconds

Exercise
Assume that you have to program a robot to
cross a busy road at a pedestrian crossing with
traffic lights.
Can you write an algorithm to do this listing
down the steps involved?

A flow chart showing the robot how to cross


the road

Interrupts
In real-time systems, there could be events that require immediate (within a
specified delay) action from the CPU. The vast majority of MPU/MCUs have the
capability to deal with a range of such events.
In the case of a microcontroller, requests for service may come from an internal
peripheral device, such as a timer overflowing, or the completion of an analog
to digital conversion, or from a source entirely external to the device in the
outside world.
At the very least, on reset (a type of external hardware event) the MCU must be
able to get (vector) to the first instruction of the main program.

In the same manner an external service request or interrupt


when answered must lead to the start of the special subroutine
known as an interrupt service routine

Interrupt Controlling
Polling.
Is some mechanism which will keep on
checking for some thing.
This is the simplest way of
checking for some thing.
Resource consumption is high.
Processor is always busy.
There are chances to miss some
events.

Interrupt Controlling..
cont
Interrupt
Once event get occurred only it will notify to

relevant handling party.


Less resource consuming.
Complex to handle.
Event can be prioritized.
Context switching can be done in very effective

way.

Way of Handling
Interrupts
void main()
{
INTCON.GIE = 1;
INTCON.INTE = 1;
OPTION_REG.INTEDG = 0;
while(1)
{ }
}
void interrupt()
{
if (INTCON.INTF)
{
PORTD = 0xff;
Delay_ms(200);
INTCON.INTF = 0;
}
}

//Enable general interrupts


//Enable RB0 Interrupt
//Set Edge triggering

Rising Edge/ Falling Edge

Multiple Interrupts
Each of these sources can set an associated

interrupt flag bit.


In the cases where more than one source of

interrupt requests are enabled, these flags can


be monitored by software by polling to check
the origin of the requests.
Although a flag is set by an external event, it

can be cleared in software. It is recommended


that you clear it in the interrupt service routine.

void interrupt()
{
if(INTF)
go to relevant sub rooting
else if (RBIF)
go to relevant sub rooting
}

Time to think
Design a Counter to count the

customers entering a shop


Design of the over all implementation.

What are the Interrupts we need?

What are the sensors we need?

Etc..

PWM (Pulse Width


Modulation)

Way of Handling PWM


void main()
{
TRISC = 0x01;
Pwm_Init(5000);

Pwm_Change_Duty(255);
Pwm_Start();
Delay_ms(200);
Pwm_Change_Duty(191);
Pwm_Start();
Delay_ms(200);
Pwm_Change_Duty(127);
Pwm_Start();
Delay_ms(200);
Pwm_Change_Duty(64);
Pwm_Start();
Delay_ms(200);
}

// PORTC is output
//initialize PWM

//Change duty

Cycle

Motor Control
Use L293D motor control circuit.

Motor Control
void main()
{
TRISD = 0x00;
while(1)
{
PORTD = 0b00000101;
Delay_ms(1000);
PORTD = 0b00000001;
Delay_ms(500);
PORTD = 0b00000100;
Delay_ms(500);
PORTD = 0b00000101;
Delay_ms(1000);
}
}

Mechatronic

Você também pode gostar