i'm using ADS1118 for thermocouple interface and working on microchip PIC16f886 micro-controller. i'm facing problem with SPI interface. Normally the system is working perfectly. however sometimes the system hangs. using ICD i've tried to troubleshoot the system and observed that the program stucks at check of EOC. Despite my efforts i'm unable to identify the reasons.
/* program modified for on-off controller modified in control action loop */ // ADC module connections #define Chip_Select1 PORTC.F1 #define Chip_Select2 PORTC.F2 sbit Chip_Select_Direction1 at TRISC1_bit; sbit Chip_Select_Direction2 at TRISC2_bit; //sbit Din_Select_Direction at TRISC4_bit; #define dout PORTC.F4 #define dout_pin TRISC.F4 #define ts_config1 0x85 #define ts_config2 0x9B #define tc1_config1 0x85 #define tc1_config2 0x8B #define tc2_config1 0xB5 #define tc2_config2 0x8B // End ADC module connections #define out1 portb.f4 #define out2 portb.f5 #define out3 portb.f3 #define out4 porta.f0 //LCD connections sbit LCD_RS at RA4_bit; sbit LCD_EN at RA5_bit; sbit LCD_D4 at RA0_bit; sbit LCD_D5 at RA1_bit; sbit LCD_D6 at RA2_bit; sbit LCD_D7 at RA3_bit; sbit LCD_RS_Direction at TRISA4_bit; sbit LCD_EN_Direction at TRISA5_bit; sbit LCD_D4_Direction at TRISA0_bit; sbit LCD_D5_Direction at TRISA1_bit; sbit LCD_D6_Direction at TRISA2_bit; sbit LCD_D7_Direction at TRISA3_bit; unsigned int abc=0; typedef enum _lcd_function { ln1=0x80,ln3=0x94, ln2=0xC0,ln4=0xD4 } _LCD_function; const char *textt = "Temp:"; const char *textb = "PORTB.F1 = b"; const char *texty = "PORTB.F1 = Y"; /////////////////////////////////////////////////////////////////// const unsigned int m_v[] = {0,50,101,151,202,253,303,354,405,456, 507,558,609,660,711,762,814,865,916,968, 1019,1071,1122,1174,1226,1277,1329,1381,1433,1485, 1537,1589,1641,1693,1745,1797,1849,1902,1954,2006, 2059,2111,2164,2216,2269,2322,2374,2427,2480,2532, 2585,2638,2691,2744,2797,2850,2903,2956,3009,3062, 3116,3169,3222,3275,3329,3382,3436,3489,3543,3596, 3650,3703,3757,3810,3864,3918,3971,4025,4079,4133, 4187,4240,4294,4348,4402,4456,4510,4564,4618,4672, 4726,4781,4835,4889,4943,4997,5052,5106,5160,5215, 5269,5323,5378,5432,5487,5541,5595,5650,5705,5759, 5814,5868,5923,5977,6032,6087,6141,6196,6251,6306, 6360,6415,6470,6525,6579,6634,6689,6744,6799,6854, 6909,6964,7019,7074,7129,7184,7239,7294,7349,7404, 7459,7514,7569,7624,7679,7734,7789,7844,7900,7955, 8010,8065,8120,8175,8231,8286,8341,8396,8452,8507, 8562,8618,8673,8728,8783,8839,8894,8949,9005,9060, 9115,9171,9226,9282,9337,9392,9448,9503,9559,9614, 9669,9725,9780,9836,9891,9947,10002,10057,10113,10168, 10224,10279,10335,10390,10446,10501,10557,10612,10668,10723, 10779,10834,10890,10945,11001,11056,11112,11167,11223,11278, 11334,11389,11445,11501,11556,11612,11667,11723,11778,11834, 11889,11945,12000,12056,12111,12167,12222,12278,12334,12389, 12445,12500,12556,12611,12667,12722,12778,12833,12889,12944, 13000,13056,13111,13167,13222,13278,13333,13389,13444,13500, 13555,13611,13666,13722,13777,13833,13888,13944,13999,14055, 14110,14166,14221,14277,14332,14388,14443,14499,14554,14609, 14665,14720,14776,14831,14887,14942,14998,15053,15109,15164, 15219,15275,15330,15386,15441,15496,15552,15607,15663,15718, 15773,15829,15884,15940,15995,16050,16106,16161,16216,16272, 16327,16383,16438,16493,16549,16604,16659,16715,16770,16825, 16881,16936,16991,17046,17102,17157,17212,17268,17323,17378, 17434,17489,17544,17599,17655,17710,17765,17820,17876,17931, 17986,18041,18097,18152,18207,18262,18318,18373,18428,18483, 18538,18594,18649,18704,18759,18814,18870,18925,18980,19035, 19090,19146,19201,19256,19311,19366,19422,19477,19532,19587, 19642,19697,19753,19808,19863,19918,19973,20028,20083,20139, 20194,20249,20304,20359,20414,20469,20525,20580,20635,20690, 20745,20800,20855,20911,20966,21021,21076,21131,21186,21241, 21297,21352,21407,21462,21517,21572,21627,21683,21738,21793, 21848,21903,21958,22014,22069,22124,22179,22234,22289,22345, 22400,22455,22510,22565,22620,22676,22731,22786,22841,22896, 22952,23007,23062,23117,23172,23228,23283,23338,23393,23449, 23504,23559,23614,23670,23725,23780,23835,23891,23946,24001, 24057,24112,24167,24223,24278,24333,24389,24444,24499,24555, 24610,24665,24721,24776,24832,24887,24943,24998,25053,25109, 25164,25220,25275,25331,25386,25442,25497,25553,25608,25664, 25720,25775,25831,25886,25942,25998,26053,26109,26165,26220, 26276,26332,26387,26443,26499,26555,26610,26666,26722,26778, 26834,26889,26945,27001,27057,27113,27169,27225,27281,27337, 27393,27449,27505,27561,27617,27673,27729,27785,27841,27897}; float tc_1 = 0.0,tc_2=0.0,cmv=0,temper1=0.0,fc=0.0; unsigned int temperature_1=0, c_m=0,temp=0; unsigned int tx_data,tim=0,timb=0,timy=0; unsigned short txs[4]={0},ang_zr=0,a=0,zz=0,zcd=0; unsigned short temp1=0,temp2=0, buffer; unsigned int rr=0,y=122,b=242,angle=0,rtimer=0,ytimer=0,btimer=0,ofc1=40,ofc2=40,ofc3=40,ofc4=40; unsigned int fi1_on1=0,fi1_on2=0,fi2_on1=0,fi2_on2=0,fi3_on1=0,fi3_on2=0,fi4_on1=0,fi4_on2=0,er[4]={0},erp[4]={0},erpp[4]={0},f_a[4]={0}; unsigned int fi1_of1=0,fi1_of2=0,fi2_of1=0,fi2_of2=0,fi3_of1=0,fi3_of2 =0,fi4_of1=0,fi4_of2=0,spt[5],rxra[10],t_mm[6],mt[6]; void message(unsigned short ln,unsigned short pos,const char *ar) { unsigned short i=0; Lcd_cmd(ln+pos-1); do { Lcd_chr_cp(ar[i++]); } while(ar[i]!=0); } ///////////////////////////////////////////////////////////////////////// void disp_float_data(float disp_d) { char txt[15]; IRP_bit=1; FloatToStr_FixLen(disp_d,txt,6); Lcd_Chr_cp(txt[0]); Lcd_Chr_cp(txt[1]); Lcd_Chr_cp(txt[2]); Lcd_Chr_cp(txt[3]); Lcd_Chr_cp(txt[4]); Lcd_Chr_cp(txt[5]); IRP_bit=0; } ///////////////////////////////////////////////////////////////////////// void disp_int_data(unsigned int disp_d) { char txt[7]; IntToStr(disp_d, txt); Lcd_Chr_cp(txt[0]); Lcd_Chr_cp(txt[1]); Lcd_Chr_cp(txt[2]); Lcd_Chr_cp(txt[3]); Lcd_Chr_cp(txt[4]); Lcd_Chr_cp(txt[5]); Lcd_Chr_cp(txt[6]); Lcd_Chr_cp(txt[7]); } void convert(unsigned int *t_m) { unsigned int i=0,j=0,k=0, te[6]={0}; tc_1=(t_m[0]>>2)*0.03125; temp=(unsigned int)(tc_1); c_m=m_v[temp]; for(j=2;j<6;j++) { if(t_m[j]==0x7FFF) { mt[++k]= 2000; } else { cmv=c_m+(t_m[j]*7.81); te[0]=(unsigned int)cmv; //t_m=t_m1000; //temperature_1=cmv; for(i=0;i<500;i++) { if(m_v[i]>te[0]) { te[1]=m_v[i]; te[2]=m_v[i-1]; temperature_1=i-1; break; } } te[3] = te[0]-te[2]; te[4]= te[1]-te[2]; fc = te[3]*10/te[4]; temperature_1 = temperature_1*100; fc = (fc*100); te[5] = (unsigned int) (fc); temperature_1 = temperature_1+te[5]; mt[++k] = temperature_1*0.01; } } } /////////////////////////////////////////////////////////////////////////////// void set_extint(void) { //<<<<<<<<--make pin 21 as ext. interrupt-->>>>>>>> TRISB.F0=1; OPTION_REG=0x80; INTCON.INTE=1; INTCON=0xD0; } //////////////////////////////////////////////////////// void timer1_init(void) { //<<<<<<<<<--Set Timer1 for 55.6 us-->>>>>>>>>>>>>> TMR1H=0xFE; TMR1L=0xE9; T1CON=0x00; PIE1.TMR1IE=1; // enable TIMER2 interrupt INTCON=0x00; } //////////////////////////////////////////////////////// /////////////port initilisation///////////////////// void port_initialise(void) { ANSEL=0x00; ANSELH=0x00; TRISA=0x00; OPTION_REG=0x80; TRISB=0x07; PORTB=0x07; TRISC=0x10; PORTC=0x10; } /*void interrupt(void) { //////////////////////////////////////////////////////// if(INTCON.INTF) { INTCON.INTF=0; if(T1CON.TMR1ON==0)T1CON.TMR1ON=1; OPTION_REG=0x80; if(ang_zr++==1) { tim = 0; ang_zr = 0; } if(zcd==0) { zz = PORTB & 0x06; a = zz; while(a == (zz)) { zz= PORTB; zz = zz&0x06; } if(zz == 0x02) { timy=120; timb=240; } if(zz == 0x04) { timb=120; timy=240; } zcd=1; } //return; } if(PIR1.TMR1IF) //55.6us timer { PIR1.TMR1IF=0; TMR1H=0xFE; TMR1L=0xF0; //out3=!out3; if(tim++==0) { rr = 0; } if(tim==timb) { y = 0; } if(tim==timy) { b = 0; } //if(angle>359)angle=0; if(((++rr>=fi1_on1) && (rr<fi1_of1))||((rr>=fi1_on2) && (rr<fi1_of2))) { out1=1; } else { out1=0; //out2=0; //out3=0; } if((++y>=fi2_on1 && y<fi2_of1)||(y>=fi2_on2 && y<fi2_of2)) { out2=1; } else { out2=0; } if((++b>=fi3_on1 && b<fi3_of1)||(b>=fi3_on2 && b<fi3_of2)) { out3=1; } else { out3=0; } if((b>=fi4_on1 && b<fi4_of1)||(b>=fi4_on2 && b<fi4_of2)) { out4=1; } else { out4=0; } //T1CON.TMR1ON=1; } } void fr_ang(unsigned int fis1,unsigned int fis2,unsigned int fis3,unsigned int fis4) { fi1_on1=fis1; fi1_of1=fis1+ofc1; fi1_on2=fis1+180; fi1_of2=fis1+180+ofc1; fi2_on1=fis2; fi2_of1=fis2+ofc2; fi2_on2=fis2+180; fi2_of2=fis2+180+ofc2; fi3_on1=fis3; fi3_of1=fis3+ofc3; fi3_on2=fis3+180; fi3_of2=fis3+180+ofc3; fi4_on1=fis4; fi4_of1=fis4+ofc4; fi4_on2=fis4+180; fi4_of2=fis4+180+ofc4; } */ void con_ac(void) { unsigned short i=0; unsigned int k1[4]={10,10,10,10}; float dc,k2[4]={0.1,0.1,0.1,0.1},k3[4]={0.1,0.1,0.1,0.1}; // PID control action /* for(i=1;i<4;i++) { er[i]=spt[i]-mt[i]; dc=k1[i]*(er[i]-erp[i])+k2[i]*er[i]+k3[i]*(erp[i]-2*er[i]+erpp[i]); if(dc>10)f_a[i]=0; else f_a[i]=dc/10; erpp[i]=erp[i]; erp[i]=er[i]; } fr_ang(f_a[0],f_a[1],f_a[2],f_a[3]); */ //for on off control if(spt[1]>mt[1])out1=1; else out1=0; if(spt[2]>mt[2])out2=1; else out2=0; if(spt[3]>mt[3])out3=1; else out3=0; if(spt[4]>mt[4])out4=1; else out4=0; } void main() { unsigned int t_temp,c_temp=0,ref_temp=0; //unsigned short ts_config[6]={0x8F,0x9B,0x8F,0x8B,0xBF,0x8B},j=0,i=0; unsigned short ts_config[6]={0x8F,0x9B,0xBF,0x8B,0x8F,0x8B},j=0,i=0; spt[1]=50; spt[2]=50; spt[3]=50; spt[4]=50; //fr_ang(10,0,10,10); port_initialise(); /////////////////////// // timer and phase detection are commented /*timer1_init(); fr_ang(40,40,40,40); set_extint(); */ out1=1; out2=1; out3=1; out4=1; Delay_ms(1000); out1=0; out2=0; out3=0; out4=0; Chip_Select_Direction1 = 0; // Set CS# pin as Output Chip_Select_Direction2 = 0; /*Chip_Select1 = 1; Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Delay_ms(300); Lcd_Cmd(_LCD_CURSOR_OFF); //Chip_Select2 = ; // select // dout_pin=1; //SPI1_Init(); */ //void SPIx_Init_Advanced(unsigned short master_slav, unsigned short data_sample, unsigned short clock_idle, unsigned short transmit_edge); SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_END, _SPI_CLK_IDLE_LOW, _SPI_HIGH_2_LOW); Delay_ms(1000); mt[1]=0; mt[2]=0; mt[3]=0; mt[4]=0; do { con_ac(); /*if(zz==0x02) { message(ln1,1,textb); } if(zz==0x04) { message(ln1,1,texty); } */ Chip_Select1 = 0; Chip_Select2 = 1; ////////ADS1118-1////////////////////// i=0; j=0; SPI1_Write(ts_config[j]); SPI1_Write(ts_config[j+1]); delay_ms(1); while(i<3) { while(dout!=0); j=+2; temp1 = SPI1_Read(ts_config[j]); temp2 = SPI1_Read(ts_config[j+1]); Delay_ms(1); t_mm[i++]=((temp1<<8)|temp2); } ////////ADS1118-2////////////////////// j=0; Chip_Select1 = 1; Chip_Select2 = 0; SPI1_Write(ts_config[j]); SPI1_Write(ts_config[j+1]); delay_ms(1); while(i<6) { while(dout!=0); j=+2; temp1 = SPI1_Read(ts_config[j]); temp2 = SPI1_Read(ts_config[j+1]); Delay_ms(1); t_mm[i++]=((temp1<<8)|temp2); } tc_1=(t_mm[0]>>2)*0.03125; tc_2=(t_mm[3]>>2)*0.03125; convert(&t_mm); //convert(&rxra); // con_ac(&t_mm); message(ln1,1,textt); disp_float_data(temper1); //goto a2; } while(1); }