-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEDIII_TP_Final.c
More file actions
347 lines (298 loc) · 8.12 KB
/
EDIII_TP_Final.c
File metadata and controls
347 lines (298 loc) · 8.12 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
===============================================================================
Name : TP_Final.c
Author : $(author)
Version :
Copyright : $(copyright)
Description : main definition
===============================================================================
*/
#ifdef __USE_CMSIS
#include <lpc17xx.h>
#include <lpc17xx_adc.h>
#include <lpc17xx_dac.h>
#include <lpc17xx_gpdma.h>
#include <lpc17xx_pinsel.h>
#include <lpc17xx_timer.h>
#include <lpc17xx_exti.h>
#include <lpc17xx_gpio.h>
#include <lpc17xx_uart.h>
#include <lpc17xx_clkpwr.h>
#endif
void configADC();
void configPins();
void configEXTI();
void configTimer();
void calibrate();
void check_interval(int);
void configDMA_DAC(); //configuracion LLI DMA
void configDAC(); //configuracion DAC para sonido
void startAlarm(int); //comienza transmision M2P
void configUART();
void configDMA_UART();
//void sendMessage(uint8_t*);
void sendMessage();
void clearMessage();
void configUARTTimer();
//void delay();
int sample_number = 0;
int calibration_on = 0;
int first_value = 0;
int calib_flag = 0;
uint16_t adc_values[10];
int prom = 0;
int check = 0;
uint8_t message0[7];
/----------------------------DMA VARIABLES----------------------------/
GPDMA_Channel_CFG_Type GPDMACfg_DAC;
GPDMA_LLI_Type DMA_LLI_Struct_DAC;
GPDMA_LLI_Type DMA_LLI_Struct_UART;
GPDMA_Channel_CFG_Type GPDMACfg_UART;
uint32_t signal[60]={32768, 36160, 39552, 42880, 46080, 49152, 51968, 54656, 57088, 59264
, 61120, 62656, 63872, 64768, 65344, 65472, 65344, 64768, 63872, 62656, 61120, 59264
, 57088, 54656, 51968, 49152, 46080, 42880, 39552, 36160, 32768, 29376, 25984, 22656
, 19456, 16384, 13568, 10880, 8448, 6272, 4416, 2880, 1664, 768, 192, 0, 192, 768, 1664
, 2880, 4416, 6272, 8448, 10880, 13568, 16384, 19456, 22656, 25984, 29376};
int main(void) {
configPins();
GPDMA_Init();
configADC();
configEXTI();
configTimer();
configDAC();
configUART();
while(1);
return 0;
}
void configPins(){
PINSEL_CFG_Type ad_pin; //adc pin
ad_pin.Portnum = 0;
ad_pin.Pinnum = 24;
ad_pin.Pinmode = PINSEL_PINMODE_TRISTATE;
ad_pin.Funcnum = 1;
ad_pin.OpenDrain = PINSEL_PINMODE_NORMAL;
PINSEL_ConfigPin(&ad_pin);
PINSEL_CFG_Type dac_pin; //dac pin
dac_pin.Portnum = 0;
dac_pin.Pinnum = 26;
dac_pin.Pinmode = PINSEL_PINMODE_TRISTATE;
dac_pin.Funcnum = 2;
dac_pin.OpenDrain = PINSEL_PINMODE_NORMAL;
PINSEL_ConfigPin(&dac_pin);
PINSEL_CFG_Type calib_pin; //calibration pin
calib_pin.Portnum = 2;
calib_pin.Pinnum = 10;
calib_pin.Pinmode = PINSEL_PINMODE_PULLUP;
calib_pin.Funcnum = 1;
calib_pin.OpenDrain = PINSEL_PINMODE_NORMAL;
PINSEL_ConfigPin(&calib_pin);
PINSEL_CFG_Type leds;
leds.Portnum = 2;
leds.Pinmode = PINSEL_PINMODE_TRISTATE;
leds.OpenDrain = PINSEL_PINMODE_NORMAL;
leds.Funcnum = 0;
for (int i = 0; i < 6 ; i++)
{
leds.Pinnum = i;
PINSEL_ConfigPin(&leds);
}
GPIO_SetDir(2, 0x3F, 1);
PINSEL_CFG_Type uart_pin;
uart_pin.Portnum = 0;
uart_pin.Pinnum = 2;
uart_pin.Funcnum = 1;
uart_pin.OpenDrain = PINSEL_PINMODE_NORMAL;
uart_pin.Pinmode = PINSEL_PINMODE_PULLUP;
PINSEL_ConfigPin(&uart_pin);
uart_pin.Pinnum = 3;
PINSEL_ConfigPin(&uart_pin);
}
void configADC(){
ADC_Init(LPC_ADC, 200000);
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_1, ENABLE);
ADC_IntConfig(LPC_ADC, ADC_ADINTEN1, ENABLE);
NVIC_EnableIRQ(ADC_IRQn);
}
void configEXTI(){
EXTI_InitTypeDef exti_init;
exti_init.EXTI_Line = 0;
exti_init.EXTI_Mode = EXTI_MODE_EDGE_SENSITIVE;
exti_init.EXTI_polarity = EXTI_POLARITY_LOW_ACTIVE_OR_FALLING_EDGE;
EXTI_Config(&exti_init);
EXTI_Init();
NVIC_EnableIRQ(EINT0_IRQn);
}
void configTimer(){
TIM_TIMERCFG_Type tim_cfg;
tim_cfg.PrescaleOption = TIM_PRESCALE_USVAL;
tim_cfg.PrescaleValue = 1000;
TIM_MATCHCFG_Type mtch_cfg;
mtch_cfg.MatchValue = 50;
mtch_cfg.ExtMatchOutputType = TIM_EXTMATCH_TOGGLE;
mtch_cfg.IntOnMatch = DISABLE;
mtch_cfg.MatchChannel = 1;
mtch_cfg.ResetOnMatch = ENABLE;
mtch_cfg.StopOnMatch = DISABLE;
TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &tim_cfg);
TIM_ConfigMatch(LPC_TIM0, &mtch_cfg);
}
void ADC_IRQHandle(){
if(calibration_on)
calibrate();
else
{
int temp_value = ADC_ChannelGetData(LPC_ADC, ADC_CHANNEL_1);
if(sample_number < 10)
{
adc_values[sample_number] = temp_value;
prom += adc_values[sample_number];
sample_number++;
}
else
{
sample_number = 0;
check = prom/10;
check_interval(check - first_value);
prom = 0;
}
}
}
void EINT0_IRQHandler(){
calibration_on = 1;
TIM_ResetCounter(LPC_TIM0);
TIM_Cmd(LPC_TIM0, ENABLE);
ADC_StartCmd(LPC_ADC, ADC_START_ON_MAT01);
ADC_EdgeStartConfig(LPC_ADC, ADC_START_ON_FALLING);
EXTI_ClearEXTIFlag(EXTI_EINT0);
configDMA_UART();
}
void calibrate(){
if(calib_flag < 10){
first_value += ADC_ChannelGetData(LPC_ADC, ADC_CHANNEL_1);
calib_flag++;
}else if(calib_flag == 10){
first_value = first_value/10;
calibration_on = 0;
calib_flag = 0;
}
}
void check_interval(int new){
GPIO_ClearValue(2, 0x3F);
if(new > 100) {
startAlarm(1000);
GPIO_SetValue(2, 1<<0);
message0[0] = '#';
}
else
message0[0] = ' ';
if(new > 300){
GPIO_SetValue(2,1<<1);
message0[1] = '#';
}
else
message0[1] = ' ';
if(new > 600){
GPIO_SetValue(2,1<<2);
startAlarm(5000);
message0[2] = '#';
}
else
message0[2] = ' ';
if(new > 900){
GPIO_SetValue(2,1<<3);
message0[3] = '#';
}
else
message0[3] = ' ';
if(new > 1200){
GPIO_SetValue(2,1<<4);
startAlarm(12000);
message0[4] = '#';
}
else
message0[4] = ' ';
if(new > 1500){
GPIO_SetValue(2,1<<5);
message0[5] = '#';
}
else
message0[5] = ' ';
message0[6] = '\n';
}
void configDMA_DAC(){
DMA_LLI_Struct_DAC.SrcAddr= (uint32_t)signal; //el source va a ser el arreglo de la funcion seno
DMA_LLI_Struct_DAC.DstAddr= (uint32_t)&(LPC_DAC->DACR);//el destination es el registro del DAC
DMA_LLI_Struct_DAC.NextLLI= (uint32_t)&DMA_LLI_Struct_DAC;
DMA_LLI_Struct_DAC.Control= 60
| (2<<18)//Fuente: 32bits
| (2<<21)//Destino: 32bits
| (1<<26)//Incremento automático de la fuente
;
GPDMACfg_DAC.ChannelNum = 0;
GPDMACfg_DAC.SrcMemAddr = (uint32_t)signal;
GPDMACfg_DAC.DstMemAddr = 0;
GPDMACfg_DAC.TransferSize = 60;
GPDMACfg_DAC.TransferWidth = 0;
GPDMACfg_DAC.TransferType = GPDMA_TRANSFERTYPE_M2P;
GPDMACfg_DAC.SrcConn = 0;
GPDMACfg_DAC.DstConn = GPDMA_CONN_DAC;
GPDMACfg_DAC.DMALLI= (uint32_t)&DMA_LLI_Struct_DAC;
GPDMA_Setup(&GPDMACfg_DAC);
}
void configDAC(){
DAC_CONVERTER_CFG_Type dacStruc;
dacStruc.CNT_ENA = 1;
dacStruc.DMA_ENA = 1;
DAC_Init(LPC_DAC);// Inicializo controlador de GPDMA
DAC_ConfigDAConverterControl(LPC_DAC, &dacStruc);
}
void startAlarm(int frecuencia){ //hay que pasarle la frec que queremos que suene
//stopAlarm();
uint32_t time_out = 25000000/(frecuencia*60);
configDMA_DAC();
DAC_SetDMATimeOut(LPC_DAC, time_out);
GPDMA_ChannelCmd(0, ENABLE); //Enciende el modulo DMA channel0
}
void configUART(void){
UART_CFG_Type UARTConfigStruct;
UART_FIFO_CFG_Type UARTFIFOConfigStruct;
//Configuracion por defecto:
UART_ConfigStructInit(&UARTConfigStruct);
//Inicializa periferico
UART_Init(LPC_UART0, &UARTConfigStruct);
//Configuracion de la FIFO para DMA
UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE;
UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV0;
UARTFIFOConfigStruct.FIFO_ResetRxBuf = ENABLE;
UARTFIFOConfigStruct.FIFO_ResetTxBuf = ENABLE;
//Inicializa FIFO
UART_FIFOConfig(LPC_UART0, &UARTFIFOConfigStruct);
//Habilita transmision
UART_TxCmd(LPC_UART0, ENABLE);
}
void configDMA_UART(void){
DMA_LLI_Struct_UART.SrcAddr= (uint32_t)message0;
DMA_LLI_Struct_UART.DstAddr= (uint32_t)&LPC_UART0->THR;
DMA_LLI_Struct_UART.NextLLI= &DMA_LLI_Struct_UART;
DMA_LLI_Struct_UART.Control= sizeof(message0)
| (2<<12)
| (1<<26)
;
NVIC_DisableIRQ(DMA_IRQn);
GPDMACfg_UART.ChannelNum = 1;
GPDMACfg_UART.SrcMemAddr = (uint32_t)message0;
GPDMACfg_UART.DstMemAddr = 0;
GPDMACfg_UART.TransferSize = sizeof(message0);
GPDMACfg_UART.TransferWidth = 0;
GPDMACfg_UART.TransferType = GPDMA_TRANSFERTYPE_M2P;
GPDMACfg_UART.SrcConn = 0;
GPDMACfg_UART.DstConn = GPDMA_CONN_UART0_Tx;
GPDMACfg_UART.DMALLI = (uint32_t)&DMA_LLI_Struct_UART;
GPDMA_Setup(&GPDMACfg_UART);
GPDMA_ChannelCmd(1, ENABLE);
}
void clearMessage(){
for(int i = 0; i<7; i++){
message0[i] = ' ';
}
}