Você está na página 1de 21

PIC Instruction

Create and Execute PIC18F MASM Programs in MPLAB IDE

PROCEDURE:

Step 1: Create a new project. Create a folder and workspace (provided by MPLAB) in which all
files will be stored.

To create a new project, open MPLAB IDE by double clicking on its icon and follow the steps
given below:

1. Select Project Project Wizard. The Welcome screen displayed  Next

2. Select Device. Use Pull-down menu to select PIC18F452 (or 4520) or whatever device you
are usingNext

3. Select a Language Tool suite. Use the pull-down menu to select Microchip MPASM Tool
suite for writing MASM programs. The complete path is shown at Location of Select Tool on the
screen. If it is incorrect or empty, use Browse to locate Mpasmwin.exe file  Next

4. Name your Project. For Project directory, select Browse and Find  Next

5. Add any existing source files.  Next

6. Project Wizard Summary  Finish

Step 2: Select Project  Build All. If there are no errors, you will get an output message “Build
Succeeded”. If there are errors you will get an output message “Build Failed” and it will list all
the errors. Double click on one of the errors, the screen will show the Source Code with an arrow
pointing to an Error. After correcting those errors rebuild the project

Step 3. Execute the project using the MPLAB Simulator and examine the output.

The steps in setting up the MPLAB Simulator are as follows:

1. Select Debugger  Select Tool  MPLAB SIM

2. Select Debugger  Settings. Set the clock frequency of the simulator and break points if
needed. Otherwise accept the defaults.  Apply  OK

3. Select View Watch. The View provides menu options such as Program Memory, File
Registers, and Special Function Registers and Watch. Or type in the names if the registers in the
space provided we want to Observe. The screen shows the Address of a register selected, Symbol
Name and its Value.

4. Select Debugger  Reset.

5. Select Debugger  Run


Experiment No.: Page No.:
Date: Roll No.:

Arithmetic Operations using PIC18F

AIM:

To write and execute MASM program to perform arithmetic operations using PIC18
Microcontroller

REQUIREMENT:

1. MPLAB IDE 8.91


2. MASM Assembler Tool suite

ALGORITHM: (a) ADDITION

Step 1: Load WREG with the literal value from the file register at 0x00.

Step 2: Add the number stored at file register 0x01 with the number in the WREG register
and the sum will be stored in the WREG register.

Step 3: Move the contents of the WREG register to the file register at 0x02

ASSEMBLY CODE:

REG0 EQU 0X00

REG1 EQU 0X01

REG2 EQU 0X02

MOVLW 0X03

MOVWF REG0,0

MOVLW 0X08

MOVWF REG1,0

ADDWF REG0,0

MOVWF REG2,0

END
OUTPUT:

RESULT:

The program was executed successfully and the result verified.


Experiment No.: Page No.:
Date: Roll No.:

ALGORITHM: (b) SUBTRACTION

Step 1: Load WREG with the literal value from the file register at 0x00.

Step 2: Subtract the number stored at file register 0x01 with the number in the WREG
register and the answer will be stored in the WREG register.

Step 3: Move the contents of the WREG register to the file register at 0x02

ASSEMBLY CODE:

REG0 EQU 0X00

REG1 EQU 0X01

REG2 EQU 0X02

MOVLW 0X02

MOVWF REG0,0

MOVLW 0X08

MOVWF REG1,0

SUBWF REG0,0

MOVWF REG2,0

END
OUTPUT:

RESULT:

The program was executed successfully and the result verified.


Experiment No.: Page No.:
Date: Roll No.:

ALGORITHM: (c) MULTIPLICATION

Step 1: Load WREG with the literal value from the file register at 0x00.

Step 2: Multiply the number stored at file register 0x02 with the number in the WREG register.
An unsigned multiplication is carried out between the contents of W and the 8-bit literal.
The 16-bit result is placed in PRODH: PRODL register pair.

Step 3: PRODH contains the high byte. PRODL contains the low byte. W is unchanged. None
of the status flags are affected.

ASSEMBLY CODE:

PRODH EQU 0X01

PRODL EQU 0X02

MOVLW 0X02

MULLW 0X08

MOVFF PRODH,0X01

MOVFF PRODL,0X02

END
OUTPUT:

RESULT:

The program was executed successfully and the result verified.


Experiment No.: Page No.:
Date: Roll No.:

Sum of the Series

AIM:

To write and execute MASM program to calculate the sum of the series using PIC18
Microcontroller

REQUIREMENT:

