sa h - site.iugaza.edu.pssite.iugaza.edu.ps/mawad/wp-content/uploads/lab1_embeddedlab.pdfmikrobasic...
TRANSCRIPT
Feb. 2014
By
: EN
G. W
AF
AA
AU
DA
H
By
: EN
G. W
AL
EE
D M
OU
SA
1
2 Embedded Systems Lab
Grading Policy
- MID Term Exam 20
(10 practical – 10 Theoretical)
- Final Term Exam 30
- Reports 20
- Project 15
- Quizzes 5
- Attendance+ lab-work 10
Total 100
3 Embedded Systems Lab
Objectives
- To be familiar with microcontrollers, PIC184550 microcontroller.
- To be familiar with some software tools like MPLAB, mikroBasic, PROTEUS
and QL_PROGen knowing their components and some features.
- To know how to make a project using MPLAB and mikroBasic and then get
the hex file of the software program of the project.
- To know how to simulate your program using PROTEUS.
- To know how to load the code into microcontroller using hardware
components.
Introduction
A microcontroller (or MCU) is a computer-on-a-chip. It is a type of
microprocessor emphasizing self-sufficiency and cost-effectiveness, in
contrast to a general-purpose microprocessor (the kind used in a PC).
A microcontroller is a single integrated circuit, commonly with the following
features:
- Central processing unit - ranging from small and simple 4-bit processors to
sophisticated 32- or 64-bit processors
- Input/output interfaces such as serial ports (UARTs)
- Peripherals such as timers and watchdog
- RAM for data storage
- ROM, EPROM, EEPROM or Flash memory for program storage
- Clock generator - often an oscillator for a quartz timing crystal, resonator or
RC circuit
- Many include analog-to-digital converters
Introduction to Software Tools
4 Embedded Systems Lab
Microcontroller versus Microprocessor
A microcontroller differs from a microprocessor in many ways. The first and
most important difference is its functionality. In order that the
microprocessor may be used, other components such as memory must be
added to it. Even though the microprocessors are considered to be powerful
computing machines, their weak point is that they are not adjusted to
communicating to peripheral equipment. On the other hand, the
microcontroller is designed to be all of that in one. No other specialized
external components are needed for its application because all necessary
circuits are already built into it.
Input/output Ports
Each microcontroller has one or more registers (called a “port”) connected to
the microcontroller pins. Every pin is one bit << PIC has many ports (general
function register: a, b, c, d, e) << Ports are used for inputting and outputting
data (ex: input by button, output by LED) << Every port is connected with
special function register called Tris << Tris is used to make configuration for
the port to indicate if it is input (tris=1) or output (tris=0).
5 Embedded Systems Lab
For example:
To configure port B as an input we but Trisb=11111111 and read the value
form portB and to configure the first pin in port C as output we put
Trisc=11111110. You will get used to this during the lab.
for tris register 1 is input and 0 is output. Remember:
1nput 0utput
Memory Unit
Memory is part of the microcontroller used for data storage.
1. Read Only Memory (ROM)
ROM (Read Only Memory) is used to permanently save the program being
executed. The size of a program that can be written depends on the size of
this memory.
2. Flash memory
The contents of this memory can be written and cleared practically an
unlimited number of times, the microcontrollers with Flash ROM are ideal for
learning, experimentation and small-scale manufacture.
3. Random Access Memory (RAM)
Once the power supply is off the contents of RAM (Random Access Memory)
is cleared. It is used for temporary storing data and intermediate results
created and used during the operation of the microcontroller.
4. Electrically Erasable Programmable ROM (EEPROM)
The contents of the EEPROM may be changed during operation (similar to
RAM), but remains permanently saved even upon the power supply goes off
(similar to ROM). Accordingly, an EEPROM is often used to store values,
created during operation, which must be permanently saved. For example, if
6 Embedded Systems Lab
you design an electronic lock or an alarm, it would be great to enable the user
to create and enter a password, but useless if it is lost every time the power
supply goes off. The ideal solution is the microcontroller with an embedded
EEPROM.
Central Processor Unit (CPU)
As its name suggests, this is a unit which monitors and controls all processes
inside the microcontroller. It consists of several smaller subunits like
Instruction Decoder, Arithmetical Logical Unit (ALU) and Accumulator.
PIC Microcontrollers
PIC (Peripheral Interface Controller) microcontrollers designed by Microchip
Technology are likely the right choice for you if you are a beginner.
PIC18F4550
This is the microcontroller we are going to use during our lab.
- High Performance RISC CPU.
- Operating Frequency: DC – 48 MHz
- Linear program memory addressing to 32 Kbytes.
- Can store up to 16,384 single-word instructions.
- Linear data memory addressing to 2 Kbytes.
- 256 bytes EEPROM memory.
- Five bidirectional I/O Ports: A, B, C, D, E.
- Three independent Timers/Counters & Watch-dog timer.
- Serial Communications:
o Enhanced USART Module, Supports RS-485, RS-232.
o Master Synchronous Serial Port (MSSP): supports SPI and I2C mode.
7 Embedded Systems Lab
- Streaming Parallel Port (SPP).
- Incorporate a fully featured Universal Serial Bus communications module
that is
- Compliant with the USB Specification Revision 2.0.
- Compatible 10-bit Analog-to-Digital Converter: 13 input channels.
Pin Diagram
Datasheet link
ww1.microchip.com/downloads/en/devicedoc/39632e.pdf
Pin Diagram clear photo
http://www.mediafire.com/view/?nd3hzb6gz7jmb25
8 Embedded Systems Lab
Programs used
These programs are the backbone of the microprocessor and microcontroller
based systems; since using MPLAB we can build the software of the project
using C or Assembly language, and then we can simulate the project virtually
using PROTEUS, finally we can download the program on the
microcontroller and see the results practically using WinPic. So this
experiment includes all the knowledge the student will need to get started to
these programs.
MPLAB Program
MPLAB is a software program that runs on your PC to provide a development
environment for your embedded system design. In other words it is a
Windows program package that makes writing and developing a program
easier. It could best be described as developing environment for a standard
program language that is intended for programming microcontrollers.
Get started to MPLAB
- From the project menu choose project wizard.
- Choose PIC18F4550.
- Browse and name the project.
- From view menu choose project; to view the project
- Add P18F4550.inc, P18F4550.lkr files to project.
- At path: <<Program Files<<MicroChip\MPASM Suite (or \LKR)
(You can copy this file into the path in LKR folder to aviod errors << add it to project)
- To write your code, open new file from here or (file-new), write your code,
save it with ex: Name.asm. (don’t forget .asm !!)
- Then add a file to the source files by right clicking on source files, and choose
add files and then name the file any Name.asm.
9 Embedded Systems Lab
An assembler is a program that converts source-code programs written in
assembly language into object files in machine language.
A linker is a program that combines your program’s object file created by
the assembler with other object files and link libraries, and produces a
single executable program.
Steps to Create a Program
Files Used By MPLAB IDE
.asm: Assembly language source file – MPASM assembler
.hex: Machine code in hexadecimal format file
.inc: Assembly language include file – Microchip assemblers
.lkr: Linker script file – MPLINK linker
For complete description about MPLAB see MPLAB_User_Guide
10 Embedded Systems Lab
Program main structure
Make BuildAll
include<p18f4550.inc>: similar to import
global main : start to execute from „main‟ label
org 0x2A : address of main
main : start the code from here
movlw 0x00 : move literal(value) to work register (wreg): wreg=0
movwf TRISB : move from wreg to file register :portb-output
movlw 0x8A : wreg=8A
movwf PORTB : portb=8A
end : end of the main code
Description
11 Embedded Systems Lab
In order to make writing code easier, you can disable case sensitivity:
Project << Build option << project << MPASM Assembler << tick ‘Disable case
sensitivity’.
- Avoid some mistakes in your work: don’t change the name of file when you
add it << don’t put the file in Arabic words path.
MikroBasic
We will be using also Mikrobasic language, for more details you have to check
the language reference for MikroBasic
<< Starting new MikroBasic Project
Note
Avoid !!
12 Embedded Systems Lab
Enter a name and choose a path to your project and then choose you PIC
device –for our lab it will be PIC18F4550- and if you use an external oscillator
then you have to specify its value –we will use a crystal of 8MHz value-.
Now you have to check some flags in the device –we will talk about them
later- but for now just check:
Save the scheme with a
name in order to avoid
searching again for the
flags.
13 Embedded Systems Lab
Program main structure
program Progname
‘function declaration
main:
‘variable declaration
adcon1 = adcon1 or 0x0f 'convert input to digital
‘tris configuration
while true
‘program body
wend
end.
PROTEUS Program
Proteus contains everything you need to develop; test and virtually
prototype your embedded system designs based around the Microchip
Technologies™ PIC 18 series of microcontrollers. This design synergy allows
engineers to evolve their projects more quickly, empowering them with the
flexibility to make hardware or firmware changes at will and reducing the
time to market. Proteus VSM models will fundamentally work with the exact
same HEX file as you would program the physical device with, binary files
(i.e. Intel or Motorola Hex files) produced by any assembler or compiler.
How to setup PROTEUS?
1. Open Labcenter.Proteus.Professional.v7.10 folder and run the setup.
2. Press next until you reach the window which asks for the key.
3. From browse for key; browse until you reach the same folder where the
setup exists, and then open the crack folder.
4. Then click on Grassington North Yorkshire.lxk and then press install.
5. Then choose yes and close the window, after that browse and install the
program.
6. After that run the patch LXK Proteus 7.10 which exists in the Crack folder
as an administrator, then click update.
14 Embedded Systems Lab
Get started to PROTEUS
1. From start menu chose the PROTEUS, and then chose ISIS (blue).
2. To get a part click on Devices (P) … some needed components are here:
Part Keyword
Microcontroller PIC18F4550
Resistor res (change value!)
Button button or push
LEDs led-
3. Write the name of the PIC18F4550.
Show your
hardware
parts
Add new
parts
Show terminals:
VDD and GND
15 Embedded Systems Lab
Don’t forget to connect a VDD to the MasterClear Pin (pin#1) in order for the
Microcontroller to work.
To install your program to the Microcontroller: double-click on it and choose the
.hex file then assign the right 8MHz clock because we use a crystal of 8MHz
16 Embedded Systems Lab
Now you can run the simulation
P Hardware work
Basic hardware connections
Pin# Pin Name What to connect
1 Active low Mater clear +5V
11 VDD +5V
12 VSS Ground
13 OSC1 Crystal
14 OSC2 Crystal
31 VSS Ground
32 VDD +5V
17 Embedded Systems Lab
QL_PROGen (used to program the PIC)
Put the PIC at the right place in programmer
Connect the programmer to the computer
Chip select << Port select << Load (.hex file): when ‘Ready’ appears … your
work seems to be correct … then click Program..
18 Embedded Systems Lab
“Programming complete“ tells you that PIC has been programmed
successfully and ready to be used at your kit
19 Embedded Systems Lab
<< Proteus circuit connection is shown at the following page >>
For hardware connection:
- Load the code at the pic using Programmer (connect it to the computer <<put
the pic at the correct position << load the code).
- Put the pic at the suitable place at the kit and use wires to represents the
needed connection in order to show the output using LEDs.
- Don’t forget to use a power source to activate your work.
1. Write an assembly language program that load “0x03” to
port B.
2. Simulate the circuit using Proteus ISIS program.
3. Connect the circuit at the hardware kit.
4. Connect the circuit at the hardwatre Kit.
Lab Work 1
20 Embedded Systems Lab
Complement is made by using “ Not “ …. Ex: x=0Xff, y=not x, y=0x00
1. Write a mikroBasic program that work as a flusher that load
“0x55” then load its complement to port B continuously.
2. Simulate the circuit using Proteus ISIS program.
3. Connect the circuit at the hardware kit.
Lab Work 2
21 Embedded Systems Lab
At mikroBasic code you must use delay. Delay is used in order to let the user se the
change … origional change is very fast and the user can’t notice it.
delay_ms(300) function : make a delay for 300 milliseconds.
… Good Luck …
Note
Include all your work during the lab for a complete report that
contains codes, simulation and hardware work with results.
Homework