-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
234 lines (202 loc) · 4.11 KB
/
main.c
File metadata and controls
234 lines (202 loc) · 4.11 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* main.c
*
* Created: 5/11/2022 9:51:29 AM
* Author: Xiangsheng
*/
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "avr.h"
#include "lcd.h"
#include "avr.c"
#include "lcd.c"
/**************************************** Project 4: Volt-meter ****************************************/
// Display all 4 readouts
typedef struct
{
short InstantV;
short MaxV;
short MinV;
double AvgV;
} readouts;
// Sampling rate is at least 2 samples/second -> every 500ms
const int samplingRate = 500; //ms
// Computing Average: Sum and totalCount
unsigned long Sum = 0;
unsigned long totalCount = 0;
int is_pressed(int r, int c)
{
// Set all 8 GPIOs to N/C
DDRC = 0;
PORTC = 0;
// Set r to "0"
SET_BIT(DDRC, r);
CLR_BIT(PORTC, r);
// Set c to "w1"
CLR_BIT(DDRC, c + 4);
SET_BIT(PORTC, c + 4);
avr_wait(1);
if (GET_BIT(PINC, c + 4) == 0) //value of c == 0
{
return 1;
}
else
{
return 0;
}
}
int get_key()
{
int i, j;
for (i=0; i < 4; i++)
{
for (j=0; j < 4; j++)
{
if (is_pressed(i, j))
{
return 4 * i + j + 1;
}
}
}
return 0;
}
char int_to_char(int key)
{
// Transform integer to actual character of the key for the functionality purpose.
switch (key)
{
case 1:
return '0' + key;
case 2:
return '0' + key;
case 3:
return '0' + key;
case 4:
return 'A';
case 5:
return '0' + key - 1;
case 6:
return '0' + key - 1;
case 7:
return '0' + key - 1;
case 8:
return 'B';
case 9:
return '0' + key - 2;
case 10:
return '0' + key - 2;
case 11:
return '0' + key - 2;
case 12:
return 'C';
case 13:
return '*';
case 14:
return '0';
case 15:
return '#';
case 16:
return 'D';
}
return '$';
}
void print_Welcome()
{
for (int i=0; i < 10; i++)
{
avr_wait(200);
CLR_BIT(PORTB, 4);
avr_wait(200);
SET_BIT(PORTB, 4);
}
// LCD display
char topLine[17];
char bottomLine[17];
lcd_pos(0, 0);
sprintf(topLine, "---Welcome---");
lcd_puts2(topLine);
lcd_pos(1, 0);
sprintf(bottomLine, "Sys is loading");
lcd_puts2(bottomLine);
avr_wait(2000);
lcd_clr();
}
int get_sample()
{
// configure the ADC -> read from PA0
ADMUX = 0b01000000;
// start conversion -> single conversion
ADCSRA = 0b11000000;
// wait for conversion result: while(bit 6 of ADCSRA)
while (GET_BIT(ADCSRA, 6) == 1);
return ADC;
}
int main ()
{
SET_BIT(DDRB, 4);
SET_BIT(PORTB, 4);
avr_init();
lcd_init();
lcd_clr();
print_Welcome();
readouts showVolts;
// ADCSRA: ADC Control and Status Register
// SET_BIT(ADCSRA, 6);
// Initialize all 4 readouts
showVolts.InstantV = 0;
showVolts.MaxV = 0;
showVolts.MinV = 1024;
showVolts.AvgV = 0;
Sum = 0;
totalCount = 0;
while (1)
{
int key = get_key();
char actual_key = int_to_char(key);
switch(actual_key)
{
case 'B':
// Resets max\min\avg to blank -> how about instant volt?
lcd_clr();
showVolts.InstantV = 0;
showVolts.MaxV = 0;
showVolts.MinV = 1024;
Sum = 0;
totalCount = 0;
break;
}
//sampling here
avr_wait(samplingRate);
// Get Intsant
showVolts.InstantV = get_sample();
// Get Max
if(showVolts.InstantV > showVolts.MaxV)
{
showVolts.MaxV = showVolts.InstantV;
}
// Get Min
if(showVolts.InstantV < showVolts.MinV)
{
showVolts.MinV = showVolts.InstantV;
}
// For Calculating Avg
Sum += (showVolts.InstantV / 1023.0) * 5;
totalCount += 1;
showVolts.AvgV = (double)Sum / totalCount;
// Display Volts
// LCD display
char topLine[17];
char bottomLine[17];
// Converting from normalized to volts?
lcd_pos(0, 0);
sprintf(topLine, "%.2f V %.2f V", (showVolts.MaxV / 1023.0) * 5, (showVolts.MinV / 1023.0) * 5);
lcd_puts2(topLine);
lcd_pos(1, 0);
// Display the avg volt:
sprintf(bottomLine, "%.2f V %.2f V", (showVolts.InstantV / 1023.0) * 5, showVolts.AvgV);
lcd_puts2(bottomLine);
}
return 0;
}
/**************************************** Project 4 ****************************************/