PIC Microcontroller Programming & Practical: PIC MicroController with MikroC [2 ed.]

This is 2nd edition of PIC microcontroller Programming & Practical and its Medium level Project book with Programmin

2,332 436 2MB

English Pages [57]

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

PIC Microcontroller Programming & Practical: PIC MicroController with MikroC [2 ed.]

Citation preview

PIC MICROCONTROLLER PROGRAMMING & PRACTICALS

FAYYAZ HUSSAIN ELEKTRONICS GARAGE Rawalpindi Pakistan

PIC Microcontroller Programming & Practical MikroC Edition

With Complete Circuit diagrams & Descriptions

Fayyaz Hussain

1|Page

Preface I am working to manage my PIC controller projects data in book form in which just little idea, circuit and MikroC code is written. In this view it my 2nd effort and small handy book in which I have discus some medium level Projects for students and new learners before I have written basic level book of ten projects which is available on amazon. Next I am working on advance level Projects and managing in book form. Insha allah that version will be complete with basic, medium and advance level projects.

Dedication I am dedicating my all these efforts to my honorable parents and respected teachers.

About Writer Lecturer: Swedish Institute of technology Rawalpindi Pakistan Qualification: DAE Electronics B-Tech Electronics (Preston University Islamabad) MSc Electronics (ISRA University Islamabad) MS / MPhil Electronics (Continue) from ISRA University Islamabad Email: [email protected] Blog: www.elektronicsgarage1.blogspot.com Wordpress: www.elektronicsgarage.wordpress.com

2|Page

Contents Preface .......................................................................................................................................................... 2 Dedication ..................................................................................................................................................... 2 About Writer ................................................................................................................................................. 2 1.

Display on Dot matrix............................................................................................................................ 4

2.

Scrolling text on LCD ............................................................................................................................. 7

3.

Password based door lock & block ..................................................................................................... 11

4.

Counting with falling edge of push button ......................................................................................... 16

5.

Interface temperature sensor (LM35) ................................................................................................ 19

6.

Electric boiler (ON/OFF) Controller ..................................................................................................... 23

7.

Interface LDR for light intensity measurement................................................................................... 27

8.

Sun tracker .......................................................................................................................................... 31

9.

PWM (Variable duty cycle) using potentiometer ............................................................................... 35

10.

Ampere meter ................................................................................................................................. 38

11.

Digital Volt meter using PIC microcontroller .................................................................................. 43

12.

Digital Watt meter .......................................................................................................................... 47

13.

Stepper motor control .................................................................................................................... 51

14.

UART Communication ..................................................................................................................... 54

3|Page

1. Display on Dot matrix Dot matrix is an array of LEDs (group of LEDs) which use to display Graphic or text by different combination of ON LEDs.

Programming of dot matrix is very tricky let start to make a Program for “E” Text Graphic to display like as under. Dot matrix consist on 7 rows and 5 columns, So scan columns by logic 1 and apply logic 0 to that row where desire LED exists to ON. For “E” text Graphic: 1. Column1=high all other Columns =0, & all rows = low to display 1st line. 2. Column2=high all other columns =0, & R1, R4 and R7 low and all other high. 3. Same as rows logic for Column3 and 4 4. Column5=high all other low & in rows R1 and R7 low all other rows remain high to display Complete text graphic “E” Anode of LEDs Connected with Columns and cathode connected with rows so positive logic is for columns and negative logic for rows.

4|Page

7x5 Dot matrix Circuit:

MikroC Programming: // Dot Matrix 7x5 // //Elektronicsgarage1.blogspot.com// #include int i=0, j=0; void main() { TRISB=0x00; TRISC=0x00; PORTB=0x00; PORTC=0x00; while (1){ Delay_ms(1); PORTC=0x10; PORTB=0x00; Delay_ms(1); PORTC=0x08; 5|Page

PORTB=0x36; Delay_ms(1); PORTC=0x04; PORTB=0x36; Delay_ms(1); PORTC=0x02; PORTB=0x36; Delay_ms(1); PORTC=0x01; PORTB=0x3E; } }

