0% found this document useful (0 votes)
116 views

7 Segment Display

This document describes interfacing a 7-segment display to a PIC microcontroller. It discusses connecting a single LED, the electrical specifications of the PIC16F877A, 7-segment display devices including a dual 7-segment display, an example connection to the PIC16F877A, a test circuit diagram, configuring the ports, a 7-segment decoder array in code, displaying a counting sequence from 0 to 99 on the display, using integer division and modulo operators, and completing the project in MPLAB with potential for modularization.

Uploaded by

Gagah Prakoso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

7 Segment Display

This document describes interfacing a 7-segment display to a PIC microcontroller. It discusses connecting a single LED, the electrical specifications of the PIC16F877A, 7-segment display devices including a dual 7-segment display, an example connection to the PIC16F877A, a test circuit diagram, configuring the ports, a 7-segment decoder array in code, displaying a counting sequence from 0 to 99 on the display, using integer division and modulo operators, and completing the project in MPLAB with potential for modularization.

Uploaded by

Gagah Prakoso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

ECE 3730

Principles of Embedded Systems:


Display Devices
Interfacing Display Devices to
Embedded Microcontrollers

Single LED Interface


Direct connection to a port pin
Typical current required to switch LED on is about 1-5 mA.
Typical maximum current, which must not be exceeded is about 90
mA.
Note: R can control the brightness of the LED
I

LED

5 IR 2 0 V
5 2V
R
1 k
3 mA

25 mA
Maximum
2

PIC16F877A Electrical Specifications


Page 173 of PIC16F87XA Data Sheet:

25 mA

7 Segment Display Devices


Example: LTD-4608JR (dual seven-segment display)
Most Significant Digit
(MSD)

Least Significant Digit


(LSD)
Common Cathode

10

a
f

c
d

EXAMPLE CONNECTION OF
LTD-4608JR TO PIC16F877A

7-Segment Display
Test Circuit Diagram
VDD (5 V)
VSS (GND)

10 F

RD6
RD5
RD4
RD3

Programming Data
Programming Clock

40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
RD5
RD3
RD6
RD4
RD2

10 9

1 k

PIC16F877A

RJ-12

RE0
RE2
RD0
RE1
RD1
7 8 9 10 11 12 13 14 15 16 17 18 19 20

Master Clear

RD2
10 k

3.3 k

33 pF
1 k

VDD (5 V)
VSS (GND)
RE0

RE2
RE1

Port D and Port E Configuration


Port D configured as output to connect to the 7-Segment Display
outputs A, B, C, D, E, F, G, and DP.
Port E bit-0 (RE0) configured as output to connect to the 7-Segment
Display Most Significant Digit (MSD) driver
Port E bit 1 (RE1) configured as output to connect to the 7-Segment
Display Least Significant Digit (LSD) driver

C Code for Configuring Port D and Port E


TRISD = 0b00000000;
TRISE0 = 0;
TRISE1 = 0;
TRISE2 = 0;

//
//
//
//

Output
Output
Output
Output

for
for
for
for

7-segments
MSD Driver
LSD Driver
decimal point

The names TRISD, TRISE1, etc are the standard names for the
PIC16F877A

The name and type is declared in file pic16f877a.h


// Register: TRISD
volatile unsigned char TRISD @ 0x088;

7 Segment Decoder
#

A B G F D E C
RD6 RD5 RD4 RD3 RD2 RD1 RD0

10

A
F

G
E

C
D

0
1

1 0 0 1 0 0 0 0
1 1 0 1 1 1 1 0

2
3
4

1 0 0 0 1 0 0 1
1 0 0 0 1 0 1 0
1 1 0 0 0 1 1 0

5
6

1 0 1 0 0 0 1 0
1 0 1 0 0 0 0 0

7
8
9

1 0 0 1 1 1 1 0
1 0 0 0 0 0 0 0
1 0 0 0 0 0 1 0
9

C Code for 7 Segment Decoder Array


// Declaration for 7 segment decoder array
unsigned char sevenSegmentLookUp[10];

A B G F D E C
RD6 RD5 RD4 RD3 RD2 RD1 RD0

