[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! :)