You are on page 1of 57

Programming PIC16F877A

Architecture
2

One Pin can serve more than one function

MCLR : RA0-5 : RB0-7 : RC0-7 : Input/Output port A Input/Output port B Input/Output port C Vpp : THV : VREF+/: SS : T0CKI : T1OSO : T1OSI : T1CKI : PGD : PGC : PGM : INT : RD : WR : CS : PSP0-7 : VDD : Vss :

Master Clear ( Active low Reset ) Programming voltage input

High voltage test mode control


Reference voltage Slave select for the synchronous serial port Clock input to Timer0 Timer1 oscillator output Timer1 oscillator input Clock input to Timer1 Serial programming data Serial programming clock Low voltage programinng input External interrupt Read control for the parallel slave port Write control for the parallel slave port Select control for the parallel slave Parallel slave port Positive supply for logic and I/O pins Ground reference for logic and I/O pins

RD0-7 :
RE0-2 : AN0-7 : RX : TX : SCK :

Input/Output port D
Input/Output port E Analog input port USART Asynchronous Receive USART Asynchronous Transmit Synchronous serial clock input

SCL :
DT : CK : SDO : SDI : SDA :

Output for both SPI and I2C modes


Synchronous Data Synchronous Clock SPI Data Out ( SPI mode ) SPI Data In ( SPI mode ) Data I/O ( I2C mode )

CCP1,2 :
OSC1/CLKIN : OSC2/CLKOUT :

Capture In/Compare Out/PWM Out


Oscillator In/Ecternal Clock In Oscillator Out/Clock Out

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 PIC 2. RA0 port A pin 0 3. RA1 port A pin 1 4. RA2 port A pin 2 5. RA3 port A pin 3 6. RA4 port A pin 4 7. RA5 port A pin 5 8. RE0 port E pin 0 9. RE1 - port E pin 1 10. RE2 port E pin 2 11. VDD power supply 12. VSS ground 13. OSC1 connect to oscillator 14. OSC2 connect to oscillator 15. RC0 port C pin 0 16. RC1 port C pin 0 17. RC2 port C pin 0 18. RC3 port C pin 0 19. RD0 - port D pin 0 20. RD1 - port D pin 1 21. RD2 - port D pin 2 22. RD3 - port D pin 3 23. RC4 - port C pin 4 24. RC5 - port C pin 5 25. RC6 - port C pin 6 26. RC7 - port C pin 7 27. RD4 - port D pin 4 28. RD5 - port D pin 5 29. RD6 - port D pin 6 30. RD7 - port D pin 7 31. VSS - ground 32. VDD power supply 33. RB0 - port B pin 0 34. RB1 - port B pin 1 35. RB2 - port B pin 2 36. RB3 - port B pin 3 37. RB4 - port B pin 4 38. RB5 - port B pin 5 39. RB6 - port B pin 6 40. 4 RB7 - port B pin 7

So Whats 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

Make Project: LED Pattern Display

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

Description Circuit in Robotics Kit


1. Main PIC

2. Power Supply
7

3. MICRO SWITCH

4. LDR SENSOR

5. LED

6. RS232C R: Red Y: Yellow G: Green

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

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.

10

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.

11

Executing ADC Function


Below are MikroC specific program codes related to execution of ADC function: 1. Adc_Read Function: Start reading signal from an analog port Command: Adc_Read(No_channel_analog_port);

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

12

ADCON1 SETTINGS
PCFG3: PCF G0 0000 0001 0010 0011 0100 0101 AN7 R E 2 A A D D D D A A D D D D AN6 R E 1 A A D D D D AN5 R E 0 A A A A D D AN4 R A 5 A Vref+ A Vref+ A Vref+ AN3 R A 3 A A A A D D AN2 R A 2 A A A A A A AN1 R A 1 A A A A A A AN0 R A 0 Vref+ Vref-

Vdd RA3 Vdd RA3 Vdd RA3

Vss Vss Vss Vss Vss Vss

0110
0111 1000 1001 1010 1011 1100

D
D A D D D D

D
D A D D D D

D
D A A A A D

D
D A A A A A

D
D Vref+ A Vref+ Vref+ Vref+

D
D VrefA A VrefVref-

D
D A A A A A

D
D A A A A A

Vdd
Vdd RA3 Vdd RA3 RA3 RA3

Vss
Vss RA2 Vss Vss RA2 RA2

1101
1110 1111

D
D D

D
D D

D
D D

D
D D

Vref+
D Vref+

VrefD Vref-

A
D D

A
A A

RA3
Vdd RA3

RA2
Vss RA2

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

13

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.

14

Some commands related to USART feature


1. Usart_Init Prototype Usart_Init(const unsigned long baud_rate);

Returns
Description

