Update repo
This commit is contained in:
1 parent
5f3e7678cd
commit
7c6fbea774
11 files changed
+303
-2
No files matched your search
@@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
|
||||
# Locate the Zephyr build system as a CMake package
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
|
||||
# Project name
|
||||
project(adc_single_channel)
|
||||
|
||||
# Add source files belong to this application
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
@@ -0,0 +1,25 @@
|
||||
/ {
|
||||
aliases {
|
||||
led0 = &red_led;
|
||||
led1 = &blue_led;
|
||||
led2 = &green_led;
|
||||
sw0 = &user_button0;
|
||||
};
|
||||
|
||||
gpio_keys {
|
||||
compatible = "gpio-keys";
|
||||
|
||||
user_button0: button {
|
||||
label = "Button";
|
||||
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
|
||||
zephyr,code = <INPUT_KEY_0>;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
&adc1{
|
||||
pinctrl-0 = <&adc1_in1_pa1>;
|
||||
pinctrl-names = "default";
|
||||
st,adc-prescaler = <4>;
|
||||
status = "okay";
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
/ {
|
||||
aliases {
|
||||
led0 = &red_led_5;
|
||||
led1 = &blue_led_6;
|
||||
led2 = &green_led_4;
|
||||
led3 = &orange_led_3;
|
||||
sw0 = &user_button;
|
||||
};
|
||||
};
|
||||
|
||||
&adc1{
|
||||
pinctrl-0 = <&adc1_in1_pa1>;
|
||||
pinctrl-names = "default";
|
||||
st,adc-prescaler = <4>;
|
||||
status = "okay";
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
CONFIG_GPIO=y
|
||||
CONFIG_SERIAL=y
|
||||
CONFIG_ADC=y
|
||||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||
CONFIG_CONSOLE=y
|
||||
CONFIG_PRINTK=y
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/uart.h>
|
||||
#include <zephyr/drivers/adc.h>
|
||||
|
||||
|
||||
#define ADC_NODE DT_NODELABEL(adc1)
|
||||
|
||||
#if !DT_NODE_HAS_STATUS(ADC_NODE, okay)
|
||||
#error "ADC1 not available; Enable it in a board overlay if needed."
|
||||
#endif
|
||||
|
||||
static const struct device *const adc_dev =
|
||||
DEVICE_DT_GET(ADC_NODE);
|
||||
|
||||
static uint16_t sample;
|
||||
|
||||
// Channel Configuration
|
||||
static struct adc_channel_cfg ch1_cfg = {
|
||||
.gain = ADC_GAIN_1,
|
||||
.reference = ADC_REF_INTERNAL,
|
||||
.acquisition_time = ADC_ACQ_TIME_DEFAULT,
|
||||
.channel_id = 1,
|
||||
.differential = 0,
|
||||
};
|
||||
|
||||
static struct adc_sequence seq = {
|
||||
.channels = BIT(1), // Channel bitmap
|
||||
.buffer = &sample,
|
||||
.buffer_size = sizeof(sample),
|
||||
.resolution = 12, // 12-bit
|
||||
};
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if(!device_is_ready(adc_dev)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Configure the channel
|
||||
if(adc_channel_setup(adc_dev, &ch1_cfg) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
printk("\r\n[ADC] PA1(ADVC1_IN1) polling\r\n");
|
||||
|
||||
while (1) {
|
||||
|
||||
int rc = adc_read(adc_dev, &seq);
|
||||
if(rc == 0) {
|
||||
uint16_t raw = sample;
|
||||
printk("ADC raw value = %u\r\n", raw);
|
||||
}
|
||||
|
||||
k_sleep(K_MSEC(50));
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user