0% found this document useful (0 votes)
34 views121 pages

PIC Microcontroller Programming Guide

Uploaded by

sujanendra07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views121 pages

PIC Microcontroller Programming Guide

Uploaded by

sujanendra07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

UNIT III

PIC MICROCONTROLLER AND


PROGRAMMING
PIC MICROCONTROLLER AND PROGRAMMING

 PIC18FX Pin connection


 Architecture
 WREG register
 File register
 Status register
 I/O Ports
 Data type and Time delay in C
 Logical operation
 Data sterilization
 Program ROM Allocation
 Data RAM allocation
 Introduction to MPLAB IDE.
PIC - Peripheral Interface Controller.
DEVICE FEATURES- PIC18F4420
• Frequency - 40 MHz
• Program Memory (Bytes)- 16384
• Program Memory(Instructions)-8192
• Data Memory (Bytes) -768
• Data EEPROM Memory (Bytes) - 256
• Interrupt Sources- 20
• I/O Ports - Ports A, B, C, D, E
• Timers- 4
• Capture/Compare/PWM Modules - 1
• Serial Communications- MSSP, Enhanced USART
• Parallel Communications (PSP)
DEVICE FEATURES…
• 10-bit Analog-to-Digital Module
– 13 Input Channels
• Resets (and Delays)
– POR, BOR
– RESET Instruction,
– Stack Full, Stack
– Underflow (PWRT, OST),
– MCLR (optional), WDT
• Instruction Set - 75 Instructions; 83 with Extended
• Packages - 40-pin
Architecture
WREG register
File register
Status register
WREG register
WREG register

• WREG register – 8-bit register


• WREG stands for working register
• WREG is same as the accumulator in other
microprocessor.
• WREG register is used for all arithmetic and
logical instructions.
WREG Register
 To understand the use of the WREG register we
will show it in the context of two simple
instruction.

MOVE and ADD


MOVLW 25H W 25H
ADDLW 34H W=W+34H

WREG: ???????
UNIT 3-

STATUS REGISTER
Status register
FILE REGISTER
FILE REGISTER
• DATA memory is also called as file register.
• File register size ranging from 32 bytes to
several thousand bytes.
• Varies chip to chip.
• File register data RAM divided into:
– Special function register (SFR)
– General purpose register(GP-RAM)
PIN DIAGRAM

PIN DIAGRAM
I/O PORTS
• PIC microcontroller contains 40 pins among that only 33
pins are used for I/O purpose and remaining pins are
used for the special purpose.

• These 33 pins are divided into 5 different PORTS as


follows.
– PORTA – 6 pins.
– PORTB – 8 pins.
– PORTC – 8 pins.
– PORTD – 8 pins.
– PORTE – 3 pins.
• These pins are bidirectional (i.e. Input and Output).User
have to decide the particular purpose of the pins.
Configuring PORTS

• For port configuration two registers are used.


They are listed below.

• TRIS Register.
• PORT Register.

• Each PORT is having their individual TRIS


register.
Port B –Configuration Register- TRIS_B

• If RBx bit is 0, that pin will be set as output pin


• If RBx bit is 1, that pin will be set as input pin
• If TRIS_B = 00000000 = 00H all pins are output
• If TRIS_B = 11111111 = FFH all pins are input
• If TRIS_B = 01010101 = 55H RB7,RB5,RB3,RB1
are output pins RB6,RB4,RB2,RB0 are input pins
DATA TYPE AND TIME DELAY IN C
Unsigned char
• Pic18 – 8 bit microcontroller char data type
is the most natural choice for many
application
• Unsigned char is an 8-bit data type 0-FF(0-
255) .
• Remember that C compilers use the signed
char as the default unless we put the
keyword unsigned in front of char.
• Use the Unsigned char data type for string of
ASCII char.
Unsigned char
• In variable declaring we must pay careful
attention to the size of the data and try to
use unsigned char instead of int if possible.

• Write a C18 program to send values 00-FF to


