You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello guys, I have been wanting to read one ADC pin, P0.04 but I am able to read it, it keeps giving me an error when trying to read, i have changed the channel_id but still no luck, if someone has experience i will appreciate it a lot :)
#include <zephyr/types.h>
#include <bluetooth/bluetooth.h>
#include <pm/pm.h>
#include <drivers/adc.h>
#include <drivers/gpio.h>
#include <sys/printk.h>
/* MISSING ADVERTISEMENT_PAYLOAD on purpose */
static const struct bt_data advertisement_payload[] = {
BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA,0,0,0,0,0,0,0,0)
};
bt_addr_t addr = { 0,0,0,0,0,0 };
static void bt_ready(int err)
{
/* Bluetooth initialization error handler */
if (err) { return; }
/* Start advertising */
err = bt_le_adv_start(
BT_LE_ADV_NCONN_IDENTITY, advertisement_payload,
ARRAY_SIZE(advertisement_payload),
NULL, // Don't send the second packet with device name
0
);
if (err) { return; }
}
int read_adc_value(void)
{
const struct device *adc_dev = device_get_binding(DT_LABEL(DT_NODELABEL(adc)));
int ret;
int16_t sample_buffer[1];
// Assuming that P0.04 is mapped to channel 0?
const uint8_t channel_id = 0;
struct adc_channel_cfg channel_cfg = {
.gain = ADC_GAIN_1,
.reference = ADC_REF_INTERNAL,
.acquisition_time = ADC_ACQ_TIME_DEFAULT,
.channel_id = channel_id,
.differential = 0,
};
struct adc_sequence sequence = {
.channels = BIT(channel_id),
.buffer = sample_buffer,
.buffer_size = sizeof(sample_buffer),
.resolution = 12,
};
if (!adc_dev) {
printk("ADC device not found\n");
return -1;
}
ret = adc_channel_setup(adc_dev, &channel_cfg);
if (ret != 0) {
printk("Setting up of the ADC channel failed with code %d\n", ret);
return -1;
}
ret = adc_read(adc_dev, &sequence);
if (ret != 0) {
printk("ADC reading failed with code %d\n", ret);
return -1;
}
return sample_buffer[0];
}
void main(void)
{
printk("hello there\n");
bt_addr_le_t ble_addr_0 = { BT_ADDR_LE_RANDOM, addr };
bt_id_create(&ble_addr_0, NULL);
bt_enable(bt_ready);
// After starting advertising, put the system to sleep for 60 seconds
while (1) {
k_sleep(K_SECONDS(3));
bt_le_adv_stop();
int adc_value = read_adc_value();
printk("ADC value:");
printk(adc_value);
printk("\n");
bt_addr_le_t ble_addr_1 = { BT_ADDR_LE_RANDOM, addr };
bt_id_create(&ble_addr_1, NULL);
k_sleep(K_SECONDS(5));
bt_le_adv_start(
BT_LE_ADV_NCONN_IDENTITY, advertisement_payload,
ARRAY_SIZE(advertisement_payload),
NULL, // Don't send the second packet with device name
0
);
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hello guys, I have been wanting to read one ADC pin, P0.04 but I am able to read it, it keeps giving me an error when trying to read, i have changed the channel_id but still no luck, if someone has experience i will appreciate it a lot :)
Beta Was this translation helpful? Give feedback.
All reactions