Lecture12-Using PIC Micro Controller 16F877A

57
1 Programming PIC16F877A

Transcript of Lecture12-Using PIC Micro Controller 16F877A

Page 1: Lecture12-Using PIC Micro Controller 16F877A

1

Programming PIC16F877A

Page 2: Lecture12-Using PIC Micro Controller 16F877A

ArchitectureArchitecture2

Page 3: Lecture12-Using PIC Micro Controller 16F877A

3

RA0-5 : Input/Output port A

RB0-7 : Input/Output port B

RC0-7 : Input/Output port C

RD0-7 : Input/Output port D

RE0-2 : Input/Output port E

AN0-7 : Analog input port

RX : USART Asynchronous Receive

TX : USART Asynchronous Transmit

SCK : Synchronous serial clock input

SCL : Output for both SPI and I2C modes

DT : Synchronous Data

CK : Synchronous Clock

SDO : SPI Data Out ( SPI mode )

SDI : SPI Data In ( SPI mode )

SDA : Data I/O ( I2C mode )

CCP1,2 : Capture In/Compare Out/PWM Out

OSC1/CLKIN : Oscillator In/Ecternal Clock In

OSC2/CLKOUT : Oscillator Out/Clock Out

MCLR : Master Clear ( Active low Reset )

Vpp : Programming voltage input

THV : High voltage test mode control

VREF+/- : Reference voltage

SS : Slave select for the synchronous serial port

T0CKI : Clock input to Timer0

T1OSO : Timer1 oscillator output

T1OSI : Timer1 oscillator input

T1CKI : Clock input to Timer1

PGD : Serial programming data

PGC : Serial programming clock

PGM : Low voltage programinng input

INT : External interrupt

RD : Read control for the parallel slave port

WR : Write control for the parallel slave port

CS : Select control for the parallel slave

PSP0-7 : Parallel slave port

VDD : Positive supply for logic and I/O pins

Vss : Ground reference for logic and I/O pins

One Pin can serve more than one function

Page 4: Lecture12-Using PIC Micro Controller 16F877A

4

I/ O pins There are 40 pins on PIC16F877A. Most of them can be used as an IO pin. Others are already for specific functions. These are the pin functions.

1. MCLR – to reset the PIC2. RA0 – port A pin 03. RA1 – port A pin 14. RA2 – port A pin 25. RA3 – port A pin 36. RA4 – port A pin 47. RA5 – port A pin 58. RE0 – port E pin 09. RE1 -  port E pin 110. RE2 – port E pin 211. VDD – power supply12. VSS – ground13. OSC1 – connect to oscillator14. OSC2 – connect to oscillator15. RC0 – port C pin 016. RC1 – port C pin 017. RC2 – port C pin 018. RC3 – port C pin 019. RD0 - port D pin 020. RD1 - port D pin 1

21. RD2 - port D pin 222. RD3 - port D pin 323. RC4 - port C pin 424. RC5 - port C pin 525. RC6 - port C pin 626. RC7 - port C pin 727. RD4 - port D pin 428. RD5 - port D pin 529. RD6 - port D pin 630. RD7 - port D pin 731. VSS - ground32. VDD – power supply33. RB0 - port B pin 034. RB1 - port B pin 135. RB2 - port B pin 236. RB3 - port B pin 337. RB4 - port B pin 438. RB5 - port B pin 539. RB6 - port B pin 640. RB7 - port B pin 7

Page 5: Lecture12-Using PIC Micro Controller 16F877A

So What’s Important For Us?So What’s Important For Us?5

The 5 ports! Port A – 6 bits Port B – 8 bits Port C – 8 bits Port D – 8 bits Port E – 3 bits

Page 6: Lecture12-Using PIC Micro Controller 16F877A

6

//PROGRAM: ALTERNATING LEDvoid main(){PORTB = 0; //initial value PORTB=0TRISB = 0; //PORTB as Output{do{PORTB = 0b00111111; //binary number 00111111Delay_ms(1000);PORTB = 0b00101010; // binary number 00101010Delay_ms(1000);PORTB = 0b00010101; // binary number 00010101Delay_ms(1000);}while(1) ;}}