port B.
Signed char
• The signed char is an 8bit data type that used
the most significant bit (D7 of (D7-D0) to
represent the – or + value.
• 7bits for the magnitude of signed number,
giving us value from -128 to +127.
• + or – are need to represent a given quantity
such as temperature…..
Example
• Write a C18 program to send values of -4 to +4 to port B.

• O/P:
– ,FC,FD,FE, FF, 0,1,2,3,4
UNSIGNED INT
• The signed int is a 16-bit data type
• Ranges from 0 to 65,535(0000-FFFF)
• Unsigned int is used to define 16-bit variable
• Int data type takes two bytes of RAM
Signed int
• Signed int is a 16-bit data type that uses most
significant bit (D15 of (D0-D15) to represent
the – or + value.
• Ranges from -32,768 to +32,767
Other data types
• Short long -24bit
• Long – 32 bit
Example 1
• Write a C18 program to toggle all bits of PORT
B 50,000 times.
Example 2
• Write a c18 program to toggle all bits of PORT B
1,00,000 times
Time delay
• There are two ways to create a time delay in
C18.
• Simple for loop
• PIC18 timers
use oscilloscope to measure the duration
of our time delay.
Time delay using FOR loop
• Two factors that can affect the accuracy of
the time delay
• The crystal frequency connected to the OSC1-
OSC2 input pins is the most important factor
in the time delay calculation.
• The duration of the clock period for the
instruction cycle is a function of this crystal
frequency.
Time delay using FOR loop
• Second factor
• Time delay in c-program
• Compiler used to compile the C program.
• Assembly language control the exact instructions
and their sequences used in delay subroutine.
• c-program compiler convert into assembly
• As a result different compiler produced different
hex code.
• So difficult to calculate time delay using C
Time delay using FOR loop
• Use the Oscilloscope to measure the exact
duration
Example 3
• Write a C18 program to toggle all the bits of
PORT B ports continuously with a 250ms
delay. Assume that the system is pic18F458
with XTAL = 10MHz
Example 4
• Write a c18 program to toggle all the bits of
port C and port D continuously with a 250ms
delay.
I/O programming
• Byte size i/o
• Bit – addressable programming
Byte size i/o
• Ports :PORTA- PORTE are byte accessible.
• PORTA – PORTE Labels are defined in the C18
header file.
Example 5
• LEDs are connected to bits in port b and port
c. write a c18 program that shows the count
from 0 to FF on the LEDS.

• #define LED PORTC


Example 6
• Write a c18 program to get a byte of data from
port B wait ½ second and then send it to Port C.
Example 7
• Write a C18 program to get a byte of data
from PORT C. if it is less than 100,send it to
PORT B; otherwise send it to PORTD
Bit-addressable I/O programming
• The I/O Ports of PIC18 are bit addressable.
• A single bit can access without disturbing the
rest of the PORT.
• [Link] to access a single bit of PORTX,
where x= port A,B,C,D,E and y is the bit(0-7) of
that port.
• We access the TRISx registers in same way
where TRISBbits.
• TRISB7 –indicates the D7 of TRISB
Example 8
• Write a c18 program to toggle only bit RB4
continuously without disturbing the rest of the
bits of Port B.
Example 9
• Write a C18 program to monitor bit RC5. if it is
high, send 55H to port b; otherwise, send AAH
to PORT D
Example 10
• A door sensor is connected to the RB1 pin and
a buzzer is connected to RC7. write a C18
program to monitor the door sensor, and
when it opens, sound the buzzer. You can
sound the buzzer by sending a square wave of
few hundred HZ frequency to it.
Example 11
• Write a C18 program to turn bit 5 of Port B on
and off 50,000 times.
Example 12
• Write a c18 program to get the status of bit
RB0 and send it to RC7 continuously.
Logical operation in C
• One of the most important and powerful
feature of the C language is ability to perform
bit manipulation.
Bit- wise logical operators for C

– AND, OR, EX-OR Inverter.


• AND- &
• OR-|
• NOT-!
• EX-OR- ^
• Inverter ~
Shift right >>
Shift left <<
EXAMPLE
• 0x35 & 0x0F =0X05
• 0X04| 0X68 = 0X6C
• 0X54 ^ 0X78 = 0X2C
• ~0X55=0XAA
Bit-wise shift operators in C
• There are two bit- wise shift operators in C
• SHIFT RIGHT >>
• SHIFT LEFT <<

• 0X9A >> 3 = 0X13


• 0X77 << 4 = 0X70
• 0X6 << 4 = 0X60
Example 14
• Write a c18 program to toggle all the bits of
port B and port C continuously with 250 ms
delay. Using inverting operator.
Example 15
• Write the C18 program to toggle all the bits of
port B, port C and port D continuously with a
250 ms delay. Use the EX-OR operator.
Example 16
• Write the C18 Program to get RB0 and send it
to RC7 after inverting.
DATA SERIALIZATION
• Serializing data is a way of sending a bytes of
data one bit at time through a single pin of
microcontroller.
• There are two ways to transfer a byte of data
serially.
• Using serial port.
• Serializing data is to transfer data one bit a time
control the sequence of data and spaces
between them.
Example 17
• Write a c18 program to send out the value 44h
serially one bit at a time via RC0. The LSB
should go out first.
Example 18
• Write a C18 Program to send out the value
88h serially one bit at a time via RC0. The MSB
should go out first.
Example 19
• Write a C18 program to bring in a byte of data
serially one bit at a time via the RB0 pin. Place
the byte on PORT D . The LSB should come
first.
Example: 20
• Write a C18 program to bring in a byte of data
serially one bit at a time via the RB0 pin. Place
the byte on PORT D . The MSB should come
first.
PROGRAM ROM ALLOCATION IN
C18
RAM Data space Vs code data space
 PIC18 have two spaces in which to store data.
 4098bytes of data RAM space with address
range 000-FFF.
 2M of code (program) space with addresses of
000000-1FFFFF. (ROM)
 This 2M bytes of on-chip ROM space is used
for storing programs and therefore is directly
under control of program counter (PC).
RAM Data space Vs code data space
• Problem – using program code space for storage of fixed
data.
• More code space used for data, less for program memory.
• For ex:
• PIC18F252 – 4K on chip ROM
– 1K – Look – up table
– 3k – left for the program.
– For this reason microchip has added EEPROM memory


Allocating program space to data
• In all our c18 example so far, byte – size
variable were stored in the data RAM.
• Common practice to use the on-chip program
ROM for the purpose of storing fixed data
such as strings.
• This is specially useful since limited amount of
file register data RAM.
Allocating program space to data
• To make the c18 compiler use the program
(code) ROM space for fixed data,
• We use keyword rom
– rom char mynum[]= “hello”; // use code space for
data
– rom char weekdays=7;, month=12;
Example: 21
• Write a c program to send the data 0 to 9 to
port b (use program ROM space for fixed data)

• #include <pic.h>
rom const char mynum[]=“0123456789”
NEAR and FAR
• ROM – 2M on chip memory.
• Not every family member having much on
chip program ROM.
– Some chip with 4k ,128k….
– For efficient use of code space c18 compiler use
– NEAR - First 64k of Program Memory
– FAR - anywhere in 2M ROM.
Example 22
• WRITE a c18 for send string of data “hello” to
port b (using near code memory)

• near rom const char mydata[]=“hello”;


Pragma and allocating a fixed address to
data and code
• Pragma is a section of directive
• Section is a portion of application (code or data)
that can be assigned an specific memory
address location.
• We have two option:
• Code - #pragma used for Executable instructions
• Rom Data - # pragma romdata directive is used
for fixed data such as string and lookup table.
Putting code in a specific ROM address
• To place the code at specific address location
of program ROM - #pragma code directive.
Example :23
#include <pic.h>
#pragma code main = 0x50
Void Msdelay(unsigned int)

Void main()
Led blinking program?????????????
#pragma code Msdelay = 0x300
Putting data in a specific ROM address
• To place the data (containing variable ,
constants, strings, and look-up tables) at
specific address of program ROM.

#pragma romdata
Example: 24
Write pic18 program to send string “hello”
to PORTB. By putting data in a specific ROM
address.

#include <pic.h>
# pragma romdata mydata =0x200
near rom const char mydata[]=“hello”;
DATA RAM ALLOCATION IN C18
• RAM size pic18-4K
• RAM vary from 256-4096bytes
• RAM _ SFR – undisturbed, rest of the RAM to
the stack and variable declared by C program.
Example: 25
• #include <pic.h>
• Void main()
{
unsigned char x=5,y=5; //uses data RAM to
store data.
Example 26
• #include <pic.h>
• Void main()
{
unsigned char mynum[]= “123456789”; //uses
data RAM to store data.
Example 27
• #include <pic.h>
• Void main()
{
unsigned char mynum[100]; // 100 byte space
in RAM.
NEAR and FAR
• NEAR : limited to access bank only.
• FAR: Anywhere in data RAM
Example 28
• #include <pic.h>
• Void main()
{
near unsigned char mydata[100];
Example 29
#include <pic.h>
Void main()
{
far unsigned char mydata[100];
Putting data in specific RAM address
• #pragma directive also used for data RAM
– idata – initialized data
– Udata - uninitialized data
Example 30 for idata
#include <pic.h>
# pragma idata mydata =0x150
unsigned char mydata[]=“hello”; // RAM data
Example 31- udata
#include <pic.h>
# pragma udata x =0x102
unsigned char x; // it is uninitialized data
Example 32
#include <pic.h>
# pragma udata x =0x102
unsigned char x; // it is uninitialized data
# pragma idata y =0x103
unsigned char y=20; it is initialized data
Example :33
• Write an embedded C program to toggle all
the bits of PORT B, PORT C and PORT D
continuously with a 250 ms delay. Use the EX-
OR operator. Place the main code at ROM
address at 0x50 and delay code at ROM
address 0x350
Example :34
• Write an embedded C program to save the
array of 100 numbers in RAM and send the
copy of data to PORTB for count down from
0xFF with time delay of 500ms
Example :35
• Write an embedded C program to get the byte
of data (0x55) from a switch serially one bit at
a time with half second delay via RB0 and
place the byte of data on PORTB. The LSB
should come in first. Toggle the same data at
PORTC continuously with 250 ms delay.
OVERLAY STORAGE CLASS
• In memory two variable to share the same
physical address.
– overlay unsigned char x=0;
overlay unsigned char y=0;

You might also like