Top Banner
PIC Microcontroller Programing using MikroC Lecturer 02-IRC
33

Pic

Dec 11, 2015

Download

Documents

Malith Wox

pic
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Slide 1

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 devices.

Computer Architecture

PIC16f877A ArchitectureWhy 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..

IDE and CompilerProgrammerMicrocontrollerPIC Programming

CompilerProgrammerPackages 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/ Hexa-Decimal Number Systems

Basic C Program to drive LEDs using PORTDvoid main(){

TRISD = 0x00; // PORTD is output PORTD = 0x00; //initialize PORTD while(1) { PORTD = 1; //move 1 to port D Delay_ms(100); //wait for few seconds 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); }}ExerciseAssume 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 InterruptsIn 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 interruptwhen answered must lead to the start of the special subroutineknown as an interrupt service routineInterrupt ControllingPolling.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.. contInterruptOnce 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; //Enable general interrupts INTCON.INTE = 1; //Enable RB0 Interrupt OPTION_REG.INTEDG = 0; //Set Edge triggering

while(1) { }}

void interrupt() { if (INTCON.INTF) { PORTD = 0xff; Delay_ms(200); INTCON.INTF = 0; }}

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 rootingelse if (RBIF)go to relevant sub rooting}

Time to thinkDesign a Counter to count the customers entering a shopDesign 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 PWMvoid main(){

TRISC = 0x01; // PORTC is output Pwm_Init(5000); //initialize PWM

Pwm_Change_Duty(255); //Change duty Cycle 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); }Motor ControlUse L293D motor control circuit.

Motor Controlvoid 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