SA H - site.iugaza.edu.pssite.iugaza.edu.ps/mawad/wp-content/uploads/Lab2_Basic...
Transcript of SA H - site.iugaza.edu.pssite.iugaza.edu.ps/mawad/wp-content/uploads/Lab2_Basic...
Feb. 2014
By
: EN
G. W
AF
AA
AU
DA
H
By
: EN
G. W
AL
EE
D M
OU
SA
2
2 Embedded Systems Lab
Objectives
- To know how to use switches, push buttons, LEDs and 7-segments.
- To know the meaning and implementation of 7-segment multiplexing.
- To be more familiar with mikrobasic and assembly codes.
Switches and Buttons
Switches and buttons are the basic input devices used to supply certain
voltage level for microcontrollers. Button/switch is a mechanical component
which connects or disconnects two points A and B over its contacts. By
function, button/switch contacts can be normally open or normally closed.
Basic I/O
3 Embedded Systems Lab
Switches
Mikrobasic
- DIPSW consists of 8 switches that contain on/off choices.
- It has two sides, one is connected to power source and the other is
connected into the PIC.
- Resistor is needed (as will be shown later with button).
- We can get it from Proteus by keyword “dipsw” then choose the
type 8.
Note
Work with switch doesn’t
need specific extra code,
PIC just deal with it as input
bits (0 & 1) across its pins.
1. Write a program that inputs data from port D (by DIPSW) and
sends it to portB.
2. Simulate the circuit using Proteus ISIS program.
Lab Work 1
4 Embedded Systems Lab
Assembly
Proteus
Move from file register
to another destination
(wreg here).
<< Two stages >>
Move from file register
to file register directly.
<< One stage >>
5 Embedded Systems Lab
Push-Buttons
Buttons can be connected to the microcontroller in one of two ways:
Button with pull down resistor
o When the button is presses it will supply logical one (+5V) to MCU.
o When the button is not pressed the pull down resistor will supply logical
zero(0v) to the MCU.
Button with pull up resistor
o When the button is presses it will supply logical zero(0v) to the MCU.
o When the button is not pressed the pull up resistor will supply logical
one (+5V) to MCU.
- Push button is connected to power and the PIC with its two sides.
- Pull down resistor will be used through the work in order to avoid
Floating case (Not Zero & not 5V): if there is no resistor and the user
doesn’t press the button, this case happened which means no
constant state for the button … it is problem!! , so pull-down
resistor is needed to give ‘0’ state normally.
- We can get it from Proteus by keyword “push” then choose first
choice.
6 Embedded Systems Lab
Proteus
The needed time for the user to press the push button is approximately 200
ms, the speed of the PIC’s work is very high, so ‘while true’ loop will be
executed a lot of times through this period (200 ms), which means that
button has been pressed many times, but in fact the user did it just once!!
So delay is needed .. we can use 300 ms for general use
Note
1. Write a Mikrobasic program that toggles a LED when clicking
on a push button.
Lab Work 2
7 Embedded Systems Lab
Mikrobasic
How to think …
- We need to check the status of button: using “if” conditional.
If condition then
‘your action
end if
- Condition : porta.2=1 (just one “=”).
- We need to check the 2nd bit of porta “porta.2”.
- Toggle can be made by xor (x xor 1= x’).
- Delay is needed after “if” in order to let the PIC see just one press on
the button.
- If we want to toggle all bits of the port: xor 0xff.
8 Embedded Systems Lab
2. If we want to toggle an enterd number (ex: 0x05) and show the
result into leds.
Mikrobasic
Proteus
Assembly
9 Embedded Systems Lab
How to think …
- 3 important items we will make in order to build a
corresponding assembly code to mikrobasic code:
1. toggle: comf portd (complement file reg.)
2. loop (in order to make the work continuous)
x your code
goto x (equals ‘while true’, x as a label)
3. delay (to see the states clearly)
Delay details will be shown later at course and lab,
here some important notes are needed to be
understood now.
- The principle of the delay is that we count down
from a previously set number, and when it reaches
zero, we stop counting. The zero value indicates the
end of the delay, and we continue on our way
through the program. The largest number we can
have is 255 or FF h in hex
- ‘delay’ as a label of the function, end with ‘return’.
- .255: “.” means decimal number.
- Movwf 0x30: location in memory to store number.
- Decfsz: decrement file register skip if zero.
- Inner loop (1l) executed inside outer loop (l2); total
= 255*255 times.
10 Embedded Systems Lab
Seven Segment Display
Seven-segment displays are commonly used in electronics as a method of
displaying decimal numeric feedback on the internal operations of devices. A seven
segment display, as its name indicates, is composed of seven elements.
Individually on or off, they can be combined to produce simplified representations
of the Hindu-Arabic numerals. The most commonly used is so called 7- segment
display.
It is composed of 8 LEDs- 7 segments are arranged as a rectangle for symbol
displaying and there is an additional segment for decimal point displaying. The
segments of a 7 segment display are referred to by the letters A to G, as follows:
11 Embedded Systems Lab
Seven segment display can be divided into 2 types of connection.
One is called common anode of which all the anodes of the LEDs are
connected together, leaving the cathodes open for connection.
The other one is called common cathode of which all the cathodes of the
LEDs are connected together, leaving the anodes open for connection.
Through the lab we will use common cathode 7-segment
Pins to work << VCC
Enable << GND
12 Embedded Systems Lab
To view a number (ex: 5H) on 7-segment, just load its 7-seg representation number
to the port
g f e d c b a
5H: 0 1 1 0 1 1 0 1 => 6d
Mikrobasic Assembly
Proteus
13 Embedded Systems Lab
Mikrobasic
How to think …
- In order to make work easier, we will build a function that
get the 7-seg representation of number 0-9.
- Function is written before main.
- sub function name (parameter : dim name as type) as
returned type.
- Ends with end sub.
- dealing with many states (0-9) can be easier with select case
(as: switch (case) in JAVA).
- select ends with end select.
1. Write a program that counts form 0-9 and displays the digits
on the 7-seg.
2. Simulate the circuit using Proteus ISIS program.
Lab Work 3
14 Embedded Systems Lab
Function
15 Embedded Systems Lab
Main
Assembly
Nothing is new !!
Just load numbers << make
a delay …
(no use of converting
function)
How to think …
- We will call a function with a number (0-
9), so we need to have variable: counter.
- We can view initial number on the 7-seg.
- Counter value must be incremented with
a delay between each number.
- Counter must be returned to 0 after 9.
16 Embedded Systems Lab
7-segment Multiplexing
The simplest way to drive a display is via a display driver. These are available for up
to 4 displays. Alternatively displays can be driven by a microcontroller and if more
than one display is required, the method of driving them is called "multiplexing."
If a single display is to be driven from a microcontroller, 7 lines will be needed plus
one for the decimal point. For each additional display, only one extra line is needed.
To produce a 4, 5 or 6 digit display, all the 7- segment displays are connected in
parallel. The common line (the common-cathode line) is taken out separately and
this line is taken low for a short period of time to turn on the display.
1. Write a program that displays the number ‘27’ on two 7-seg
using multiplexing techniques.
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 4
17 Embedded Systems Lab
Proteus
How to think …
- Instead of reserving two ports to view 2-digits, multiplexing is used.
- One port will be used… but it can’t carry two values at the same time!.
So we will make a trick that: enable just the 1st 7-seg << load first digit
into it << make a short delay << enable just the 2nd 7-seg << load second
digit into it << make a short delay << all in a loop.
- By this way two digits will appear approximately together .. for the
user eyes.
- Load and delay are the same of a previous work, new work will be with
enable.
- Common cathode 7-seg is enabled by GND, here we want to control
enabling. So we connect the enable of both 7-seg to two of PIC pins and
give/don’t them a power as we need.
18 Embedded Systems Lab
Mikrobasic Assembly
At hardware work in the lab, the 7-seg at the kit works as the following:
Pins to work << VCC
Enable << VCC
So in order to make the same previous work, you must change porta
values:
- Enable first 7-seg(by 1): 00000001 = 0x01
- Enable second 7-seg(by 1): 00000010 = 0x02
- Enable first 7-seg(by 0):
11111110 = 0xfe
- Enable second 7-seg(by 0):
11111101 = 0xfd
19 Embedded Systems Lab
… Good Luck …
1. Write a Mikrobasic code that toggles 3 LEDs when
controlling that with clicking on 3 push buttons.
2. Write an Assembly and Mikrobasic programs that display
‘271’ on three 7-segs using multiplexing techniques.
Bonus
Implement a system that counts from 00 to 99 and
increments every one click on a push button. (Include
code and circuit with short description).
Include all your work for a complete report that contains
codes and simulation work with results.
Homework
- Comment at Mikrobasic: “ ‘ “ - Comment at MPLAB: “ ; “
- To be more familiar with assembly instructions, see:
http://technology.niagarac.on.ca/staff/mboldin/18F_Instruction_Set/
Extra