Make Project: LED Pattern Display

Page 7: Lecture12-Using PIC Micro Controller 16F877A

7

Description Circuit in Robotics Kit

1. Main PIC

2. Power Supply

Page 8: Lecture12-Using PIC Micro Controller 16F877A

8

3. MICRO SWITCH

5. LED

4. LDR SENSOR

6. RS232C

R: Red

Y: Yellow

G: Green

Page 9: Lecture12-Using PIC Micro Controller 16F877A

9

Reading Data From Sensor

PIC16F887A can also be programmed to read sensor data with Analog to Digital Converter function and availability of Analog Input Port at 8 pins. Refer to table below for pin assignment of Analog Inputs.

PIN ASSIGNMENT

A0 A1 A2 A3 A5 E0 E1 E2

ANALOG INPUT

AN0 AN1 AN2 AN3 AN4 AN5 AN6 AN7

Page 10: Lecture12-Using PIC Micro Controller 16F877A

10

Analog-Digital Conversion

Also commonly referred as ADC, A/D or A to D, Analog-Digital Conversion is a process to transform a continuous analog signal to discrete digital form of signal. The ADC may be available in dedicated ADC IC (example AD7400 series from Analog Devices) or dedicated hardware that was normally referred as DAQ card (example PCI 6221 from NI) or can exist in-built of the IC that does the analog to digital conversion function such as one in PIC16F887A.

Page 11: Lecture12-Using PIC Micro Controller 16F877A

11

ADC Using PIC

PIC16F877A may receive analog signal input from up to 8 channels at the mentioned assigned pins. The analog signal input at the pin must be within range of 0-5 Volts. If the signal from input source is beyond that range, specific signal conditioning must be designed before input at the pin so that the signal will be converted to be within range.

Page 12: Lecture12-Using PIC Micro Controller 16F877A

12

Executing ADC Function

Below are MikroC specific program codes related to execution of ADC function:

1. Adc_ReadFunction: Start reading signal from an analog portCommand: Adc_Read(No_channel_analog_port);

2. ADCON1Function: Start activating ADC at all analog ports from bit-7 to bit-0,setting Vref+ at Vdd and Vref- at Vss Command: ADCON1 = 0x80;

Page 13: Lecture12-Using PIC Micro Controller 16F877A

ADCON1 SETTINGS

PCFG3:PCFG0

AN7RE2

AN6RE1

AN5RE0

AN4RA5

AN3RA3

AN2RA2

AN1RA1

AN0RA0

Vref+ Vref-

0000 A A A A A A A A Vdd Vss

0001 A A A A Vref+ A A A RA3 Vss

0010 D D D A A A A A Vdd Vss

0011 D D D A Vref+ A A A RA3 Vss

0100 D D D D A D A A Vdd Vss

0101 D D D D Vref+ D A A RA3 Vss

0110 D D D D D D D D Vdd Vss

0111 D D D D D D D D Vdd Vss

1000 A A A A Vref+ Vref- A A RA3 RA2

1001 D D A A A A A A Vdd Vss

1010 D D A A Vref+ A A A RA3 Vss

1011 D D A A Vref+ Vref- A A RA3 RA2

1100 D D D A Vref+ Vref- A A RA3 RA2

1101 D D D D Vref+ Vref- A A RA3 RA2

1110 D D D D D D D A Vdd Vss

1111 D D D D Vref+ Vref- D A RA3 RA2

13Table: Map settings analog-digital selections and Vref configurations at bit PCFG3:0

Page 14: Lecture12-Using PIC Micro Controller 16F877A

14

USART

USART, short form of Universal Synchronous/Asynchronous Receiver/Transmitter is feature of PIC that enables data communication between PIC and computer (PC) or between PIC and another microcontroller. Communication between PIC and PC is made possible through serial port (RS232 or USB).

To use this feature an ADC (3 pin) cable is connected from the kit board to the PC serial port.

Page 15: Lecture12-Using PIC Micro Controller 16F877A

15