1. MPLAB IDE 8.9


2. MASM Assembler Tool suite

ALGORITHM:

Step 1: Initialize the counter n.

Step 2: Declare the variables sum and i.

Step 3: Initialize sum=0 and i=0.increment I by one.

Step 4: Calculate the value of sum by adding i. increment i to get the number in sequence

Step 5: Do the step 4 till the value of i>n.

Step 6: Store the sum and exit.

ASSEMBLY CODE:

N SET 0X0B
SUM SET 0X01
I SET 0X02
ORG 0X00
START CLRF SUM
CLRF I
INCF I, F
SUM_LP MOVLW N
CPFSGT I
BRA ADD_LP
BRA EXIT_SUM
ADD_LP MOVF I, W
ADDWFC SUM, F
INCF I, F
BRA SUM_LP
EXIT_SUM NOP
END
OUTPUT:

RESULT:

The program was executed successfully and the result verified.


Experiment No.: Page No.:
Date: Roll No.:

Accessing I/O Port

AIM:

To write and execute MASM program to access I/O port using PIC18 Microcontroller

REQUIREMENT:

1. MPLAB IDE 8.9


2. MASM Assembler Tool suite

ALGORITHM:

Step 1: Initialize the file registers RI and R2.

Step 2: PORTB is set to output port.

Step 3: Assign the value 55 to PORTB.

Step 4: Call the delay function.

Step 5: Toggle the PORTB bits to AA after the time delay.

Step 6: Continue the process to toggle the PORTB bits.

ASSEMBLY CODE:

R2 SET 0X20
R1 SET 0X30
PORTB SET 0X10
TRISB MOVLW 0X00
L1
MOVLW 0X55
MOVWF PORTB
MOVFF PORTB, R1
CALL DELAY
MOVLW 0XAA
MOVWF PORTB
MOVFF PORTB, R1
CALL DELAY
GOTO L1
DELAY
MOVLW 0X0A
MOVWF R2
D1
NOP
DECF R2, F
BNZ D1
RETURN
END
OUTPUT:

Result: The program was executed successfully and result verified.


Experiment No.: Page No.:
Date: Roll No.:

Flashing LED

Aim:
To execute the program to understand single bit manipulation of PORTB by flashing LED using
MPLAB IDE

Requirements:
1. MPLAB IDE 8.91
2. Hi-Tech C Compiler
3. Proteus Simulator 8

Programming the PIC Microcontroller


The PIC microcontroller programming is performed through MPLAB software. Here Hi-tech C
compiler is used for building the program
.
Project Wizard
• Start MPLAB IDE.
• Click Project menu and open Project wizard. Click Next.
• Select PIC18F452 as your Device.
• Select HI-TECH Universal Tool Suite in front of Active Tool suite. (Note: Hi-Tech
compiler must be installed in your system.)
• Give a name of project and select the location of project. Project is successfully created.
• Clicking View/ Project to view the project window.
• Add ledblink.c to source file.
• Build the ledblink.c by clicking the black square with red spot.
It will create a .HEX file and its name will be same as your project name in the same directory
where the project is saved. This hex file is to be loaded in PIC18F452 (Proteus Simulation
Circuit).

Simulating the Circuit


The simulation software allows the user to know the circuit performance & to rectify the errors of
the program. Here Proteus 8 software is used to check the circuit performance.
• Open the project in the Proteus software.
• Click on the ‘Debug’ menu.
• Select the ‘Start VSM Debugging’ option.
• Select Run the Simulation option
• The LED starts blinking, which indicates the circuit is running.
• After some time, select the ‘Stop VSM Debugging’ option or Halt the simulation. The
LED will now stop blinking.
Source Code :

#include<htc.h>

__CONFIG (1, OSCSDIS & HSPLL);


__CONFIG (2, BORDIS & PWRTDIS & WDTDIS);
__CONFIG (3, CCP2RC1);
__CONFIG (4, DEBUGEN & LVPEN & STVREN);
__CONFIG (5, UNPROTECT);
__CONFIG (6, WRTEN);
__CONFIG (7, TRU);

#define _XTAL_FREQ 40000000

void delay_sec (unsigned char seconds)


{
unsigned char i,j;
for (i=0; i<seconds;i++)
for (j=0; j<100; j++)
__delay_ms (10);
}

