Lab 3 - Interfacing To 7-Segment Displays and Buzzer Objectives
Lab 3 - Interfacing To 7-Segment Displays and Buzzer Objectives
Lab 3 - Interfacing To 7-Segment Displays and Buzzer Objectives
__________________________________________________________________
Objectives
Introduction / Briefing
Considering only one 7-segment display (shown below) and answer the
following questions:
5V
7-segment
PIC18F4550 display A
COMMON
RD0
A B
F
G
RD1 B
RD2 C E C
Digital
output dp
D
port
RD6 G
Common anode
Cathodes
Q1: Are the LED’s in the 7-segment display connected in the “common anode”
mode or the “common cathode” mode? ____________________
Q2: What must RD0 produce (logic ‘0’ or logic ‘1’) to turn on segment A?
__________
Q3: What must PORT D produce (in binary format) to show the digit “1” on
the 7-segment display? PORTD = 0b .
Q4: Of course, PORT D must be configured as an output port. Give the 2-line
C command to configure PORT D as a digital output port and to show the
digit “5” on the 7-segment display:
RB0
RB1
RB2
RD2 C
RD6 G
RD7 dp
Common anode
PIC18F4550 Cathodes
A A A A
F B F B F B F B
G G G G
E C E C E C E C
dp dp dp dp
D D D D
PORT D = 0b ________
PORTBbits.RB3 = _____
PORTBbits.RB2 = _____
PORTBbits.RB1 = _____
PORTBbits.RB0 = _____
while (1)
{
PORTB = 0b00000001; // enable DIG0
PORTD = 0b00111111; // display 0
// Some delay
Your answer:_____________________________________________
_______________________________________________________
Your answer:
_______________________________________________
You will find out the answer to the two previous questions in the
experiment.
Buzzer at Port C
In this experiment, you will also be turning on and off a buzzer connected
to PORT C to produce a “tone”.
By toggling ( on -> off -> on -> off …. ) RC0 continuously, a tone can be
produced by the buzzer.
Activites:
Before you begin, ensure that the Micro-controller Board is connected to the
General I/O Board. The General I/O Board is further connected to a 7-
Segment/Switch Board.
PORTD – 8 segments (‘a’ to ‘g’, and decimal point) of all 4 digits, active high (‘1’
turns on a segment, ‘0’ turns off a segment).
PORTB – RB0 to RB3 – COM pins of all 4 digits (DIG0 to DIG3), active high (‘1’
enables digit, ‘0’ disables digit).
PORTB – RB5 – push button switch, active low (pressed gives ‘0’, released gives
‘1’).
So PORTD controls the number e.g. ‘8’ to be displayed on a digit, while PORTB
controls which digit displays the number.
1. Launch the MPLABX IDE and create a new project called Lab3.
Display 2. Add the file Single7Seg.c to the Lab3 project Source File folder.
“0” on
DIG0. Make sure Copy is ticked before you click Select. If you have forgotten
the steps, you will need to refer to the previous lab sheet.
3. Study the code and describe what this program will do:
__________________________________________________
4. Build, download and execute the program. Observe the result and see if it
is as expected.
Display
“1” on
DIG1. 5. Modify the code to display the digit “1” on the next 7-segment i.e. DIG1.
Build, download and execute the program to verify your coding.
Answer: _____________________________________________
8. Study the code and describe what this program will do:
__________________________________________________
9. Build, download and execute the program. Observe the result and see if it
is as expected.
Increase
delay 10. Increase the delay between digits. What do you observe?
__________________________________________________
Decrease
delay 11. Decrease the delay between digits. What do you observe?
__________________________________________________
12. As can be seen, multiplexing technique here involves turning on only one
digit of display at a time, and after a short delay, move on to the next
digit etc:
The delay is to give time for the LED’s to light up and the number to be
seen. Too long a delay will cause the numbers to flicker and too short and
the display will become blur, as the LED’s do not have time to turn on
properly and be seen.
13. You may try to display today’s date as DDMM and show it to your
classmates.
(Do this only if you still have time. Otherwise, skip to the next section to
try out the “buzzer”.)
14.
Q-no.
Replace Four7Seg.c with Count7SegSw.c.
system
15. Study the code and describe what this program will do:
__________________________________________________
17. The decimal numbers to display on the four 7-segments are stored in an
array of 4 unsigned chars
unsigned char val[4]; // i.e. val [0], val [1], val [2], val [3]
These are initialised to val [3] = 9; val [2] = 8; val [1] = 7; val [0] = 6; in
the main program. So, the initial display should be “9 8 7 6”.
18. 9876 are what you want to see. However, what the 7-segments want to
be told are the binary patterns 0b01101111 [9], 0b01111111 [8],
0b00000111 [7], 0b01111101 [6].
19. The function convert (in the seg7_utilities) produces the binary pattern
required to show a decimal number on a 7-segment. E.g. if decimal number
(“digit”) = “0”, binary pattern (“leddata”) = 0b00111111.
20. As you know by now, multiplexing technique is used to enable each of the
4 digits in turn, so that the number of PIC pins required to display 4
digits (including the decimal points) is fewer than 4 x 8.
21. Here an unsigned char variable point is used to control which digit is
lighting up.
23. Putting all these ideas together, you get the following chunk of codes:
point = point << 1; // shift left by 1 bit, to enable the next DIG,
// so 0b00000001 becomes 0b00000010, then 0b00000100,
// then 0b00001000
… // some delay
}
24. Whenever the switch connected to RB5 is pressed, the 4-digit display is
incremented by 1. This is done by the following lines of code and the
function update:
25. A micro-controller can work very fast. When a switch is pressed “one
time”, a micro-controller could have read it several times, and increment
the display several times, as shown below:
switch
PIC
checking
switch
status
26. To solve this problem, a variable press is used to control the flow of the
program:
…
press = 0; // initially… in the “switch not pressed” state
while (1)
{
… // display the decimal numbers
Display is only
if (press == 0) // starting from the “switch not pressed” state incremented the
{
first time the
if (PORTBbits.RB5 == 0) // if switch pressed
switched is found
{
press = 1; // change to the “switch pressed” state
to be pressed.
… // increment the 4-digit number to display
}
}
Subsequently,
if (PORTBbits.RB5 == 1) // switch released only check for
press = 0; // change to the “switch not pressed” state, ready for next round the release of
} the switch.
27. The best way to check whether you have understood this program is to
try to explain it to a classmate.
28. Once you have understood it, build, download and execute the program.
Observe the result and see if it is as expected.
29. Describe how you can use this in a Q-number system. What else do you
need?
a.) display
b.) UP button
c.) ticket
Your Q-number is
printer +
3208 “counter” +
user button
31. Study the code and describe what this program will do:
__________________________________________________
32. Note that in Buzzone.c (under the function onetone), the variable is used:
k.
The “for” loop i.e. k determines the duration of the buzzing, while the
delay_us() determines the pitch of the buzzing.
33. Build, download and execute the program. Observe the result and see if it
is as expected.
Different
tone
34. Modify the program by adding another function named twotone with a
different value in delay_us(value). Include twotone in the main program
and test out the sound effect, as follows:
while (1)
{
Onetone ();
PORTD = 0b10101010; // pattern on LEDs
delay_ms(500);
Twotone ();
PORTD = 0b01010101; // another pattern on LEDs
delay_ms(500);
// Single7Seg.c
// Program to test 1 7-segment display
#include <xc.h>
void main(void)
{
ADCON1 = 0x0F;
while(1) //repeat
{
}
}
#include <xc.h>
#include "delays.h"
void main(void)
{
while(1) //repeat
{
}
}
// Count7SegSw.c
// Counting on 4 7-segment display by a switch on 7-seg Board
#include <xc.h>
#include "delays.h"
#include "seg7.h"
void main(void) {
char i;
TRISB = 0b11110000; //RB3 to RB0 are connected DIG3 to DIG0
//RB5 is connected to a switch
}
}
/*
* File: seg7_utilities.c
*
* Created on 14 January, 2016, 7:59 PM
*/
#include <xc.h>
if(digit==0)leddata=0b00111111;
if(digit==1)leddata=0b00000110;
if(digit==2)leddata=0b01011011;
if(digit==3)leddata=0b01001111;
if(digit==4)leddata=0b01100110;
if(digit==5)leddata=0b01101101;
if(digit==6)leddata=0b01111101;
if(digit==7)leddata=0b00000111;
if(digit==8)leddata=0b01111111;
if(digit==9)leddata=0b01101111;
return(leddata);
}
if(val[1]>=10)
{
val[2]=val[2]+1;
val[1]=0;
}
if(val[2]>=10)
{
val[3]=val[3]+1;
val[2]=0;
}
if(val[3]>=10)
{
val[3]=0;
}
}
// BuzzOne.c
// Program to activate buzzer with one tone
// For project using USB interface with Bootloader
#include <xc.h>
#include "delays.h"
void main(void) {
{
onetone(); //sound ON then OFF
PORTD = 0b10101010; //pattern on LEDs
delay_ms(500);