Pic Programming - Line Follower

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Introduction to µC

Sanjay Dhakar sanjaydhakar07@gmail.com


Content

• Digital Electronics
• Elementary Components
• understanding ICs
• MCU introduction
• PIC Microcontroller
• Development Board
• PIC custom Board
•Schematic and PCB Design
•Line following Robot
Digital Logic
Digital Logic
•High (+5 V, 3.3 V, 1.8 V,….) Digital ‘1’
•Low (0 V) Digital ‘0’

Basic Digital Gates


AND (2 input, 3 input,…….)
OR (2 input, 3 input,..…..)
NOT (only 1 input)

AND, where ALL inputs must be “1” for


the output to be “1”

OR, where ANY of the inputs can be “1”


for the output to be “1”

NOT (or the Inverter) where the output


is the opposite (compliment) of the
input.
GATEs –
tangible???
VCC +5V
•Basic logic chips 14-pin packages. Pin 14 Pin 8
•Package sizes and styles vary.
•Pin 1 is indicated with a dot or half-circle
•Numbers are read counter-clockwise from
pin 1 (viewed from the top)

Chips require a voltage to function


Vcc/Vdd/Vpp/Avdd is equal to 5 volts
(traditionally) and is typically pin 14
Pin 1 Pin 7 Gnd 0V
Ground/Vss/Avss is typically pin 7
Chip- Inside???

All the logic chips are vailable in the 74__


series ICs. IN
Vcc IN
Ex. Chip IC 7400
OUT
Quad 2-input NAND gate

Small Scale integration (SSI)- # transistors


used per chip is low Vcc Probe

Having got the Logic Chip…


How do I work with it???
What about Input???
What about Output???
Reading
Datasheet

Datasheet provides :
Pin configuration
Application hint
Function Table Truth
table
Dimensions Why?? For
PCB designing
Electrical Charateristics
Input

Input can be
Logic “Low” voltage ≈0V
Logic “High” voltage ≈5V, 3.3V, 1.8V,..

Where can input come from??


•Switches (2 terminals)
•Numpad
•Sensors
•Another circuitry

•Resistor Value is typically 10K


ohm
•A capacitance can be put in
parallel with it such that
RC < Time period of switching
Output

Output is again
Logic High +5V
Logic Low 0V

How do you monitor the output??

•Use a multimeter probe


•Use a CRO
(in case you are dealing with AC
signals or for than matter changing
DC signals)
•Use LED (Light Emitting Diode)
•7 Segment displays
•LCD
•Buzzers………………………………………….
Outputs cont.

What if you want to control something BIG…


Home Appliances current>>20mA
Solution
You need an amplifier (simply put a power transistor)
OR
A rudimentary way (use a relay)
Output
•LED draws a current of (say 10mA)
•Output from a Logic Chip (say 5 V)
•LED being a diode has a voltage drop
of around 1 V

R= (5V – 1V) / (10mA) = 400 Ohm

•Typical values (330 Ohm to 1 Kohm)

Output HIGH  LED glows


Ouput LOW  LED doesn’t glow
Circuit
complexity!!!!

SOLUTIONS
•Use of PCB (Printed Circuit Boards)
•Use of Microcontrollers
Electronic Lock
Lock Code (SET)

Why use so many gates and so much Relay switched if


complexity SET= INPUTDoor
Instead use a simple Microcontroller and Opens
be off with it…

Lock Code (INPUT)


Microcontroller Vs Microprocessor
• Architecture
Microprocessor = CPU
Microcontroller = CPU+ peripherals + memory
Peripherals = ports + clock + timers + USART + ADC converters +LCD drivers +
DAC + other stuff
Memory = EEPROM + SRAM + EPROM + flash
Microcontroller Vs Microprocessor
• Uses
• Microcontroller-
– designed to perform a small set of specific functions
– Typical car has around 30 uC
– washing machines, microwave ovens, and telephones
• Microcontroller-
– designed to perform a wider set of general purpose functions.
– personal computers or other general purpose applications
Microcontroller
• MCUs have been made available by many forms –
Microchip’s PIC, Motorola, Atmels’s Atmega, etc but its
internal design remains almost the same.

•Programming a PIC is quite easy through a USB port.


(A laptop would do)

•Software used is MPLAB IDE, MCC18(compiler).

•Programming language is C.
MCU Functions

