PIC Microcontroller Programing using MikroC Mr. Charith Sucharitharathna Department of Electronic and Computer Engineering Sri Lanka Institute of Information Technology
What is a microcontroller? uhsfl%dlkafg%da,rhla hkq ixhqla; ;ksj l%shdl< yels mß.klhla jeks fohls' iïmq¾k fm%dfiirhls' u;lh yd Input/Output w;+re uqyqk;a tlg tlaj mj;sk WmlrKhls'
Computer Architecture
PIC16f877A Architecture
PIC fhdod.eksug fya;+.. tys we;s ir,;dj úodk,ndosfï myiqj wfkl+;a uhsfl%dlkafg%da,r iu. iei SfïoS tys we;s,dnodhsnj. nyq, jyfhka fmd;m; yd wjyh f;dr;+re,nd.eksfï yelshdj. wvq n,yla;s mßfndackh. f;dard.eksï rdyshla iys;ùu.
fm%da.%ï lsífïos ie,lshhq;+ uq,sl lrekq.. uhsfl%dlkafg%da,rhla fm%da.%ï lsífïos ie,lsh hq;+ lrekq fudkjdo@
uhsfl%dlkafg%da,rhla fm%da.%ï lsífïos uq,sl jyfhka ;sìh hq;+ foaj,a fudkjdo''' IDE and Compiler Microcontr oller Programmer
PIC Programming Programmer Compiler
Packages of PIC Microcontrollers
Packages of PIC Microcontrollers
PIC 16F877A (DIP Package)
PIC16F8xxx
uq,sl Input/Output w;+re uqyqk;a PIC16F877A uhsfl%dlkafg%da,rh fodrgq mylska iukaú;fõ (PORT A,B,C,D,E ) fodrgqj, osydj ;SrKh lr.eksug TRIS kue;s frðiagrh Ndú;d lsíug is fõ' 1 Ndú;fhka Input fodrgqjla f,io, 0 Ndú;fhka Output fodrgqjla f,io ilid.; yel'
Binary / Decimal/ Hexa-Decimal Number Systems
Basic C Program to drive LEDs using PORTD void main() { } TRISD = 0x00; PORTD = 0x00; while(1) { } 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); // PORTD is output //initialize PORTD //move 1 to port D //wait for few seconds
Exercise wdf,dal ú,s ix{d iys; ld¾hnyq, ud¾.hla yryd.uka lrk frdfndahka;%hlg.e,sï igykla w kak'
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. wvq iïm;a mßfndackh. md,kh lsífï wmyiqnj. isoaëka i yd uq,sl;ajh osfï l%shdms<sfj;la ks¾udkh lsífï yelshdj'
Interrupt md,kh lrk wdldrh void main() { INTCON.GIE = 1; INTCON.INTE = 1; //Enable general interrupts //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 rooting else if (RBIF) go to relevant sub rooting }
Time to think fj< ie,lg we;+,ajk.kqfokqlrejka m%udkh.eksu i yd.kl hka;%hla ks¾udkh lrkak' l+uk wdldrfha Interrupts Ndú;dl, hq;+o@ l+uk wdldrfha ixfõol Ndú;dl, hq;+o@
PWM (Pulse Width Modulation)
PWM Ndú;d lrk wdldrhka void main() { TRISC = 0x01; Pwm_Init(5000); // PORTC is output //initialize PWM Pwm_Change_Duty(255); Pwm_Start(); Delay_ms(200); //Change duty Cycle 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 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