#pragma optsize- //optimize for speed #include #include #include // for sine #include #include #include "character.h" /************************************* Definitions ************************************/ #define begin { #define end } /************************************* State Machine States ************************************/ #define inc_ref 0 #define sample 1 #define done 2 #define user_input 3 #define post 4 #define wait 5 #define display 6 #define increase_freq 7 #define test_case 8 #define wait2 9 #define phase 10 #define lag_lead 11 #define wait3 12 /************************************* More Constant Definitions ************************************/ #define amplitude 1 #define ScreenTop 30 #define ScreenBot 230 #define lineTime 1018 #define lead 1 #define lag 0 #define width 126 /************************************* Video Generation Variables ************************************/ #pragma regalloc- register unsigned char counter @3; register char v1 @4; register char v2 @5; register char v3 @6; register char v4 @7; register char v5 @8; register char v6 @9; register char v7 @10; register char v8 @11; register int i @12; #pragma regalloc+ char syncON, syncOFF; int LineCount; char x, y, s, vx, vy; char screen[1600], t, ts[10]; char cu1[]="CORNELL"; char cu2[]="ECE476"; /************************************* Variable Declaration ************************************/ unsigned char time2; float amp,maxVolt, offset; unsigned char volt; unsigned char state, Ain; unsigned char phase1, phase2; unsigned char counter2; char lcd_buffer[17]; unsigned int R; char j; unsigned char sampleState; float C; unsigned char AmpArray[22]; int impArray[22]; unsigned char yArray[22]; unsigned char PhaseArray[22]; int lagleadArray[22]; bit highfreq; unsigned char xpos; unsigned char yscale; unsigned char cursorY; unsigned char testFreq; int maxImp; /*************************************** Function Declaration ***************************************/ void initialize(void); void phaseCal(void); void reset(void); char dbMag(char); char findMax(); int impCal(char); /*************************************** Plot a point at (x,y) on TV ***************************************/ void video_pt(char x, char y, char c) { #asm ; i=(x>>3) + ((int)y<<4) ; the byte with the pixel in it push r16 ldd r30,y+2 ;get x lsr r30 lsr r30 lsr r30 ;divide x by 8 ldd r12,y+1 ;get y lsl r12 ;mult y by 16 clr r13 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 add r12, r30 ;add in x/8 ;v2 = screen[i]; r5 ;v3 = pos[x & 7]; r6 ;v4 = c r7 ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 ld r5,Z ;get screen byte ldd r26,y+2 ;get x ldi r27,0 andi r26,0x07 ;form x & 7 ldi r30,low(_pos*2) ldi r31,high(_pos*2) add r30,r26 adc r31,r27 lpm r6,Z ld r16,y ;get c ;if (v4==1) screen[i] = v2 | v3 ; ;if (v4==0) screen[i] = v2 & ~v3; ;if (v4==2) screen[i] = v2 ^ v3 ; cpi r16,1 brne tst0 or r5,r6 tst0: cpi r16,0 brne tst2 com r6 and r5,r6 tst2: cpi r16,2 brne writescrn eor r5,r6 writescrn: ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 st Z, r5 ;write the byte back to the screen pop r16 #endasm } #pragma warn+ //================================== // put a big character on the screen // c is index into bitmap void video_putchar(char x, char y, char c) { v7 = x; for (v6=0;v6<7;v6++) begin v1 = bitmap[c][v6]; v8 = y+v6; video_pt(v7, v8, (v1 & 0x80)==0x80); video_pt(v7+1, v8, (v1 & 0x40)==0x40); video_pt(v7+2, v8, (v1 & 0x20)==0x20); video_pt(v7+3, v8, (v1 & 0x10)==0x10); video_pt(v7+4, v8, (v1 & 0x08)==0x08); end } //================================== // put a string of big characters on the screen void video_puts(char x, char y, char *str) { char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_putchar(x,y,str[i]-0x30); else video_putchar(x,y,str[i]-0x40+9); x = x+6; end } //================================== // put a small character on the screen // x-cood must be on divisible by 4 // c is index into bitmap void video_smallchar(char x, char y, char c) { char mask; i=((int)x>>3) + ((int)y<<4) ; if (x == (x & 0xf8)) mask = 0x0f; //f8 else mask = 0xf0; screen[i] = (screen[i] & mask) | (smallbitmap[c][0] & ~mask); screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask); screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask); screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask); screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask); } //================================== // put a string of small characters on the screen // x-cood must be on divisible by 4 void video_putsmalls(char x, char y, char *str) { char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_smallchar(x,y,str[i]-0x30); else if (str[i] == 0x2e) { video_smallchar(x,y,39); } else if (str[i] == 0x20) { video_smallchar(x,y,12); } else video_smallchar(x,y,str[i]-0x40+12); x = x+4; end } //================================== //plot a line //at x1,y1 to x2,y2 with color 1=white 0=black 2=invert //NOTE: this function requires signed chars //Code is from David Rodgers, //"Procedural Elements of Computer Graphics",1985 void video_line(char x1, char y1, char x2, char y2, char c) { int e; signed char dx,dy,j, temp; signed char s1,s2, xchange; signed char x,y; x = x1; y = y1; dx = cabs(x2-x1); dy = cabs(y2-y1); s1 = csign(x2-x1); s2 = csign(y2-y1); xchange = 0; if (dy>dx) begin temp = dx; dx = dy; dy = temp; xchange = 1; end e = ((int)dy<<1) - dx; for (j=0; j<=dx; j++) begin video_pt(x,y,c) ; if (e>=0) begin if (xchange==1) x = x + s1; else y = y + s2; e = e - ((int)dx<<1); end if (xchange==1) y = y + s2; else x = x + s1; e = e + ((int)dy<<1); end } //================================== //return the value of one point //at x,y with color nonzero=white 0=black char video_set(char x, char y) { //The following construction //detects exactly one bit at the x,y location i=((int)x>>3) + ((int)y<<4) ; return ( screen[i] & 1<<(7-(x & 0x7))); } //================================== //This is the sync generator and raster generator. It MUST be entered from //sleep mode to get accurate timing of the sync pulses #pragma warn- interrupt [TIM1_COMPA] void t1_cmpA(void) { //start the Horizontal sync pulse PORTD = syncON; //update the curent scanline number LineCount ++ ; //begin inverted (Vertical) synch after line 247 if (LineCount==248) begin syncON = 0b00100000; syncOFF = 0; end //back to regular sync after line 250 if (LineCount==251) begin syncON = 0; syncOFF = 0b00100000; end //start new frame after line 262 if (LineCount==263) begin LineCount = 1; end delay_us(2); //adjust to make 5 us pulses //end sync pulse PORTD = syncOFF; if (LineCount=ScreenTop) begin //compute byte index for beginning of the next line //left-shift 4 would be individual lines // <<3 means line-double the pixels //The 0xfff8 truncates the odd line bit //i=(LineCount-ScreenTop)<<3 & 0xfff8; // #asm push r16 lds r12, _LineCount lds r13, _Linecount+1 ldi r16, 30 sub r12, r16 ldi r16,0 sbc r13, r16 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 mov r16,r12 andi r16,0xf0 mov r12,r16 pop r16 #endasm //load 16 registers with screen info #asm push r14 push r15 push r16 push r17 push r18 push r19 push r26 push r27 ldi r26,low(_screen) ;base address of screen ldi r27,high(_screen) add r26,r12 ;offset into screen (add i) adc r27,r13 ld r4,x+ ;load 16 registers and inc pointer ld r5,x+ ld r6,x+ ld r7,x+ ld r8,x+ ld r9,x+ ld r10,x+ ld r11,x+ ld r12,x+ ld r13,x+ ld r14,x+ ld r15,x+ ld r16,x+ ld r17,x+ ld r18,x+ ld r19,x pop r27 pop r26 #endasm delay_us(4); //adjust to center image on screen //blast 16 bytes to the screen #asm ;but first a macro to make the code shorter ;the macro takes a register number as a parameter ;and dumps its bits serially to portD.6 ;the nop can be eliminated to make the display narrower .macro videobits ;regnum BST @0,7 IN R30,0x12 BLD R30,4 nop OUT 0x12,R30 BST @0,6 IN R30,0x12 BLD R30,4 nop OUT 0x12,R30 BST @0,5 IN R30,0x12 BLD R30,4 nop OUT 0x12,R30 BST @0,4 IN R30,0x12 BLD R30,4 nop OUT 0x12,R30 BST @0,3 IN R30,0x12 BLD R30,4 nop OUT 0x12,R30 BST @0,2 IN R30,0x12 BLD R30,4 nop OUT 0x12,R30 BST @0,1 IN R30,0x12 BLD R30,4 nop OUT 0x12,R30 BST @0,0 IN R30,0x12 BLD R30,4 nop OUT 0x12,R30 .endm videobits r4 ;video line -- byte 1 videobits r5 ;byte 2 videobits r6 ;byte 3 videobits r7 ;byte 4 videobits r8 ;byte 5 videobits r9 ;byte 6 videobits r10 ;byte 7 videobits r11 ;byte 8 videobits r12 ;byte 9 videobits r13 ;byte 10 videobits r14 ;byte 11 videobits r15 ;byte 12 videobits r16 ;byte 13 videobits r17 ;byte 14 videobits r18 ;byte 15 videobits r19 ;byte 16 clt ;clear video after the last pixel on the line IN R30,0x12 BLD R30,4 OUT 0x12,R30 pop r19 pop r18 pop r17 pop r16 pop r15 pop r14 #endasm end } #pragma warn+ /*************************************** MAX7480 Filter Clock Signal, this ISR runs when the video ISR is turned off ***************************************/ interrupt [TIM1_COMPB] void t1_cmpB(void) begin PORTA.3 = ~PINA.3; //printf("%d", OCR1A); end /*************************************** MAX7480 Voltage In Signal, this ISR toggles PINA.2 to generate a square wave at the desired sine wave frequency ***************************************/ interrupt [TIM2_COMP] void t2_cmpA(void) begin PORTA.2 = ~PINA.2; end /*************************************** TIMER0 is evoked every 1 ms. This ISR keeps the timing for the program ***************************************/ interrupt [TIM0_COMP] void sgen(void) begin if(time2>0) time2--; end /*************************************** Main Function ***************************************/ void main(void) { initialize(); // Initialize everything while(1) { #asm ("sleep"); // Put CPU to sleep when generating video // ensure uniform entry time into the ISR if (LineCount==231) begin switch (state) { // State Machine /****************************** wait state *****************************/ case wait: // Wait State for delay if (time2 == 0) { state = increase_freq; // Jump to Increase Frequency state } break; /****************************** Whether to increase the reference voltage *****************************/ case inc_ref: // Determine whether to increase reference voltage at PORTB if (Ain <= 10) { // If ADC sample is close to 0V, reference voltage is at the peak of the sine wave printf("%d, ",j); // Print out the the user AmpArray[j] = volt; // Store the PORTB value which is equivalent to the sine wave peak value printf("Ain: %d, volt:%d\r\n", Ain,AmpArray[j]); // Print out the result to the user volt = 0; // Reset PORTB to 0V PORTB = volt; state = increase_freq; // Increase the sine wave frequency } else { if (volt == 255) { // PORTB is swept to 5V without peak detection AmpArray[j] = 0; // Store 0 to ampArray printf("%d, ",j); // Print out error message with the amplitude out of range printf("Ain: invalid range\r\n"); volt = 0; // Reset PORTB voltage PORTB = volt; state = increase_freq; // Increase the sine wave frequency } else { volt++; // No peak detection then increase the voltage on PORTB PORTB = volt; state = wait2; // Wait for 50ms time2 = 50; } } break; /****************************** Another wait State *****************************/ case wait2: // Wait for voltage settling and propagation after changing the reference voltage if(time2==0) { if (sampleState == amplitude) { // If sampling amplitude at the moment, jump to sample state = sample; } else { // Otherwise jump to state phase state = phase; } } break; /****************************** Sample State *****************************/ case sample: // Take an ADC sample Ain = ADCH; ADCSR.6=1; state = inc_ref; // Determine whether peak has been detected break; /****************************** Phase State *****************************/ case phase: // Measure phase Ain = ADCH; // Take in an ADC sample ADCSR.6=1; PhaseArray[j] = Ain; // Store Phase Information time2 = 1; // Wait for 1 ms state = wait; printf("%d: phase: %d\r\n", j, Ain); // Print out phase information to user break; /****************************** Unused State *****************************/ case lag_lead: Ain = ADCH; ADMUX = 0b00100100; ADCSR.6=1; if (Ain > 127) { // Voriginal leads VDUT lagleadArray[j] = lead; } else { lagleadArray[j] = lag; } state = increase_freq; break; /****************************** Increase sine wave Frequency *****************************/ case increase_freq: j++; // Increment clock prescaler index if (j >= 22) { // If all frequencies have been swept if (sampleState == amplitude) { // Sampling amplitude right now? ADMUX = 0b00100001; // Switch ADC input pin sampleState = 0; // Switch to Phase sampling mode j = -1; // Reset clock prescaler index } else { // Both phase and amplitude have been sampled state = post; // Jump to post-processing state TCCR1B = 0; // Turn off TIMER1 and TIMER2 that generates the square waves TCCR2 = 0; } } else { // Must change clock frequency prescaler for different frequencies of square waves if (j == 1) { #asm("CLI"); TCCR2 = 0b00001010; // prescaler 8 TCCR1B = 0b00001010; // prescaler 8 #asm("SEI"); } if (j == 3) { #asm("CLI"); TCCR1B = 0b00001001; // prescaler fullspeed #asm("SEI"); } if (j == 5) { #asm("CLI"); TCCR2 = 0b00001001; // prescaler fullspeed #asm("SEI"); } if (j == 0) { #asm("CLI"); TCCR2 = 0b00001111; // prescaler 8 TCCR1B = 0b00001001; // prescaler fullspeed #asm("SEI"); } // Load the clock prescaler from arrays OCR1A = freqOCR1B[j]; OCR2 = freqOCR2[j]; state = wait2; // Wait for sine wave to be stabilized by the filter time2 = 255; } break; /****************************** Post-processing of Data *****************************/ case post: phaseCal(); // phase calculation from raw ADC values yscale = 128.57; // Preset scale to 100Hz per 5 pixels for (j = 0; j<22;j++) { impArray[j] = impCal(j); // convert amplitude measurement to impedance yArray[j] = (char) (70-(impArray[j]/yscale)+15); // Calculate the y position of the impedance on the plot // the max is at 9500 ohm at y = 15; and min at 500 ohm // at y =85 //printf("y:%d,impedance:%d", yArray[j],impArray[j]); video_pt(bodeX[j], yArray[j],1); // plot the dots into the video buffer } state = display; break; /****************************** Turn on video *****************************/ case display: TIMSK = 0b10010010; // Turn on TIMER1A ISR TCCR1B = 0b00001001; // Turn on TIMER1, prescaler set at fullspeed OCR1A = lineTime; TCCR2 = 0; // Turn off TIMER2 and 0 TCCR0 = 0; state = done; // Go to done state to display results break; /****************************** Display results to the user *****************************/ case done: if (t>0) t--; // Subtract t if not 0, the state is reached only during video generation // Therefore t is decremented at 60 Hz //code to move cursor around // If PINC.3 is pressed, t is checked to debounce button if (PINC.3 == 0 && t == 0 && cursorY < 21) { cursorY++; // Variable keeps track of the position of the cursor sprintf(lcd_buffer,"Z: "); // Display impedance video_putsmalls(36,92,lcd_buffer); sprintf(lcd_buffer,"%05d",impArray[cursorY]); video_putsmalls(48,92,lcd_buffer); sprintf(lcd_buffer,"P: "); // Display phase video_putsmalls(4,92,lcd_buffer); sprintf(lcd_buffer,"%03d",lagleadArray[cursorY]); video_putsmalls(16,92,lcd_buffer); sprintf(lcd_buffer,"FREQ: "); // Display frequency video_putsmalls(80,92,lcd_buffer); sprintf(lcd_buffer,"%04d",sinefreq[cursorY]); video_putsmalls(104,92,lcd_buffer); // erase cursor at previous position video_line(bodeX[cursorY-1]-1,yArray[cursorY-1],bodeX[cursorY-1]+1,yArray[cursorY-1],0); video_line(bodeX[cursorY-1],yArray[cursorY-1]-1,bodeX[cursorY-1],yArray[cursorY-1]+1,0); // redraw the datapoint video_line(bodeX[cursorY-1],yArray[cursorY-1],bodeX[cursorY-1],yArray[cursorY-1],1); //draw cursor at the new position video_line(bodeX[cursorY]-1,yArray[cursorY],bodeX[cursorY]+1,yArray[cursorY],1); video_line(bodeX[cursorY],yArray[cursorY]-1,bodeX[cursorY],yArray[cursorY]+1,1); t = 10; // Reset timer count } // If PINC.4 is pressed, t is checked to debounce button if (PINC.4 == 0 && t == 0 && cursorY > 0) { cursorY--; // Decrement the cursor position sprintf(lcd_buffer,"Z: "); // Display Impedance video_putsmalls(36,92,lcd_buffer); sprintf(lcd_buffer,"%05d",impArray[cursorY]); video_putsmalls(48,92,lcd_buffer); sprintf(lcd_buffer,"P: "); // Display phase video_putsmalls(4,92,lcd_buffer); sprintf(lcd_buffer,"%03d",lagleadArray[cursorY]); video_putsmalls(16,92,lcd_buffer); sprintf(lcd_buffer,"FREQ: "); // Display frequency video_putsmalls(80,92,lcd_buffer); sprintf(lcd_buffer,"%04d",sinefreq[cursorY]); video_putsmalls(104,92,lcd_buffer); // Erase cursor at the previous position video_line(bodeX[cursorY+1]-1,yArray[cursorY+1],bodeX[cursorY+1]+1,yArray[cursorY+1],0); video_line(bodeX[cursorY+1],yArray[cursorY+1]-1,bodeX[cursorY+1],yArray[cursorY+1]+1,0); // Redraw previous data point video_line(bodeX[cursorY+1],yArray[cursorY+1],bodeX[cursorY+1],yArray[cursorY+1],1); // Draw cursor at the new position video_line(bodeX[cursorY]-1,yArray[cursorY],bodeX[cursorY]+1,yArray[cursorY],1); video_line(bodeX[cursorY],yArray[cursorY]-1,bodeX[cursorY],yArray[cursorY]+1,1); t = 10; // Reset timer count } if (PINC.2 == 0 && t == 0) { // TIMSK = 0b10001010; //enable interrupt T1B, T2 and T0 cmp // TCCR1A = 0x00; // TCCR0 = 0b00001011; // Prescaler set at 64 // OCR0 = 249; /* ADMUX = 0b00100000; // Initialize the ADC Ain = ADCH; // Start Conversion ADCSR.6=1; volt= 0; PORTB = volt; j = 0; */ TCCR1B = 0x00; reset(); } break; /****************************** State for testing purposes *****************************/ case 400: if (PINC.3 == 0 && time2 == 0) { PORTB++; printf("%d;", PINB); time2 = 50; } if (PINC.4 == 0 && time2 == 0) { PORTB--; printf("%d;", PINB); time2 = 50; } Ain = ADCH; ADCSR.6=1; printf("phase: %d; ",Ain); break; case 500: if (PINC.3 == 0 && time2 == 0) { j++; if (j == 1) { #asm("CLI"); TCCR2 = 0b00001010; // prescaler 8 TCCR1B = 0b00001010; // prescaler 8 #asm("SEI"); } if (j == 3) { #asm("CLI"); TCCR1B = 0b00001001; // prescaler fullspeed #asm("SEI"); } if (j == 5) { #asm("CLI"); TCCR2 = 0b00001001; // prescaler fullspeed #asm("SEI"); } if (j == 0) { #asm("CLI"); TCCR2 = 0b00001111; // prescaler 8 TCCR1B = 0b00001001; #asm("SEI"); } if (j==22) j=0; //TCCR2 = 0b00001010; // prescaler 8 //TCCR1B = 0b00001010; // prescaler 8 //TCCR2 = 0b00001001; // prescaler fullspeed //TCCR1B = 0b00001001; // prescaler fullspeed //TCCR2 = 0b00001111; // prescaler 1024 //TCCR1B = 0b00001100; // prescaler 256 OCR2 = freqOCR2[j]; // 400kHz OCR1A = freqOCR1B[j]; // 1 ms time2 = 50; } break; } end //line 231 } // end while } //end main /*************************************** Initalization Routine ***************************************/ void initialize(void) { MCUCR = 0b10000000; ADCSR = 0b11000110; DDRA = 0b00001100; // ADC input and Sqaure wave output DDRB = 0xff; // DAC output DDRC = 0x00; // Switches input DDRD = 0xf0; //video out //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor UCSRB = 0b00011000 ; //Hyperterm communication UBRRL = 103 ; // Video Initialization LineCount = 231; syncON = 0b00000000; syncOFF = 0b00100000; // Variable Initalization offset = 0.11; maxVolt = 4.83; reset(); } void reset(void) { ADMUX = 0b00100000; // Initialize the ADC Ain = ADCH; // Start Conversion ADCSR.6=1; printf("\n\rstarting...\r\n"); // DAC initialization volt= 0; PORTB = volt; j = 0; counter = 0; counter2 =0; //Print "CORNELL" video_puts(13,3,cu1); //Print "ECE476" video_puts(65,3,cu2); //side lines video_line(0,0,0,99,1); video_line(width,0,width,99,1); //top line & bottom lines video_line(0,0,width,0,1); video_line(0,99,width,99,1); video_line(0,11,width,11,1); video_line(0,89,width,89,1); //axis and titles video_line(5,85,120,85,1); //define x-axis of plot video_line(5,85,5,15,1); //define y-axis of plot // draw ticks on each axis for (xpos = 5; xpos <= 120; xpos = xpos+5){ video_line(xpos,85,xpos,84,1); if (xpos <= 60){ video_line(5,85-xpos,6,85-xpos,1); } } //init software timer t=0; //init animation x = 64; y=50; vx=1; vy=1; // State Machine Initalization state = wait2; time2 = 255; //state = 400; testFreq = 0; highfreq =0; sampleState = amplitude; #asm ("cli"); TIMSK = 0b10001010; //enable interrupt T1B, T2 and T0 cmp TCCR1A = 0x00; TCCR0 = 0b00001011; // Prescaler set at 64 OCR0 = 249; // 1 ms if (highfreq == 1) { // high freq set up TCCR2 = 0b00001001; // prescaler fullspeed TCCR1B = 0b00001001; // prescaler fullspeed OCR2 = freqOCR2[20]; // 400kHz OCR1A = freqOCR1B[20]; // 1 ms } else { // low freq set up TCCR2 = 0b00001111; // prescaler 1024 TCCR1B = 0b00001100; // prescaler 256 //TCCR2 = 0b00001010; // prescaler 8 //TCCR1B = 0b00001010; // prescaler 8 OCR2 = freqOCR2[0]; // 400kHz OCR1A = freqOCR1B[0]; // 1 ms } // Testing Purpose if (testFreq == 1) { TCCR2 = 0b00001010; // prescaler 8 TCCR1B = 0b00001010; // prescaler 8 TCCR2 = 0b00001001; // prescaler fullspeed TCCR1B = 0b00001001; // prescaler fullspeed //TCCR2 = 0b00001111; // prescaler 1024 //TCCR1B = 0b00001100; // prescaler 256 OCR2 = freqOCR2[21]; // 400kHz OCR1A = freqOCR1B[21]; // 1 ms ADMUX = 0b00100001; ADCSR.6=1; } printf("done"); #asm ("sei"); } /******************************** Unused: Calculate DB magnitude ********************************/ char dbMag(char index) { amp = AmpArray[index] * maxVolt/255 + offset; return (char) (-20.0*log10(amp/ 2.0) + 20); } /******************************** Calculate impedance from amplitude measurement ********************************/ int impCal (char index) { return (int) (((float)(AmpArray[index]) * 0.01960784) / current[index]); // Return calculated impedance } /******************************** Convert all phase measurement into degree ********************************/ void phaseCal() { char index; for (index = 0; index < 22; index++) { //if (lagleadArray[index] == lead) { // lead is capacitive lagleadArray[index] = ((float)(PhaseArray[index])) * 0.705882; //} //else { // lagleadArray[index] = (PhaseArray[index]-3) * -0.705882; //} //printf("phase: %d;, %d; ",lagleadArray[index],PhaseArray[index]); } } /******************************** Unused: find the maximum impedance ********************************/ char findMax() { char index; char max; max = 0; for (index = 1; index <22; index++) { // printf("%d\r\n",AmpArray[index]); if (AmpArray[index] > AmpArray[max]) { max =index; } } return max; }