Lab 2 - Interfacing To Switches and LED's Objectives
Lab 2 - Interfacing To Switches and LED's Objectives
__________________________________________________________________
Objectives
Introduction / Briefing
Switches at Port A
In this experiment, you will be reading the status of the dip switches
connected to Port A.
Below are the ways in which the switches are named and used in software.
Switch at RA3 has the name PORTAbits.RA3
Switch at RA4 has the name PORTAbits.RA4
Switch at RA5 has the name PORTAbits.RA5
PORTA on its own refers to all 8 bits and not individual bits.
_______________________________________________________
(The above is a tough question. Hint: imagine someone making the mistake
of configuring Port A as output AND producing a logic ‘1’ at one of
RA3:RA5 AND the corresponding dip switch is closed.)
Q4: Are the switches connected in the “active high” or “active low” manner?
(An “active high” switch gives a logic ‘1’ when closed.)
____________________
Q5: What will a Port A pin read (logic ‘1’ or ‘0’) when a corresponding dip
switch is closed? __________
[ Refer to the “insert” on the next page to understand the last column. ]
Q6: Give the C-command to configure Port A as a digital input port (hint:
ADCON1):
When a dip switch is closed, the corresponding Port A pin will read a logic
‘0’.
Q7: Give the C-command to check if the dip switch connected to RA3 is
closed (hint: PORTA):
In this experiment, you will also be turning on and off an LED bar
connected to Port D.
Q8: How many LED’s are there in the LED bar? __________
Q10: What is the purpose of the 470 ohm resistors in the SIP (Single-In-Line
package)? ______________________________
Q11: Are the LED’s connected in the “common anode” or “common cathode”
manner? ____________________
Q12: What must a Port D pin produce (logic ‘1’ or ‘0’) to turn on a corresponding
LED? __________
Q13: Give the C-command to configure Port D as a digital output port (hint:
TRISD):
Q14: Give the C-command to turn on the LED’s as follows (hint: PORTD):
ON
Activities:
1. Launch the MPLABX IDE and create a new project called Lab2.
Below is a summary of the steps needed or you can refer to Lab1.
6d. Codeoffset
1000
2. Use “Add Existing Item” to add the file OnOffLeds.c to the Lab2
project Source File folder, as follows. Make sure Copy is ticked
before you click Select.
// OnOffLeds.c
// Program to use switches to control 8 leds on General I/O Board
#include <xc.h>
void main(void) {
ADCON1 = 0x0F; //make Port A digital
__________________________________________________
__________________________________________________
5. Build, download and execute the program. Observe the result and see if it
is as expected.
6. Change the program to use the switch at RA5, such that if RA5 is on, all
the LEDs should be on and if RA5 is off, all the LEDs should be off.
7. Build, download and execute the program. Observe the result and see if it
is as expected.
9. Study the code and describe what this program will do:
__________________________________________________
10. Note that the program uses the delay function delay_ms() and contains
#include “delays.h”. The file delays.h needs to be added to Header Files
while the file delays_utilities.c needs to be added to Source Files.
11. Build, download and execute the program. Observe the result and see if it
is as expected.
Pause an
action 12. Add the following line to the while(1) loop:
while(1)
{
while (PORTAbits.RA3 == 0); // loop here when switch is on
PORTD=0b10101010;
……. // other existing lines – don’t touch
}
__________________________________________________
14. Build, download and execute the program. Observe the result and see if it
is as expected.
Right to
left
“scan”
15. Modify the program to do “scanning”, such that one LED light repeatedly
moves from right to left (after a short delay).
Pause the
“scan” 16. Add in a switch control line such that when the switch connected to RA3
is closed, the “scanning” is paused.
2-way
“scan” 18. Modify the program to do a “right to left scan”, followed by a “left to
right scan” repeatedly. Include the switch control line to pause the
scanning with a closed switch at RA3.
20.
Slow down
the “scan”
Modify the program such that a closed switch at RA4 slows down the
scanning (while a closed switch at RA3 pauses the scanning). (Hint: add
delay if switch at RA4 is closed.)
LED’s “counting”
Counting
22. Replace BlinkLeds.c with CountLeds.c.
23. Study the code and describe what this program will do:
__________________________________________________
24. Build, download and execute the program. Observe the result and see if it
is as expected.
Counting
Up/down 25. Modify the program such that a closed switch at RA5 causes counting up
while an opened switch causes counting down. Here are some hints:
while (1)
{
….. // other lines unchanged
if (PORTAbits.RA5 == 0) // if switch is closed
____________________ // count up
else
____________________ // count down
}
Slow down
the
counting
26. Add in a line such that a closed switch at RA4 slows down the counting.
27. Add in another line such that a closed switch at RA3 pauses the counting.
Pause the
counting
Extra Exercise
// OnOffLeds.c
// Program to use 1 switch to control 8 leds on General I/O Board
#include <xc.h>
void main(void) {
ADCON1 = 0x0F; //make Port A digital
// BlinkLeds.c
// Program to light up alternate leds on General I/O Board
#include <xc.h>
#include "delays.h"
void main(void) {
ADCON1 = 0x0F; //make Port A digital
PORTD = 0b01010101;
delay_ms(1000);
}
}
// CountLeds.c
// Program to make 8 leds on General I/O Board do binary up counting
#include <xc.h>
#include "delays.h"
void main(void)
{
ADCON1 = 0x0F; //make Port A digital
j=0; //beginning
while(1) //repeat
{
delay_ms(500);
j++; // Increment j
}
}