7 Segment Display
7 Segment Display
LED
5 IR 2 0 V
5 2V
R
1 k
3 mA
25 mA
Maximum
2
25 mA
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
//
//
//
//
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
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
A B G F D E C
RD6 RD5 RD4 RD3 RD2 RD1 RD0
01 0 0 1 0 0 0 0
1
2
3
4
5
6
7
8
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
,
11
LSD
00
01
01
09
10
11
98
99
00
01
value = 0
value
== 99
?
value = value + 1
12
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
15
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
// 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
=
=
=
=
=
=
=
=
=
=
0b10010000;
0b11011110;
0b10001001;
0b10001010;
0b11000110;
0b10100010;
0b10100000;
0b10011110;
0b10000000;
0b10000010;
//
//
//
//
//
//
//
//
//
//
7
7
7
7
7
7
7
7
7
7
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
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
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;
//
//
//
//
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;
//
//
//
//
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'
}
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
32