ADC and DAC affect each other #160
Replies: 1 comment
-
any variable used as a string should reserve space for the null char at the end analog[3]="000" I am not sure if that will give you problem or not but the way i trouble shoot is breaking the code down into sections and testing . Like first make sure you uart comms are working read uart and print out what it is receiving and make sure it is correct. write to gpio and make sure it is toggleing pin read adc and print out values. Once everything works independently then start combining and testing as you go. Yeah, it is |
Beta Was this translation helpful? Give feedback.
-
Hello, i have a little code for communication with a simulation software on pc.
The Code works well, when i comment out the part with the adc or with the dac. But together it does not work.
Could it be that it is because of the configuration of the adc oder the dac?
And the two adc Channels affect each other, too. When i connect a potentiometer to adc1, then adc2 reacts too.
Can someone help me, what i'am doing wrong here?
I was planning to convert arduino code to esp idf. the working arduino code is under point 2.
------- 1.) ------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "setup.h"
#include "driver/dac.h"
#include "driver/adc.h"
#define BUF_SIZE 1024
#define uart UART_NUM_0
#define gpio4 GPIO_NUM_4
#define gpio5 GPIO_NUM_5
#define btn1 15
#define btn2 32
#define high 1
#define low 0
static void task()
{
uint8_t data[6];
}
void app_main()
{
int baudrate = 115200; // Baudrate später per Tasten auswählbar.
}
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "driver/dac.h"
#include "driver/adc.h"
#define TXD_PIN 1
#define RXD_PIN 3
#define BUF_SIZE 1024
#define uart UART_NUM_0
void setup_uart(int baudrate){
uart_config_t uart_config = {
.baud_rate = baudrate,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
/.rx_flow_ctrl_thresh = 122,/
};
}
void setup_gpio(){
gpio_set_direction(4,GPIO_MODE_OUTPUT);
gpio_set_direction(5,GPIO_MODE_OUTPUT);
}
void setup_adc_dac(){
dac_output_enable(DAC_CHANNEL_1);
dac_output_enable(DAC_CHANNEL_2);
}
--------- 2.) ------- Arduino Code
const uint8_t DOUT1_PIN = 4;
const uint8_t DOUT2_PIN = 15;
const uint8_t AIN1_PIN = 32;
const uint8_t AIN2_PIN = 33;
const uint8_t DIN1_PIN = 34;
const uint8_t DIN2_PIN = 35;
void setup()
{
pinMode(DOUT1_PIN, OUTPUT);
pinMode(DOUT2_PIN, OUTPUT);
pinMode(DIN1_PIN, INPUT);
pinMode(DIN2_PIN, INPUT);
pinMode(AIN1_PIN, INPUT);
pinMode(AIN2_PIN, INPUT);
Serial.begin(38400);
}
// Kommandos von BORIS:
//
// !Dxy : Setze Digitalausgang x (x = 1,2) auf y (y = 0,1)
// !Axyyy : Setze Analogausgang x (x = 1,2) auf yyy (yyy = 000..255)
// ?Dx : Liefere Wert von Digitaleingang x (x = 1,2), Rückgabewert vom Arduino: 0,1
// ?Ax : Liefere Wert von Analogeingang x (x = 1,2), Rückgabewert vom Arduino: 0..4095
void loop()
{
while (Serial.available() && (Serial.peek() != '!') and (Serial.peek() != '?')) {Serial.read();} // auf ! oder ? warten
if (Serial.available() > 2)
{
char firstChar = (char)Serial.read();
// Wert setzen
if (firstChar == '!')
{
char varType = (char)Serial.read();
char varIndex = (char)Serial.read();
while (Serial.available() < 1) {};
if (varType == 'D')
{
char varDValue = (char)Serial.read();
if (varIndex == '1')
{
if (varDValue == '0') {digitalWrite(DOUT1_PIN, LOW);} else {digitalWrite(DOUT1_PIN, HIGH);}
}
if (varIndex == '2')
{
if (varDValue == '0') {digitalWrite(DOUT2_PIN, LOW);} else {digitalWrite(DOUT2_PIN, HIGH);}
}
}
if (varType == 'A')
{
String valueStringInt = "000";
while (Serial.available() < 3) {};
valueStringInt.setCharAt(0, (char)Serial.read());
valueStringInt.setCharAt(1, (char)Serial.read());
valueStringInt.setCharAt(2, (char)Serial.read());
if (varIndex == '1') {dacWrite(DAC1, valueStringInt.toInt());}
if (varIndex == '2') {dacWrite(DAC2, valueStringInt.toInt());}
}
Serial.println("OK");
}
// Wert lesen
if (firstChar == '?')
{
char varType = (char)Serial.read();
char varIndex = (char)Serial.read();
if (varType == 'A')
{
if (varIndex == '1') {Serial.println(analogRead(AIN1_PIN));}
if (varIndex == '2') {Serial.println(analogRead(AIN2_PIN));}
}
if (varType == 'D')
{
if (varIndex == '1') {Serial.println(digitalRead(DIN1_PIN));}
if (varIndex == '2') {Serial.println(digitalRead(DIN2_PIN));}
}
}
}
}
Beta Was this translation helpful? Give feedback.
All reactions