Letibobo
Newbie
Hello, need some helps please i have few time (one week) to translate an arduino code in MPLAB. The code allows us to convert analog value voltage to numerical signal. We have done this by use arduino software with arduino due board and now we want to have the fonctionalities in mplab. The problem is that there is not library for ADS1262 in the mplab so we need to code the bits ourselves. Please, there is someone here who can us please ? We are lost, THANK YOU so much in advance.
This is the code:
#include <SPI.h>
#include "ads1262.h" // Include the ADS1262 library header file
#define ADS1262_DRDY_PIN 6
#define ADS1262_CS_PIN 7
#define ADS1262_START_PIN 5
#define ADS1262_PWDN_PIN 4
ads1262 PC_ADS1262;
void setup() {
// Initialize SPI communication
SPI.begin();
// Set ADS1262 control pins as outputs
pinMode(ADS1262_DRDY_PIN, INPUT);
pinMode(ADS1262_CS_PIN, OUTPUT);
pinMode(ADS1262_START_PIN, OUTPUT);
pinMode(ADS1262_PWDN_PIN, OUTPUT);
// Initialize ADS1262
PC_ADS1262.ads1262_Init();
// Reset ADS1262 to ensure a clean start
PC_ADS1262.ads1262_Reset();
// Configure ADS1262 for differential input on AIN9 (channel 9)
PC_ADS1262.ads1262_Reg_Write(0x02, 0x20); // MUX0 register: Configure AINP = AIN9, AINN = AINCOM
// Enable start of conversions
PC_ADS1262.ads1262_Enable_Start();
Serial.begin(9600);
Serial.println("ADS1262 initialized successfully");
}
void loop() {
// Check for data ready (DRDY) signal from ADS1262
if (digitalRead(ADS1262_DRDY_PIN) == LOW) {
// Start reading data from ADS1262
PC_ADS1262.ads1262_Start_Read_Data_Continuous();
// Wait for data conversion to complete
delay(100); // Adjust delay as needed based on ADS1262 output data rate
// Read ADC data from ADS1262
char* data = PC_ADS1262.ads1262_Read_Data();
// Process ADC data (assuming 24-bit data format)
int32_t adcValue = ((int32_t)data[1] << 16) | ((int32_t)data[2] << 8) | (int32_t)data[3];
// Convert ADC data to voltage (assuming 2.5V reference and gain of 1)
float voltage = (adcValue * 2.5) / 8388607.0; // 2^23 - 1 (for 24-bit resolution)
// Read analog input from Arduino Due (A5) and convert to voltage
int analogValue = analogRead(A5);
float analogVoltage = analogValue * (3.3 / 1023.0); // Assuming 3.3V reference on Arduino Due
// Display both voltages on Serial Monitor
Serial.print("Readout voltage from ADS1262 (AIN9): ");
Serial.print(voltage);
Serial.println(" V");
Serial.print("Analog input voltage from Arduino Due (A5): ");
Serial.print(analogVoltage);
Serial.println(" V");
// Stop continuous data read mode
PC_ADS1262.ads1262_Stop_Read_Data_Continuous();
}
}
This is the code:
#include <SPI.h>
#include "ads1262.h" // Include the ADS1262 library header file
#define ADS1262_DRDY_PIN 6
#define ADS1262_CS_PIN 7
#define ADS1262_START_PIN 5
#define ADS1262_PWDN_PIN 4
ads1262 PC_ADS1262;
void setup() {
// Initialize SPI communication
SPI.begin();
// Set ADS1262 control pins as outputs
pinMode(ADS1262_DRDY_PIN, INPUT);
pinMode(ADS1262_CS_PIN, OUTPUT);
pinMode(ADS1262_START_PIN, OUTPUT);
pinMode(ADS1262_PWDN_PIN, OUTPUT);
// Initialize ADS1262
PC_ADS1262.ads1262_Init();
// Reset ADS1262 to ensure a clean start
PC_ADS1262.ads1262_Reset();
// Configure ADS1262 for differential input on AIN9 (channel 9)
PC_ADS1262.ads1262_Reg_Write(0x02, 0x20); // MUX0 register: Configure AINP = AIN9, AINN = AINCOM
// Enable start of conversions
PC_ADS1262.ads1262_Enable_Start();
Serial.begin(9600);
Serial.println("ADS1262 initialized successfully");
}
void loop() {
// Check for data ready (DRDY) signal from ADS1262
if (digitalRead(ADS1262_DRDY_PIN) == LOW) {
// Start reading data from ADS1262
PC_ADS1262.ads1262_Start_Read_Data_Continuous();
// Wait for data conversion to complete
delay(100); // Adjust delay as needed based on ADS1262 output data rate
// Read ADC data from ADS1262
char* data = PC_ADS1262.ads1262_Read_Data();
// Process ADC data (assuming 24-bit data format)
int32_t adcValue = ((int32_t)data[1] << 16) | ((int32_t)data[2] << 8) | (int32_t)data[3];
// Convert ADC data to voltage (assuming 2.5V reference and gain of 1)
float voltage = (adcValue * 2.5) / 8388607.0; // 2^23 - 1 (for 24-bit resolution)
// Read analog input from Arduino Due (A5) and convert to voltage
int analogValue = analogRead(A5);
float analogVoltage = analogValue * (3.3 / 1023.0); // Assuming 3.3V reference on Arduino Due
// Display both voltages on Serial Monitor
Serial.print("Readout voltage from ADS1262 (AIN9): ");
Serial.print(voltage);
Serial.println(" V");
Serial.print("Analog input voltage from Arduino Due (A5): ");
Serial.print(analogVoltage);
Serial.println(" V");
// Stop continuous data read mode
PC_ADS1262.ads1262_Stop_Read_Data_Continuous();
}
}