ADC MAX11603 - Page 1
Forum section: electronics / beginners

author: juicycrunchwrap
author_rank: Contributor
timestamp: July 21, 2023, 02:55:08 pm
content: Hi guys,I am communicating via I2C between a PIC24FJ128GA110 and an ADC MAXIM11603. Currently, to get the result of the conversion, I am toggling a LED and the delay between the ON state and the OFF state is the result of this conversion. So when I put no voltage in AN0 for example, it blinks really fast, and when I put like 12V, it blinks a little slower. But I cannot get the value of the result, I can just know if there is a voltage or not, so I was wondering if any of you had an idea on how to get the proper value of the result of the conversion ? As I am working on MPLAB, I don't know if/where I can print anything like I would do in a regular terminal.Thank you for y'all help!

author: m k
author_rank: Super Contributor
timestamp: July 23, 2023, 07:18:41 pm
content: You can always morse blink the led.External terminal is finally quite a bit more practical.

author: dobsonr741
author_rank: Frequent Contributor
timestamp: July 24, 2023, 07:09:09 am
content: Bring up the UART. Your board should have two. Use that for the debug output.

author: juicycrunchwrap
author_rank: Contributor
timestamp: July 24, 2023, 02:15:22 pm
content: Thank you. I will try to use the LED and the the uart.The thing is don't understand, and I realized that only later, is that I toggle the LED based on the result of the ADC, but for some reason, the 'ON' time is wayy shorter than the 'OFF' time (see attachement).I have included my code under and I just wonder how is this possible considering I give the same delay.//ADC integration//void __attribute__((interrupt, auto_psv)) _T1Interrupt(void) {IFS0bits.T1IF = 0; // Clear the Timer1 interrupt flagtimerCount++; // Increment the timer countif (timerCount >= DELAY_SECONDS) {timerCount = 0; // Reset the timer counttimerFlag = true; // Set the flag to indicate the delay has elapsed}}void configureTimer() {T1CONbits.TON = 0;T1CONbits.TCS = 0; // Select internal instruction cycle clock (Fosc/2)T1CONbits.TCKPS = 0b01; // Set prescaler to 1:1TMR1 = 0; // Clear Timer1 valuePR1 = 50000; // Set the period register for a 1-second delay (assuming 8 MHz Fosc)62500 20000IFS0bits.T1IF = 0; // Clear the Timer1 interrupt flagIEC0bits.T1IE = 1; // Enable Timer1 interruptT1CONbits.TON = 1; // Enable Timer1}void delay() {timerFlag = false;while (!timerFlag) {}}//TEST I2C 3LED_IGN_TRIS = LED_OUTPUT;LED_IGN_LAT = LED_OFF;INV_TRIS = INV_OUTPUT;INV_LAT = INV_OFF;unsigned char data;i2c2_init();configureTimer();send_i2c2_byte(MAX_ADDRESS << 1); //R/W = 0send_i2c2_byte(0x80);while (1) {send_i2c2_byte(0x61);//AIN0//send_i2c2_byte(0x63);//AIN1i2c2_restart();send_i2c2_byte((MAX_ADDRESS << 1 | 0x01)); //R/W = 1data = receive_i2c2_byte(0);data = receive_i2c2_byte(1);LED_IGN_LAT = LED_ON;INV_LAT = INV_ON;__delay_ms(data);LED_IGN_LAT = LED_OFF;INV_LAT = INV_OFF;delay();}return 0;tbh i'm not sure of what I should even get as a result, it's just weird because I can see a difference in voltage but idk if it makes sense. So if any one had ideas or knew which result of the conversion I should obtain I would be really grateful !Thanls y'all !

author: dobsonr741
author_rank: Frequent Contributor
timestamp: July 24, 2023, 03:45:54 pm
content: The code expected to have 3 recognizable parts:1. Initialization of the ADC and if you want interrupts, the interrupt system.2. Triggering a measurement3. Getting a resultIdeally, the code blocks should be functions, named accordingly. It will help to review and will help you later when revisit this code. Using interrupts is rather an advanced topic, it would be easier to bring up the ADC with polling/waiting for the ready flag.

author: juicycrunchwrap
author_rank: Contributor
timestamp: July 24, 2023, 04:24:31 pm
content: Thank you so much for your reply. I created a ADC.c file where I have my function blocks named accordingly as you suggested.Code:ADC.c :#define MAX_ADDRESS 0x6Dvoid init_ADC(){i2c2_init();send_i2c2_byte(MAX_ADDRESS << 1); // R/W = 0send_i2c2_byte(0x80); // Setup registersend_i2c2_byte(0x61);// Configuration register for AIN0}void mesurement_ADC(){send_i2c2_byte((MAX_ADDRESS << 1 | 0x01)); // R/W = 1}void result_ADC(){INV_TRIS = INV_OUTPUT;INV_LAT = INV_OFF;unsigned int result;__delay_ms(10);result = receive_i2c2_byte(0); // Read the MSB of the ADC resultresult = (result << 8) | receive_i2c2_byte(1);INV_LAT = INV_ON;__delay_ms(result);INV_LAT = INV_OFF;__delay_ms(result);}Code:ADC.h#define INV_LAT LATGbits.LATG6 //E#define INV_TRIS TRISGbits.TRISG6#define INV_INPUT 1#define INV_OUTPUT 0#define INV_ON 1#define INV_OFF 0void init_ADC();void mesurement_ADC();void result_ADC();Code:main.c :init_ADC();while(1){mesurement_ADC();result_ADC();}And i tried not to use any interrupts. I still have this thing where the 'OFF' time of the LED is longer than the 'ON' time while I have put on the same delay and I reckon I have no idea why. Maybe it's because I forget something in the main, at this point I wonder if it even does any conversion.Thanks again for your help !

author: dobsonr741
author_rank: Frequent Contributor
timestamp: July 24, 2023, 06:52:06 pm
content: Which line checks if the ADC finished the conversion or not?

author: juicycrunchwrap
author_rank: Contributor
timestamp: July 24, 2023, 06:59:06 pm
content: The adc in question is a MAX11603. Unfortunately, i think there is no ack flag in the status register of this ADC so i don't know how to figure if the conversion is complete or not ...

author: dobsonr741
author_rank: Frequent Contributor
timestamp: July 24, 2023, 07:54:07 pm
content: You’ll need to go trough the datasheet, decide which config options to use (clock mode, scan mode). One involves I2C clock stretching. There is fifo and internal RAM involved, you’ll need to understand all of those to be able to handle the various situations.

author: juicycrunchwrap
author_rank: Contributor
timestamp: July 24, 2023, 08:42:25 pm
content: Thank you for your help. I've kinda already done that, but I don't understand why clock stretching would be useful in my case. In the end I just want to have the result of the conversion (i don't have a lot of experience with microcontrollers sorry)