-
Notifications
You must be signed in to change notification settings - Fork 0
/
comp_e_interrupt_output_toggle_Vref12V.c
130 lines (113 loc) · 4.48 KB
/
comp_e_interrupt_output_toggle_Vref12V.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
//*****************************************************************************
//
// MSP432比较器例程01 - 简单电压比较
//
// 该例程将引脚上的输入电压与内部1.2V参考电压进行比较;
// 对外部通道A0和A1进行采样,差分模式,直接使用AVCC(3.3v)作为参考电压源;
// 对于转换结果,格式为补码形式。
// 在中断中将值转换为浮点型,以供用户在调试器中观察。
//
// -------MSP-EXP432P401R硬件描述 -----------
//
// MSP-EXP432P401R
// +------------------+
// /|\| |
// | | |
// --|RST P5.7/C1.6 |<--Vcompare
// | |
// | P1.0|----> 'high'(Vcompare>1.2V); 'low'(Vcompare<1.2)
// | | |
// | | | LED 'ON'(Vcompare>1.2V); 'OFF'(Vcompare<1.2V)
// | |
//
//-------------------------------------------------------
//
//
//*****************************************************************************
/******************************************************************************
* MSP432 Comparator - Interrupt Capability; Vcompare is compared against
* internal 1.2V reference
*
* Description: Use COMPE and internal reference to determine if input'Vcompare'
* is high or low. For the first time, when Vcompare exceeds the 1.2V internal
* reference, CEIFG is set and device enters the COMPE ISR. In the ISR, CEIES is
* toggled such that when Vcompare is less than 1.2V internal reference;
* CEIFG is set. The LED is toggled inside the ISR to illustrate the change.
* The filter feature of the Comp module is used to filter out any noise on
* the line and assure no spurious interrupts occur.
*
******************************************************************************/
//常见标准头文件包含
#include <stdint.h>
#include <stdbool.h>
//外设驱动库DriverLib 头文件包含
//--如果编译此处出错,请查看工程的设置, 看下是否把SDK路径包含进来
//--Properties / Build / ARM Compiler / Include Options
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>
//![Simple Comp Config]
/* Comparator configuration structure */
const COMP_E_Config compConfig =
{
COMP_E_VREF, // Positive Input Terminal
COMP_E_INPUT6, // Negative Input Terminal
COMP_E_FILTEROUTPUT_DLYLVL4, // Delay Level 4 Filter (max)
COMP_E_NORMALOUTPUTPOLARITY // Normal Output Polarity
};
int main(void)
{
//停用看门狗
WDT_A_holdTimer();
/* Set P1.0 as an output pin for LED */
GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
/* Set P5.7 to be comparator in (C1.6) */
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN7,GPIO_TERTIARY_MODULE_FUNCTION);
/* Initialize the Comparator module
* Comparator Instance 1
* Reference Voltage to Postive(+) Terminal
* Pin CE16 to negative(-) Terminal
* Normal Power Mode
* Output Filter On with max delay
* Non-Inverted Output Polarity
*/
COMP_E_initModule(COMP_E1_BASE, &compConfig);
/*
* Base Address of Comparator E,
* Reference Voltage of 1.2 V,
* Lower Limit of 1.2*(32/32) = 1.2V,
* Upper Limit of 1.2*(32/32) = 1.2V
*/
COMP_E_setReferenceVoltage(COMP_E1_BASE, COMP_E_VREFBASE1_2V, 32, 32);
/* Enable COMP_E Interrupt on default rising edge for CEIFG */
COMP_E_setInterruptEdgeDirection(COMP_E1_BASE, COMP_E_RISINGEDGE);
/* Enable Interrupts
* Comparator Instance 1,
* Enable COMPE Interrupt on default rising edge for CEIFG
*/
COMP_E_clearInterruptFlag(COMP_E1_BASE, COMP_E_OUTPUT_INTERRUPT);
COMP_E_enableInterrupt(COMP_E1_BASE, COMP_E_OUTPUT_INTERRUPT);
Interrupt_enableInterrupt(INT_COMP_E1);
Interrupt_enableMaster();
//![Simple COMP_E Example]
/* Allow power to Comparator module */
COMP_E_enableModule(COMP_E1_BASE);
while (1)
{
/* Goto Sleep */
PCM_gotoLPM0();
}
}
/******************************************************************************
*
* This is the COMP_VECTOR interrupt vector service routine.
*
******************************************************************************/
void COMP_E1_IRQHandler(void)
{
/* Toggle the edge at which an interrupt is generated */
COMP_E_toggleInterruptEdgeDirection(COMP_E1_BASE);
/* Clear Interrupt flag */
COMP_E_clearInterruptFlag(COMP_E1_BASE, COMP_E_OUTPUT_INTERRUPT_FLAG);
/* Toggle P1.0 output pin */
GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0);
}