-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·160 lines (113 loc) · 3.66 KB
/
main.cpp
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
extern "C" {
#include <math.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_i2c.h"
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
#include "driverlib/i2c.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "utils/ustdlib.h"
#include "stdint.h"
#define SYSTICKS_PER_SECOND 1000
static unsigned long milliSec = 0;
void SysTickHandler()
{
milliSec++;
}
uint32_t millis(){
return milliSec;
}
void InitConsole(void)
{
//
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
//
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
//
MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Initialize the UART for console I/O.
//
UARTStdioInit(0);
}
}
#include "INA226.h"
static unsigned long ulClockMS=0;
int main(void)
{
MAP_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |SYSCTL_XTAL_12MHZ); //50MHZ
//
// Enable peripherals to operate when CPU is in sleep.
//
MAP_SysCtlPeripheralClockGating(true);
//
// Configure SysTick to occur 1000 times per second, to use as a time
// reference. Enable SysTick to generate interrupts.
//
MAP_SysTickPeriodSet(MAP_SysCtlClockGet() / SYSTICKS_PER_SECOND);
MAP_SysTickIntEnable();
MAP_SysTickEnable();
//
// Get the current processor clock frequency.
//
ulClockMS = MAP_SysCtlClockGet() / (3 * 1000);
InitConsole();
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Initialize the UART as a console for text I/O.
#ifdef DEBUG
UARTprintf("Setting up UART ... \n");
#endif
UARTStdioInitExpClk(0,115200);
#ifdef DEBUG
UARTprintf("SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0)\n");
#endif
//I2C
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
#ifdef DEBUG
UARTprintf("GPIOPinTypeI2C(GPIO_PORTB_BASE,GPIO_PIN_2 | GPIO_PIN_3);\n");
#endif
GPIOPinTypeI2C(GPIO_PORTB_BASE,GPIO_PIN_2 | GPIO_PIN_3);
#ifdef DEBUG
UARTprintf("I2CMasterInitExpClk(I2C0_MASTER_BASE,SysCtlClockGet(),false);\n");
#endif
I2CMasterInitExpClk(I2C0_MASTER_BASE,SysCtlClockGet(),false); //false = 100khz , true = 400khz
I2CMasterTimeoutSet(I2C0_MASTER_BASE, 1000);
INA226 power_meter = INA226(0x45);
uint16_t read_ina;
read_ina = power_meter.read_register(REG_CONFGURATION);
UARTprintf("Conf reg = %X\n", read_ina);
power_meter.set_sample_average(4);
read_ina = power_meter.read_register(REG_CONFGURATION);
power_meter.set_sample_average(16);
read_ina = power_meter.read_register(REG_CONFGURATION);
UARTprintf("Conf reg = %X\n", read_ina);
power_meter.set_calibration_value(445);
read_ina = power_meter.get_calibration_value();
UARTprintf("Calib. value = %d\n", read_ina);
power_meter.set_bus_voltage_limit(6.2);
power_meter.set_mask_enable_register(BUS_UNDER_LIMIT);
while (1)
{
read_ina = power_meter.get_bus_voltage();
UARTprintf("Bus Voltage = %d mV\n", read_ina);
read_ina = power_meter.get_shunt_voltage();
UARTprintf("Shunt Voltage = %d uV\n", read_ina);
read_ina = power_meter.get_bus_current();
UARTprintf("Current = %d mA\n", read_ina);
SysCtlDelay(200*ulClockMS);
}
}