Escolar Documentos
Profissional Documentos
Cultura Documentos
using MikroC
Lecturer 02-IRC
What is a
microcontroller?
A microcontroller is a
Computer Architecture
PIC16f877A Architecture
low .
Lots of recourses and references are there.
Power consumption is low.
Lot of Choices.
IDE and
Compiler
PIC Programming
Programmer
Compiler
PIC16F8xxx
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?
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.
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
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;
}
}
Multiple Interrupts
Each of these sources can set an associated
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
Etc..
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