LTlib LaurTec Library
4.0.3
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
Main Page
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
main.c
Go to the documentation of this file.
1
2
#include <xc.h>
3
4
#include "
LTlib.h
"
5
6
#include "
module_IO.h
"
7
#include "
module_IO.c
"
8
9
#include "
LTlib_delay.h
"
10
#include "
LTlib_delay.c
"
11
12
#include "
module_SPI.h
"
13
#include "
module_SPI.c
"
14
15
#include "
MCP4822.h
"
16
#include "
MCP4822.c
"
17
18
19
//*************************************
20
// Voltage reference
21
//*************************************
22
23
#define REFERENCE_0000 0
24
#define REFERENCE_1500 1500
25
#define REFERENCE_1800 1800
26
#define REFERENCE_3300 3300
27
28
//*************************************
29
// Constants
30
//*************************************
31
32
#define STATUS_ON 0x01
33
#define STATUS_OFF 0x00
34
35
#define WAIT_TIME 1000
36
#define DEBOUNCE_TIME 10
37
38
#define BUTTON_1 IO_BIT4
39
#define BUTTON_2 IO_BIT5
40
#define BUTTON_3 IO_BIT6
41
#define BUTTON_4 IO_BIT7
42
#define BUTTON_PRESSED 0x00
43
44
45
int
main
(
void
) {
46
47
unsigned
char
system_status =
STATUS_OFF
;
48
unsigned
int
last_voltage =
REFERENCE_0000
;
49
50
IO_set_all_ports_as_inputs
();
51
IO_enable_pull_up_resistors
(
IO_PORTB
, 0xFF);
52
IO_set_port_direction
(
IO_PORTD
,
IO_ALL_PORT_OUTPUT
);
53
54
MCP4822_initialize
(
MCP4822_CLOCK_FOSC_64
);
55
56
IO_write_port
(
IO_PORTD
,
STATUS_OFF
);
57
58
while
(1) {
59
60
//*************************************
61
// BT1
62
//*************************************
63
64
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_1
) ==
BUTTON_PRESSED
) {
65
66
delay_ms
(
DEBOUNCE_TIME
);
67
68
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_1
) ==
BUTTON_PRESSED
) {
69
MCP4822_set_amplitude
(
REFERENCE_1500
,
MCP4822_DAC_A
);
70
last_voltage =
REFERENCE_1500
;
71
}
72
}
73
74
//*************************************
75
// BT2
76
//*************************************
77
78
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_2
) ==
BUTTON_PRESSED
) {
79
80
delay_ms
(
DEBOUNCE_TIME
);
81
82
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_2
) ==
BUTTON_PRESSED
) {
83
MCP4822_set_amplitude
(
REFERENCE_1800
,
MCP4822_DAC_A
);
84
last_voltage =
REFERENCE_1800
;
85
}
86
}
87
88
//*************************************
89
// BT3
90
//*************************************
91
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_3
) ==
BUTTON_PRESSED
){
92
93
delay_ms
(
DEBOUNCE_TIME
);
94
95
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_3
) ==
BUTTON_PRESSED
){
96
MCP4822_set_amplitude
(
REFERENCE_3300
,
MCP4822_DAC_A
);
97
last_voltage =
REFERENCE_3300
;
98
}
99
100
}
101
102
//*************************************
103
// BT4
104
//*************************************
105
106
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_4
) ==
BUTTON_PRESSED
&& system_status ==
STATUS_ON
){
107
108
delay_ms
(
DEBOUNCE_TIME
);
109
110
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_4
) ==
BUTTON_PRESSED
){
111
MCP4822_set_options
(
MCP4822_GAIN_2
,
MCP4822_SHUTDOWN_ON
,
MCP4822_DAC_A
);
112
MCP4822_set_amplitude
(
REFERENCE_0000
,
MCP4822_DAC_A
);
113
114
IO_write_port
(
IO_PORTD
,
STATUS_OFF
);
115
system_status =
STATUS_OFF
;
116
delay_ms
(1000);
117
}
118
}
119
120
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_4
) ==
BUTTON_PRESSED
&& system_status ==
STATUS_OFF
){
121
122
delay_ms
(
DEBOUNCE_TIME
);
123
124
if
(
IO_read_port_bit
(
IO_PORTB
,
BUTTON_4
) ==
BUTTON_PRESSED
){
125
MCP4822_set_options
(
MCP4822_GAIN_2
,
MCP4822_SHUTDOWN_OFF
,
MCP4822_DAC_A
);
126
MCP4822_set_amplitude
(last_voltage,
MCP4822_DAC_A
);
127
128
IO_write_port
(
IO_PORTD
,
STATUS_ON
);
129
system_status =
STATUS_ON
;
130
delay_ms
(1000);
131
}
132
}
133
}
134
}
LTlib_v_4.0.3
ex
PIC18
MCP4822
01 - MCP4822_Voltage_reference_generator.X
main.c
Generated on Wed Aug 3 2016 19:57:31 for LTlib LaurTec Library by
1.8.3.1