Some commands related to USART feature

Prototype Usart_Init(const unsigned long baud_rate);

Returns Nothing.

Description Initializes hardware USART module with the desired baud rate. Refer to the device data sheet for baud rates allowed for specific Fosc. If you specify the unsupported baud rate, compiler will report an error.

Requires You need PIC MCU with hardware USART.

Usart_Init needs to be called before using other functions from USART Library.

Example This will initialize hardware USART and establish the communication at 2400 bps:Usart_Init(2400);

1. Usart_Init

Page 16: Lecture12-Using PIC Micro Controller 16F877A

Some commands related to USART feature

Prototype unsigned short Usart_Data_Ready();

Returns Function returns 1 if data is ready or 0 if there is no data.

Description Use the function to test if data in receive buffer is ready for reading.

Requires USART HW module must be initialized and communication established before using this function.

Example If data is ready, read it:

int receive; ...

if (Usart_Data_Ready()) receive = Usart_Read;

16

Some commands related to USART feature

2. Usart_Data_Ready

Page 17: Lecture12-Using PIC Micro Controller 16F877A

Prototype unsigned short Usart_Read();

Returns Returns the received byte. If byte is not received, returns 0.

Description Function receives a byte via USART. Use the function Usart_Data_Ready to test if data is ready first.

Requires USART HW module must be initialized and communication established before using this function.

Example If data is ready, read it:

int receive; ...

if (Usart_Data_Ready()) receive = Usart_Read;

17

Some commands related to USART featureSome commands related to USART featureSome commands related to USART featureSome commands related to USART feature

3. Usart_Read

Page 18: Lecture12-Using PIC Micro Controller 16F877A

18

Prototype Usart_Write(unsigned short data);

Returns Nothing.

Description Function transmits a byte (data) via USART.

Requires USART HW module must be initialized and communication established before using this function.

Example int chunk = 0x1E;

Usart_Write(chunk); /* send chunk via USART */

Some commands related to USART feature

4. Usart_Write

Page 19: Lecture12-Using PIC Micro Controller 16F877A

19

Write the program below in MikroC. Next compile and download the codes to PIC16F877A. Switch ON the kit to execute the program.//Program Name: HALOvoid main(){ USART_init(9600); // setting baud rate to 9600 do { //Sending halo Usart_Write('h'); Usart_Write('a'); Usart_Write('l'); Usart_Write('o'); //Time separator Delay_ms(2000); //Sending numbers Usart_Write(0b11111111); //binary format 11111111 = 255 decimal Usart_Write(0xAF); //hexadecimal format = 175 decimal Usart_Write(220); //decimal format 220 Usart_Write("212"); //Character Delay_ms(3000); } while(1);}

Exercise: Sending data from PIC to PC

Page 20: Lecture12-Using PIC Micro Controller 16F877A

20

1. From MikroC menu, select Tool>> USART Terminal (or press Ctrl+T)

A dialog box will appear:

Exercise: Sending data from PIC to PC

Page 21: Lecture12-Using PIC Micro Controller 16F877A

21

Exercise: Sending data from PIC to PC

2. In the Settings section, Select the particular COM Port and set the Baud as 9600 (because in the programming the Baud rate was initially set to 9600 through command USART_init(9600);).

3. Next, Click Connect, and the dialog box will appear as below (where in the box the communication details will be shown in codes). You can view the codes in different formats: Decimal, HEX or ASCII by selecting the desired

Page 22: Lecture12-Using PIC Micro Controller 16F877A

22

Exercise

In this section we will read data sensor LDR. LDR is a optoelectronic type of sensor, which responds to. Increasing light intensity will cause decrease in LDR resistance. Using voltage divider circuit, the resistance change will be trnasformed to voltage change.

Page 23: Lecture12-Using PIC Micro Controller 16F877A

23

Connection Diagram

The diagram shows the connection that will be used in the exercise. 3 LDR sensors will be used connected to PORT A0, A1 dan A2 of the PIC in the kit board. The objective is for us to see the sensor reading values in the PC and one of the LDR reading will be manifested as LED flashings at PORT B0 through B5.

