split from "ADC + Loop = Confusion"

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[cliff: these posts have been split out of a previous thread]

Hi I am a noob to AVR and C as well and I am having some problem with synchronising 2ADCs with AVR162. I am using ADS1252 - 24 bits resolution.

I use 'volatile unsigned char timp1 and timp2' to get 8 bits of data, then use 'long char data1 and data2' get the data by shifting bytes out to PORTA. The waveforms I got weren't what I expected. Can someone please look at my code and tell me what I got wrong? Thanks!

#include 
#include 

#define F_CPU 1000000UL

volatile unsigned char RDY, timp1, timp2;
long int data1, data2;

void init (void) {

	DDRC = 0x00;      // PORTC is input
	DDRB = 0xFF;       // PORTB is output
	DDRA = 0xFF;         // PORTA is output

	PORTA = 0x00;
	PORTB = 0x00;

	RDY = 0x00;

	MCUCR |= 0x03;  // set up interrupt (0x03 shows rising edge interrupt)
	GICR |= 0x40; //enable pin INT0 for use
	sei(); //interrupt enable
}

ISR (INT0_vect) {  //interrupt function with its vector
    RDY = 0xFF;     //if there's input, interrupt happends and set flag high
    cli();
}

int main (void) {

    int k, i;

    init();

	while (1) {

        if (RDY == 0xFF) {      //if interrupt flag is set high, start taking data

            timp1 = 0x00;
            timp2 = 0x00;
            data1 = 0x00;
            data2 = 0x00;

            TCCR1B |= (1 << CS10); // Set up timer

            while (TCNT1 <= 2304) //reset ADCs - 6conversion cycles
            {
                PORTB |= 0x02;  //SCLK high for 2304
            }

            PORTB &= ~0x02;  // then SCLK is set low

            TCNT1 = 0; // Reset timer value

            while (TCNT1 <= 40) {}  //waste at least 36CLK for DRDY mode

            for (i=2; i>=0; i--)  //long char takes 3 bytes data - 24bits
            {

                for (k=7; k>=0; k--) {  //int takes 8bits data - 1byte

                    timp1 |= (PINC0 << k);  //variables to store 8bits data
                    timp2 |= (PINC2 << k);

                    PORTB |= 0x02;  //toggle SCLK
                    PORTB &= ~0x02;
                }

                data1 |= timp1 << (i*8);    //atfer taking 8bits data, shift bytes in data1
                data2 |= timp2 << (i*8);

                PORTA = data1;      //bytes of data is shifted out through PORTA for observation
                PORTA = data2;

            }

            RDY = 0x00;
            sei();
        }
    }
	return(0);
}

My code might seems silly but please forgive me, I am a serious noob. THANKS IN ADVANCE! :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi I am a noob to AVR and C as well and I am having some problem with synchronising 2ADCs with AVR162. I am using ADS1252 - 24 bits resolution.

I use 'volatile unsigned char timp1 and timp2' to get 8 bits of data, then use 'long char data1 and data2' get the data by shifting bytes out to PORTA. The waveforms I got weren't what I expected. Can someone please look at my code and tell me what I got wrong? Thanks!

#include 
#include 

#define F_CPU 1000000UL

volatile unsigned char RDY, timp1, timp2;
long int data1, data2;

void init (void) {

	DDRC = 0x00;      // PORTC is input
	DDRB = 0xFF;       // PORTB is output
	DDRA = 0xFF;         // PORTA is output

	PORTA = 0x00;
	PORTB = 0x00;

	RDY = 0x00;

	MCUCR |= 0x03;  // set up interrupt (0x03 shows rising edge interrupt)
	GICR |= 0x40; //enable pin INT0 for use
	sei(); //interrupt enable
}

ISR (INT0_vect) {  //interrupt function with its vector
    RDY = 0xFF;     //if there's input, interrupt happends and set flag high
    cli();
}

int main (void) {

    int k, i;

    init();

	while (1) {

        if (RDY == 0xFF) {      //if interrupt flag is set high, start taking data

            timp1 = 0x00;
            timp2 = 0x00;
            data1 = 0x00;
            data2 = 0x00;

            TCCR1B |= (1 << CS10); // Set up timer

            while (TCNT1 <= 2304) //reset ADCs - 6conversion cycles
            {
                PORTB |= 0x02;  //SCLK high for 2304
            }

            PORTB &= ~0x02;  // then SCLK is set low

            TCNT1 = 0; // Reset timer value

            while (TCNT1 <= 40) {}  //waste at least 36CLK for DRDY mode

            for (i=2; i>=0; i--)  //long char takes 3 bytes data - 24bits
            {

                for (k=7; k>=0; k--) {  //int takes 8bits data - 1byte

                    timp1 |= (PINC0 << k);  //variables to store 8bits data
                    timp2 |= (PINC2 << k);

                    PORTB |= 0x02;  //toggle SCLK
                    PORTB &= ~0x02;
                }

                data1 |= timp1 << (i*8);    //atfer taking 8bits data, shift bytes in data1
                data2 |= timp2 << (i*8);

                PORTA = data1;      //bytes of data is shifted out through PORTA for observation
                PORTA = data2;

            }

            RDY = 0x00;
            sei();
        }
    }
	return(0);
}

My code might seems silly but please forgive me, I am a serious noob. THANKS IN ADVANCE! :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Aww it looks messy with the comments, I am sorry

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Hi I am a noob

Which is apparent since you are hijacking someone else's thread instead of creating your own.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Haha. Indeed, I thought that we can share forums as the title of bugmenott's thread is similar to the problem that I am having here?

Should I delete those post and create a new thread? SORRY.. please advice :oops:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
as the title of bugmenott's thread is similar to the problem that I am having here

It is not similar at all. He is using the internal ADC and you are using an external one. The operation of the two are completely different.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can split out the thread hijack to a separate thread if required?

Cliff [moderator]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, please clawson. Thanks. I am terribly sorry :(