Dear Sir,
I am interfacing ADS1271 for one of my Automation project using PIC IC. I am Getting very unstable Counts which fluctuates in 10 lakhs and its not getting saturated with Vref also.
I have been working for long time. I am using High Resolution Mode, SPI interface. But the DRDY is coming inverse with ref to datasheet. I could not find the problem.
Since its time bound project pl guide me at the earliest. Here with I attach my shematic and Signal images and code.
/* Project Name : Square Wave Function Gen Hardware Dev. : Vinayagam/Radha Software Dev. : Radha Description : -------------- This Program Get the user input for frequncey OP and Gen op in RB3 */ #include<htc.h> #include<stdio.h> #include<stdlib.h> #define LD4 RB5 #define LD3 RB4 #define LD2 RB3 #define LD1 RB2 #define LCD_RW RC0 #define LCD_RS RB1 #define LCD_CS RB0 #define AD_SCLK RA0 #define AD_SYN RA1 #define AD_DRDY RA4 #define AD_SDATA RA5 __CONFIG(0x0FE4); //07e4 // _CONFIG2(0x08FF); // Variables // signed long ADCSampleVal; unsigned char TMPSTR[30]; /* union { struct { unsigned BIT0 : 1; unsigned BIT1 : 1; unsigned BIT2 : 1; unsigned BIT3 : 1; unsigned BIT4 : 1; unsigned BIT5 : 1; unsigned BIT6 : 1; unsigned BIT7 : 1; }; unsigned char DISPDATA; }DOT_DATA; */ union { struct { unsigned char LcdBit0 : 1 ; unsigned char LcdBit1 : 1 ; unsigned char LcdBit2 : 1 ; unsigned char LcdBit3 : 1 ; unsigned char LcdBit4 : 1 ; unsigned char LcdBit5 : 1 ; unsigned char LcdBit6 : 1 ; unsigned char LcdBit7 : 1 ; } LCDbit ; unsigned char all ; }LCDDATA ; unsigned char T_DATA; unsigned char CHARBUF[5]; unsigned char TMPCHBUF[6]; unsigned char DelayOver =0; unsigned char RXDATA= 0; unsigned char Count = 0; unsigned long Com_DlyCount=0; bit TMPD_DATA; bit TXBUFEMPTY; bit RXPRESENT; bit STARTBYTE = 0; bit ENDBYTE = 0; bit NO_COMDATA = 0; bit UPDATE = 0; bit STARPRV_FLAG = 0; void interrupt ISR() { // Serial Int Function if(TXIF) { TXIF = 0; TXIE=0; TXBUFEMPTY = 1; } if(RCIF) { if(RCREG== '%')Count=0; TMPCHBUF[Count++] =RCREG; if(Count>4) { if((TMPCHBUF[0]=='%') && (TMPCHBUF[4]=='$')) { ENDBYTE=1; Com_DlyCount = 0; } Count=0; } } if(TMR1IF) { DelayOver = 1; if(++Com_DlyCount > (62000 * 2)) // 5 Sec { NO_COMDATA=1; } /* if(UPDATE) { if(DISPSCAN >= 4)DISPSCAN = 0;BYTE_NO =0;BIT_DATA=0; if(BYTE_NO >= 16) { BYTE_NO = 0; BIT_DATA = 0; DISPSCAN++; ENABLE = 1; NOP(); LATCH = 1; NOP(); LATCH = 1; NOP(); } if(DISPSCAN==0){ LOW_ADD=0; LOWST_ADD =0;} else if(DISPSCAN==1){LOW_ADD=1; LOWST_ADD =1;} else if(DISPSCAN==2){LOW_ADD=0; LOWST_ADD =1;} else if(DISPSCAN==3){LOW_ADD=1; LOWST_ADD =0;} ENABLE = 0; NOP(); if(BIT_DATA >=8) BIT_DATA=0; BYTE_NO++; DOT_DATA.DISPDATA = DISPBUFFER[((DISPSCAN * 16 ) + BYTE_NO)]; // Locate the data DOT_DATA.DISPDATA = ~DOT_DATA.DISPDATA; // Have to edit in fut if(BIT_DATA==0)TMPD_DATA = DOT_DATA.BIT0; else if(BIT_DATA == 1) TMPD_DATA = DOT_DATA.BIT1; else if(BIT_DATA == 2) TMPD_DATA = DOT_DATA.BIT2; else if(BIT_DATA == 3) TMPD_DATA = DOT_DATA.BIT3; else if(BIT_DATA == 4) TMPD_DATA = DOT_DATA.BIT4; else if(BIT_DATA == 5) TMPD_DATA = DOT_DATA.BIT5; else if(BIT_DATA == 6) TMPD_DATA = DOT_DATA.BIT6; else if(BIT_DATA == 7) TMPD_DATA = DOT_DATA.BIT7; CLK = 0; NOP(); D_DATA = TMPD_DATA; NOP(); CLK = 1; NOP(); BIT_DATA++; } */ TMR1IF = 0; TMR1H = 0xFF; TMR1L = 0x60; T1CON = 0x41; } } void Delay100Micro(unsigned long value) { for(;value>0;value--) { DelayOver = 0; while(!DelayOver); } } void Delay_ms(unsigned char dval) { for(;dval>0;dval--) { Delay100Micro(100);//10 } } void Delay1Sec(unsigned char dval) { for(;dval>0;dval--) { Delay100Micro(10000); } } void XmitData(unsigned char Txdata) { // while(!TXBUFEMPTY); //Check Buffer is empty B4 sending the data TXREG = Txdata ; while(TXBUFEMPTY); // Wait till data sent } void txn() { unsigned char k; for(k=0;k<5;k++) { if(TXBUFEMPTY) { TXBUFEMPTY=0; TXIE=1; // TXREG=DISPBUFFER[k]; } } TXIE=1; TXREG=' '; } unsigned char getch() { /* retrieve one byte */ while(!RCIF) /* set when register is not empty */ continue; return RCREG; } void putch(unsigned char byte) { /* output one byte */ while(!TXIF) /* set when register is empty */ continue; TXREG = byte; } void InitLCDPortIO(void) { TRISB &= 0; } void LCD_Strobe(void) { LCD_CS=1; Delay_ms(1); LCD_CS=0; } void Write2Lcd(unsigned char LData2Write) { Delay_ms(1); LCDDATA.all = LData2Write; LD1 = LCDDATA.LCDbit.LcdBit4;NOP(); LD2 = LCDDATA.LCDbit.LcdBit5;NOP(); LD3 = LCDDATA.LCDbit.LcdBit6;NOP(); LD4 = LCDDATA.LCDbit.LcdBit7;NOP(); LCD_Strobe(); LD1 = LCDDATA.LCDbit.LcdBit0;NOP(); LD2 = LCDDATA.LCDbit.LcdBit1;NOP(); LD3 = LCDDATA.LCDbit.LcdBit2;NOP(); LD4 = LCDDATA.LCDbit.LcdBit3;NOP(); LCD_Strobe(); } void ClearLcd(void) { LCD_RS = 0; Write2Lcd(0x1); Delay_ms(2); } void InitLCDDisplay(void) { //initialises the display InitLCDPortIO(); LCD_RW =0; NOP(); LCD_RS =0; NOP(); LCD_CS =0; NOP(); Delay_ms(15); // wait 15mSec after power applied, LCDDATA.all = 0x30; LD1 = LCDDATA.LCDbit.LcdBit4; NOP(); LD2 = LCDDATA.LCDbit.LcdBit5; NOP(); LD3 = LCDDATA.LCDbit.LcdBit6; NOP(); LD4 = LCDDATA.LCDbit.LcdBit7; NOP(); LCD_Strobe(); Delay_ms(5); LCD_Strobe(); Delay_ms(1); LCD_Strobe(); Delay_ms(1); LCDDATA.all = 0x20; LD1 = LCDDATA.LCDbit.LcdBit4; NOP(); LD2 = LCDDATA.LCDbit.LcdBit5; NOP(); LD3 = LCDDATA.LCDbit.LcdBit6; NOP(); LD4 = LCDDATA.LCDbit.LcdBit7; NOP(); // Four bit mode LCD_Strobe(); Write2Lcd(0x28); Write2Lcd(0x14); Write2Lcd(0x0C); // Write2Lcd(0xF); ClearLcd(); // Clear screen Write2Lcd(0x6); // Set entry Mode } void lcd_puts(const char * s) { LCD_RS = 1; // write characters NOP(); LCD_RW = 0; NOP(); while(*s) Write2Lcd(*s++); } /* write one character to the LCD */ void lcd_putch(char c) { LCD_RS = 1; NOP(); // write characters LCD_RW = 0; NOP(); Write2Lcd( c ); } /* * Go to the specified position */ void lcd_goto(unsigned char pos) { LCD_RS = 0;NOP(); if(pos < 16) Write2Lcd(0x80 |pos); else Write2Lcd(0xc0 |(pos - 16)); } void ReadADCCounts(void) { unsigned char ADI; signed long ADCOUNTSDATA; ADCOUNTSDATA = 0; ADCSampleVal = 0; AD_SCLK = 1; AD_SYN = 1; AD_DRDY == 1; Delay_ms(1); // AD_SYN = 0; // Delay_ms(1); // AD_SYN = 1; //START Conversion AD_SDATA = 1; while(AD_DRDY == 1); Delay100Micro(10); for(ADI = 0; ADI < 24;ADI++) { AD_SCLK = 1; NOP(); NOP(); NOP(); NOP(); NOP(); NOP(); AD_SCLK = 0; NOP(); NOP(); NOP(); NOP(); NOP(); // Falling Edge if(AD_SDATA)ADCOUNTSDATA |= 1; else ADCOUNTSDATA |= 0; if(ADI < 23) ADCOUNTSDATA <<= 1; // Dont shift 24th Bit } ADCSampleVal = ADCOUNTSDATA; } void FillBuf (unsigned char *bufptr, unsigned int noofchars, unsigned char bufdata) { for (; noofchars > 0; noofchars--) { *bufptr = bufdata; bufptr++; } } void Long2String (long intval, unsigned char decimals) { long divisor; unsigned char xx; unsigned char *tmpptr; FillBuf (&TMPSTR[0], 8, ' '); tmpptr = &TMPSTR[0]; if (intval < 0) *tmpptr = '-'; else *tmpptr = ' '; //First digit -/+ tmpptr++; intval = labs (intval); intval %= 10000000; //Make 7 digits divisor = 1000000; for (xx=0; xx<(6-decimals); xx++) { if ((intval / divisor) > 0) break; //Make Leading Spaces insted of Zeros *tmpptr = ' '; tmpptr++; intval %= divisor; divisor /= 10; } for (; xx<7; xx++) { if ((decimals > 0) && ((7-xx) == decimals)) { *tmpptr = '.'; //Place Decimal Point tmpptr++; } *tmpptr = (intval / divisor) | 0x30; tmpptr++; intval %= divisor; divisor /= 10; } if (decimals == 0) TMPSTR [8] = '\x0'; else TMPSTR [9] = '\x0'; //*tmpptr = '\x0'; // return (&TMPSTR [0]); } void ADC_Init(void) { TRISA0 = 0; //OP SCK TRISA1 = 0; //OP SYN TRISA4 = 1; //IP DRDY TRISA5 = 1; //IP DOUT } void main() { unsigned char i; unsigned char Avgcnt; signed long ADC_AVGCNTS; GIE = 1; PEIE = 1; TXIE = 1; RCIE = 1; INTE = 0; OSCCON =0xFB; //0xFB; // OSCCON = 0xF2; OSCTUNE = 0x7F; //0x1F; for(i =0; i<250;i++); //warm up delay TRISA = 0x00; //configuers as op ANSELA = 0x00; TRISB = 0x00; //configuers as op ANSELB = 0x00; TRISC |= 0x00; // SET Key as Input // LCD_Init(); // Timer1 over flow Init TMR1IE = 0x01; TMR1H = 0xFF; // F9 TMR1L = 0x60; // C0 T1CON = 0x41; // 41 T1GCON = 0; TRISC6 = 1; TRISC7 = 1; BAUDCON = 0x00; SPBRGH = 0x00; SPBRG = 103; // 16 mhz 25 for 4 mhz TXSTA = 0x24; RCSTA = 0x90; TRISB = 0; ANSELB = 0; WPUB =0x00; InitLCDDisplay(); ADC_Init(); ClearLcd(); lcd_goto(0); AD_SYN = 1; lcd_puts("Adc Test.."); // Delay_1s(1); ClearLcd(); lcd_goto(0); lcd_puts("C:"); do{ ADC_AVGCNTS = 0; ADCSampleVal=0; for(Avgcnt = 0;Avgcnt < 100;Avgcnt++) { ReadADCCounts(); ADC_AVGCNTS+= ADCSampleVal; } ADCSampleVal = ADC_AVGCNTS /100; ADCSampleVal = ADCSampleVal/6; Long2String(ADCSampleVal,0); lcd_goto(2); lcd_putch(TMPSTR[0]); lcd_putch(TMPSTR[1]); lcd_putch(TMPSTR[2]); lcd_putch(TMPSTR[3]); lcd_putch(TMPSTR[4]); lcd_putch(TMPSTR[5]); lcd_putch(TMPSTR[6]); lcd_putch(TMPSTR[7]); Delay_ms(10); // Delay_ms(10); // Delay_ms(10); // Delay_ms(10); // Delay_ms(10); } while(1); }