|
3 | 3 | #include "hardware/i2c.h"
|
4 | 4 | #include <math.h>
|
5 | 5 |
|
6 |
| -// GPIOs, see circuit diagram |
7 |
| -#define I2C_SDA 8 |
8 |
| -#define I2C_SCL 9 |
| 6 | +// LED GPIOs |
9 | 7 | #define EXT_LED_GPIO 2
|
10 | 8 |
|
11 |
| -// Shunt resistor values in ohms |
| 9 | +// INA228 integrated shunt resistance |
12 | 10 | #define R_SHUNT 0.015
|
13 | 11 |
|
14 | 12 | // Max expected current through shunt resistor
|
@@ -49,16 +47,15 @@ static void ina228_init() {
|
49 | 47 | i2c_write_blocking(i2c_default, I2C_ADDR, shunt_buf, 3, false);
|
50 | 48 |
|
51 | 49 | // Write to ADC config register (need to enable continuous mode to access accumilation variables energy and charge)
|
52 |
| - // To enable contintinuous vbus, vshunt and temp, write 0x0F to bits 15-12 |
53 |
| - uint8_t adc_msb = 0xF1; |
54 |
| - uint8_t adc_lsb = 0x11; |
| 50 | + uint8_t adc_msb = 0xF0; |
| 51 | + uint8_t adc_lsb = 0x00; |
55 | 52 | uint8_t adc_buf[3] = {ADC_CONFIG_REG, adc_msb, adc_lsb};
|
56 | 53 | i2c_write_blocking(i2c_default, I2C_ADDR, adc_buf, 3, false);
|
57 | 54 | }
|
58 | 55 |
|
59 | 56 | static void ina228_read(float *vshunt, float *vbus, float *dietemp, float *current, float *power, float *energy, float *charge) {
|
60 | 57 | // Buffers for writing register measurments to
|
61 |
| - // Some registers are 2 byte, some are 3 byte, the accumilation registers are 5 byte |
| 58 | + // Some registers are 2 bytes, some are 3 bytes, the accumilation registers are 5 bytes |
62 | 59 | uint8_t buffer_2[2];
|
63 | 60 | uint8_t buffer_3[3];
|
64 | 61 | uint8_t buffer_5[5];
|
@@ -106,10 +103,10 @@ int main()
|
106 | 103 | i2c_init(i2c_default, 400*1000);
|
107 | 104 |
|
108 | 105 | // GPIO initialisiation
|
109 |
| - gpio_set_function(I2C_SDA, GPIO_FUNC_I2C); |
110 |
| - gpio_set_function(I2C_SCL, GPIO_FUNC_I2C); |
111 |
| - gpio_pull_up(I2C_SDA); |
112 |
| - gpio_pull_up(I2C_SCL); |
| 106 | + gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C); |
| 107 | + gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C); |
| 108 | + gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN); |
| 109 | + gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN); |
113 | 110 |
|
114 | 111 | // Initialise ina228
|
115 | 112 | ina228_init();
|
|
0 commit comments