-
Notifications
You must be signed in to change notification settings - Fork 0
/
mq3.c
106 lines (81 loc) · 2.17 KB
/
mq3.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
#include <stdint.h>
typedef uint8_t bool;
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/adc.h"
#include "pt.h"
#include "common.h"
#include "mq3.h"
// Port & pin mappings
const uint32_t mq3Peripheral = SYSCTL_PERIPH_GPIOE;
const uint32_t mq3Port = GPIO_PORTE_BASE;
const uint32_t mq3Pin = GPIO_PIN_3; // PE3 (AIN0)
const uint32_t mq3ADCPeripheral = SYSCTL_PERIPH_ADC0;
const uint32_t mq3ADC = ADC0_BASE;
const uint32_t mq3ADCSeq = 1; // ADC sequence number
uint8_t mq3Flags = 0;
static unsigned long ulData[mq3ADCFifoDepth];
uint16_t mq3Value = 0;
// Timer
uint32_t *mq3Timer;
void mq3setup(void)
{
// Configure input pin
if(!SysCtlPeripheralReady(mq3Peripheral))
{
SysCtlPeripheralEnable(mq3Peripheral);
while(!SysCtlPeripheralReady(mq3Peripheral));
}
// Make the pin ADC
GPIOPinTypeADC(mq3Port, mq3Pin);
// Configure ADC peripheral
if(!SysCtlPeripheralReady(mq3ADCPeripheral))
{
SysCtlPeripheralEnable(mq3ADCPeripheral);
while(!SysCtlPeripheralReady(mq3ADCPeripheral));
}
ADCSequenceDisable(mq3ADC, mq3ADCSeq);
ADCSequenceConfigure(mq3ADC, mq3ADCSeq, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(mq3ADC, mq3ADCSeq, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);
ADCSequenceEnable(mq3ADC, mq3ADCSeq);
mq3Timer = getFreeTimer();
}
PT_THREAD(mq3Loop(struct pt *pt))
{
PT_BEGIN(pt);
while(1)
{
if(mq3Timer) {
*mq3Timer = MQ3_TIME_INTERVAL;
PT_WAIT_WHILE(pt, *mq3Timer);
}
// Do ADC conversion
ADCIntClear(mq3ADC, mq3ADCSeq);
ADCProcessorTrigger(mq3ADC, mq3ADCSeq);
// Wait until conversion is complete (yeild from thread)
PT_WAIT_UNTIL(pt, ADCIntStatus(mq3ADC, mq3ADCSeq, 0));
ADCSequenceDataGet(mq3ADC, mq3ADCSeq, ulData);
mq3Value = (uint16_t)ulData[0];
mq3Flags |= (MQ3_DATA_VALID | MQ3_NEW_DATA);
}
PT_END(pt);
}
uint8_t mq3DataValid()
{
return (mq3Flags & MQ3_DATA_VALID) ? 1 : 0;
}
uint8_t mq3NewData()
{
return (mq3Flags & MQ3_NEW_DATA) ? 1 : 0;
}
void mq3ResetNewData()
{
mq3Flags &= ~MQ3_NEW_DATA;
}
uint16_t mq3GetValue()
{
return mq3Value;
}