|  | Dual Active Bridge Development Board (Part-No. ) |
Content |  |  |  |  |  |

Loading...
Searching...
No Matches
useful_macros.h
1
2// This is a guard condition so that contents of this file are not included
3// more than once.
4#ifndef USEFUL_MACROS_H
5#define USEFUL_MACROS_H
6
7#include <xc.h> // include processor files - each processor file is guarded.
8
9
13#define _rnd(a) ((int16_t)((a)+((a)<0?-0.5:0.5)))
14#define _min(a,b) (((a)>(b)) ? (b):(a))
15#define _max(a,b) (((a)>(b)) ? (a):(b))
16
17#define _rnd_int32_t(a) ((int32_t)((a)+((a)<0?-0.5:0.5)))
18
19// following 2 macros used to get high byte and low byte of a word
20#define WordLowByte(word) ((uint8_t) (word & 0x00FF))
21#define WordHighByte(word) ((uint8_t) (word >> 8))
22
23// to convert number to QFORMAT (positive) with max resolution, multiply by 2^QFORMAT_SHIFT
24// this gives max possibly resolution with 15 bits available
25#define QFORMAT_SHIFT(value) ((value) < ((float)(1<<0)) ? (15) : \
26 ((value) < ((float)(1<<1)) ? (14) : \
27 ((value) < ((float)(1<<2)) ? (13) : \
28 ((value) < ((float)(1<<3)) ? (12) : \
29 ((value) < ((float)(1<<4)) ? (11) : \
30 ((value) < ((float)(1<<5)) ? (10) : \
31 ((value) < ((float)(1<<6)) ? (9) : \
32 ((value) < ((float)(1<<7)) ? (8) : \
33 ((value) < ((float)(1<<8)) ? (7) : \
34 ((value) < ((float)(1<<9)) ? (6) : \
35 ((value) < ((float)(1<<10)) ? (5) : \
36 ((value) < ((float)(1<<11)) ? (4) : \
37 ((value) < ((float)(1<<13)) ? (3) : \
38 ((value) < ((float)(1<<13)) ? (2) : \
39 ((value) < ((float)(1<<14)) ? (1) : (0))))))))))))))))
40
41// TIME_TO_TICKS: convert time in seconds to counter ticks
42#define TIME_TO_TICKS(time, tick_period) (_rnd(time/tick_period))
43
44// RMS_TO_PEAK: sqrt(2): rms to peak-peak is 2*sqrt(2), so rms to peak is sqrt(2)
45#define RMS_TO_PEAK (1.414213562)
46
47// AVG_TO_RMS: PI / (2* sqrt(2)) = 1.11
48#define AVG_TO_RMS (1.11)
49
50#define RMS_TO_AVG (1.0/AVG_TO_RMS)
51
52// convert frequency in Hz to integer setting that can be loaded into the PWMxPER register
53// PWM_HR_EDGE_ALIGNED_PGxPER: high resolution, edge aligned mode
54// assuming a PWM clock frequency of 4GHz
55#define PWM_HR_EDGE_ALIGNED_PGxPER(freq_hz) ((uint16_t)(_rnd_int32_t((8.0*500.0e+6)/(float)freq_hz)-8.0))
56
57// convert duty cycle in percent to an integer that can be loaded into the PGxDC register
58// duty_percent should be between 0 and 100
59#define PWM_HR_EDGE_ALIGNED_PGxDC(PGxPER, duty_percent) ((uint16_t)(_rnd_int32_t((PGxPER+8)*(duty_percent/100.0))))
60
61// convert trigger offset in seconds to an integer that can be loaded into PGxTRIGy register
62#define PWM_HR_PGxTRIGy(trigger_offset) ((uint16_t)(_rnd_int32_t(8.0*500.0e+6*trigger_offset)))
63
64// converter dead times in seconds to integer that can be loaded into PGxDTy register
65#define PWM_HR_PGxDTy(dead_time) ((uint16_t)(_rnd_int32_t(8.0*500.0e+6*dead_time)))
66
67// convert a threshold in engineering units (volts,amps etc,) or amps to ADC threshold
68// formula is:
69// integer threshold = (volts or amps threshold)*gain + offset
70#define UNITS_FROM_ENG_TO_ADC(threshold, gain, offset) (_rnd((((float)threshold*(float)gain) + (float)offset)/3.3*4095.0))
71
72#define UNITS_FROM_ADC_TO_ENG(AdcValue, Gain) (_rnd((((float)AdcValue /(float)Gain))/4095.0 * 3.3))
73
// end of group ~~~~~~~~~~~~~~~~~~~~
75
76#endif /* USEFUL_MACROS_H */
77