sevenSegmentLookUp[0] = 0b10010000; // '0'.

01 0 0 1 0 0 0 0

sevenSegmentLookUp[1] = 0b11011110; // '1'.

1
2
3
4
5
6
7
8
9

sevenSegmentLookUp[2] = 0b10001001; // '2'.


sevenSegmentLookUp[3] = 0b10001010; // '3'.
sevenSegmentLookUp[4] = 0b11000110; // '4'.
sevenSegmentLookUp[5] = 0b10100010; // '5'.
sevenSegmentLookUp[6] = 0b10100000; // '6'.

sevenSegmentLookUp[7] = 0b10011110; // '7'.


sevenSegmentLookUp[8] = 0b10000000; // '8'.
sevenSegmentLookUp[9] = 0b10000010; // '9'.

1
1
1
1
1
1
1
1
1

1
0
0
1
0
0
0
0
0

0
0
0
0
1
1
0
0
0

1
0
0
0
0
0
1
0
0

1
1
1
0
0
0
1
0
0

1
0
0
1
0
0
1
0
0

1
0
1
1
1
0
1
0
1

0
1
0
0
0
0
0
0
0
10

7-Segment Display
Test Plan

Send a counter value to the 7-segment display continuously


Increment the counter as follows 0, 1, , 99, 0, 1,
LEDs must be continuously driven for the light to show
Human eye cannot detect short changes
To display a 2-digit number:
MSD off LSD on
MSD on LSD off
MSD off LSD on

,
11

Main Loop Flow Chart


value = 0
MSD

get the LSD and MSD of value

LSD

00
01
01

09
10
11

98
99
00
01

Get sevenSegmentLookUp[LSD] and write to PORTD

MSD Driver off, LSD Driver on

Get sevenSegmentLookUp[MSD] and write to PORTD

MSD Driver on, LSD Driver off

value = 0

value
== 99
?

value = value + 1
12

C Code for the Main loop


value = 0;
while (1) {
// 1 = true, means loop forever
for(i = 0; i < 1000; i++) {// Display "value" for about 1s
// Set the 7-segment lines on PORTD to match LSD of value:
PORTD = sevenSegmentLookUp[value%10];
RE0 = 0;
// Turn off MSD
RE1 = 1;
// Turn on LSD
// Set the 7-segment lines on PORTD to match MSD of value
PORTD = sevenSegmentLookUp[value/10];
RE0 = 1;
// Turn on MSD
RE1 = 0;
// Turn off LSD
}
if(value == 99){
value = 0;
}
else {
value++;
}
} //end of while (1)
}
// end of main() function.
13

Modulo Operator
n mod m (C syntax is: n % m)
Given two positive numbers, n and m, n mod m
is the remainder that results after the division of n by m.
The division of n by m yields an integer part and a remainder part
Example: 37%3 = 1, because 373 = 12 with remainder 1
Special case: when m is the base of the number system, then n %
m will yield the value of the least significant digit
n % m = LSD(n), when m = base of number system
14

Integer Division Operator


n/m
Given two positive numbers, n and m, n / m is the integer that
results after the division of n by m.
The division of n by m yields an integer part and a remainder
part
Example: 37 / 3 = 12, because 373 = 12 remainder 1
Special case: when m is the base of the number system, then n / m
will remove the least significant digit

15

Example: Integer Division and Modulus Operators

7
7 % 10 = 7
7 / 10 = 0

50
50 % 10 = 0
50 / 10 = 5

34
34 % 10 = 4
34 / 10 = 3

99
99 % 10 = 9
99 / 10 = 9

16

Complete MPLAB Project


#include <xc.h>

// Insert file xc.h here. This file contains PIC register definitions