Page 24: Lecture12-Using PIC Micro Controller 16F877A

24

Program

//PROGRAM NAME: ADCMIKROC//Determining variable formatunsigned short input_a0, input_a1, input_a2,separator=0;void main(){ADCON1 = 0; // Activating ADC at analog input portTRISA = 255; // PORTA as inputTRISB = 0; // PORTB as outputUSART_init(9600);do { input_a0 = ADC_Read(0); // get data analog from A0 input_a1 = ADC_Read(1); // get data analog from A1 input_a2 = ADC_Read(2); // get data analog from A2 PORTB = input_a0; //Put data analog A0 in PORTB Usart_Write(input_a0); //write data A0 to PC Delay_ms(500); Usart_Write(input_a1); //write data A1 to PC Delay_ms(500); Usart_Write(input_a2); // write data A2 to PC Delay_ms(2000); Usart_Write(separator); //As batch data separator}while(1);}

PORTB will manifest reading from pin A0 only

Page 25: Lecture12-Using PIC Micro Controller 16F877A

25

Result

After connecting serial cable between PC and robot kit, switch ON the robot kit. Put the robot kit on a piece of white paper. In Windows Terminal from MikroC we will be able to see the sensor reading data from the 3 LDRs in real time transferred from the PIC. In example below the readings were configured in decimal format. Note that ‘0’ has been set as separator for each batch of readings (each executed looping).

Page 26: Lecture12-Using PIC Micro Controller 16F877A

26

Result

From the terminal we can see sensor LDR 1 (at Port A0) reading as 195, sensor LDR 2 (A1) as 63 and LDR 3 (A2) as 139. These values are within range of 0 to 255. Value 0 is equivalent to 0000 0000, while 255 is 1111 1111. Range 0-255 is also equivalent to 0 – 5V (voltage range operating at PIC pins). Every bit change of binary number will result in voltage change of 19.6 mV. Hence 195 is equivalent to 195 x 19.6 mV = 3.822 V.

Page 27: Lecture12-Using PIC Micro Controller 16F877A

27

LDR Calibration

In this experiment, the robotic kit was put on a wide piece of white paper that covered all the 3 LDRs. Hence the light intensity for all the LDRs should be the same. But instead of getting the same readings for each LDR we can see from transmitted data that the readings were different. This could be due to different characteristics of material compositing individual LDR or due to manufacturing impairment. Calibration should be done such that when the LDRs is exposed to the same light intensity, the readings should be about the same. Let us set for this kind of intensity the value should be 150. Hence for sensor1 the value should be decreased by 45; for sensor2 should be added by 87 while for sensor3 should be added by 11. The program that included calibration procedure is in the next slide.

Page 28: Lecture12-Using PIC Micro Controller 16F877A

28

Program (including Calibration)

//PROGRAM: LDR SENSOR CALIBRATIONunsigned short input_a0, input_a1, input_a2,separator=0;unsigned short kal_a0, kal_a1, kal_a2;//void main(){ADCON1 = 0; // Activating all analog input pinsTRISA = 255; // PORTA as inputTRISB = 0; // PORTB as outputUSART_init(9600);do { input_a0 = ADC_Read(0); // Get data analog from A0 input_a1 = ADC_Read(1); // Get data analog from A1 input_a2 = ADC_Read(2); // Get data analog from A2 //Calibration kal_a0 = input_a0-45; kal_a1 = input_a1+87; kal_a2 = input_a2+11; // PORTB = kal_a0; //Displaying data analog in binary from B0 at PORTB //B0 Usart_Write(kal_a0); //Sending data B0 to PC Delay_ms(500); //B1 Usart_Write(kal_a1); //Sending data B1 to PC Delay_ms(500); //B2 Usart_Write(kal_a2); //Sending data B2 to PC Delay_ms(2000); Usart_Write(separator); //As batch separator}while(1);}

Page 29: Lecture12-Using PIC Micro Controller 16F877A

29

Result

After execution we can see that the sensor reading is now more precise under the same condition (±15).

