-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac2.h
227 lines (196 loc) · 5.62 KB
/
ac2.h
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
// Copyright (c) 2018 Cláudio Patrício
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <detpic32.h>
#include "stdbool.h"
/* Defines */
#define DisableUart1RxInterrupt() IEC0bits.U1RXIE = 0;
#define EnableUart1RxInterrupt() IEC0bits.U1RXIE = 1;
#define DisableUart1TxInterrupt() IEC0bits.U1TXIE = 0;
#define EnableUart1TxInterrupt() IEC0bits.U1TXIE = 1;
#define BUF_SIZE 8
#define INDEX_MASK (BUF_SIZE-1)
/* SPI2 */
#define RDSR 0x05
#define WRITE 0x02
#define READ 0x03
#define EEPROM_CLOCK 500000
#define SPI2_WREN 0x06
/* Structures */
typedef unsigned char uchar;
typedef unsigned int uint;
typedef struct
{
uchar data[BUF_SIZE];
uint head;
uint tail;
uint count;
bool overrun;
} ring;
/* Global Vars */
enum _REbits {
RE0 = 0x0001,
RE1 = 0x0002,
RE2 = 0x0004,
RE3 = 0x0008,
RE4 = 0x0010,
RE5 = 0x0020,
RE6 = 0x0040,
RE7 = 0x0080,
RE_ALL = 0x00FF,
RE1L = 0x0003,
RE2L = 0x0007,
RE3L = 0x000F,
RE4L = 0x001F,
RE5L = 0x003F,
RE6L = 0x007F
};
enum _RBbits {
RB0 = 0x0001,
RB1 = 0x0002,
RB2 = 0x0004,
RB3 = 0x0008,
RB4 = 0x0010,
RB5 = 0x0020,
RB6 = 0x0040,
RB7 = 0x0080,
RB8 = 0x0100,
RB9 = 0x0200,
RB_ALL = 0x03FF,
RB1L = 0x0003,
RB2L = 0x0007,
RB3L = 0x000F,
RB4L = 0x001F,
RB5L = 0x003F,
RB6L = 0x007F,
RB7L = 0x00FF,
RB8L = 0x01FF
};
enum _RBLEDbits {
B = RB0,
A = RB1,
F = RB2,
G = RB3,
E = RB4,
D = RB5,
C = RB6,
DOT = RB7,
S_HI = RB8,
S_LO = RB9
};
enum _LEDbits {
L_0 = A|B|C|D|E|F, //0x0077
L_1 = B|C, //0x0041
L_2 = A|B|G|E|D, //0x003B
L_3 = A|B|G|C|D, //0x006B
L_4 = F|G|B|C,
L_5 = A|F|G|C|D,
L_6 = F|E|D|C|G,
L_7 = A|B|C,
L_8 = A|B|C|D|E|F|G,
L_9 = A|B|C|D|F|G,
L_A = A|B|C|E|F|G,
L_B = F|E|D|C|G,
L_C = A|F|E|D,
L_D = G|E|D|C|B,
L_E = A|F|G|E|D,
L_F = A|F|G|E
};
/* I2C */
#define I2C_READ 1
#define I2C_WRITE 0
#define I2C_ACK 0
#define I2C_NACK 1
#define SENS_ADDRESS 0x4D // For temperature device
#define TC74_CLK_FREQ 100000 // 100KHz
#define RTR 0
#define ADDR_WR ((SENS_ADDRESS << 1) | I2C_WRITE)
#define ADDR_RD ((SENS_ADDRESS << 1) | I2C_READ)
/* Global Variables */
extern volatile bool led;
extern volatile uint volt;
extern bool debug;
/* Functions */
/* Start of Global functions */
int powi(int x, int y); // pow
uint abs(int val); // Absolute value
void delay(uint n_intervals); // Software delay in ms
void delay_ct(uint n_intervals); // Hardware dealy in ms
/* Start of Global functions */
/* Start of display functions */
void init_led(); // Initialize hi led
void init_led_lo(); // Initialize lo led
void init_led_dual(); // Initialize both leds
int int2led(int value, bool dot, int base);
int char2led(uchar value, bool dot, int base);
/* End of display functions */
/* Start of switch functions */
void init_switch();
/* End of switch functions */
/* Start of ADC functions */
void init_analog_converter(); // Initialize analog converter RB14
void init_adc_ints(); // Enable ADC Interrupts
/* End of ADC functions */
/* Start of Timers functions */
void init_timerT1(int timer); // Initialize timer T1
void init_timerT2(int timer); // Initialize timer T2
void init_timerT3(int timer); // Initialize timer T3
void init_TimerT1_ints(); // Enable timer T1 Interrupts
void init_TimerT2_ints(); // Enable timer T2 Interrupts
void init_TimerT3_ints(); // Enable timer T3 Interrupts
int getScalerT1(int scale);
int getScalerT2(int scale);
int getScalerT3(int scale);
int getPrescalerT1(int timer);
int getPrescalerT2(int timer);
int getPrescalerT3(int timer);
void setT3PWM(uint dutycycle);
void setPWMT3(uint dutycycle);
/* End of Timers functions */
/* Start of UART functions */
void config_UART(int baudrate, char parity, int stop); // Configure UART
void init_UART(); // Initialize UART
void init_UART_ints(); // Enable UART Interrupts
void putc_val(char c);
void puts_val(char* s);
char getc_val();
void comDrv_flush(volatile ring *buffer);
void comDrv_putc(volatile ring *buffer, char c);
void comDrv_puts(volatile ring *buffer, char* s);
void comDrv_puti(volatile ring *buffer, int val);
bool comDrv_getc(volatile ring *buffer, char* c);
/* End of UART functions */
/* Start of I2C1 functions */
int getTemperature(int *temperature);
void init_i2c1(uint sclrate, uint dutycycle);
void i2c1_start();
void i2c1_stop();
int i2c1_send(uchar value);
char i2c1_receive(char ack);
/* End of I2C1 functions */
/* Start of EEPROM/SPI functions */
void spi2_setClock(uint clock_freq);
void spi2_init();
char eeprom_readStatus();
void eeprom_writeStatusCommand(char command);
void eeprom_writeData(int address, char value);
char eeprom_readData(int address);
void eeprom_clearData();
/* End of EEPROM/SPI functions */