-
Notifications
You must be signed in to change notification settings - Fork 949
Add ina260 example #703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
peterharperuk
wants to merge
1
commit into
raspberrypi:develop
Choose a base branch
from
peterharperuk:add_ina260_example
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add ina260 example #703
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
add_executable(ina260_i2c | ||
ina260_i2c.c | ||
) | ||
target_link_libraries(ina260_i2c | ||
pico_stdlib | ||
hardware_i2c | ||
) | ||
pico_add_extra_outputs(ina260_i2c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||
#include <stdio.h> | ||||||
#include "pico/stdlib.h" | ||||||
#include "hardware/i2c.h" | ||||||
|
||||||
#define CURRENT_REGISTER 0x01 | ||||||
#define VOLTAGE_REGISTER 0x02 | ||||||
#define POWER_REGISTER 0x03 | ||||||
#define I2C_ADDRESS 0x40 | ||||||
|
||||||
#define ByteSwap(u) (uint16_t)((u << 8)|(u >> 8)) | ||||||
|
||||||
// Read register value | ||||||
static uint16_t read_reg(uint8_t reg) { | ||||||
// Set the register address | ||||||
int ret = i2c_write_blocking(i2c_default, I2C_ADDRESS, ®, 1, true); // no stop is set | ||||||
assert(ret == 1); | ||||||
if (ret != 1) return 0; | ||||||
// Read the value | ||||||
uint16_t data; | ||||||
ret = i2c_read_blocking(i2c_default, I2C_ADDRESS, (uint8_t*)&data, 2, false); | ||||||
assert(ret == 2); | ||||||
if (ret != 2) return 0; | ||||||
return ByteSwap(data); | ||||||
} | ||||||
|
||||||
int main() { | ||||||
setup_default_uart(); | ||||||
printf("ina260 test\n"); | ||||||
|
||||||
// Initialise i2c | ||||||
i2c_init(i2c_default, 100 * 1000); | ||||||
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C); | ||||||
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C); | ||||||
Comment on lines
+32
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice (for completeness) if this did the bi_pins thing 🙂 |
||||||
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN); | ||||||
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN); | ||||||
|
||||||
while(true) { | ||||||
|
||||||
// Read current and convert to mA | ||||||
float ma = read_reg(CURRENT_REGISTER) * 1.250f; | ||||||
if (ma > 15000) ma = 0; | ||||||
// Read the voltage | ||||||
float v = read_reg(VOLTAGE_REGISTER) * 0.00125f; | ||||||
// Read power and convert to mW | ||||||
uint16_t mw = read_reg(POWER_REGISTER) * 10; | ||||||
|
||||||
// Display results | ||||||
printf("current: %.2f mA voltage: %.2f v power: %u mW\n", ma, v, mw); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
sleep_ms(1000); | ||||||
} | ||||||
return 0; | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be using stdio rather than explicitly using uart, so that the
printf
output can instead be sent to USB by adding the necessary commands toCMakeLists.txt
?