-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab3_sobreA_1_1.c
216 lines (178 loc) · 6.45 KB
/
lab3_sobreA_1_1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Main.c file generated by New Project wizard
*
* Created: Sat Mar 12 2022
* Processor: PIC18F45K22
* Compiler: MPLAB XC8
*/
#include <xc.h>
#include <stdbool.h>
#include "config.h"
#define LOGIC_HIGH 1
#define LOGIC_LOW 0
#define _XTAL_FREQ 8000000
#define DIS0 PORTAbits.RA0
#define DIS1 PORTAbits.RA1
#define DIS2 PORTAbits.RA2
#define DIS3 PORTAbits.RA3
enum status {RUNNING, STOPPED};
const char NUMBERS[] = {0b00111111, 0b00000110,
0b01011011, 0b01001111,
0b01100110, 0b01101101,
0b01111101, 0b00000111,
0b01111111, 0b01101111};
//Print number to 7SEG displays
//pointPos > 0
void imprimir_segments(int number, bool is_decimal, char pointPos)
{
for (int currDisplay = 0; currDisplay < 4; ++currDisplay)
{
int value = number % 10;
//If decimal NUMBERS[] or 0b10000000 (8th bit is dp bit)
//Otherwise just print value loaded from array
if (is_decimal)
{
if (currDisplay == pointPos) LATD = NUMBERS[value] | 0x80;
else LATD = NUMBERS[value];
}
else LATD = NUMBERS[value];
switch (currDisplay)
{
case 0: DIS0 = LOGIC_HIGH; break;
case 1: DIS1 = LOGIC_HIGH; break;
case 2: DIS2 = LOGIC_HIGH; break;
case 3: DIS3 = LOGIC_HIGH; break;
}
__delay_us(50);
DIS0 = DIS1 = DIS2 = DIS3 = 0x0;
number = number / 10;
}
}
//Muestra digito "digit" por la pantalla
//"display" del set de pantallas de 7-Segmentos
void displayDigit(char display, int digit)
{
//If decimal NUMBERS[] or 0b10000000 (8th bit is dp bit)
//Otherwise just print value loaded from array
LATD = NUMBERS[digit];
switch (display)
{
case 0: DIS0 = LOGIC_HIGH; break;
case 1: DIS1 = LOGIC_HIGH; break;
case 2: DIS2 = LOGIC_HIGH; break;
case 3: DIS3 = LOGIC_HIGH; break;
}
__delay_us(50);
DIS0 = DIS1 = DIS2 = DIS3 = 0x0;
}
//Muestra cuatro cifras de menor peso
//de un número decimal por pantalla
//de 7-Segmentos
void displayNumber(int number)
{
for (int currDisplay = 0; currDisplay < 4; ++currDisplay)
{
int value = number % 10;
displayDigit(currDisplay, value);
number = number / 10;
}
}
//increase contador according to pin_number value
void addPORTB(int *counter, char pin_number)
{
switch(pin_number)
{
case 0: *counter = *counter + 1; break;
case 1: *counter = *counter + 10; break;
case 2: *counter = *counter + 100; break;
case 3: *counter = *counter + 1000; break;
}
}
// setup PORTs for I/O
void config_PIC(void)
{
ANSELA = 0b00000000; // Set pins for digital I/O
ANSELB = 0b00000000; // Set pins for digital I/O
ANSELC = 0b00000000; // Set pins for digital I/O
ANSELD = 0b00000000; // Set pins for digital I/O
ANSELE = 0b00000000; // Set pins for digital I/O
TRISBbits.RB0 = LOGIC_HIGH; // Set pin as input
TRISBbits.RB1 = LOGIC_HIGH; // Set pin as input
TRISBbits.RB2 = LOGIC_HIGH; // Set pin as input
TRISBbits.RB3 = LOGIC_HIGH; // Set pin as input
TRISCbits.RC0 = LOGIC_LOW; // Set pin as output
TRISCbits.RC1 = LOGIC_LOW; // Set pin as output
TRISAbits.RA0 = LOGIC_LOW; // Set pin as output
TRISAbits.RA1 = LOGIC_LOW; // Set pin as output
TRISAbits.RA2 = LOGIC_LOW; // Set pin as output
TRISAbits.RA3 = LOGIC_LOW; // Set pin as output
TRISEbits.RE0 = LOGIC_HIGH; // Set pin as input
TRISEbits.RE1 = LOGIC_HIGH; // Set pin as input
TRISEbits.RE2 = LOGIC_HIGH; // Set pin as input
TRISD = 0x00; // Set all pins from PORTD as output
LATD = 0x00;
LATA = 0x00; //7SEG displays are turned off at start
}
//setup interrupts for PIC18
void setupInterruptions(void)
{
}
//DISPLAY binary 1 if players wins the game
int winner() { return 0b00000001; }
//DISPLAY binary 2 if players loses the game
int loser() { return 0b00000010; }
//start game
void game_start(int *_game_stat) {*_game_stat = RUNNING;}
//stop game
void game_stop(int *_game_stat) {*_game_stat = STOPPED;}
void main(void)
{
config_PIC();
setupInterruptions();
bool is_pressed = false;
int contador = 0;
int random_number = 0;
int GAME_STATUS = STOPPED;
// Write your code here
while (1)
{
//game entry point
if (PORTEbits.RE0)
{
random_number = rand() % 10000;
//busy wait until user press RE1
//input button to start the game
while (!PORTEbits.RE1 && GAME_STATUS == STOPPED) displayNumber(random_number);
contador = 0;
game_start(&GAME_STATUS);
bool RB0, RB1, RB2, RB3;
RB0 = RB1 = RB2 = RB3 = false;
bool can_stop = false;
bool RE2_down = false;
while (GAME_STATUS == RUNNING)
{
//Increase counter by 1 on RB0 falling edge
if (PORTBbits.RB0) RB0 = true;
else if (!PORTBbits.RB0 && RB0) {RB0=false; addPORTB(&contador, 0);}
//Increase counter by 10 on RB1 falling edge
if (PORTBbits.RB1) RB1 = true;
else if (!PORTBbits.RB1 && RB1) {RB1=false; addPORTB(&contador, 1);}
//Increase counter by 100 on RB2 falling edge
if (PORTBbits.RB2) RB2 = true;
else if (!PORTBbits.RB2 && RB2) {RB2=false; addPORTB(&contador, 2);}
//Increase counter by 1000 on RB3 falling edge
if (PORTBbits.RB3) RB3 = true;
else if (!PORTBbits.RB3 && RB3) {RB3=false; addPORTB(&contador, 3);}
if (PORTEbits.RE2) RE2_down = true;
else if (!PORTEbits.RE2 && RE2_down) can_stop = true;
if (can_stop && !PORTEbits.RE2) {game_stop(&GAME_STATUS);}
displayNumber(contador);
}
//check if player won
PORTC = (contador == random_number) ? winner() : loser();
//Display correct number
for (int i = 0; i < 300; ++i) displayNumber(random_number);
}
PORTD = 0;
DIS0 = DIS1 = DIS2 = DIS3 = 0x0;
}
}