KEMBAR78
Lecture 4 | PDF | Analog To Digital Converter | Computer Science
0% found this document useful (0 votes)
3 views21 pages

Lecture 4

Embedded systems 366

Uploaded by

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

Lecture 4

Embedded systems 366

Uploaded by

Rayen Cherbib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Department of Electrical Engineering

Embedded Systems

Digital Input and Output

Lecture 4 Dr F Bensaali
Content:

• Introduction to digital terminology


• Digital outputs on the mbed
o Digital Input/Output pins
o Example: using external LEDs
o Digital output buses
• Digital inputs on the mbed
o Example: connecting switches to the mbed
o Digital input buses

2
Introduction to digital terminology

 Human world signals are usually based upon continuous


analogue signals that vary in time and space
o e.g. temperature variation in the room over a day

 To represent this analogue world digitally we have to assign


numbers to successive parts of the analogue signal and
process them instead

 It’s a conversion process from the continuous analogue world


to the digital world
 Analogue to Digital Converters (ADCs) are used to perform
this operation

3
Introduction to digital terminology (Cont’d)
Temperature
(oC)

30

25

20

15

10

5
Time of day
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

A.M. P.M.
Temperature
(oC)

Each value represented


by a dot can be 30

25
represented as a digital 20

code that consists of a 15

series of 1s and 0s 10

5
Time of day
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

A.M. P.M.

4
Introduction to digital terminology (Cont’d)

 Most computers are digital, being constructed out of digital


circuits
o Voltage levels are used to represent the binary values ‘0’ and
‘1’ (often GND=‘0’ and VCC=‘1’)
 The processor is simply a large digital circuit
o It operates on digital inputs and produces digital outputs
o Each individual input/output signal is either a ‘0’ or ‘1’

5
Introduction to digital terminology (Cont’d)
 The mbed uses a power rail of 3.3 V
o 0 V indicates OFF

o 3.3 V indicates ON
 Different terms are used to describe ON and OFF in digital
systems:

0V 3.3 V
Open Closed
Low High
Clear Set
Logic 0 (or ‘0’) Logic 1 (or ‘1’)
False True

6
Digital outputs on the mbed

 The four on-board LEDs are digital outputs


o They are configured to operate with no extra wires or
connections needed

 The mbed has also 26 digital


Input/Output (I/O) pins (pins 5-30)
o They can be configured as
inputs or outputs

LEDs
7
Digital outputs on the mbed (Cont’d)

 The DigitalOut creates a C++ class

 The class has a set of member functions:

Function Usage
DigitalOut Create a DigitalOut object connected to the specified pin
write Set the output, specified as ‘0’ or ‘1’ (int)
read Return the output setting, represented as 0 or 1 (int)
Operator ‘=’ A shorthand for write( )
Operator ‘int( )’ A shorthand for read( )

8
Digital outputs on the mbed (Cont’d)

 Using the DigitalOut constructor, C++ objects can be created

Example: Using one of the four LEDs


DigitalOut myled(LED1);

 The object myled has been created and it can be written to


and read from it

myled.write(1); X = myled.read();
Or Or
myled = 1; X = int(myled);

9
Digital I/O pins

 The digital I/O pins can be configured by defining them at the


start of the program
o Each digital I/O is given a name and associated pin
Example

DigitalOut mydevice1(p11);
DigitalOut mydevice2(p20);
DigitalOut mydevice3(p28);

 The DigitalOut can be used to set/read the state of the output


pin (‘0’ to turn it OFF, or ‘1’ to turn it ON)

10
Example: Using external LEDs

#include "mbed.h"
DigitalOut redled(p5);
DigitalOut greenled(p6);
int main()
{
while(1)
{
redled = 1;
greenled = 0;
wait(0.5);
redled = 0;
greenled = 1;
wait(0.5);
}
}
11
Wait ( ) function

 The wait( )is a function from the mbed library

o It causes a delay
o The parameter is in seconds and defines the length of
the delay
 There are further options:

Function Action
wait Waits for the number of seconds specified
wait_ms Waits for the number of milliseconds specified
wait_us Waits for the number of microseconds specified

12
BusOut interface
 The 26 digital I/O pins in the mbed can be configured as a
digital input or output bus
 The BusOut interface is used to create a number of parallel
DigitalOut pins that can be written as one numeric value
 The functions that can be used are:

Function Usage
BusOut Create a BusOut object, connected to the specified pins
write Write the value (int value) to the output bus
read Return the value currently output on the bus
Operator ‘=’ A shorthand for write()
Operator ‘int( )’ A shorthand for read()

13
BusOut interface - Example
Switch the on-board LEDs in a specific order (left shift)
#include "mbed.h" #include "mbed.h"
DigitalOut led1(LED1); BusOut myleds(LED4, LED3, LED2, LED1);
DigitalOut led2(LED2); char x;
DigitalOut led3(LED3);
DigitalOut led4(LED4); int main()
{
int main() while(1)
{ {
while(1) x = 1;
{ myleds = x;
led1 = 0; led2 = 0; wait(0.25);
led3 = 0; led4 = 1;
wait(0.25);
for(int i = 0; i < 3; i++)
led1 = 0; led2 = 0;
{
led3 = 1; led4 = 0;
x = x << 1;
wait(0.25);
myleds = x;
led1 = 0; led2 = 1;
wait(0.25);
led3 = 0; led4 = 0;
}
wait(0.25);
}
led1 = 1; led2 = 0;
led3 = 0; led4 = 0; }
wait(0.25);
} Using BusOut
} 14
BusOut interface – Example (Cont’d)
Alternative solution:

#include "mbed.h"
BusOut myleds(LED4, LED3, LED2, LED1);
int main()
{
while(1)
{
myleds = 1;
wait(0.25);

for(int i = 0; i < 3; i++)


{
myleds = myleds << 1;;
wait(0.25);
}
}
}

15
Digital inputs on the mbed

 Digital inputs values can be read only


 The same 26 pins (pins 5-30) can be configured as digital
inputs using DigitalIn

Example

DigitalIn mypin1(p5);
DigitalIn mypin2(p6);
DigitalIn mypin3(p7);

16
Digital inputs on the mbed (Cont’d)

 The available library functions are:

Function Usage
DigitalIn Create a DigitalIn connected to the specified pin
read Return the input, represented as 0 or 1 (int)
mode Set the input pin mode
Operator ‘int( )’ A shorthand for read( )

 DigitalIn enables by default a pull-down resistor

 This can be disabled, or an internal pull-up enabled, using the


mode( )function
o Parameters that can be used are: PullUp, PullDown and
PullNone
17
Example: Connecting switches

#include "mbed.h"
DigitalOut redled(p5);
DigitalOut greenled(p6);
DigitalIn sw(p7);
int main() {
while(1) {
if (sw == 1)
{
redled = 0; // red led is OFF
greenled = 1; // flash green led
wait(0.5);
greenled = 0;
wait(0.5);
}
else
{
greenled = 0; // green led is OFF
redled = 1; // flash red led
wait(0.5);
redled = 0;
wait(0.5);
}
}
}
18
BusIn interface
 The BusIn interface is used to create a number of DigitalIn pins
that can be read as one value
 Any of the numbered mbed pins can be used as a DigitalIn in
the BusIn.

Function Usage
BusIn Create a BusIn object, connected to the specified pins
read Read the value of the input bus
mode Set the input pin mode
Operator ‘int( )’ A shorthand for read( )

19
BusIn interface - Example

#include "mbed.h"
BusIn pinStatus(p5, p6, p18, p11);
int main()
{
while(1)
{
switch(pinStatus)
{
case 0x3: printf("p5 and p6 are 1\n\r"); break;
case 0x8: printf("p11 is 1\n\r"); break;
}
}
}

20
BusIn interface - Exercise

 Change the previous code to display the status of the input pins
on the four LEDs using BusOut.

21

You might also like