6|Page

2. Scrolling text on LCD Here is new Project related to LCD text displaying and scrolling. Sometime if text is large to display on LCD then we scroll it toward left side or some time we scroll data right side depends on application of the project. Components list 1. 2. 3. 4. 5. 6. 7.

PIC16F877A (Pin out in 1st Practical) / 18F452 (same pin out) LCD 16x2 (for Character display) Crystal 10MHz Capacitors 33pf Pot-meter (variable resistor) Push button Polar Capacitor 47uf 16v (Electrolytic)

Pin Out 18F452:

7|Page

16x2 LCD Pin Out:

Circuit Diagram:

MikroC Programming Code: //// +++ Designed by Elecktronics garage ////// //// www.elektronicsgarage1.blogspot.com ////

8|Page

#include //LCD define pins and set directions sbit LCD_RS at RB0_bit; sbit LCD_EN at RB1_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit; sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit;

sbit LCD_EN_Direction at TRISB0_bit; sbit LCD_RS_Direction at TRISB1_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit; char text1[]=" Designed by ";

// Text to display

char text2[]=" FAYYAZ HUSSAIN "; char text3[]="www.elektronicsgarage1.blogspot.com"; int i=0; void main() { LCD_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); // to off cursor 9|Page

while (1) { Lcd_Out(1,1,text1); // text1 array display here Lcd_Out(2,1,text2); // text2 array display here Delay_ms(1000); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(2,1,text3); for(i=0; i0){ Delay_ms(30);i=0; if (num5==2636) // four digit password you can change it but must 4 digit { Delay_ms(100);dor = 1; Lcd_Out(2,1," OK- DOOR OPEN ");} else Lcd_Out(2,1," *** WRONG *** "); } Delay_ms(30); if (PORTD==0x48){Delay_ms(30); //# to exit ledkp=0;Delay_ms(300);dor = 0; break; }}}}}

15 | P a g e

4. Counting with falling edge of push button This is simple Project but some tricky because of working / counting number when we leave the push button (means working on falling edge) and Counting displayed on 16x2 LCD. Here are some learning points for students 1. 2. 3. 4. 5.

How to interface 16x2 LCD How to use pull up resistor How to use push button for falling edge How to use built-in libraries of MikroC Compiler How to use conversion libraries of MikroC

List of Components: 1. 2. 3. 4. 5. 6.

PIC16F877A Crystal 10MHz Non Pol Capacitor 22pf Push button Resistors 1k ohm LCD 16x2

Circuit Diagram:

16 | P a g e

MikroC Programming: //// Falling edge counter /// /// www.Elektronicsgarage1.blogspot.com/// /// www.elektronicsgarage.wordpress.com /// #include sbit pbu at RD0_bit; //sbit pbd at RD1_bit; unsigned int num=0; unsigned int num1[4]; // Array defined for Ascii

sbit LCD_RS at RB4_bit; // LCD Pin declaration sbit LCD_EN at RB5_bit; sbit LCD_D4 at RB0_bit; sbit LCD_D5 at RB1_bit; sbit LCD_D6 at RB2_bit; sbit LCD_D7 at RB3_bit; sbit LCD_RS_Direction at TRISB4_bit; sbit LCD_EN_Direction at TRISB5_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB1_bit; sbit LCD_D6_Direction at TRISB2_bit; sbit LCD_D7_Direction at TRISB3_bit;

void main() {

17 | P a g e

// main Program

Lcd_init();

// LCD initializing

TRISB=0x00;

// PORTB as output

TRISD=0x0F;

// PORTD DL=input, DH=output

PORTB=0x00; PORTD=0x00; Lcd_Cmd(_LCD_CLEAR);

// LCD clear

Lcd_Cmd(_LCD_CURSOR_OFF);

// LCD cursor OFF

Lcd_Out(1,1," [ EG PROJECT ] "); // Text display on LCD

while(1){

// forever loop

if (~pbu==1){ Delay_ms(10); if (pbu==1){num++; Delay_ms(10); IntToStr(num, num1); // int to string Converssion Lcd_Out(2,1,num1); } } Delay_ms(10); } }

18 | P a g e

5. Interface temperature sensor (LM35) Temperature sensor is also a very important application for domestic and industrial field. Here we are just learning about the interfacing of LM35 temperature sensor IC with PIC microcontroller 16F877A. By using this basic but important project we can create lot of major applications like boiler control just ON OFF or as well linear control. There are some following learnings for students. 1. 2. 3. 4. 5. 6.

What is LM35 How to program LM35 How to read ADC How to convert ADC value into temperature value How to convert value into degree How to interface LCD 16x2 with PIC16F877A

Components list: 1. 2. 3. 4. 5. 6. 7. 8.

16F877A Crystal 10MHz Non-pol capacitor 22pf LCD16x2 Pot-meter Resister 10k ohm LM35 temperature sensor 5V 1A adopter

Circuit Diagram: Next Page

19 | P a g e

MikroC Code: //// Temperature sensor //// //// elektronicsgarage1.blogspot.com //// #include unsigned int val1=0,val2=0,i=0; float temp=0; char dgre=176; sbit LCD_RS at RB4_bit; sbit LCD_EN at RB5_bit; sbit LCD_D4 at RB0_bit;

20 | P a g e

sbit LCD_D5 at RB1_bit; sbit LCD_D6 at RB2_bit; sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit; sbit LCD_EN_Direction at TRISB5_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB1_bit; sbit LCD_D6_Direction at TRISB2_bit; sbit LCD_D7_Direction at TRISB3_bit; unsigned int display1[4]; void main() { Lcd_init(); ADC_init(); TRISB = 0x00; TRISA = 0xFF; PORTB = 0x00; PORTA = 0x00; Lcd_Cmd(_LCD_CLEAR);Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1,"ELEKTRONICS GARAGE"); for(i=0; i10){mfor();} // Motor rotation on difference of 10 if (dif2>>10){mrev();} else st1=0;st2=0;st3=0;st4=0; // if difference < 10 then no Move } } void mfor() // here is just stepper motor forward clocking sequence {st1=1;Delay_ms(5);st2=0;Delay_ms(5);st3=0;Delay_ms(5);st4=0;Delay_ms(5); st1=0;Delay_ms(5);st2=1;Delay_ms(5);st3=0;Delay_ms(5);st4=0;Delay_ms(5); st1=0;Delay_ms(5);st2=0;Delay_ms(5);st3=1;Delay_ms(5);st4=0;Delay_ms(5); st1=0;Delay_ms(5);st2=0;Delay_ms(5);st3=0;Delay_ms(5);st4=1;Delay_ms(5); } void mrev() //stepper motor reverse clocking sequence

33 | P a g e

{st1=0;Delay_ms(5);st2=0;Delay_ms(5);st3=0;Delay_ms(5);st4=1;Delay_ms(5); st1=0;Delay_ms(5);st2=0;Delay_ms(5);st3=1;Delay_ms(5);st4=0;Delay_ms(5); st1=0;Delay_ms(5);st2=1;Delay_ms(5);st3=0;Delay_ms(5);st4=0;Delay_ms(5); st1=1;Delay_ms(5);st2=0;Delay_ms(5);st3=0;Delay_ms(5);st4=1;Delay_ms(5); }

34 | P a g e

9. PWM (Variable duty cycle) using potentiometer In this project we want to control the duty cycle of a waveform to control the switching time of BJT for converters, in SMPS (Switched Mode Power Supply), to control speed of DC motor likewise examples. In this project duty cycle change from 1% to 99%. Here are proteus results. Here are some learning points for Students: 1. 2. 3. 4. 5.

How to read analog values How to program ADC for analog values How to program PWM How to see oscilloscope results in proteus How to use built-in libraries of MikroC

List of components: 1. 2. 3. 4. 5. 6. 7.

PIC16F877A Crystal 10MHz Non pol capacitors 33pf Resistors BJT 2N3904 LED Potentiometer

Circuit Diagram:

35 | P a g e

Proteus Oscilloscope result:

MikroC Programming: /// Variable PWM using Potentiometer /// ////www.Elektronicsgarage1.blogspot.com ///

#include int potval=0; unsigned short current_duty, old_duty, current_duty1, old_duty1; void main() { TRISA = 0xFF; PORTA = 0x00; TRISC = 0x00; PORTC = 0x00; PWM1_Init(1023); ADC_Init(); 36 | P a g e

// Initialize PWM1 module at 1KHz

current_duty = 1;

// initial value for current_duty

PWM1_Start();

// start PWM1

PWM1_Set_Duty(current_duty);

while (1) {

// Set current duty for PWM1

// endless loop

potval = ADC_Read(0); Potval = potval; Delay_ms(5); current_duty = potval;

// PWM changing factor position

PWM1_Set_Duty(current_duty); } Delay_ms(5); }

37 | P a g e

10.

Ampere meter

Ampere meter using PIC microcontroller is a very important application. Here we are discussing idea of Digital Ampere meter only for single range 1 mA to 1000 mA but it’s extendable for multi range by changing the shunt resister. In this project there are some learning points for students and learning.      

How to Program ADC How to set voltage ratio for ADC input How to adjust attenuator for voltmeter Interfacing of LCD Reading values of ADC How to use built-in libraries of MikroC Pro for PIC

Here is the list of components     

PIC16F877A Crystal 10MHz Non pol capacitors 33pf Resistors LCD 16x2

Here is not such a complex calculation required but it’s must to adjust the shunt resistor for other range.

First of all here is the Known parameters   

Vsh must be 5v Max Applied voltage 50v Max Current range 500mA

Now find the following parameters  

Value of Rsh Value of RL for max Current

First case: IT = 500mA and VSH = 5v (Max)

38 | P a g e

So RSH =

VSH IT

5𝑣

= 500𝑚𝐴 = 10 Ω

𝑉𝐿

45𝑣

And RL = 𝐼𝑇 = 500𝑚𝐴 = 90 Ω Now in programming point of view what should do? When Vin Min at RA0 (Analog ADC pin no 0) then ADC will read ten zeros (like: 0000000000) and when Vin Max at input then max 5v Controller convert this into digital of ten bits (like: 1111111111). 0000000000 means – 0 and ten ones 1111111111 means 210 = 1024 So now here is very simple that when VIN max at 5V then Display at LCD 500mA but ADC actual value is 1024 so fined the divider factor for programming. So dividing factor =

1024 = 500

2.084

volt = ADC_Read(0); // ADC reading v1=volt/factor; // ADC output divided by factor Circuit Diagram:

39 | P a g e

MikroC Programming Code: //// Digital Ampere meter //// //// elektronicsgarage1.blogspot.com ////

#include

float v1=0,v2=0; int i=0; float factor = 1.024; // factor calculated int volt=0;

sbit LCD_RS at RB4_bit; 40 | P a g e

sbit LCD_EN at RB5_bit; sbit LCD_D4 at RB0_bit; sbit LCD_D5 at RB1_bit; sbit LCD_D6 at RB2_bit; sbit LCD_D7 at RB3_bit; sbit LCD_RS_Direction at TRISB4_bit; sbit LCD_EN_Direction at TRISB5_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB1_bit; sbit LCD_D6_Direction at TRISB2_bit; sbit LCD_D7_Direction at TRISB3_bit; unsigned int display1[4]; // array for Converted value void main() { Lcd_init();// initialize LCD ADC_init(); // initialize ADC TRISB = 0x00; TRISA = 0xFF; PORTB = 0x00; PORTA = 0x00; Lcd_Cmd(_LCD_CLEAR);Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1," PROJECT BY "); Lcd_Out(2,1,"ELEKTRONICS GARAGE"); for(i=0; i