#pragma config BOREN = OFF, CPD = OFF, DEBUG = ON, WRT = OFF, FOSC = EXTRC, WDTE = OFF, CP = OFF, LVP = OFF, PWRTE = OFF
void main(void)
{
unsigned char value;
unsigned char sevenSegmentLookUp[10];
unsigned int i;
PCFG3 = 0;
PCFG2 = 1;
PCFG1 = 1;

// 7 segment decoder

// Configure all port pins as digital I/O.

TRISD = 0b00000000; // Configure PORTD as


TRISE2 = 0; // Configure PORTE2 as output
TRISE0 = 0; // Configure PORTE0 as output
TRISE1 = 0; // Configure PORTE1 as output
sevenSegmentLookUp[0]
sevenSegmentLookUp[1]
sevenSegmentLookUp[2]
sevenSegmentLookUp[3]
sevenSegmentLookUp[4]
sevenSegmentLookUp[5]
sevenSegmentLookUp[6]
sevenSegmentLookUp[7]
sevenSegmentLookUp[8]
sevenSegmentLookUp[9]

=
=
=
=
=
=
=
=
=
=

0b10010000;
0b11011110;
0b10001001;
0b10001010;
0b11000110;
0b10100010;
0b10100000;
0b10011110;
0b10000000;
0b10000010;

//
//
//
//
//
//
//
//
//
//

7
7
7
7
7
7
7
7
7
7

output for the A-F segments


for the decimal point (dp) segment
to drive the MSD
to drive the LSD
Segment
Segment
Segment
Segment
Segment
Segment
Segment
Segment
Segment
Segment

code
code
code
code
code
code
code
code
code
code

for
for
for
for
for
for
for
for
for
for

'0'
'1'
'2'
'3'
'4'
'5'
'6'
'7'
'8'
'9'

17

Complete MPLAB Project (Contd)


RE2 = 1;
// Switch decimal point off
value = 0;
while (1) {
// Loop forever
for(i = 0; i < 1000; i++) {// Display "value" for about 1s
// Set the 7-segment lines on PORTD to match LSD of value
PORTD = sevenSegmentLookUp[value%10];
RE0 = 0;
// Turn off MSD
RE1 = 1;
// Turn on LSD
// Set the 7-segment lines on PORTD to match MSD of value
PORTD = sevenSegmentLookUp[value/10];
RE0 = 1;
// Turn on MSD
RE1 = 0;
// Turn off LSD
}
if(value == 99){
value = 0;
}
else {
value++;
}
} //end of while (1)
}
// end of main
18

Project Modularization
A project can be made more modular
Easier to maintain
Easier to read and debug
Easier to manage
Encapsulate similar items into their own file
C files that do certain function and that function only
Include files (*.h) files that contain definitions (or code)

19

Example of a Modular MPLAB Project


Project split into 6 files:
MainFunctionPrototypes.h
ProcessorConfiguration.h
SevenSegmentDefinitions.h
Main.c
PortModule.c
SevenSegment.c

20

Main.c
#include "ProcessorConfiguration.h"
#include "MainFunctionPrototypes.h" //Needed b/c it calls 3 functions.
void main(void) {
unsigned char value;
unsigned int i;
portInit();
sevenSegmentInit();
value = 0;
while (1) { // Loop forever.
for (i = 0; i < 1000; i++) {// Display "value" for about 1 s
sevenSegmentDisplay(value);
}
if (value == 99) {
value = 0;
}
else {
value++;
}
} //end of while (1)
} // end of main.
21

Main.c Notes
At most one file in a project must contain the configuration bits
settings:
#include "ProcessorConfiguration.h
Must include the signature of each function main() calls: portInit(),
sevenSegmentInit(), and sevenSegmentDisplay():
#include "MainFunctionPrototypes.h

Notice that main() does not interact with the port or the seven
segment display device directly; it invokes functions within files
whose responsibility it is to interface with the port or the segment
device directly.
22

ProcessorConfiguration.h
#pragma config
BOREN = OFF, CPD = OFF, DEBUG = ON, WRT = OFF, FOSC =
EXTRC, WDTE = OFF, CP = OFF, LVP = OFF, PWRTE = OFF

23

MainFunctionPrototypes.h
extern void sevenSegmentDisplay(unsigned char);
extern void portInit(void);
extern void sevenSegmentInit();

24

