Author Topic: ADC MAX11603  (Read 463 times)

0 Members and 1 Guest are viewing this topic.

Offline juicycrunchwrapTopic starter

  • Contributor
  • Posts: 21
  • Country: au
ADC MAX11603
« on: July 21, 2023, 02:55:08 pm »
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!
 

Offline m k

  • Super Contributor
  • ***
  • Posts: 2038
  • Country: fi
Re: ADC MAX11603
« Reply #1 on: July 23, 2023, 07:18:41 pm »
You can always morse blink the led.
External terminal is finally quite a bit more practical.
Advance-Aneng-Appa-AVO-Beckman-Data Tech-Fluke-General Radio-H. W. Sullivan-Heathkit-HP-Kaise-Kyoritsu-Leeds & Northrup-Mastech-REO-Simpson-Sinclair-Tektronix-Tokyo Rikosha-Triplett-YFE
(plus lesser brands from the work shop of the world)
 

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 674
  • Country: us
Re: ADC MAX11603
« Reply #2 on: July 24, 2023, 07:09:09 am »
Bring up the UART. Your board should have two. Use that for the debug output.
 

Offline juicycrunchwrapTopic starter

  • Contributor
  • Posts: 21
  • Country: au
Re: ADC MAX11603
« Reply #3 on: July 24, 2023, 02:15:22 pm »
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 flag
    timerCount++; // Increment the timer count

    if (timerCount >= DELAY_SECONDS) {
        timerCount = 0; // Reset the timer count
        timerFlag = 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:1
    TMR1 = 0; // Clear Timer1 value
    PR1 = 50000; // Set the period register for a 1-second delay (assuming 8 MHz Fosc)62500 20000
    IFS0bits.T1IF = 0; // Clear the Timer1 interrupt flag
    IEC0bits.T1IE = 1; // Enable Timer1 interrupt
    T1CONbits.TON = 1; // Enable Timer1
}

void delay() {
    timerFlag = false;
    while (!timerFlag) {

    }
}
   
    //TEST I2C 3
    LED_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 = 0
    send_i2c2_byte(0x80);



    while (1) {
        send_i2c2_byte(0x61);//AIN0
        //send_i2c2_byte(0x63);//AIN1
        i2c2_restart();
        send_i2c2_byte((MAX_ADDRESS << 1 | 0x01));  //R/W = 1
        data = 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 !
 

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 674
  • Country: us
Re: ADC MAX11603
« Reply #4 on: July 24, 2023, 03:45:54 pm »
The code expected to have 3 recognizable parts:

1. Initialization of the ADC and if you want interrupts, the interrupt system.
2. Triggering a measurement
3. Getting a result

Ideally, 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.
 

Offline juicycrunchwrapTopic starter

  • Contributor
  • Posts: 21
  • Country: au
Re: ADC MAX11603
« Reply #5 on: July 24, 2023, 04:24:31 pm »
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: [Select]
ADC.c :
#define MAX_ADDRESS 0x6D

void init_ADC(){
    i2c2_init();
    send_i2c2_byte(MAX_ADDRESS << 1); // R/W = 0
    send_i2c2_byte(0x80); // Setup register
    send_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 result
    result = (result << 8) | receive_i2c2_byte(1);
    INV_LAT = INV_ON;
    __delay_ms(result);
    INV_LAT = INV_OFF;
    __delay_ms(result);
}

Code: [Select]
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     0

   
void init_ADC();
void mesurement_ADC();
void result_ADC();

Code: [Select]
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 !
 

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 674
  • Country: us
Re: ADC MAX11603
« Reply #6 on: July 24, 2023, 06:52:06 pm »
Which line checks if the ADC finished the conversion or not?
 

Offline juicycrunchwrapTopic starter

  • Contributor
  • Posts: 21
  • Country: au
Re: ADC MAX11603
« Reply #7 on: July 24, 2023, 06:59:06 pm »
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 ...  :-\
 

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 674
  • Country: us
Re: ADC MAX11603
« Reply #8 on: July 24, 2023, 07:54:07 pm »
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.
 

Offline juicycrunchwrapTopic starter

  • Contributor
  • Posts: 21
  • Country: au
Re: ADC MAX11603
« Reply #9 on: July 24, 2023, 08:42:25 pm »
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)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf