-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.c
224 lines (177 loc) · 5.79 KB
/
app.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
217
218
219
220
221
222
223
224
/******************************************************************************************
File Name : app.c
Author&Editor Name : Kevin Tom
Professional Masters in Embedded Systems
Fall 2021, UCB.
Editor email id : keto99192@colorado.edu
IDE used for Coding : ccstudio IDE
Compiler : GCC
Date : 4th December 2021
Info on Code obtained from other sources: None
*******************************************************************************************/
/*header files*/
#include <stdio.h>
#include "msp.h"
#include "autocorrelation.h"
#include "lcd.h"
#include <string.h>
#include "uart.h"
/*macros*/
#define ERROR_LIMIT 1
#define BUFSIZE 1024
#define SAMPLING_FREQUENCY 32000
/*static variables*/
static uint16_t buffer[BUFSIZE];
/***************************************************************************
* to detect the signal frequency
*
* Parameters: ipFrequency --> user provided input frequency
*
* Returns: void
**************************************************************************/
void detectFrequency(register uint16_t ipFrequency)
{
uint32_t result;
register int index = 0;
register int period = 0;
register int frequency = 0;
register int lowerLimit = 0;
register int upperLimit = 0;
register int noOfAttempts = 0;
char str[100];
/*setting tolerant frequency range*/
lowerLimit = ipFrequency - ERROR_LIMIT;
upperLimit = ipFrequency + ERROR_LIMIT;
while(1)
{
/*start a conversion*/
ADC14->CTL0 |= 1;
/*wait till conversion is complete*/
while(!ADC14->IFGR0);
/*read conversion result*/
result = ADC14->MEM[5];
buffer[index] = result;
index++;
/*
* when the buffer fills up, then trigger
* the autocorrelation algorithm
*/
if(index == (BUFSIZE-1))
{
/*reset index to zero for future buffer filling*/
index = 0;
/*autocorrelation algorithm for 14 bit precision ADC values*/
period = autocorrelate_detect_period(buffer, BUFSIZE,kAC_14bps_unsigned);
frequency = SAMPLING_FREQUENCY / period;
noOfAttempts++;
if((frequency >= lowerLimit) && (frequency <= upperLimit))
{
displaySucessMessage();
sprintf(str, "\r\nString tuned: %d Hz\r\n", frequency);
putStr(str);
sprintf(str,"\n\n\r");
putStr(str);
break;
}
else if(frequency > 0)
{
sprintf(str, "\n\rFrequency : %d Hz", frequency);
putStr(str);
}
/*
* if we reach maximum no of attempts, then
* break away from the loop
*/
if(20 == noOfAttempts)
{
break;
}
}
}
}
/***************************************************************************
* convert string input fed by user to its frequency
*
* Parameters: ipFrequency --> user provided input frequency
*
* Returns: void
**************************************************************************/
int convertStringToFreq(char *guitarString)
{
int retVal = 0;
if(0 == strcmp("E4",guitarString))
retVal = 330;
if(0 == strcmp("B3",guitarString))
retVal = 247;
if(0 == strcmp("G3",guitarString))
retVal = 196;
if(0 == strcmp("D3", guitarString))
retVal = 147;
if(0 == strcmp("A2", guitarString))
retVal = 110;
if(0 == strcmp("E2", guitarString))
retVal = 82;
return retVal;
}
/***************************************************************************
* prints the user interface
*
* Parameters: void
*
* Returns: void
**************************************************************************/
void printUserInterface()
{
char str[100];
sprintf(str, "\n\rGUITAR TUNER\r\n");
putStr(str);
sprintf(str, "***************************************\r\n");
putStr(str);
sprintf(str, "ESD Project by Ananth & Kevin\r\n");
putStr(str);
sprintf(str, "***************Strings*****************\r\n");
putStr(str);
sprintf(str, "---Frequency(Hz)---------Notation------\r\n");
putStr(str);
sprintf(str, " 329.63 E4 \r\n");
putStr(str);
sprintf(str, " 246.94 B3 \r\n");
putStr(str);
sprintf(str, " 196 G3 \r\n");
putStr(str);
sprintf(str, " 146.83 D3 \r\n");
putStr(str);
sprintf(str, " 110 A2 \r\n");
putStr(str);
sprintf(str, " 82.41 E2 \r\n");
putStr(str);
sprintf(str, "**************************************\r\n");
putStr(str);
}
/***************************************************************************
* detect the current string frequency and thus achive guitar tuning
*
* Parameters: void
*
* Returns: void
**************************************************************************/
void tuneGuitarString()
{
char guitarString[2];
char str[100];
register int ipFreq = 0;
char tuneMessage[20] = "Tuning for ";
/*Prompt the user to input string of his choice*/
sprintf(str, "\r\nEnter the string being tuned \r\n");
putStr(str);
gets(guitarString);
tuneMessage[11] = guitarString[0];
tuneMessage[12] = guitarString[1];
displayString(tuneMessage);
ipFreq = convertStringToFreq(guitarString);
/*display the respective string frequency*/
sprintf(str, "Frequency for tuning : %d Hz\n\r", ipFreq);
putStr(str);
detectFrequency(ipFreq);
}
/*****************************end of file***********************************/