Crash Course: 3 Hours Introduction To Pic Microcontroller by
Crash Course: 3 Hours Introduction To Pic Microcontroller by
3 HOURS
INTRODUCTION TO PIC MICROCONTROLLER
By
www.astanadigital.com
Embedded C Programming
1. What is microcontroller?
2. Embedded C
3. Send Command – Assign Instruction
4. Read Status – Equality Check
5. If else
6. Functions
7. While – Do While
8. Variable
9. For-Loop
A microcontroller
• A programmable electronic device
• A small computer system for specific applications.
(Standard computer i.e. PC is for general purpose
applications)
• The brain of many of smart electronic systems
• The wiki says : A microcontroller (sometimes
abbreviated µC or uC) is a small computer on a
single integrated circuit containing a processor
core, memory, and programmable input/output
peripherals.
www.astanadigital.com 3
PIC Microcontroller
• 8 Bit Microcontroller
• 8000 Words
• 368 Bytes
• 40 Pins
www.astanadigital.com 4
Why is the need of a microcontroller?
• Simple electronic system
+ve
Resistor
Push Button
LED
-ve
www.astanadigital.com 5
Microcontroller makes it smarter
• A system that can receive information (input
data) , manipulate the data according to
preset algorithms (instructions) and send
command (output data) can be considered as
smart.
www.astanadigital.com 6
The smart system
• Adding a microcontroller to the circuitry
RM20.00 Only
Vdd
pic16f887
Push
Button
LED
Microchip
IO Pin read digital
signal at Pin 15
IO Pin send digital
signal to Pin 26
Input Processing
www.astanadigital.com Output 7
How it works ?
• Input signal (information) in a form of electrical
signal is received by a microcontroller IO pin
• The signal is translated into a digital form
• Core processor in the microcontroller manipulate
(process) the digital signal according to
instructions /algorithms written by users.
• Output signal (result) in a form of electrical signal
is send out from a microcontroller pin
www.astanadigital.com 8
PIC16F887
Output Circuitry
PIC16F887 Pin Connection
Input Output Pin (IO Pin)
RA0,RA1,RA2,RA3,RA4,RA5 (Analog Input or Digital IO)
RB0,RB1,RB2,RB3,RB4,RB5,RB6,RB7 (Analog input or Digital IO)
RC0,RC1,RC2,RC3,RC4,RC5,RC6,RC7 (Digital IO Only)
RD0,RD1,RD2,RD3,RD4,RD5,RD6,RD7 (Parallel Or Digital IO)
RE0,RE1,RE2 (analog input or digital)and RE3 (digital input only)
www.astanadigital.com 13
Memory – Program & Data
Program Memory - 14 Bits Width Data Memory – 8 Bits Width (where the
(instructions that we write). General Purpose Register and Special
Function Register is stored here)
PORTA
PORTB
PORTC
PORTD
PORTE
TRISC
• MPLAB IDE
• Hitech C
• Configuration
Software Setup
• Target Board
• USB Programmer
• Sensor Pack
Main Program
lab1.c
void main(void) // the main function where the program will start operating
{ // start of main
// enter your code here
} //end of main
Program
RC0
0
IF Statement
if ( expression)
{
//code to run here if expression is true
}
IF Statement
if ( RC0==1)
{
//code to run here if expression is true
}
If - else
__CONFIG(0x3F32);
#define _XTAL_FREQ 20000000
void main(void)
{
TRISC =0b00000000;
PORTC=0b00000000;
here:
RC7=1;
__delay_ms(39);
RC7=0;
__delay_ms(39);
goto here;
}
#define ON 1
#define PUSHBUTTON RC0
constant name
Create Function
• Subroutines
• Elements:
– Function Prototype
– Function Declaration
– Function Arguments
– Function Returns
while(expression) {
// execute code within this segment while the
// expression is true or value =1
}
while(1){
RC7=1;
}
Notes: Use Push Button to toggle the LED and the buzzer.
Add one more Push Button.
Differentiate active low and active high circuit.
DO-While Statement
do {
//code to run here
}
while (expression);
DO-While Statement
do {
RC7=0;
__delay_ms(39);
RC7=1;
__delay_ms(39); }
while (RC0==1);
Variable
Allocate a byte data memory in microcontroller (RAM
space) and give name to it.
data type
x
counter
mamat
Variable
Notes: Make the system beeps once after LED blinks 10 times
A ‘second’ function prototype
void __delay_sec(int x) //function declaration
{
int j;
int k;
for (j=0;j<x;j++)
for (k=0;k<50;k++)
__delay_ms(20);
}
Thank You!
www.astanadigital.com
Phone/Fax: 03 8920 1296
Mobile: 013 620 2657
Email: saiful@astanadigital.com