I have now managed to use the RMT function to access data from my GPIO, but the output only shows 1,1,1,1,1,1.
Now how do I make it write the analog value 0 - 4096 of this GPIO to the buffer?
Here my code:
short explanation. A 1kHz signal is generated with GPIO 32 and read back on GPIO 35. Now the analogue value (0-4096) should be written into the buffer.
Code: Select all
#include <Arduino.h>
#include <esp_err.h>
#include <esp_log.h>
#include <driver/rmt.h>
#include <driver/periph_ctrl.h>
#include <soc/rmt_reg.h>
// PWM
#define PWM1_Ch 0
#define PWM1_Res 10 // 10 bits = 1024 levels = 0,05859375A per step
#define PWM1_Freq 1000 // 980 - 1020 range, 1000 nominal
int PWM1_DutyCycle = 1000; // 0= -12V 1000= 12V
#define CTRL_PILOT 32
#include <driver/adc.h>
//#define DATA_433 GPIO_NUM_35
#define ADC1_GPIO (adc1_channel_t)35
#define RMT_RX_CHANNEL RMT_CHANNEL_0
#define RMT_TICK_10_US (80000000/RMT_CLK_DIV/100000) /*!< RMT counter value for 10 us.(Source clock is APB clock) */
void setup() {
Serial.begin(115200);
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ADC1_GPIO, ADC_ATTEN_DB_11);
// Configure RMT
rmt_config_t rmt_rx;
rmt_rx.channel = (rmt_channel_t)RMT_RX_CHANNEL; // RMT channel
rmt_rx.gpio_num = (gpio_num_t)ADC1_GPIO; // GPIO pin
rmt_rx.clk_div = 80; // Clock divider
rmt_rx.mem_block_num = 1; // number of mem blocks used
rmt_rx.rmt_mode = RMT_MODE_RX; // Receive mode
rmt_rx.rx_config.filter_en = false; // Enable filter
// rmt_rx.rx_config.filter_ticks_thresh = 100; // Filter all shorter than 100 ticks
rmt_rx.rx_config.idle_threshold = 1000; // Timeout after 2000 ticks
rmt_config(&rmt_rx); // Init channel
rmt_driver_install(rmt_rx.channel, 4000, 0); // Install driver with ring buffer size 1000
rmt_rx_start(RMT_RX_CHANNEL, true);
ledcAttachPin(CTRL_PILOT, PWM1_Ch);
ledcSetup(PWM1_Ch, PWM1_Freq, PWM1_Res);
ledcWrite(PWM1_Ch, 500);
}
void loop() {
RingbufHandle_t rb = NULL;
uint32_t cnt = 0;
Serial.print("\n\rTEST");
// Get RMT RX ring buffer
rmt_get_ringbuf_handle(RMT_RX_CHANNEL, &rb);
if (rb) {
size_t rx_size = 0;
rmt_item32_t* item = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, 500);
while (item) {
for (uint32_t i = 0; i < rx_size / sizeof(rmt_item32_t); i++) {
Serial.print(item[i].duration0);
Serial.print(",");
}
Serial.println();
vRingbufferReturnItem(rb, (void*) item);
item = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, 500);
}
}
delay(100);
/*
RingbufHandle_t rb = NULL;
uint32_t cnt = 0;
//get RMT RX ringbuffer
rmt_get_ringbuf_handle((rmt_channel_t)RMT_RX_CHANNEL, &rb);
rmt_rx_start((rmt_channel_t)RMT_RX_CHANNEL, 1);
if(rb) {
size_t rx_size = 0;
//try to receive data from ringbuffer.
//RMT driver will push all the data it receives to its ringbuffer.
//We just need to parse the value and return the spaces of ringbuffer.
rmt_item32_t* item = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, 500);
while(item) {
char buffer[120];
rmt_item32_t *it = item;
for(uint32_t i=0;i<rx_size/sizeof(rmt_item32_t);i++)
{
Serial.print("here");
Serial.print(buffer);
it++;
}
vRingbufferReturnItem(rb, (void*) item);
item = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, 500);
}
}
Serial.println("No more items.");
*/
}