help me with implementation of reading adc values and printing them via rs485, now it doesnt output anything, here are my files.
adc.h
#ifndef ADC_H
#define ADC_H
RCC->APB2PCENR |= RCC_ADC1EN;
// PCLK2 = 8 MHz ADC_CLK = 4 MHz
RCC->CFGR0 |= RCC_ADCPRE_DIV2;
//TODO: if moving average method does big uncertainity remake to median
// with max core clock
//
// PCLK2 - 72MHz / 6 = ADC_CLK = 12 MHz - max Adc speed
//RCC->CFGR0 |= RCC_ADCPRE_DIV6;
ADC1->CTLR2 |= ADC_JEXTSEL; // 1:1:1 JEXTTRIG software trigger
ADC1->CTLR2 |= ADC_JEXTTRIG; // conversion on external signal
// A1 and A2 channel
//ADC1->SAMPTR2 |= ADC_SMP0; // 1:1:1 239 cycles conversion
ADC1->SAMPTR2 &= ~(ADC_SMP1 | ADC_SMP2); //0:0:0 1.5 cycles conversion
ADC1->SAMPTR2 |= (ADC_SMP1_2 | ADC_SMP2_2); //1:0:0 45 cycles conversion
ADC1->ISQR |= (2 << 20); // two channel
ADC1->ISQR |= (1 << 15); // channel 1
ADC1->ISQR |= (2 << 10); // channel 2
ADC1->CTLR2 |= ADC_ADON;
// ADC_ResetCalibration(ADC1);
ADC1->CTLR2 |= ADC_RSTCAL;
// while(ADC_GetResetCalibrationStatus(ADC1));
while (ADC1->CTLR2 & ADC_RSTCAL) {}
// ADC_StartCalibration(ADC1);
ADC1->CTLR2 |= ADC_CAL;
// while (ADC_GetCalibrationStatus(ADC1));
while (ADC1->CTLR2 & ADC_CAL) {}
Calibrattion_Val = ADC1->RDATAR;
// Calibrattion_Val = Get_CalibrationValue(ADC1);
Assistant
}
void Adc::handler() { getAdc(); }
void Adc::sendDataOverRS485() {
uint8_t data[2];
data[0] = static_cast<uint8_t>(R >> 8); // High byte of R
data[1] = static_cast<uint8_t>(R & 0xFF); // Low byte of R
RS485::pThis->writeBytes(data, sizeof(data)); // Send the array over RS485
}
void Adc::getAdc() {
if (startConversion()) {
// started
} else {
if (isConversionCompleteted()) {
// exclude the last two bits to reduce ADC noise
R = ADC1->IDATAR1 & 0xFFFC;
// moving_average_R(ADC1->IDATAR1 & 0xFFFC);
// sendDataOverRS485();
}
}
}
actuator.cpp
#include "actuator.h"
Actuator* Actuator::pThis = nullptr;
Actuator::Actuator() {
//memset((void*)&dataReceived, 0, sizeof(ProtocolUart::DataReceive));
pThis = this;
init();
}
void Actuator::init() {}
void Actuator::handler() {
//----------------- adc handler ---------------------------------------
adc.handler();
#if 0
// TODO: experimentally get ADC_max and ADC_min to count percent
current_percent = adcToPercent(adc.averageR);
// TODO: check I,
// when actuator goes down, I ~ 200 mA, by controlling this I we can realize
// that something wrong
if (currentState == GO_DOWN) {
uint16_t mA = (5000 * adc.averageI) / 4095; // 5A max
if (mA > 400) {
// TODO: pause (to remove disturbing object)
stop();
// TODO dummy pause, need to remake later by timer
for (volatile int i = 0; i < 100000; ++i) {}
down_start();
}
}
#endif
//--------------- protocol communication ------------------------------
if (!RS485::pThis->getIsNeedRead()) {
// parse received data if it is
dataReceived =
ProtocolUart::parseData(RS485::pThis->arr, RS485::SIZE_RECEIVE);
RS485::pThis->setIsNeedRead(true);
if (!dataReceived.ERR) {
receivedNewCommand = true;
}
}
if (receivedNewCommand) {
receivedNewCommand = false;
// reply current status
if (dataReceived.command == ProtocolUart::STATUS) {
Buzzer::pThis->on();
for (int i = 0; i < 2000; i++) {}
Buzzer::pThis->off();
// hardcoded current percent of actuator's ADC
// TODO: get real ADC values
current_percent = 50;
uint8_t arr[5] = {ProtocolUart::TO,
static_cast<uint8_t>(Rollette::pThis->currentRollState),
current_percent, 0, 0};
uint16_t CS = ProtocolUart::crc_chk(arr, sizeof(arr) - 2);
arr[3] = static_cast<uint8_t>(CS >> 8);
arr[4] = static_cast<uint8_t>(CS & 0xFF);
RS485::pThis->writeBytes(arr, sizeof(arr));
// open rolette mechanism
} else if (dataReceived.command == ProtocolUart::GET_ADC) {
uint8_t arr[5] = {ProtocolUart::TO,
static_cast<uint8_t>(Adc::pThis->R),
0, 0, 0};
uint16_t CS = ProtocolUart::crc_chk(arr, sizeof(arr) - 2);
arr[3] = static_cast<uint8_t>(CS >> 8);
arr[4] = static_cast<uint8_t>(CS & 0xFF);
RS485::pThis->writeBytes(arr, sizeof(arr));
} else if (dataReceived.command == ProtocolUart::TURN_ON) {
Buzzer::pThis->on();
for (int i = 0; i < 2000; i++) {}
Buzzer::pThis->off();
// send acknowledgment msg
RS485::pThis->writeBytes(RS485::pThis->arr, RS485::SIZE_RECEIVE);
// proceed motor control
Rollette::pThis->rollette_open();
} else if (dataReceived.command == ProtocolUart::TURN_OFF) {
// Buzzer::pThis->on();
// send acknowledgment msg
RS485::pThis->writeBytes(RS485::pThis->arr, RS485::SIZE_RECEIVE);
Rollette::pThis->rollette_close();
// proceed motor control
// Rollette::pThis->stopMotor();
} else if (dataReceived.command == ProtocolUart::EMERGENCY_ON) {
IsEmergency = true;
RS485::pThis->writeBytes(RS485::pThis->arr, RS485::SIZE_RECEIVE);
} else if (dataReceived.command == ProtocolUart::EMERGENCY_OFF) {
IsEmergency = false;
RS485::pThis->writeBytes(RS485::pThis->arr, RS485::SIZE_RECEIVE);
} else {
// Other command reply is mirror
RS485::pThis->writeBytes(RS485::pThis->arr, RS485::SIZE_RECEIVE);
}
}
//-------------- State Machine ---------------------------------------
switch (currentState) {
#if 0
case DOWN:
if (current_percent < 2) {}
Rollette::pThis->handler(IsEmergency);
if (Rollette::pThis->currentRollState == Rollette::OPENED_FULL) {
// TODO: wait for up signal
if (IsOn) {
currentState = GO_UP;
}
// currentState = GO_UP;
Buzzer::pThis->turnOn();
down_stop();
up_start();
}
break;
case GO_UP:
Rollette::pThis->WasOpened = true;
// Emergency button press processing
if (!EmergencyOneTime) {
if (IsEmergency) {
stop();
EmergencyOneTime = true;
}
} else {
if (!IsEmergency) {
EmergencyOneTime = false;
down_stop();
up_start();
}
}
// wait for adc in some position
if (current_percent >= stop_percent) {
stop();
currentState = UP;
}
break;
case UP:
Buzzer::pThis->turnOff();
// wait for down signal or signal to adc position
if (receivedNewCommand) {
receivedNewCommand = false;
if (dataReceived.command == ProtocolUart::TURN_OFF) {
down_start();
timerStart(TIME_FULL_CLOSE_FROM_UP_TO_DOWN);
// checkout timer start
currentState = GO_DOWN;
} else if (dataReceived.command == ProtocolUart::NEW_PERCENT) {
// check direction
if (current_percent > stop_percent) {
// go down
up_stop();
down_start();
currentState = GO_TO_NEW_UP;
} else if (current_percent < stop_percent) {
// go up
down_stop();
up_start();
currentState = GO_TO_NEW_UP;
}
}
}
// by button
if (IsOff) {
down_start();
currentState = GO_DOWN;
}
break;
case GO_TO_NEW_UP:
// check stop condition
// Emergency button press processing
Buzzer::pThis->turnOn();
if (current_percent > stop_percent + 2) {
// go down
if (!EmergencyOneTime) {
if (IsEmergency) {
stop();
EmergencyOneTime = true;
}
} else {
if (!IsEmergency) {
EmergencyOneTime = false;
up_stop();
down_start();
}
}
} else if (current_percent < stop_percent - 2) {
// go up
if (!EmergencyOneTime) {
if (IsEmergency) {
stop();
EmergencyOneTime = true;
}
} else {
if (!IsEmergency) {
EmergencyOneTime = false;
down_stop();
up_start();
}
}
} else {
// stop percent +- 1
stop();
currentState = UP;
}
break;
case GO_DOWN:
Buzzer::pThis->turnOn();
// Emergency button press processing
if (!EmergencyOneTime) {
if (IsEmergency) {
stop();
EmergencyOneTime = true;
}
} else {
if (!IsEmergency) {
EmergencyOneTime = false;
up_stop();
down_start();
}
}
// TODO: wait for adc not changing and R percent is low
if (current_percent < 2) {
// adc not changed checkout
if (!AdcChanging) {
currentState = DOWN;
}
// timer checkout
if (IsTimeout) {
IsTimeout = false;
currentState = DOWN;
}
}
break;
#endif
default:
break;
}
int main(void) {
// Static polimorphic interrupt handling subscribing to timer
ObserverForInterrupt<TIM3_IRQn, RS485, Rollette, Actuator, Buzzer, Foto> timIrq;
while (1) {
act.handler();
#if 0
if (!Rollette::pThis->endstop.getState()) {
Buzzer::pThis->on();
for (int i = 0; i < 200; i++) {}
Buzzer::pThis->off();
}
#endif
// if (Rollette::pThis->endstop.getState()) {Buzzer::pThis->on();}
}
return 0;
}
To debug the issue of ADC values not being read or sent over RS485 in your C++ embedded system program, we need to dive into your code and check the ADC reading and RS485 transmitting processes. Here's a step-by-step approach to debug and enhance your implementation:
1. Ensure Initialization
ADC Initialization: Ensure the ADC is correctly configured for the inputs you intend to measure. Double-check your init() function logic to ensure all necessary settings are properly configured.
RS485 Initialization: Similarly, verify that your RS485 is properly initialized and that any relevant hardware settings (e.g., baud rate, data bits, parity) are correctly set up. This might involve setting the correct GPIO mode for communication and enabling necessary clocks.
2. Debug ADC Reading Logic
Start Conversion: Confirm that the ADC conversion is starting. Place a breakpoint or print debug information to check whether startConversion() returns true and the conversion starts.
Conversion Complete Flag: Ensure that ADC1->STATR & ADC_JEOC correctly checks if the conversion is complete.
Read ADC Values: Verify the readability of the ADC data registers and ensure masking (& 0xFFFC) aligns with your precision and noise reduction needs.
3. Debug RS485 Transmission
Check Data Transmission: Implement an explicit check or logging inside sendDataOverRS485() to verify that the function is being called and actually sending data via RS485.
Verify RS485 Reception: To confirm that data is being received, use an oscilloscope or logic analyzer to inspect RS485 signals and reception at the other end.
4. Log/Print Debug Information
Use printf statements with a connected hardware debugger or serial monitor:
void Adc::getAdc() {
if (startConversion()) {
printf("ADC Conversion Started\n");
} else {
if (isConversionCompleteted()) {
R = ADC1->IDATAR1 & 0xFFFC;
printf("ADC Conversion Completed: R = %d\n", R);
sendDataOverRS485();
}
}
}
void Adc::sendDataOverRS485() {
printf("Sending over RS485: R = %d\n", R);
uint8_t data[2];
data[0] = static_cast<uint8_t>(R >> 8); // High byte of R
data[1] = static_cast<uint8_t>(R & 0xFF); // Low byte of R
RS485::pThis->writeBytes(data, sizeof(data));
}
5. Consider Timing and Interrupts
Ensure you handle any timing or ADC conversion in a non-blocking manner. If needed, introduce delays or examine if interrupts might affect ADC or RS485 handling.
6. Cross-Check Communication Protocol
Double-check the higher-level communication protocol between the ADC application and RS485. Make sure that the expected sequence of operations matches the transmitting and receiving logic on both sides of the communication line.
By iteratively verifying each component and logging key information through the debug statements, you'll be able to trace and resolve the issue of the ADC not outputting values via RS485.