Hi there I am using ADS1252 and AVR644p. I think I am having some problem with my USARTs.
My AVR has 2 USARTs and I am configuring USART0 in SPI mode - for reading from ADC.
Data is stored in UDR0 (data register of USART0 - ADC utility)and saved as to Data[2..0]- 3 bytes,
then ToUsart[2..0] = Data[2..0],
and lastly assigning ToUsart[2..0] to UDR1 (data register of USART1 -RS232 utility)
My code reading from ADC using USARTinSPImode looks like this
//function for reading data from adc using usart spi void USARTSPI_ReadADS1252(void) { //declare 8bit WatchDRDY to detect DRDY mode unsigned char WatchDRDy; //Watch for Data ready WatchDRDy = (PINC & 0x02 ); //if Data ready occurs, do nothing while (WatchDRDy == (PINC & 0x02 )) { //do nothing }; // make the whole delay to 22us to pass through the DRDY mode _delay_us ( 4); //PORTC0 toggle for debugging purpose PORTC ^= (1); //Write to data register to initiate data transfer UDR0= 0; //Wait till you've received 8 bits //Do nothing when Receive Complete Flag is ZERO ( means when it has not yet completely received data) while ((USART_CSR_A_Receive_Flag & UCSR0A) == 0) { } //Read the data //UDR0 is the 8- bit USART Data Register Data[2] = UDR0; UDR0 = 0; while ((USART_CSR_A_Receive_Flag & UCSR0A) == 0) { } Data[1] = UDR0; UDR0 = 0; while ((USART_CSR_A_Receive_Flag & UCSR0A) == 0) { } Data[0] = UDR0; };
My USART_rs232 code looks like this
void USART_RS232_Interfacing(void) { //formula to delay and count 8 times - to fool the optimiser int Loop_StopFlag = 0; int Loop = 0; while ( Loop_StopFlag == 0) { Loop = (Loop + 1) & 7; if (Loop == 0) { Loop_StopFlag = 1; break; } } //at the 8th count, Loop will give 0 if (Loop_StopFlag == 1 ) { //get Data[] into another variable ToUsart1[] ToUsart1[0] = Data[0]; ToUsart1[1] = Data[1]; ToUsart1[2] = Data[2]; //stream location counter initialisation int streamLoc = 0; //stream out data from ToUsart1[0..2] while (streamLoc < 3) { // Send out the byte value in the variable ToUsart[0..2] to dara register of Usart_RS232 UDR1 = ToUsart1[streamLoc]; // Do nothing until UDR is ready for more data to be written to it by checking register empty flag while ((UCSR1A & USART_CSR_A_Data_Register_Empty_Flag ) == 0) {} // increment stream location counter 0..2 streamLoc++; } while ((UCSR1A & USART_CSR_A_Data_Register_Empty_Flag ) == 0) {} }
On oscilloscope, the data sampling timing controlled by XCK0 shows inconsistant sampling intervals. (The interval between each set of 24bits data logging is irregular). I think this is a problem caused by UART. Can you see how?
Thanks in advanced :D