We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 32
PROGRAMMING WITH PIC MICROCONTROLLER
INTRODUCTION
The Micro-PIC 1 6F kit has interfaced the PIC16F series microcontroller, to various
peripherals on the board. Devices like LEDs and LCD, I2C interface based memory devices,
RTC, SD/MMC Card interface, Matrix Keypad, ADC, DAC, graphical LCD, Stepper motor,
etc can also be interfaced on the board.
In order to program the controller on the Micro-PIC16F board the following setup is,
needed.
1, MPLAB X IDE setup.
2. XC8 Compiler setup.
3 PICLoader Utility for flash programming through USB to Serial.
PC Connection Setup.
The Micro-PICI6F Board connects to the PC via the USB to Serial port. The Hex
files generated by the IDE can be downloaded into the flash memory using the ISP feature on
the microcontroller via the USB to Serial port. The Micto-PIC16F Board development kit has
a USB cable included in the package. Connect the (A type) side of the connector to the PC
and the B type side to the Micro-PIC16F Board USB to Serial .
‘MPLAB X Programming IDE.
Creating a new project
> Goto the File Tab.
> Click on New Project.
Step1: Choose Project:
¥ Select :Microchip Embedded -> Standalone Project. Click Next.
Step2: Select Device:
> Select : Family - PICL6.
» Select : Device - PICI6F877A. Click Next.
Step3: Select Tool: Simulator. Click Next.
Step4: Select Compiler - XC8. Click Next.
Step5: Select Project Name and Folder.
> Give Project Name.
> Select project Location using Browse Button.
> Uncheck Set as main project option.
> Cli
Finish.
Step6: Adjustment for Bootloader.> Select the project and Go to properties.
> Select XC8 linker.
> In Option Categories select Additional Options.
> In Code Offset: write 400.
Creating a new Source file and Header File.
> Go to the Project location in the Project window.
> Click the + sign to open the project space.
> Right Click on the Source Files folder (for a C file) and Header files (for a .h file).
New -> C Source file / or C Header File.
Compiling Project.
Step!: Go to project window.
> Right Click on the project folder and select Build or Clean and Build.
Executable Flashing Tool (PICLoader.exe).
> Connect the USB Cable to the Board.
Step 1: Double Click the PICloader.exe.
Step2: Go to Programs -> Settings.
> Select the USB to serial com port. Click OK.
Note: Make sure not to select Config Bits and EEPROM Settings as these can damage the
bootloader.
Step3: Go to Programs -> Break/Reset Mode or Press F3.
Step4: Press the Reset Switch on the Micro-PICL6F Board.
StepS: Go to Programs ->Bootloader Mode or Press F4.
Step6 : Select Hex file :
> File > Open -> Browse to location.
> Project folder ->dist -> default->production.
Step7: Go to Programs -> Write Device or Press F6
> After successful writing it will display Write Complete at the bottom,
Step8: Press Reset on the board to Run the program,EXPT NO: 1
DATE: PIC-LCD
AIM:
To write and execute program for LED blinking using PIC microcontroller
THEORY:
A 16x2 Character LCD module is interfaced on the Micro-PIC16F board. The LCD is write
only i.e. we cannot read from the LCD. Contrast control is adjusted using potentiometer P1.
Put the switch SW22 in position 1-2 to use the LCD
2x16 LCD (ev oe
+ Loo po
x ee
ee
= ee
ee
ee
8 ee
Tvs =
sc Finds
15.60 788
ep-Br mar
teo-bF mat
(coor co
tep-br was
or 785
(co-b6 tos
(eae matPROGRAM CODE:
Hinclude
idefine LCD_EN PORTAbits.RAI
define LCD_RS PORTAbits.RAO
define LCDPORT PORTB
voidied_delay(unsigned int time)
i
unsignedint ij
for
0; i < time; i++)
fort)
}
voidSendInstruction(unsigned char command)
{
j=0;j<100;j++);
LCD_RS =0; J RS low : Instruction
LCDPORT = command;
LCD_EN = 1; J EN High
Jed_delay(10);
LCD_EN //EN Low; command sampled at EN falling edge
Ied_delay(10);
}
voidSendData(unsigned char leddata)
{
LCD_RS J RS HIGH : DATA
LCDPORT = Ieddata;
LCD_EN = 1; // EN High
Jed_delay(10);
LCD_EN
JEN Low; data sampled at EN falling edgeIed_delay(10);
}
voidInitLCD(void)
{
TRISB = 0x00; /Jset data port as output
TRISAbits.TRISAO = 0; HEN pin
TRISAbits.TRISA1 = 0; JPRS pin
ADCONI = 0x06;
SendInstruction(0x38); //8 bit mode, 2 line,5x7 dots
SendInstruction(0x06); // entry mode
Sendinstruction(Ox0C); //Display ON cursor OFF
SendInstruction(0x01); //Clear display
SendInstruction(0x80); /iset address to Ist line
}
void main(void)
{
unsigned char *String] = "Micro-PIC Board”;
unsigned char *String2 =" MicroEmbedded";
Init. CDO;
while@String!)
{
SendData(#String]);
String +4;
1
SendInstruction(OxC0); _//set address to 2nd line
while(*String2)
{SendData(*String2);
String2++;
}
while(1);
}
RESULT:
Thus the program was written & executed for LED blinking using PIC
microcontrollerEXPT NO: 2
DATE: PIC- ADC
AIM:
To write and execute a program for interfacing the Analog to Digital converter
with PIC microcontroller
THEORY
‘The processor on the Micro-PICI6F contains a 10 bit ADC with 8 channels. The board has
provided the user with 2 channels, Analog voltage is provided using potentiometers P2 and
P3. ADC Ch7 and ADC Ché peripheral is connected to this input. The Analog voltage can be
measured at CN3 and CN2 respectively. AN7 can be connected to LM35 temperature sensor
by changing the jumper setting position of J2 to 2-3.
Device Pin Details
ADC2 RE 1 (AN6)
‘ADC 1/Temp Sensor RE 2 (AN7)
PROGRAM CODE:
* Interfacing: (CNB)
* DO-D7-> PORTB
* RS -> REO
*EN->REIL
*RW-> GND
ie!
#include
include
#define LCD_EN PORTAbit
#define LCD_RS PORTAbits
define LCDPORT PORTB
RAO
voidicd_delay(unsigned int time){
unsignedint ij;
for(i = 0; i < time; i++)
for(j=0;5<100;j++);
}
voidSendInstruction(unsigned char command)
{
LCD_RS = 1/ RS low : Instruction
LCDPORT = command;
LCD_EN = 1; W/ EN High
led_delay(10);
LCD_EN 1 EN Low; command sampled at EN falling edge
led_delay(10);
}
voidSendData(unsigned char leddata)
{
LCD_RS
LCDPORT = Ieddata;
LCD_EN = 1; // EN High
Jed_delay(10);
LCD_EN = 0;
Jed_delay(1
}
// RS HIGH : DATA
1 EN Low; data sampled at EN falling edge
voidInitLCD(void)
{
TRISB = 0x00; //set data port as output
‘TRISAbits.TRISAO = 0; //EN pin
TRISADits.TRISA\ / RS pin
ADCONI = 0x06;
SendInstruction(0x38); _//8 bit mode, 2 line,5x7 dots
Sendlnstruction(0x06); // entry mode
SendInstruction(0x0C);__//Display ON cursor OFF
Sendlnstruction(0x01); _//Clear display
Sendinstruction(0x80); _//set address to Ist line
}
voidADCInit(void)
; ‘TRISEbits. TRISE1 = 1; //ADC channel 6 input
TRISEbits. TRISE2 = 1; /ADC channel 7 input
ADCONI = 0b1 1000000; (Ref voltages Vdd&Vss; ANO - AN7 channels
Analog; Right Justified; FOSC/64
}unsignedintRead_ADC(unsigned char Ch)
if
unsignedint i;
ADCONO = 000000001 | (Ch<<3); ADC on; Select channel;
fi i<5000:i++);
ADCONObits.GO_DONE = 1; #/Start Conversion
while(ADCONObits.GO_DONE == 1); _//Wait till A/D conversion is complete
return ((ADRESH<<2) | (ADRESL>>6)); /Return ADC result
}
voidDisplayResult(unsigned short ADCVal)
{
unsigned char i,text{16};
unsigned short tempy:
tempy = ADCVal;,
SendInstruction(0x80); //set to Ist line
for(i=0;i<10;i++) //Display the 10 bit ADC result on LCD
{
if(tempv& 0001000000000)
{
SendData('!');
}
else
(
SendData('0):
}
tempy = tempy<
(5500/1024)mV
sprintfitext,"ADC value=%4dmv",ADCVal);_//Convert integer data to string
Sendinstruction(OxC0); Jiset to 2nd line
fori=O;i required baudrate
*BRGH =0
*SPBRG = (Fosc / (64 * n)) -1
* For 9600 baudrate, SPBRG ~=77
4
#include
#include
+#define Fosc 20000000UL
voidInitUART(unsigned intbaudrate)
{
TRISCbits.TRISC6 = 0; J/TX pin set as output
TRISCbits.TRISC7 //RX pin set as input
SPBRG = (unsigned char)(((Fosc /64)/baudrate)-1);
TXSTA = 0b00100000; //Asynchronous 8-bit; Transmit enabled; Low speed
baudrate select
RCSTA = 0610010000; //Serial port enabled; 8-bit data; single receive
enabled
}
voidSendChar(unsigned char data)
i
while(TXSTAbits. TRMT == 0); /NVait while transmit register is empty
TXREG = data; WTransmit data
}
voidputch(unsigned char data)
{
SendChar(data);
}
unsigned char GetChar(void)
{
while(!PIR Ibits.RCIF); /WNait till receive buffer becomes full
return RCREG; #/Returned received data
)
void main(void)
{
InitUART(19200);
printi("\r\nHello MicroPIC-18F: Enter any Key from Keyboard\r\n");while(1)
{
printf("%c",GetChar()); /Receive character from PC and echo back
}
while(1);
}
RESULT:
Thus the serial port program was written & executed for transmitting data
using PIC micro controllerEXPT NO:4
DATE: PIC - EXTERNAL INTERRUPTS
AIM:
To write and execute a program to enable and disable interrupts using PIC
microcontroller
THEORY:
The interrupt method, whenever any device needs its services the device notifies the
icrocontroller by sending it can interrupt signal upon Receiving an interrupt signal, the
microcontroller interrupts whatever it is doing and services the devices. The program
associated with the interrupt is called interrupt service routine.
Steps in executing an interrupt:
1. It finishes the instruction itis executing and saves the address if the next instruction (pe) on
the stack.
2. Italso saves the current status of all interrupts internally.
3. It jumps to a fixed location in memory called the interrupt vector table that holds the
address of interrupt service routine.
4, The microcontroller gets the address of ISR from INT and jumps into it. It starts to execute
the ISR until it, reaches the last instruction of subroutine which is RET1.
5. Upon executing the RET! instruction, the microcontroller returns to the place where it was
interrupted.
Enabling and disabling as interrupts:
Upon reset all interrupt are disable, meaning that none will be responded of the
microcontroller. The interrupt must be enabled by software in order for the microcontroller is.
respond to them, There is a register called interrupt enable (I and E) that is responsible for
enabling and disabling the interrupts
sv
a
RO
10K.PROGRAM CODE :
#include
unsigned char flag=0;
void delay(unsigned int time)
{
unsignedint
void interrupt ex_isr()
{
if(INTF)
int main)
{
TRISBbits.TRISBO
TRISBbits.TRISB6 = 0;
‘TRISBbits.TRISB7
PORTBDits.RB6 = 1;
PORTBbits.RB7 = 1;
INT!
//interrupt pin as input
//RB6 and RBZ as output
//Enable external interrupt INT1
INTF = 0; //Clear interrupt flag
INTEDG //nterrupt on falling edge
GIE = 1/ Enable global interrupt
while(1)
if
iffflag == 0)
PORTBbits.RB6 =~PORTBbits.RB6;
else
PORTBbits.RB7 =~PORTBbits.RB7;
delay(100);
}
}
RESULT:
Thus the program was written & executed to enable and disable interrupts using
PIC microcontrollerEXPT NO: 5
DATE: PIC - STEPPER MOTOR,
AIM:
To write and execute a program for interfacing a stepper motor with PIC micro
controller
THEORY:
A motor driver (L293D) is provided on the Micro-PICI6F board for the user. The user can
connect the stepper motor or DC Motor to connector CN6 and CN7 respectively.
ACO;
Rr
Res
Ros}
eM Tes
EN2 4
1P6
4
STEPPER MOTOR
Device Pin Details
Stepper Motor 1 RCO
Stepper Motor 2 RCL
Stepper Motor 3 RC
Stepper Motor 4 RCS.
DC Motor 1 RCL
DC Motor? RC
PROGRAM CODE:
/* ~~ Calculations
* Fose = 12MHz,
* PWM Period = [(PR2) + 1] * 4 * TMR2 Prescale Value / Fose* PWM Period
* TMR2 Prescale = 16
* Hence, PR2 = 36 or 0x24
* Duty Cycle = 10% of 200us
* Duty Cycle = 20us
* Duty Cycle = (CCPRIL:CCPICON) * TMR2 Prescale Value / Fose
* CCPICON<5:4> i>
* Hence, CCPRIL = 2 or 0x02
"
#include
void delay(unsigned int time)
{
unsignedinti,j;
for(i=O;ictimesi++)
for(j=0;5<1000;j++);
J
void main(void)
{
unsignedint is
‘TRISCbits.TRISC2 /ICCPI pin as output
CCPICON = 0600111100; (iSelect PWM mode;
Duty cycle LSB CCPICON<4:5> = <1:1>
CCPRIL = 0x02; //Duty cycle 10%
T2CON = 0b00000010; //Prescalar = 16;
Timer2 OFF
PR2 = 0x24; /?Period Register
TMR20N = 1; v/Timer2 ONwhile(1) //Loop forever
CCPRIL
i*0x02;
delay(200);
}
}
RESULT.
Thus the program was written & executed for interfacing a stepper motor with PIC
micro controllerPROGRAMMING WITH 8051 MICROCONTROLLER
INTRODUCTION
Micro-5IF is a very user friendly and easy to use development board for 8051 series
of microcontrollers.8051 microcontroller interfaced to various devices like LED, 16x2
character LCD, 7 Segment Display, 4x4 Matrix Keypad, Stepper motor, 8 Bit Analog to
Digital Controller, Relay , IC EEPROM, I2C RTC and a Serial Communication interface.
‘The Micro-51F is powered by a 5V DC wall mount adapter having a current capacity of
1.5 Amps. After POWER ON a RED colored LED (LD10) will indicate a proper power
supply. A Power ON-OFF slide switch is provided for the user. It supports programming
options including 8051 assembly and C.
Keil IDE:
The Keil Integrated Development Environment provides the developer the perfect
platform for the development and debugging of the programs. The user can write the
programs in assembly or C and can simulate the working of the program using the simulator.
‘The user can simulate all the peripherals supported by the Keil IDE. After Compiling the
program a hex file will be generated for the microcontroller which can be downloaded
into the internal Flash. Flash Magic Utility is used for Flashing the program. ‘The Micro-
SIF has a provision for programming the on-chip flash via the serial port (In System
Programming). The on-chip bootloader communicates with a flash programming utility on
the host (PC) side to program the flash
PROCEDURE :
Creating a New Project
1. Open the Keil pVision IDE.
2. Go to the Project Tab.
‘* Left Click on New 1Vision Project. Name the project and save it in preferred location.
Choose the CPU (NXP->89ES51RD). And press ENTER.
* It.will ask the user if it should Copy Standard 8051 Startup code to project folder and.
Add File to Project.
* Click Yes for developing a project in C language and No for developing a project in
assembly language.
3. Go to File Menu and left click New.
4, Save the file in the folder created for the project. If the program is in C then put the
extension as .c and if the program is in assembly then save the file with .a51 extension.
5. Then right click on Source Group! and click Add Files to Group Source Group!
6, Select the file ( program file) and press ADD. The file gets added, then press Close.
7. Now start writing the programs.Project Settings.
8, Go to Project Tab and click Options for Target "Target!’.
* Inthe Target tab set the crystal frequency as 11.0592.
‘* In the Output tab Tick Create Hex File, Also select the folder where you want to save
the Hex file. Compile the Program.
‘9, When the program is ready and all the settings are done then compile the program, Go to
Project tab and click Build Target,
Program Flashing Utility.
Steps to flash program using SST Flash Utility:
Connect kit to the PC.
My Computer (right click) -> Manage-> Device Manager.
‘The COM port should be between COMI - COM6. If not, change from Properties->
Port Settings-> Advanced Settings.
Open SST Flash utility and set COM port and baudrate
Press reset on kit.
Click OK.
Release reset. The device will unlock.
Click Download/Run User Code and load the hex file. After the programming is
complete, the user code will start running immediately
9. To burn the next program, simply click CONNECT.
10, Press OK and press reset on the kit. The device will unlock. Go to step 8.
eaeEXPTNO:
8051 — TIMER INTERRUPTS
DATE:
“AIM:
To write and execute a program to enable and disable interrupt in 8051
microcontroller
THEORY:
‘There are two 16-bit timers and counters in 8051 microcontroller: timer 0 and timer 1.
Both timers consist of 16-bit register in which the lower byte is stored in TL and the higher
byte is stored in TH. Timer can be used as a counter as well as for timing operation that,
depends on the source of clock pulses to counters.
sbi Sobre Sb Ss bie
Counters and Timers in 8051 microcontroller contain two special function registers:
TMOD (Timer Mode Register) and TCON (Timer Control Register), which are used for
activating and configuring timers and counters.
‘Timer Mode Control (TMOD): TMOD is an 8-bit register used for selecting timer or
counter and mode of timers. Lower 4-bits are used for control operation of timer 0 or
counter, and remaining 4-bits are used for control operation of timer! or counter!
imer Control Register (TCON): TCON is another register used to control operations of
counter and timers in microcontrollers. It is an 8-bit register wherein four upper bits are
responsible for timers and counters and lower bits are responsible for interrupts.PROGRAM IN ASM
PORTPIN EQU P1*0
org 0
Jjmp start jump to start on reset
org Oxb stimer 0 int subroutine
clr 0
clr trO
Icall inittimer ;intialize timer again
cpl PORTPIN scomplement the status of portpin
setb t10
reti
inittimer: sInitalize the timer for 1 6bit timer mode
clr TRO;
clr TFO;
mov TMOD, #0x1; //16 bit timer mode
mov THO, #0xFF //Count for 0.25mS, for 2KHz
mov TLO, #0x1A —_// Timer count = (0.5/2)*921600 = 230 in decimal for 2k
ret
start:
seth EA Enable All interrupts
setb ETO //Enable timer0 interrupt
lcall inittimer //Initialize the timer
seth TRO / Run the timer, square wave will be generated upon interrupt
here:
simp here infinite loop
END
RESULT
Thus the program was written & executed to enable and disable interrupt in
8051 microcontrollerEXPT NO:
8051 - MOTOR CONTROL
DATE:
AIM:
To write & execute program to enable motor control in 8051 microcontroller
THEORY:
Stepper motor is brushless DC motor, which can be rotated in small angles, these
angles are called steps. Generally stepper motor use 200 steps to complete 360 degree
rotation, means its rotate 1.8 degree per step. Stepper motor used in many devices which
needs precise rotational movement like robots, antennas, hard drives ete.
Stepper motors are basically two types: Unipolar and — Bipolar. Unipolar
stepper motor generally has five or six wire, in which four wires are one end of four stator
coils, and other end of the all four coils is tied together which represents fifth wire, this is
called common wire (common point). Bipolar stepper motor there is just four wires coming,
‘out from two sets of coils, means there are no common wire,
Stepper motor is made up of a stator and a rotator. Stator represents the four
electromagnet coils which remain stationary around the rotator, and rotator represents
permanent magnet which rotates. Whenever the coils energised by applying the current, the
electromagnetic field is created, resulting the rotation of rotator (permanent magnet). Coils
should be energised in a particular sequence to make the rotator rotate. On the basis of this
“sequence” we can divide the working method of Unipolar stepper motor in three modes:
Wave drive mode, full step drive mode and half step drive mode.
Ful dive Stepping Sequence
Step# Pint Pin2 Pin PingPROGRAM IN C
#include
unsigned int Direction = 1;
unsigned int STEP_LUT[]=(8,1,2,4};
void ex1_isr (void) interrupt 2 using 2
{
if Direction
{
))
Direction = 0;
Direction = 1;
void DelayMs(delay)
{
int iis
for(i=O;icdelay;i++)
{
for(j=0;5<50;5+4);
}
}
void main(void)
{
unsigned int i,delaytime = 5;
—--Configure INTO (external interrupt 0) to generate an interrupt on the falling-edge of
_*/
r
/INTO (P3.2). Enable the EXO interrupt and then enable theglobal interrupt flag
ITI = 1; // Configure interrupt 0 for falling edge on /INTO (P3.2)
EX 1; / Enable EXO Interrupt
EA=1;_ // Enable Global Interrupt Flag
while(1)
{
if Direction)
{
for(i=0;i<=3;i++)
~(STEP_LUTIi));
W ~(l< i);
DelayMs(delaytime);
P1=Ox0F;
DelayMs(delaytime);
RESULT
Thus the program was written &executed to enable motor control in 8051
microcontrollerEXPT NO:
8051 -LCD
DATE:
AIM:
To write &
execute
program to
check the
performanc
e for LED in
8051
microcontrol
ler.
THEORY:
The LCD used in this experiment has I4pins VCC, VSS and VEE. While VCC and
VSS provide 15s and GND VEE is used for controlling LCD contrast. There are two very
important register inside the LCD. The RS pin is used for their selection. If RS=0 the
instruction command code register is selected allowing the user to send a command such as.
clear display curser at home etc .If RS=1 data register is select. R/w input allow the user to
write information to the LCD or read from it, R/w=Iwhen reading and R/w =1 writing .The
enable pin is used by the LCD to latch information presented to its data line .when data is
supplied to data pin a high to low pulse must be applied to this pin in order for LCD to latch
the data. The bit data pin DO-D7 are used to send information to LCD or send the contents,
of the LCD's internal register .To display letters and numbers we send ASCH code for the
letter A-Z ,a-z to these pins while making RS=
PROGRAM IN C
#include
#define Enable 0x08
#define RS 0x04
void DelayMs(delay)
{
inti:
for(i=O;i