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

ECE4721 Lab3 YashGandham

The lab report summarizes an experiment connecting a 7-segment LED display to a KL25Z microcontroller board using switches to control the display. The objective was to display numbers using 4 switches. The hardware and code setup are described. The code initializes the ports, defines macros for the switches and LED pins, and uses if/else statements to light different LED segments depending on the switch states, displaying the numbers 0-9.
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)
33 views

ECE4721 Lab3 YashGandham

The lab report summarizes an experiment connecting a 7-segment LED display to a KL25Z microcontroller board using switches to control the display. The objective was to display numbers using 4 switches. The hardware and code setup are described. The code initializes the ports, defines macros for the switches and LED pins, and uses if/else statements to light different LED segments depending on the switch states, displaying the numbers 0-9.
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/ 8

Oakland University

School of Electrical & Computer Engineering


Winter 2023

Lab Report Three

Yash Gandham
3/17/2023
/*
Programmer: Yash Gandham
Date:3/17/2023
Class: ECE4721 Winter 13028-2023

1
Part 1:
Objective: The goal of the lab is to connect the 7-segment LED to the KL25Z
board and display the numbers using 4 switches.
Hardware used: Resistor, SPST Switches, KL25Z Board, Breadboard, Wires, 7
Segment LED.
Lab Setup:
1. Connect the resistor, Swithces, and 7-Segment LED to the breadboard as
shown in Figure 2.14
2. Connect from port A, B, C, D, E on board to resistor one using a wire
3. Using a wire connect a GND port on KL25Z board and breadboard ground
4. Build, debug, and run code
Code:

1 #include <MKL25Z4.h>
2
3 #define SW_1 (0) //on port B
4 #define SW_2 (1) //on port B
5 #define SW_3 (2) //on port B
6 #define SW_4 (3) //on port B
7
8 #define LED_A (1) //on port A
9 #define LED_B (2) //on port A
10 #define LED_C (4) //on port A
11 #define LED_D (5) //on port A
12 #define LED_E (12) //on port A
13 #define LED_F (13) //on port A
14 #define LED_G (16) //on port A
15
16 #define MASK(x) (1UL << (x))

2
17
18 void Init_7Segment(void);
19 void control_LEDs(int A_on, int B_on, int C_on, int D_on, int E_on, int F_on, int G_on);
20 void Segment(void);
21
22
23 void Init_7Segment(void){
24
25 //Enable Clock to port A and B
26 SIM_SCGC5 |= (SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK);
27
28 PORTA->PCR[LED_A] &= ~PORT_PCR_MUX_MASK;
29 PORTA->PCR[LED_A] |= PORT_PCR_MUX(1);
30 PORTA->PCR[LED_B] &= ~PORT_PCR_MUX_MASK;
31 PORTA->PCR[LED_B] |= PORT_PCR_MUX(1);
32 PORTA->PCR[LED_C] &= ~PORT_PCR_MUX_MASK;
33 PORTA->PCR[LED_C] |= PORT_PCR_MUX(1);
34 PORTA->PCR[LED_D] &= ~PORT_PCR_MUX_MASK;
35 PORTA->PCR[LED_D] |= PORT_PCR_MUX(1);
36 PORTA->PCR[LED_E] &= ~PORT_PCR_MUX_MASK;
37 PORTA->PCR[LED_E] |= PORT_PCR_MUX(1);
38 PORTA->PCR[LED_F] &= ~PORT_PCR_MUX_MASK;
39 PORTA->PCR[LED_F] |= PORT_PCR_MUX(1);
40 PORTA->PCR[LED_G] &= ~PORT_PCR_MUX_MASK;
41 PORTA->PCR[LED_G] |= PORT_PCR_MUX(1);
42 PORTB->PCR[SW_1] &= ~PORT_PCR_MUX_MASK;
43 PORTB->PCR[SW_1] |= PORT_PCR_MUX(1);
44 PORTB->PCR[SW_2] &= ~PORT_PCR_MUX_MASK;

3
45 PORTB->PCR[SW_2] |= PORT_PCR_MUX(1);
46 PORTB->PCR[SW_3] &= ~PORT_PCR_MUX_MASK;
47 PORTB->PCR[SW_3] |= PORT_PCR_MUX(1);
48 PORTB->PCR[SW_4] &= ~PORT_PCR_MUX_MASK;
49 PORTB->PCR[SW_4] |= PORT_PCR_MUX(1);
50
51 //Set LED bits to outputs
52 PTA->PDDR |= MASK(LED_A) | MASK(LED_B) | MASK(LED_C) | MASK(LED_D)
| MASK(LED_E) | MASK(LED_F)| MASK(LED_G);
53 //clear the bit to input
54 PTB->PDDR &= ~MASK(SW_1);
55 PTB->PDDR &= ~MASK(SW_2);
56 PTB->PDDR &= ~MASK(SW_3);
57 PTB->PDDR &= ~MASK(SW_4);
58
59 //Turn off all LED's off initially
60 PTA->PCOR=MASK(LED_A);
61 PTA->PCOR=MASK(LED_B);
62 PTA->PCOR=MASK(LED_C);
63 PTA->PCOR=MASK(LED_D);
64 PTA->PCOR=MASK(LED_E);
65 PTA->PCOR=MASK(LED_F);
66 PTA->PCOR=MASK(LED_G);
67
68 }
69 void control_LEDs(int A_on, int B_on, int C_on, int D_on, int E_on, int F_on, int G_on){
70 if (A_on)
71 PTA->PCOR = MASK(LED_A);
72 else

4
73 PTA->PSOR = MASK(LED_A);
74 if (B_on)
75 PTA->PCOR = MASK(LED_B);
76 else
77 PTA->PSOR = MASK(LED_B);
78 if (C_on)
79 PTA->PCOR = MASK(LED_C);
80 else
81 PTA->PSOR = MASK(LED_C);
82 if (D_on)
83 PTA->PCOR = MASK(LED_D);
84 else
85 PTA->PSOR = MASK(LED_D);
86 if (E_on)
87 PTA->PCOR = MASK(LED_E);
88 else
89 PTA->PSOR = MASK(LED_E);
90 if (F_on)
91 PTA->PCOR = MASK(LED_F);
92 else
93 PTA->PSOR = MASK(LED_F);
94 if (G_on)
95 PTA->PCOR = MASK(LED_G);
96 else
97 PTA->PSOR = MASK(LED_G);
98
99 }
100 void Segment(void){

5
101
102 if((!(PTB->PDIR & MASK(SW_4))) && (!(PTB->PDIR & MASK(SW_3)))&&
(!(PTB->PDIR & MASK(SW_2)))&& (PTB->PDIR & MASK(SW_1))){
103 control_LEDs(0,1,1,0,0,0,0);
104 }
105
106 else if((!(PTB->PDIR & MASK(SW_4)))&& (!(PTB->PDIR &
MASK(SW_3)))&& (PTB->PDIR & MASK(SW_2))&& (!(PTB->PDIR & MASK(SW_1)))){
107 control_LEDs(1,1,0,1,1,0,1);
108 }
109
110 else if((!(PTB->PDIR & MASK(SW_4)))&& (!(PTB->PDIR &
MASK(SW_3)))&&(PTB->PDIR & MASK(SW_2))&&(PTB->PDIR & MASK(SW_1))){
111 control_LEDs(1,1,1,1,0,0,1);
112 }
113
114 else if((!(PTB->PDIR & MASK(SW_4)))&& ((PTB->PDIR &
MASK(SW_3)))&& (!(PTB->PDIR & MASK(SW_2)))&& (!(PTB->PDIR &
MASK(SW_1)))){
115 control_LEDs(0,1,1,0,0,1,1);
116 }
117
118 else if((!(PTB->PDIR & MASK(SW_4)))&& ((PTB->PDIR &
MASK(SW_3)))&& (!(PTB->PDIR & MASK(SW_2)))&& ((PTB->PDIR & MASK(SW_1)))){
119 control_LEDs(1,0,1,1,0,1,1);
120 }
121
122 else if((!(PTB->PDIR & MASK(SW_4)))&& ((PTB->PDIR &
MASK(SW_3)))&& ((PTB->PDIR & MASK(SW_2)))&& (!(PTB->PDIR & MASK(SW_1)))){
123 control_LEDs(1,0,1,1,1,1,1);
124 }

6
125
126 else if((!(PTB->PDIR & MASK(SW_4)))&& ((PTB->PDIR &
MASK(SW_3)))&& (PTB->PDIR & MASK(SW_2))&& (PTB->PDIR & MASK(SW_1))){
127 control_LEDs(1,1,1,0,0,0,0);
128 }
129
130 else if((PTB->PDIR & MASK(SW_4))&& (!(PTB->PDIR &
MASK(SW_3)))&& (!(PTB->PDIR & MASK(SW_2)))&& (!(PTB->PDIR &
MASK(SW_1)))){
131 control_LEDs(1,1,1,1,1,1,1);
132 }
133
134 else if((PTB->PDIR & MASK(SW_4))&& (!(PTB->PDIR &
MASK(SW_3)))&&(!(PTB->PDIR & MASK(SW_2)))&&(PTB->PDIR & MASK(SW_1))){
135 control_LEDs(1,1,1,1,0,1,1);
136 }
137
138 else if((!(PTB->PDIR & MASK(SW_4)))&&(!(PTB->PDIR &
MASK(SW_3)))&&(!(PTB->PDIR & MASK(SW_2)))&&(!(PTB->PDIR & MASK(SW_1)))){
139 control_LEDs(1,1,1,1,1,1,0);
140 }
141 }
142
143 int main(void){
144 Init_7Segment();
145 while(1){
146 Segment();
147 }
148 }
149

7
Flow Chart:

You might also like