Page 30: Lecture12-Using PIC Micro Controller 16F877A

DC Motors30

Based on its name then DC motors is a motor drived by Direct Current

Page 31: Lecture12-Using PIC Micro Controller 16F877A

DC Motors31

If we open a DC Motor

Page 32: Lecture12-Using PIC Micro Controller 16F877A

How it works?32

When electric current passes through a coil in a magnetic field, the magnetic force produces a torque which turns the motor.

Force in Motor:F=ILBF = Force B = Magnetic FieldL = Length of

ConductorI = Current in

Conductor

Torque in Motor:T = IBA sin θA = LWL = Length of WindingW = Width of Winding

Page 33: Lecture12-Using PIC Micro Controller 16F877A

Speed Control of DC Motor33

Based on Ohm’s law, V=I.R, Because Coil Resistance is constant then to increase current

is use increasing voltage.

Page 34: Lecture12-Using PIC Micro Controller 16F877A

Direction Control of DC Motor34

If we can change current direction, then magnetic force also change and this can make effect to change motor direction.

Clock Wise (CW). Counter Clock Wise (CCW).

Page 35: Lecture12-Using PIC Micro Controller 16F877A

H-Bridge35

Switch Dual Pole Dual Throw in previous figure is similar with H-Bridge

Page 36: Lecture12-Using PIC Micro Controller 16F877A

DC Motors36

With Use Table below. We can change motor direction with change condition in each switch ON or OFF.

Page 37: Lecture12-Using PIC Micro Controller 16F877A

Driver for DC Motors37

The switch in previous figure can be changed with relay, transistor or mosfet. Other alternative use an IC L293D.

Page 38: Lecture12-Using PIC Micro Controller 16F877A

DC Motors38

Relation of received input for IC L293D and results of Motor Moving

Page 39: Lecture12-Using PIC Micro Controller 16F877A

39

Diagram L293D

Connect the wires as this figure

Page 40: Lecture12-Using PIC Micro Controller 16F877A

Problem to run Robot Forward and Reverse Direction

40

Problem:

Male robot to move Forward 2 s, stop 1 s, reverse 2 sec, Stop 1 s

Forward Direction set this value Motor 1: C1=1, C0=0, D0=1Motor 2: C2=1, C3=0, D1=1Reverse Direction :Motor 1: C1=1, C0=1, D0=0Motor 2: C2=1, C3=1, D1=0Stop C1 = 0 dan C2=0 (Enable=0)

Connect the wires as this figure

Page 41: Lecture12-Using PIC Micro Controller 16F877A

41

