#include "MainHeader.h" #define msec 100000 #define m2sec 100000 void SystemInit(){} unsigned int ADC_value = 0 ; float ok; void ADC_Init (); unsigned int x=0; void resistanceMeter(); void CapacitanceMeter(){} void delay_microsecond(unsigned int time); //--------------------------------delays and timer------------------------------------------------------- void Timer_Init ( unsigned long period ) { SYSCTL_RCGCTIMER_R |= 0x1 ;// enable clock for Timer0 TIMER0_CTL_R &= ~(0x1) ; // disable Timer0 before setup TIMER0_CFG_R |= 0x0 ; // configure 16-bit timer mode **** 32 TIMER0_TAMR_R |= 0x2 ; // configure periodic mode TIMER0_TAILR_R = 90000000 ; // set iinitial load value TIMER0_TAPR_R = 0x0 ; // set prescalaer for desired frequency 100 kHz TIMER0_ICR_R = 0x1 ; // clear time out interrupt // TIMER0_IMR_R |=0x1 ; // enable interrupt mask for Timer_0A // DisableInterrupts () ; // Set priority for interrupt // NVIC_PRI4_R = ( NVIC_PRI4_R & 0x00FFFFFF ) |0x40000000 ; // NVIC_EN0_R |= 0x80000 ; // enable interrupt 19 TIMER0_CTL_R |= 0x1 ; // enable Timer_0A // EnableInterrupts () ; } void Delay1() { GPIO_PORTF_DATA_R |=0x08; Delay(1000000); } //-------------------------------------------------------------------------------------------------------- //-------------------------------- ADC for C ------------------------------------------------------------- void ADC_Init_for_C () { volatile unsigned long delay ; //clock to port E and ADC 0 SYSCTL_RCGCGPIO_R |= 0x30 ; SYSCTL_RCGCADC_R |= 0x01 ; //Delay f o r c l o c k s i g n a l to s e t t l e down delay = SYSCTL_RCGCADC_R; //port E : pe0 for C GPIO_PORTE_DIR_R |= 0x0 ; GPIO_PORTE_AFSEL_R |= 0x1 ; GPIO_PORTE_DEN_R &= (~0x1) ; GPIO_PORTE_AMSEL_R &= (~0x1) ; //ADC0 configure // Clear the sample rate [pg.847] ADC0_PC_R &= 0x00; // Set sample rate equal to 125 ksps ADC0_PC_R |= 0x01; // Set the priority of sample sequencers //Sample sequence 0 has highest and SS3 has lowest priority [pg.798] ADC0_SSPRI_R |= 0x3210; // Disable sample sequence 3 befor configuration [pg .780] ADC0_ACTSS_R &= ~0x08; ADC_EMUX=0x0; ADC_SSMUX3=0x3; //Enable TS0 , IE0 and END0 bits [pg.833] ADC0_SSCTL3_R |= 0x06; //Enable 16x hardware over sampling [pg.804] ADC0_SAC_R |=0x4; // Disable Interrupt by writing 0 to corresponding bit [pg.784] ADC0_IM_R &= ~0x08; // Activate sample sequencer ADC0_ACTSS_R |= 0x08; } int ADC_for_C (void) { ADC0_SSFIFO3_R=0; // Software trigger [pg.802] ADC0_PSSI_R |= 0x08; //Poll the status of corresponding RIS bit to wait // for the end of conversion [pg.782] while((ADC0_RIS_R & 0x08)==0); //After conversion fetch the data from the FIFO [pg.817] ADC_value = (ADC0_SSFIFO3_R & 0xFFF); //Calculate the value of temperature [pg.771] ADC0_ISC_R |= 0x08; return ADC_value ; } //-------------------------------- end ------------------------------------------------------------- //-------------------------------- ADC for R ------------------------------------------------------------- void ADC_Init () { volatile unsigned long delay ; //Enable c l o c k //Enable c l o c k f o r GPIO por t A,B and F [ pg . 314] SYSCTL_RCGCGPIO_R |= 0x30 ; //Enable c l o c k f o r ADC0 [ pg . 326] SYSCTL_RCGCADC_R |= 0x01 ; //Delay f o r c l o c k s i g n a l to s e t t l e down delay = SYSCTL_RCGCADC_R; //GPIO A,B,F enabl e GPIO_PORTF_DEN_R|= 0xE; GPIO_PORTF_DIR_R=0xE ; // Set the l a t c h enabl e pins o f 7 segment as output [ pg . 622] GPIO_PORTE_DIR_R |= 0x0 ; GPIO_PORTE_AFSEL_R |= 0x4; GPIO_PORTE_DEN_R &= (~0x4) ; GPIO_PORTE_AMSEL_R &= (~0x4) ; //ADC0 configure // Clear the sample rate [pg.847] ADC0_PC_R &= 0x00; // Set sample rate equal to 125 ksps ADC0_PC_R |= 0x01; // Set the priority of sample sequencers //Sample sequence 0 has highest and SS3 has lowest priority [pg.798] ADC0_SSPRI_R |= 0x3210; // Disable sample sequence 3 befor configuration [pg .780] ADC0_ACTSS_R &= ~0x08; ADC_EMUX=0x0; ADC_SSMUX3=0x1; //Enable TS0 , IE0 and END0 bits [pg.833] ADC0_SSCTL3_R |= 0x06; //Enable 16x hardware over sampling [pg.804] ADC0_SAC_R |=0x4; // Disable Interrupt by writing 0 to corresponding bit [pg.784] ADC0_IM_R &= ~0x08; // Activate sample sequencer ADC0_ACTSS_R |= 0x08; } int ADC_Temperature (void) { ADC0_SSFIFO3_R=0; // Software trigger [pg.802] ADC0_PSSI_R |= 0x08; //Poll the status of corresponding RIS bit to wait // for the end of conversion [pg.782] while((ADC0_RIS_R & 0x08)==0); //After conversion fetch the data from the FIFO [pg.817] ADC_value = (ADC0_SSFIFO3_R & 0xFFF); //Calculate the value of temperature [pg.771] ADC0_ISC_R |= 0x08; return ADC_value ; } //-------------------------------- end ------------------------------------------------------------- //-------------------------------- ADC for L ------------------------------------------------------------- void ADC_Init_for_L () { volatile unsigned long delay ; //clock to port E and ADC 0 SYSCTL_RCGCGPIO_R |= 0x30 ; SYSCTL_RCGCADC_R |= 0x01 ; //Delay f o r c l o c k s i g n a l to s e t t l e down delay = SYSCTL_RCGCADC_R; //port E : pe0 for C GPIO_PORTE_DIR_R |= 0x0 ; GPIO_PORTE_AFSEL_R |= 0x1; GPIO_PORTE_DEN_R &= (~0x1) ; GPIO_PORTE_AMSEL_R &= (~0x1) ; //ADC0 configure // Clear the sample rate [pg.847] ADC0_PC_R &= 0x00; // Set sample rate equal to 125 ksps ADC0_PC_R |= 0x01; // Set the priority of sample sequencers //Sample sequence 0 has highest and SS3 has lowest priority [pg.798] ADC0_SSPRI_R |= 0x3210; // Disable sample sequence 3 befor configuration [pg .780] ADC0_ACTSS_R &= ~0x08; ADC_EMUX=0x0; ADC_SSMUX3=0x3; // ooOOOoooOOoOOooooooO //Enable TS0 , IE0 and END0 bits [pg.833] ADC0_SSCTL3_R |= 0x06; //Enable 16x hardware over sampling [pg.804] ADC0_SAC_R |=0x4; // Disable Interrupt by writing 0 to corresponding bit [pg.784] ADC0_IM_R &= ~0x08; // Activate sample sequencer ADC0_ACTSS_R |= 0x08; } int ADC_for_L (void) { ADC0_SSFIFO3_R=0; // Software trigger [pg.802] ADC0_PSSI_R |= 0x08; //Poll the status of corresponding RIS bit to wait // for the end of conversion [pg.782] while((ADC0_RIS_R & 0x08)==0); //After conversion fetch the data from the FIFO [pg.817] ADC_value = (ADC0_SSFIFO3_R & 0xFFF); //Calculate the value of temperature [pg.771] ADC0_ISC_R |= 0x08; return ADC_value ; } //-------------------------------- end ------------------------------------------------------------- //------------------------------------ resistance meter --------------------------------------------------- void resistanceMeter(){ //INITIALIZATIONS float ResistanceR[]={100.6,2200.6,10000.6,100000.6,615000 }; int ADC[]={0,0,0,0,0}; float VoltageUR[]={0.0,0.0,0.0,0.0,0.0}; int i; float UnknownR; float UnknownRK; float UnknownRK25; float Vmin; int check; float minimum; float choosenVoltage=0.0; float standardVoltageR=3.3; float sv1=3.2; float halfVoltage=1.65; int totalADC=4096; int temp[]={0,0,0,0,0}; LCD_Clear(); GPIO_Port_Init(); GPIO_PORTF_DATA_R |=0x02; Delay(100000); x=0; //start //calculating voltage LCD_Send_String("Calculating V"); Delay(100000); LCD_Clear(); // PC4 (with 100 R) GPIO_PORTC_DATA_R |= 0x10; //measure adc ADC[0]=ADC_Temperature(); temp[0]= totalADC-ADC[0]; //measure v VoltageUR[0]=(float) ((temp[0]*3.3)/4096); LCD_Send_Float(VoltageUR[0]); Delay(100000); LCD_Clear(); Delay(100000); GPIO_PORTC_DATA_R &= 0x00; Delay(100000); //2 // PC5 (with 1k R) GPIO_PORTC_DATA_R |= 0x20; //measure adc ADC[1]=ADC_Temperature(); temp[1]= totalADC-ADC[1]; //measure v VoltageUR[1]=(float) ((temp[1]*3.3)/4096); LCD_Send_Float(VoltageUR[1]); Delay(100000); LCD_Clear(); Delay(100000); GPIO_PORTC_DATA_R &= 0x00; Delay(100000); //3 // PC6 (with 10k R) GPIO_PORTC_DATA_R |= 0x40; //measure adc ADC[2]=ADC_Temperature(); temp[2]= totalADC-ADC[2]; //measure v VoltageUR[2]=(float) ((temp[2]*3.3)/4096); LCD_Send_Float(VoltageUR[2]); Delay(100000); LCD_Clear(); Delay(100000); GPIO_PORTC_DATA_R &= 0x00; Delay(100000); //4 // PC7 (with 100k R) GPIO_PORTC_DATA_R |= 0x80; //measure adc ADC[3]=ADC_Temperature(); temp[3]= totalADC-ADC[3]; //measure v VoltageUR[3]=(float) ((temp[3]*3.3)/4096); LCD_Send_Float(VoltageUR[3]); Delay(100000); LCD_Clear(); Delay(100000); GPIO_PORTC_DATA_R &= 0x00; Delay(100000); //5 // PD2 (with 1M R) GPIO_PORTD_DATA_R |= 0x4; //measure adc ADC[4]=ADC_Temperature(); temp[4]= totalADC-ADC[4]; //measure v VoltageUR[4]=(float) ((temp[4]*3.3)/4096); LCD_Send_Float(VoltageUR[4]); Delay(100000); LCD_Clear(); GPIO_PORTD_DATA_R = 0x0; //calculating R minimum=halfVoltage-VoltageUR[0]; if (minimum<0) { minimum=-(minimum);} choosenVoltage=VoltageUR[0]; i=0; Vmin=0; check=0; for(i=1;i<5;i++) { Vmin=halfVoltage-VoltageUR[i]; if(Vmin<0) Vmin=-(Vmin); if(Vminsv1) && (VoltageUR[1]==standardVoltageR || VoltageUR[1]>sv1) && (VoltageUR[2]==standardVoltageR || VoltageUR[2]>sv1) && (VoltageUR[3]==standardVoltageR || VoltageUR[3]>sv1)) { //display "no resistance attached" LCD_Send_String("Kindly Attach"); LCD_Goto_XY(1,2); LCD_Send_String("R First"); Delay(100000); } else { UnknownR=(choosenVoltage*ResistanceR[check])/(standardVoltageR-choosenVoltage); if(check==1 || check==2 || check==3) { UnknownRK=UnknownR/1000; //display it LCD_Send_Float(UnknownRK); LCD_Send_String("k"); }else if (check==4) { UnknownRK25=UnknownR-(UnknownR*(.25)); UnknownRK=UnknownRK25/1000000; //display it LCD_Send_Float(UnknownRK); LCD_Send_String("M"); } else { LCD_Send_Float(UnknownR); } } GPIO_PORTF_DATA_R &=0x00; //red led } //-------------------------------- end -------------------------------------------------------------- void inductanceMeter(){ unsigned int start=0; unsigned int end=0,total; double time; unsigned int ADCV=0; int y; float Voltage,induct; GPIO_Port_Init(); // // Timer_Init (60000) ; // generate a square wave for 2 Hz // GPIO_PORTF_DATA_R |=0x08; // // start = TIMER0_TAR_R; // // while(ADC_for_L() < 2589) // 2589 is 63% of input voltage i.e 3.3 // { // } // end = TIMER0_TAR_R; // // total=start-end; // //// time=(double) total/16000000; // // induct= (double)(start-end)/1600; //((counter/16M)*10k) // // //// induct=(time*10000); //// induct=induct*1000; // // LCD_Send_String("start"); // LCD_Send_Integer(start); // Delay(1000000); // LCD_Clear(); // // // LCD_Send_String("end"); // LCD_Send_Integer(end); // Delay(1000000); // LCD_Clear(); // // // LCD_Send_Integer(total); // Delay(100000); // LCD_Clear(); // // LCD_Send_Float(induct); // LCD_Send_String("mH"); // Delay(1000000); // // GPIO_PORTF_DATA_R &=0x00; // Welcome_Screen(); Delay(100000); LCD_Clear(); } void GPIO_Init () { //switching SYSCTL_RCGCGPIO_R |= PORTF_CLK_EN; SYSCTL_RCGCGPIO_R |= PORTD_CLK_EN; GPIO_PORTF_DEN_R|= 0x1E; GPIO_PORTF_DIR_R=portF_dir; //pin E0,E1 SYSCTL_RCGCGPIO_R |= PORTE_CLK_EN; SYSCTL_RCGCGPIO_R |= PORTE_CLK_EN; GPIO_PORTE_DEN_R |= PORTE_DEN_EN; GPIO_PORTE_DIR_R = PORTE_DIR_EN; } void capacitanceMeter(){ unsigned int start=0; unsigned int end=0,total; double time; unsigned int ADCV=0; float VoltageC,cap; GPIO_Port_Init(); GPIO_PORTF_DATA_R |=0x04; Timer_Init (60000) ; // generate a square wave for 2 Hz //pin D3 GPIO_PORTD_DATA_R |=0x08; start = TIMER0_TAR_R; while(ADC_for_C ()< 2589) // 2589 is 63% of input voltage i.e 3.3 { } end = TIMER0_TAR_R; total=start-end; time=(double) total/16000000; cap=(time*100); // cap=(time*1M)/10k // cap=(time*1000000000000)/10000; LCD_Send_Integer(total); Delay(100000); LCD_Clear(); LCD_Send_Float(time); Delay(100000); LCD_Clear(); LCD_Send_Float(cap); LCD_Send_String(" microF"); GPIO_PORTD_DATA_R &=0x0; GPIO_PORTF_DATA_R &=0x0; } //------------------------------------------------------------------------------------ // Main //------------------------------------------------------------------------------------ int main ( void ) { float z=35.6439; int i,x; float ss=36/9999; SYSCTL_RCGCGPIO_R |= PORTC_Clk_ENABLE; Delay(100); SYSCTL_RCGCGPIO_R |= PORTF_CLK_EN; Delay(100); SYSCTL_RCGCGPIO_R |= 0x10; Delay(100); SYSCTL_RCGCGPIO_R |= 0x1; Delay(100); SYSCTL_RCGCGPIO_R |= PORTD_CLK_EN; Delay(100); GPIO_PORTC_DEN_R |= PORTC_DEN; GPIO_PORTC_DIR_R |= PORTC_DEN; //switching GPIO_PORTF_DEN_R|= 0x1E; GPIO_PORTF_DIR_R=portF_dir; GPIO_PORTD_DIR_R |= 0xD; GPIO_PORTD_PUR_R |= 0xF0; GPIO_PORTD_DEN_R |= 0xFF; // F for 0 to 3 so to get output GPIO_PORTA_PUR_R |= 0xC7; GPIO_PORTA_DEN_R |= 0xC7; GPIO_PORTA_DIR_R = 0x00; //supply for C and L GPIO_PORTE_DEN_R |= 0x07; GPIO_PORTE_DIR_R = 0x07; GPIO_Init () ; GPIO_Port_Init(); // ADC_Init_for_L (); while (1) { if((GPIO_PORTA_DATA_R &= 0x40)==0x00) { ADC_Init(); resistanceMeter(); } if ((GPIO_PORTA_DATA_R &= 0x80)==0x00) { ADC_Init_for_C (); capacitanceMeter(); } if ((GPIO_PORTD_DATA_R &= 0xC0)==0x00) { ADC_Init_for_L (); inductanceMeter(); } } }