MainFunctionPrototypes.h Notes
The file contains:
extern void sevenSegmentDisplay(unsigned char);
extern void portInit(void);
extern void sevenSegmentInit();
These define the signature of each function that main() calls.
For example, the signature of sevenSegmentInit() is that it
returns nothing (void) and it has an unsigned char
parameter.
The extern keyword tells the compiler that the item is defined in
an external file.
25

PortModule.c
#include <xc.h>
void portInit(void) {
PCFG3 = 0;
// Configure all Port pins for digital I/O.
PCFG2 = 1;
PCFG1 = 1;
TRISD = 0b00000000;
TRISE0 = 0;
TRISE1 = 0;
TRISE2 = 0;

//
//
//
//

Output for seven segments


MSD driver
LSD driver
Decimal point segment

26

PortModule.c Notes
The file xc.h contains the definitions of the variables
PortModule.cd uses: PCFG3, PCFG2, PCFG1, TRISD,
TRISE0, TRISE1, and TRISE2.
#include <xc.h>
void portInit(void) {
PCFG3 = 0;
// Configure all Port pins for digital I/O.
PCFG2 = 1;
PCFG1 = 1;
TRISD = 0b00000000;
TRISE0 = 0;
TRISE1 = 0;
TRISE2 = 0;

//
//
//
//

Output for seven segments


MSD driver
LSD driver
Decimal point segment

27

SevenSegment.c
#include <xc.h>
#include "SevenSegmentDefinitions.h"
unsigned char sevenSegmentLookUp[10]; // 7 segment decoder
void sevenSegmentInit() {
sevenSegmentLookUp[0]
sevenSegmentLookUp[1]
sevenSegmentLookUp[2]
sevenSegmentLookUp[3]
sevenSegmentLookUp[4]
sevenSegmentLookUp[5]
sevenSegmentLookUp[6]
sevenSegmentLookUp[7]
sevenSegmentLookUp[8]
sevenSegmentLookUp[9]

=
=
=
=
=
=
=
=
=
=

0b10010000;
0b11011110;
0b10001001;
0b10001010;
0b11000110;
0b10100010;
0b10100000;
0b10011110;
0b10000000;
0b10000010;

//
//
//
//
//
//
//
//
//
//

sevenSegment = sevenSegmentLookUp[0];
leftDigit = DIGIT_ON;
// Turn on
rightDigit = DIGIT_ON;
// Turn on
DP = DP_OFF;
// Switch decimal

7
7
7
7
7
7
7
7
7
7

Segment
Segment
Segment
Segment
Segment
Segment
Segment
Segment
Segment
Segment

code
code
code
code
code
code
code
code
code
code

for
for
for
for
for
for
for
for
for
for

'0'
'1'
'2'
'3'
'4'
'5'
'6'
'7'
'8'
'9'

// Set both digits to zero.


the MSD.
the LSD.
point off

}
28

SevenSegment.c (Continued)
void sevenSegmentDisplay(unsigned char value) {
sevenSegment = sevenSegmentLookUp[value%10]; //Display LSD.
leftDigit = DIGIT_OFF; // Turn off the MSD
rightDigit = DIGIT_ON; // Turn on the LSD
sevenSegment = sevenSegmentLookUp[value/10]; // Display MSD.
rightDigit = DIGIT_OFF; // Turn off the MSD
leftDigit = DIGIT_ON;
// Turn on the LSD
}

29

SevenSegment.c Notes
Note that although SevenSegment.c does not refer
to the names of the PIC registers directly, it still needs
to include the file xc.h, because the variables
leftDigit, rightDigit, and sevenSegment,
are defined as RE0, RE1, RE2, and PORTD in the
SevenSegmentDefineitions.h file:
#define leftDigit
RE0
#define rightDigit
RE1
#define sevenSegment PORTD
#define DP

RE2
30

SevenSegmentDefinitions.h
#define
#define
#define
#define
#define

DIGIT_ON
DIGIT_OFF
leftDigit
rightDigit
sevenSegment

#define DP
#define DP_ON
#define DP_OFF

1
0
RE0
RE1
PORTD

RE2
0
1

31

XC8 Integer Data types

32

You might also like