Modules inbuilt the MCU

1. GPIO- general purpose I/O

2. PWM- pulse width modulation

3. ADC- analog to digital converter

4. TIMERS

5. Various communication protocols - SPI, I2C, USART


PWM Signal

• Pulse Width Modulation


Minimal Circuit of PIC18F4550
Development
Board

Concept of Development Board ???

What does it actually offer???


A basic processing unit (MCU / Microprocessor), Inputs, Outputs
…..thats it.
Also nowadays we have MEMS
(Micro-ElectroMechanical Devices on-board)
It acts as a complete solution

Can I built one myself???


Definitely…Custom made board are much more cost effective
You can choose off the shelf components pertaining to your design
PIC18F4550 Custom Board

• Minimal PIC circuit


•Sensor connector
• Motor driver
• Power supply- battery, switch
• Input switches
• Output LEDs
• uC pinout
• USB connector for programming
Motor Driver Circuit:
Schematic:
Schematic into DipTrace:
PCB design in DipTrace:
Final PCB Design:
Line Following robot:

1. Electrical circuit:
- PIC, PCB board, Motor driver, Battery
2. 5-Sensors array
3. Battery
4. Motors
5. Mechanical chassis
5-Sensors array :
expenditure :

S no. Item Qty. Cost


1 PIC board 1 550
2 5-channel Sensor array 1 500
3 Battery 1 150
4 Motors 2 250
5 Castor and wheels 1,2 80
6 Motor Driver 1 150
7 Mechanical chassis 1 120
8 Soldering iron n wire 1 150

Total - 1950
uC (Programming)

Sanjay Dhakar sanjaydhakar07@gmail.com


PIC Driver Installation Path
• C:\Program Files\Microchip\Microchip
Solutions\USB Tools\MCHPUSB Custom
Driver\MCHPUSB Driver\Release
OR
• C:\Microchip Solutions\USB Tools\MCHPUSB
Custom Driver\MCHPUSB Driver\Release
Software Support

MCC18
Compiler

MPLAB
v8,v9…
Programming PICDEM Tool
Environment Transfering the *.hex
file to the
microcontroller
MPLAB v8.00 (coupled with MCC18
compiler)

• Writing the C code for the Microcontroller

• Compiling the program and converting to


*.hex file to be transferred to the MCU
PICDEM Tool

• Program the microcontroller with


compiled C code in the *.hex format

• Read the code presently in the


microcontroller as a *.hex file
Running a test code

Step 1: Setting up the project folder


Step 2: Compiling the code
Step 3: Installing the driver for PIC18F4550
Step 4: Transferring the code
Step 5: Running the code
Modules We need

1. GPIO- general purpose I/O

2. PWM- pulse width modulation


GPIO
GENERAL PURPOSE INPUT/ OUTPUT

#define OUT PORTAbits.RA3


#define IN PORTAbits.RA4
TRISAbits.TRISA3=0; //for output
TRISAbits.TRISA4=1; //for input

Void function()
{
int value=IN;
OUT=1;
OUT=0;
OUT=IN;
}
Constructs

1. Include relevant Header files

2. Port alias names using #define

3. Port characteristics defined together under initialize() function

4. Function definitions- return_type function_name( parameters) {}

5. Main function- void main()


PWM

Opening the PWM channel


void OpenPWM1( char period );
void OpenPWM2( char period );

Setting the Duty Cycle


void SetDCPWM1( unsigned int dutycycle );
void SetDCPWM2( unsigned int dutycycle );

Closing the PWM channel


void ClosePWM1( void );
void ClosePWM2( void );
ADC

Settings for ADC usage


ADCON1 |= 0x0E;
TRISAbits.TRISA0=0;

Opening the ADC channel


OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_16_TAD , ADC_CH0 &
ADC_INT_OFF & ADC_REF_VDD_VSS , ADC_1ANA );

Setting the channel to pin A0


SetChanADC( ADC_CH0 );

Process for reading the ADC value


ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
i = ReadADC();
Light Sensor
Sensor Array schematic
By

•Sanjay Dhakar
sanjaydhakar07@gmail.com

•Danvir Singh
danvirsingh@gmail.com

Please put Subject as


“RoboHolix”
to filter the mails to one
category

Robotics Club IITD


http://paniit.iitd.ac.in/~iitdr/

You might also like