void main(){PORTC = 0; //initial value PORTC=0TRISC = 0b00000000; //PORTB as OutputPORTD = 0; //initial value PORTD=0TRISD = 0b00000000; //PORTD as Output{do {//FORWARDPORTC.F1 = 1; //C1=1 EN1=1PORTC.F0 = 0; //C0=0 IN1=0PORTD.F0 = 1; //D0=1 IN2=1PORTC.F2 = 1; //C2=1 EN2 =1PORTC.F3 = 0; //C3=0 IN3=0PORTD.F1 = 1; //D1=1 IN4=1Delay_ms(2000); //FORWARD 2 seconds//STOPPORTC.F1 = 0; //C1=0PORTC.F2 = 0; //C2=0Delay_ms(1000); //STOP 1 seconds

//REVERSEPORTC.F1 = 1; //C1=1 EN1= 1PORTC.F0 = 1; //C0=1 IN1= 1PORTD.F0 = 0; //D0=0 IN2= 0PORTC.F2 = 1; //C2=1 EN2= 1PORTC.F3 = 1; //C3=1 IN2= 1PORTD.F1 = 0; //D1=0 IN4= 0Delay_ms(2000); //REVERSE 2 seconds//STOPPORTC.F1 = 0; //C1=0PORTC.F2 = 0; //C2=0Delay_ms(1000); //STOP 1 seconds}while(1) ;}}

Program

Page 42: Lecture12-Using PIC Micro Controller 16F877A

42

Varying Motor Speed

The diagram above showed that pin1 and pin9 of IC L293 have function ENABLE and also PWM. If input for ENABLE =1, IC will drive the motor and hence the motor will turn but if input for ENABLE=0 Dalam gambar di atas menunjukkan bahwa dalam pada Pin 1 dan 9 terdap, IC will not drive the motor and hence motor will stop turning.

When pin 1 and 9 given PWM signal input, hence the ON/OFF switching rate in H-Bridge circuit inside IC will be determined by duty ratio of the PWM signal. If the duty ratio is big, the motor will turn fast but if the duty ratio is small, the motor will turn slower.

Page 43: Lecture12-Using PIC Micro Controller 16F877A

43

Pulse width Modulation (PWM).

PWM is pulse signal that the width can be varied.

tON is period when the pulse is HIGH (5 V) while tOFF is period when the pulse id LOW (0 V). PIC16F877A has two PORTs that can generate PWM signal which is PORT C1 (CPP2) and C2 (CPP1).

Page 44: Lecture12-Using PIC Micro Controller 16F877A

44

PWM commands in MikroC

1. Pwm_InitPrototype: Pwm1_Init(long freq); To activate PWM function at C2 (CPP1)Pwm2_Init(long freq); To activate PWM function at C1 (CPP2)Description: Initialize PWM module with duty ratio 0. Frequency value has to be defined e.g. 1000 as 1 kHz. This function has to be called first to generate PWM.Example: Pwm1_Init(5000); // Activating PWM function at signal frequency 5 kHz.

2. Pwm_Change_DutyPrototype: Pwm1_Change_Duty(short duty_ratio); To change duty ratio of PWM at C2 (CPP1)Pwm2_Change_Duty(short duty_ratio); To change duty ratio of PWM at C1 (CPP2)Description: To change value of PWM duty ratio. Value of duty ratio here is within range of 0 to 255, where 0 is 0%, 127 is 50% and 255 is 100%. Example: Pwm1_Change_Duty(192); // Set duty ratio 75%

Page 45: Lecture12-Using PIC Micro Controller 16F877A

45

3. Pwm_StartPrototype: Pwm1_Start(); Description: To start PWMExample: Pwm1_Start();

4. Pwm_StopPrototype: Pwm1_Stop(); Description: To stop PWMExample: Pwm1_Stop();

Perintah PWM dalam MikroC

When using PWM library we don’t have to set values for PORT C1 dan C2 output because it has been predetermined by PWM function.

Page 46: Lecture12-Using PIC Micro Controller 16F877A

46

Exercise

1. TaskWe would like to move the robot forward with full speed for 1 sec, then change the speed to moderate speed for the next 1 sec. After that the robot speed should reduce to low speed for 1 sec and finally the robot will stop for 1 sec.

2. PIC ConnectionConnection diagram is as side:

Page 47: Lecture12-Using PIC Micro Controller 16F877A

47

Algorthm

• Defining PORT that will be used as input and output• Setting initial value for output port• Start ProgramMove full speed: Pwm1_Init(5000); Pwm2_Init(5000); Pwm1_Change_Duty(255); //Ratio 100% Pwm2_Change_Duty(255); Pwm1_Start(); //Memulai PWM Pwm2_Start(); PORT C0=0, D0=1, C3=0, D1=1, delay 1 detikMove moderate speed: Pwm1_Change_Duty(127); //Ratio 50% Pwm2_Change_Duty(127); Delay 1 detik.Move slow speed: Pwm1_Change_Duty(26); //Ratio 10% Pwm2_Change_Duty(26); Delay 1 detik.Stop Pwm1_Stop(); Pwm2_Stop(); Delay 1 detik

Page 48: Lecture12-Using PIC Micro Controller 16F877A

48

Program

/* PROGRAM: MOTOR_FAST_SLOW*/void main(){ PORTC = 0; //initial value PORTC=0 TRISC = 0b00000000; //PORTC as Output PORTD = 0; //initial value PORTD=0 TRISD = 0b00000000; //PORTD as Output { do { //FAST FORWARD Pwm1_Init(5000); //Initialize PWM Frekwensi 5kHz Pwm2_Init(5000); Pwm1_Change_Duty(255); //Ratio 100% Pwm2_Change_Duty(255); Pwm1_Start(); //Start PWM Pwm2_Start(); PORTC.F0 = 0; //C0=0 PORTD.F0 = 1; //D0=1 PORTC.F3 = 0; //C3=0 PORTD.F1 = 1; //D1=1 Delay_ms(1000); //FAST FORWARD 1 sec // //

Page 49: Lecture12-Using PIC Micro Controller 16F877A

49

Program

//MODERATE FORWARD Pwm1_Change_Duty(127); //Duty Ratio 50% Pwm2_Change_Duty(127); Delay_ms(1000); //MODERATE FORWARD 1 sec // // //SLOW FORWARD Pwm1_Change_Duty(26); //Ratio 10% Pwm2_Change_Duty(26); Delay_ms(1000); // SLOW FORWARD 1 sec // //STOP Pwm1_Stop(); Pwm2_Stop(); Delay_ms(1000); //STOP 1 sec // } while(1) ; }}

Page 50: Lecture12-Using PIC Micro Controller 16F877A

50

Orienting Robot Movement

The robotic kit is motioned by wheels each separately controlled by a DC motor respectively. Hence we can direct the robot movement either to turn left or right by making the motor of one wheel turns faster than the motor of the other wheel. In the side picture shown a mobile robot moving. The bottom image shows robot advancing forward. For a robot to advance in straight line, the speed of both motors are the same. To make the robot turn left, the right motor must turn faster than the left motor and so vise versa.

Page 51: Lecture12-Using PIC Micro Controller 16F877A

51

Program

/* PROGRAM: ROBOT_TURN_LEFT_RIGHT*/void main(){ PORTC = 0; //intial value PORTC=0 TRISC = 0b00000000; //PORTC as Output PORTD = 0; //initial value PORTD=0 TRISD = 0b00000000; //PORTD as Output { do { //FAST FORWARD Pwm1_Init(5000); //Initialize PWM Frekwensi 5kHz Pwm2_Init(5000); Pwm1_Change_Duty(255); //Ratio 100% Pwm2_Change_Duty(255); Pwm1_Start(); //Start PWM Pwm2_Start(); PORTC.F0 = 0; //C0=0 PORTD.F0 = 1; //D0=1 PORTC.F3 = 0; //C3=0 PORTD.F1 = 1; //D1=1 Delay_ms(1000); //FAST FORWARD 1 sec //

// //MODERATE FORWARD Pwm1_Change_Duty(127); //Ratio 50% Pwm2_Change_Duty(127); Delay_ms(1000); // MODERATE FORWARD 1 sec // // //TURN RIGHT Pwm1_Change_Duty(255); //Ratio 100% Pwm2_Change_Duty(0); //Ratio 0% Delay_ms(1000); //TURN RIGHT 1 sec // //MODERATE FORWARD Pwm1_Change_Duty(127); //Ratio 50% Pwm2_Change_Duty(127); Delay_ms(2000); // MODERATE FORWARD 2 sec // //TURN LEFT Pwm1_Change_Duty(0); //Ratio 0% Pwm2_Change_Duty(255); //Ratio 100% Delay_ms(1000); //TURN LEFT 1 sec // //STOP Pwm1_Change_Duty(0); //Ratio 0% Pwm2_Change_Duty(0); //Ratio 0% Delay_ms(2000); //STOP 2 sec // } while(1) ; }}

Page 52: Lecture12-Using PIC Micro Controller 16F877A

52

Integrating Sensor, Motor and Microcontroller to Build

Line Follower Robot

Page 53: Lecture12-Using PIC Micro Controller 16F877A

53

For Report: Write, try and Analysis this program

/*PROGRAM: LFR_FOR_TWO_SENSORS *///Define all variablesunsigned short input_a0, input_a1, input_a2, separator=0;//void main(){ //Initialize initial value and setting PORT ADCON1 = 0; // Activate ADC at all AN pins TRISA = 255; // PORTA as input TRISB = 0; // PORTB as output USART_init(9600); //Initialize protocol USART baud 9600/s PORTC = 0; //Initial value PORTC=0 TRISC = 0b00000000; //PORTC as Output PORTD = 0; //Initial value PORTD=0 TRISD = 0b00000000; //PORTD as Output //Initialize Motor Pwm1_Init(5000); //Initialize PWM Frekwensi 5kHz Pwm2_Init(5000); Pwm1_Change_Duty(0); //Ratio 0% Stop Motor Pwm2_Change_Duty(0); Pwm1_Start(); //Start PWM Pwm2_Start(); Delay_ms(2);

Page 54: Lecture12-Using PIC Micro Controller 16F877A

54

///Initialize motor to move forward PORTC.F0 = 0; //C0=0 PORTD.F0 = 1; //D0=1 PORTC.F3 = 0; //C3=0 PORTD.F1 = 1; //D1=1 Pwm1_Change_Duty(255); //Ratio 78% Forward Pwm2_Change_Duty(255); Delay_ms(500); //MAJU 0.5 detik do { //****BAGIAN ADC DAN BACA DATA DARI SENSOR { //Gerbang 1 Mulai hanya untuk kalibrasi //Gunakan Pemisah agar mudah melihat data Usart_Write(pemisah); //Angka 0 Sebagai pembatas //Letakkan setiap sensor di kertas putih //Dapatkan nilai input_a0 dan a1, lalu lakukan kalibrasi agar sama input_a0 = ADC_Read(0)+0; // Dapatkan data analog dari A0 (LDR1) Delay_ms(5); input_a2 = ADC_Read(2)+33; // Dapatkan data analog dari A2 (LDR3) Delay_ms(5); //Kirim Data ke Komputer hanya untuk kalibrasi ///Kirim data mentah ke komputer Usart_Write(input_a0); //mengirim nilai input a0 ke komputer Usart_Write(input_a1); //mengirim nilai input a1 ke komputer Usart_Write(input_a2); //mengirim nilai input a2 ke komputer //Delay_ms(2); ///Kirim data mentah A0 saja ke port B untuk melihat di LED PORTB = input_a0; //Nilai binari PORTB = data analog B0 Delay_ms(5);

Page 55: Lecture12-Using PIC Micro Controller 16F877A

55

} //Gerbang 1 berakhir //*BACA ADC SELESAI //****Bagian Menjejak garis dan Mengontrol Arah { //Gerbang 2 Mulai pakai IF ELSE //jika input_a2>input_a0 maka robot ke kanan if(input_a2>input_a0) { Pwm1_Change_Duty(0); //ke kanan Pwm2_Change_Duty(150); // } //jika input_a2<input_a0 robot ke kiri else { Pwm1_Change_Duty(150); //ke kiri Pwm2_Change_Duty(0); // } } //Gerbang 2 akhir } while(1);}

Page 56: Lecture12-Using PIC Micro Controller 16F877A

56

Assignment

Page 57: Lecture12-Using PIC Micro Controller 16F877A

57

Make programs to solve the following tasks

1. Light Tracking RobotProgram structure is basically the same with LFR, requiring 2 LDR sensors. The sensors output are to be connected to pin SW1 and SW2. From these pins, connection should be made to Y1 & Y2 pins and finally to port A0 and A1, respectively. The algorithm is:

• Robot starts with moving forward and then, after a short while, LDR sensor starts reading data from light source.

• If sensor 1 > 2 hence motor will turn left, else motor will turn right.

2. Obstacle Evader RobotThe sensors are 2 microswitches that should be connected to pin SW3 and SW4. From these pins, connection should be made to pin Y3 and Y4 and finally to port A3 and A4. Port A3 and A4 should be set as digital input. The algorithm is:

• Set port A3 and A4 as digital input (Use ADCON=1) • Robot starts with moving forward and then, after a short while, PIC will

check whether any of input from port A3 or A4 is high.• If A3 high, robot will reverse and then turn right, else robot will reverse and

turn left. After that robot will move straight.