Вы находитесь на странице: 1из 43

CRASH COURSE :

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

Power Supply Circuit Crystal Clock

Central Processing Unit


Data Memory
Program Memory

Reset Circuit Input Circuitry

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)

Crystal Clock Pin


Clck In
Clck Out

Reset/Master Clear Pin


MCLR

Power Supply Pin


Vdd (Positive Supply)
Vss (Negative Supply)
Microcontroller Based System
Input Output Pin (IO Pin)
PORTA- RA0,RA1,RA2,RA3,RA4,RA5 (Analog Input or Digital IO)
PORTB-RB0,RB1,RB2,RB3,RB4,RB5,RB6,RB7 (Digital IO Only)
PORTC-RC0,RC1,RC2,RC3,RC4,RC5,RC6,RC7 (Digital IO Only)
PORTD-RD0,RD1,RD2,RD3,RD4,RD5,RD6,RD7 (Parallel Or Digital IO)
PORTE-RE0,RE1,RE2 (Analog Input or Digital IO)

Crystal Clock Pin


ClkIn
ClkOut

Reset/Master Clear Pin


MCLR

Power Supply Pin


Vdd (Positive Supply)
Vss (Negative Supply)
www.astanadigital.com 12
Logic Signal Voltage Level

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

8000 Words 368 Bytes


Port to Pin Relation
PORTC
(Content in data memory register that reflects signal from/to external circuitry via its
corresponding IO pin)

RC7 RC6 RC5 RC4 RC3 RC2 RC1 RC0


PORTC

Pin for Port C 26 25 24 23 18 17 16 15

Pin for Port C


(external physical pin for external circuitry interfacing)
Software Installation

• MPLAB IDE
• Hitech C
• Configuration
Software Setup

• Create Project Name - MyProject


• Create Project Folder - MyFolder
• Create Source File – lab0.c
• Compiling
• Downloading
Hardware Connection

• Target Board
• USB Programmer
• Sensor Pack
Main Program
lab1.c

#include <htc.h> // to include the contents before compiling (pic168xa.h)


__CONFIG(0x3F32); // Configuration Bits

void main(void) // the main function where the program will start operating
{ // start of main
// enter your code here
} //end of main
Program

#include <htc.h> // to include htc.h before compiling (pic168xa.h)


__CONFIG(0x3F32); // Configuration bits for PIC16F877A

void main(void) // the main function where the program operate


{ // start of main
TRISC = 0b00000000; //define data direction of PORTC IO
PORTC= 0b00000000; // clear PORTC register
here: // label
RC7=1; // turn on the and LED at RC7 pin
goto here; // jump to label
} // end of main
TRISX
• A special function register ( in Data Memory)
• The content of this register defines
corresponding PORT X IO pin as input or
output
i.e.
TRISC = 0b00000000; // all port C as output
TRISC = 0b00001111; // First 4 bits as input
// The rests as output
PORTX
• A special function register ( in Data Memory)
• The content of this register represents the
value of electrical signal at its IO pin
i.e.
PORTC = 0b00000000; // clear the content
// of PORTC memory
// register
Send Command
• Turn on an LED in a never ending loop using
RC7=1; // assign value 1 to PORTC Bit 7
• An LED is connected at PORTC Bit 7 (RC7)
• Complete this output circuit:
RC7
1

Notes: 1 is logic high or 5V or ON


0 is logic low or 0V 0r OFF
Documentation

// write comment in this line only

/* comment can be written


within this
section
*/
Read Status (input value)
• Connect a push button with an active high circuitry
• Define TRISC register
• Clear PORTC register
• Complete this schematic

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

if ( RC0==0) // ‘==‘ relational operator, check for equality


{
//code to run here if expression is true
}
else
{
//otherwise another code to run here
}
Blinking LED
• Turn on and off an LED in a never ending loop
here:
RC7=1;
RC7=0;
goto here;

Notes: 1. Can we see the LED blinking? Why?


2. How long it takes to execute one instruction?
DC – 200 ns /instruction cycle at 20mhz osc
__delay_ms();

• A pre build function provided by Hitech


• Set the crystal clock value paramater:

#define _XTAL_FREQ 20000000

A constant name used in _delay_ms() function as an input


parameter to the function
Blinking LED
#include <htc.h>

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

Notes: Introduce logic probe.


Add another LED and a buzzer to the
system.
Constant

preprocessor directive value to be associated

#define ON 1
#define PUSHBUTTON RC0

constant name
Create Function
• Subroutines
• Elements:
– Function Prototype
– Function Declaration
– Function Arguments
– Function Returns

Notes: Create LEDBlinking() and BuzzerBeeping() functions


While Statement

while(expression) {
// execute code within this segment while the
// expression is true or value =1
}

i.e. while(1){ }; while(RC0==0) { }; while(RC0==1) { };


while(RC0){ };

Notes: What’s the different between while and if statement?


While Statement

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

unsigned char counter, x, mamat ;

A descriptive variable name

x
counter

mamat
Variable

counter = 0b00000001; // initialize counter value to 1


counter 0 0 0 0 0 0 0 1

counter = counter + 1; // increase counter value by 1.


counter 0 0 0 0 0 0 1 0

Notes : 1. What’s the maximum value for counter in decimal?


2. See articles on C language for more explanation on variables
For – Loop Statement

for (variable = initial value; expression; variable = variable +


value)
{
//code to run here while expression is true
}
For-Loop Statement

for (counter =0; counter<10; counter++)


LEDBlinking();

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

Вам также может понравиться