void main ()
{
RB0=0;
TRISB=0;

while (1)
{
RB0=1;
delay_sec (1);
RB0=0;
delay_sec (1);

}
Output

Result :

The program was executed successfully and the result verified


Experiment No.: Page No.:
Date: Roll No.:

Stepper Motor Interfacing

Aim:
To execute the program to interface the stepper motor with PIC18F Controller using MPLAB
IDE

Requirements:
1. MPLAB IDE 8.91
2. PIC18 Simulator IDE
3. Hi-Tech C Compiler for PIC18 MCUS

Stepper Motors
Stepper motors (or, sometimes, stepping motors), are motors which can turn through
"steps"—well-defined angles—under precise electronic control.

Two-phase stepper motors


There are two basic winding arrangements for the electromagnetic coils in a two phase
stepper motor: bipolar and unipolar. The main difference between a unipolar and bipolar stepper
motors is that the former works on single polarity while the latter works on both positive and
negative voltages. Since unipolar stepper motors are quite easy to operate, these are highly
preferred among hobbyists

STEPPING MODES – Unipolar stepper motors


Mainly there are 3 types of stepping modes in Unipolar stepper motors
• Full step
• Half step
• Micro step
Full Step
In full step operation each step has a movement of 1.8 degrees and hence it takes 200 steps
to complete a full revolution. This is made possible by energizing either single of stator winding
or two phases. Since the two phases are energized at the same time in the dual phase operation,
torque and speed are greater.

There are two types of full step mode:


• One step mode / wave step
• Dual phase mode
In one step mode/ wave step, the motor is operated with only one phase energized at a time.
This mode of operation requires small amount of power.

In dual phase mode, the motor operated in both phases get energized at the same time.
This mode provides more torque and high speed performance. But this mode requires twice the
amount of power used in one step mode.

Half Step
In half step mode, the rotor moves through half the basic step angle when compared to full
step mode which results in more precise motion control and smoother motor performance.
Resolution is also increased. But in this mode, motor produces less torque compared to other
modes.

Micro step
In micro step mode, it divides the motor steps up to 256 times which improves the low
speed smoothness and low speed resonance effects. But in this mode, motor produces less torque
compared to other mode.
Applications
The stepper motors are used in applications like motion-controlled positioning system as
it is easy to produce precise position control with the help of computer controlled stepper
motors. They are widely used in biomedical equipment where precise and accurate position
control is needed. They are also present in disc drivers, computer printers and scanners,
intelligent lighting, camera lenses and various other common devices and equipment. Because
of their precision characteristic, stepper motors are preferred in robotics. 3D cameras, X Y
Plotters, CNC and some other camera platforms also impart stepper motors because of its high
reliability and precision.

Project Wizard
• Start MPLAB IDE.
• Click Project menu and open Project wizard. Click Next.
• Select PIC18F4520 as your Device.
• Select HI-TECH Universal Tool Suite in front of Active Tool suite. (Note: Hi-Tech
compiler must be installed in your system.)
• Give a name of project and select the location of project. Project is successfully created.
• Clicking View/ Project to view the project window.
• Add stm.c to source file.
• Build the stm.c by clicking the black square with red spot
• It will create a .HEX file and its name will be same as your project name in the same
directory where the project is saved. This hex file is to be loaded in PIC18F4520.

Simulation:

PIC18 simulator IDE


Here are the simple steps to simulate the program.
• Install PIC18 Simulator IDE on your system. Open PIC18 simulator IDE.
• Go to File/Load Program Load the .HEX file which was created by building the project.
• Now open Stepper Motor Phase Simulation from Tools menu.
• Select Microcontroller–>PIC18F4520 and Clock Frequency –> 11.0592 MHz
• Start Simulation from Simulation/start.

The graphical display of stepper motor rotation will be displayed in a window.


Source Code:

#include<p18f4520.h>
#include<delays.h>

#pragma config WDT=OFF, OSC=HS, LVP=OFF, IESO=OFF, FCMEN=ON, XINST=OFF

#define DELAY 10
#define HALF_STEP // change this for the mode of excitation.

#ifdef HALF_STEP
#define SEQ 8
#endif

#ifdef HALF_STEP
unsigned char stepping_seq[8]={0b00001000, 0b00001100, 0b00000100,
0b00000110,0b00000010, 0b00000011, 0b00000001, 0b00001001};
#endif

void main ()
{
unsigned char count = 0;
TRISB = 0x00;

while (1)
{
if (count == SEQ)
count = 0;
LATB = stepping_seq[count];
Delay10KTCYx (DELAY);
count++;
}

}
Output:

Result:

The program was executed successfully and the result verified.

Você também pode gostar