Nothing.
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. You need PIC MCU with hardware USART. Usart_Init needs to be called before using other functions from USART Library. This will initialize hardware USART and establish the communication at 2400 bps:Usart_Init(2400);
15

Requires

Example

Some commands related to USART feature

2. Usart_Data_Ready
Prototype Returns Description unsigned short Usart_Data_Ready(); Function returns 1 if data is ready or 0 if there is no data. Use the function to test if data in receive buffer is ready for reading. USART HW module must be initialized and communication established before using this function.

Requires

Example

If data is ready, read it: int receive; ... if (Usart_Data_Ready()) receive = Usart_Read;
16

Some commands related to USART feature

3. Usart_Read Prototype Returns Description unsigned short Usart_Read(); Returns the received byte. If byte is not received, returns 0. Function receives a byte via USART. Use the function Usart_Data_Ready to test if data is ready first. USART HW module must be initialized and communication established before using this function. If data is ready, read it: int receive; ... if (Usart_Data_Ready()) receive = Usart_Read;
17

Requires Example

Some commands related to USART feature


18

4. Usart_Write Prototype Usart_Write(unsigned short data);

Returns
Description Requires

Nothing.
Function transmits a byte (data) via USART. USART HW module must be initialized and communication established before using this function. int chunk = 0x1E; Usart_Write(chunk); /* send chunk via USART */

Example

Exercise: Sending data from PIC to PC

Write the program below in MikroC. Next compile and download the codes to PIC16F877A. Switch ON the kit to execute the program. //Program Name: HALO void 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); }

19

Exercise: Sending data from PIC to PC

1. From MikroC menu, select Tool>> USART Terminal (or press Ctrl+T) A dialog box will appear:

20

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

21

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.

22

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

Program
//PROGRAM NAME: ADCMIKROC //Determining variable format unsigned short input_a0, input_a1, input_a2,separator=0; void main() { ADCON1 = 0; // Activating ADC at analog input port TRISA = 255; // PORTA as input TRISB = 0; // PORTB as output USART_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

24

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

25

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.

26

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.

27

Program (including Calibration)


//PROGRAM: LDR SENSOR CALIBRATION unsigned 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 pins TRISA = 255; // PORTA as input TRISB = 0; // PORTB as output USART_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); } 28

Result

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

DC Motors
30

Based on its name then DC motors is a motor drived

by Direct Current

DC Motors
31

If we open a DC Motor

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=ILB F = Force B = Magnetic Field L = Length of Conductor I = Current in Conductor Torque in Motor: T = IBA sin A = LW L = Length of Winding W = Width of Winding

Speed Control of DC Motor


33

Based on Ohms law, V=I.R, Because Coil Resistance is constant then to increase current is use increasing voltage.

Direction Control of DC Motor


34

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

H-Bridge
35

Switch Dual Pole Dual Throw in previous figure is

similar with H-Bridge

DC Motors
36

With Use Table below. We can change motor

direction with change condition in each switch ON or OFF.

Driver for DC Motors


37

The switch in previous figure can be changed with relay, transistor or

mosfet. Other alternative use an IC L293D.

DC Motors
38

Relation of received input for IC L293D and results

of Motor Moving

Diagram L293D Connect the wires as this figure

39

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 Connect the wires as this figure
Forward Direction set this value Motor 1: C1=1, C0=0, D0=1 Motor 2: C2=1, C3=0, D1=1 Reverse Direction : Motor 1: C1=1, C0=1, D0=0 Motor 2: C2=1, C3=1, D1=0 Stop C1 = 0 dan C2=0 (Enable=0)

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

41

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

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

43

PWM commands in MikroC


1. Pwm_Init Prototype: 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_Duty Prototype: 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%

44

Perintah PWM dalam MikroC

3. Pwm_Start Prototype: Pwm1_Start(); Description: To start PWM Example: Pwm1_Start(); 4. Pwm_Stop Prototype: Pwm1_Stop(); Description: To stop PWM Example: Pwm1_Stop(); When using PWM library we dont have to set values for PORT C1 dan C2 output because it has been predetermined by PWM function.

45

Exercise
1. Task We 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 Connection Connection diagram is as side:

46

Algorthm
Defining PORT that will be used as input and output Setting initial value for output port Start Program Move 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 detik Move 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(); 47 Delay 1 detik

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 // //

48

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) ; } }

49

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.

50

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) ;

51

}
}

Integrating Sensor, Motor and Microcontroller to Build Line Follower Robot

52

For Report: Write, try and Analysis this program


/*PROGRAM: LFR_FOR_TWO_SENSORS */ //Define all variables unsigned 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); 53

///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);

54

} //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); }

55

Assignment

56

Make programs to solve the following tasks

1. Light Tracking Robot Program 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 Robot The 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.

57

You might also like