-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2.c
More file actions
165 lines (139 loc) · 5.89 KB
/
Lab2.c
File metadata and controls
165 lines (139 loc) · 5.89 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
// ECE231 Lab Assignment #2
//Joshua Silva
//33435438
// Using temperature sensor displays the temperature on LED Matrix, uses a button to switch between displaying Fahrenheit and Celsius
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#define PERSISTENCE 5
#define COUNTTIME 200 // # time between counts (ms)
#define Vref 1.1
void uart_init(void);
void uart_send(char letter);
void send_string(char *stringAddress);
void adc_init(void);
unsigned int get_adc(void);
int main(void){
unsigned char led24digits[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D,0x07, 0x7F, 0x67};
unsigned char led3digits[] = {0xBF, 0x86, 0xDB, 0xCF, 0xE6, 0xED, 0xFD,0x87, 0xFF, 0xE7};
unsigned char units[] = {0x39, 0x71};
unsigned char DIG1, DIG2, DIG3;
DDRC = 0x0F; // display segments a-d use PC0-PC3
DDRD = 0xF0; // display segments e-g & dp use PD4-7
PORTD |= 1<<PORTD2; // Set pull-up on PD2
DDRB = 0xFF; // Digit enable pins
unsigned int Vout;
double temperatureC, temperatureF;
adc_init();
uart_init();
while (1) {
Vout = get_adc();
temperatureC = Vout*Vref/10.24 - 50; // temperature in Celsius
temperatureF = temperatureC*1.8 + 32; // temperature in Fahrenheit
if ((PIND & (1<<PIND2)) == 0){
DIG1 = temperatureC/10; // 10's digit
DIG2 = (int)temperatureC%10; // 1's digit
DIG3 = (temperatureC - (DIG1*10) - DIG2)*10; // 0.1's digit
int j;
for (j=0; j<COUNTTIME/PERSISTENCE/4; j++){
send_string("Temperature in degrees of Celsius: ");
// 10's digit
PORTC = led24digits[DIG1];
PORTD = led24digits[DIG1];
uart_send(DIG1+'0'); // Tx 10's digit
PORTB = ~ (1<<4); // Enable DIG4
_delay_ms(PERSISTENCE);
// 1's digit
PORTC = led3digits[DIG2];
PORTD = led3digits[DIG2];
uart_send(DIG2+'0'); // Tx 1's digit
uart_send('.');
PORTB = ~ (1<<3); // DIG3
_delay_ms(PERSISTENCE);
// 0.1's digit
PORTC = led24digits[DIG3];
PORTD = led24digits[DIG3];
uart_send(DIG3+'0'); // Tx 0.1's digit
PORTB = ~ (1<<2); // DIG2
_delay_ms(PERSISTENCE);
// Display units
PORTC = units[0];
PORTD = units[0];
uart_send('C');
PORTB = ~ (1<<1); // DIG1
_delay_ms(PERSISTENCE);
PORTB = 0xFF; // Disable digits
uart_send(13); // Tx carriage return
uart_send(10); // Tx line feed
}
}else {
DIG1 = temperatureF/10; // 10's digit
DIG2 = (int)temperatureF%10; // 1's digit
DIG3 = (temperatureF - (DIG1*10) - DIG2)*10; // 0.1's digit
int j;
for (j=0; j<COUNTTIME/PERSISTENCE/4; j++){
send_string("Temperature in degrees of Fahrenheit: ");
// 10's digit
PORTC = led24digits[DIG1];
PORTD = led24digits[DIG1];
uart_send(DIG1+'0'); // Tx 10's digit
PORTB = ~ (1<<4); // DIG4
_delay_ms(PERSISTENCE);
// 1's digit
PORTC = led3digits[DIG2];
PORTD = led3digits[DIG2];
uart_send(DIG2+'0'); // Tx 1's digit
uart_send('.');
PORTB = ~ (1<<3); // DIG3
_delay_ms(PERSISTENCE);
// 0.1's digit
PORTC = led24digits[DIG3];
PORTD = led24digits[DIG3];
uart_send(DIG3+'0'); // Tx 0.1's digit
PORTB = ~ (1<<2); // DIG2
_delay_ms(PERSISTENCE);
// Display units
PORTC = units[1];
PORTD = units[1];
uart_send('F');
PORTB = ~ (1<<1); // DIG1
_delay_ms(PERSISTENCE);
PORTB = 0xFF; // Disable digits
uart_send(13); // Tx carriage return
uart_send(10); // Tx line feed
}
}
}
return 0;
}
// Initialize ADC and select ADC5; Vref=1.1v;
// Enable ADC and set speed to 125 KHz for a 16 MHz clock
void adc_init(void){
ADMUX = 0xc5; // Select ADC5 Vref=1.1V
ADCSRA = 0x87; // Enable ADC and setting speed to 125 KHz for 16 MHz clock
}
// Read ADC value
unsigned int get_adc(){
ADCSRA |= (1 << ADSC); //starting ADC conversion
while (!(ADCSRA & (1 << ADIF)));
return ADCL | (ADCH << 8); //read ADCL first
}
// Initialize ATmega328P UART enable TX
void uart_init(void){
UCSR0B = (1 << TXEN0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
UBRR0L = 103;
}
// Send ASCII character via UART (single character)
void uart_send(char letter){
while (!(UCSR0A & (1 << UDRE0)));
UDR0 = letter;
}
// Send string of ASCII characters
void send_string(char *stringAddress){
unsigned char i;
for (i = 0; i < strlen(stringAddress); i++){
uart_send(stringAddress[i]);
}
}