Introduction Overview Hardware Software
Results Conclusions Code Schematic
Photos Costs Roles References

 
 
 
   
Download

CodeVision Project (.zip)

Source Code

/*
 Michael Austin and Bryan Doyle
 ECE 476 Final project Spring 2005
 CubeSat diagnostics board v1
 
 NOTES:
    transistors also voltage limit us/ no voltages above ~3.5 V   
    transistors are current limiting the circuit in this design
    so you can't get the actual current
    However, we don't care as long as there is a current
     
 PORT Designations:
 PORTA = LCD
 PORTB = voltage mux(0), programming(1-3), select lines(4-7)
 PORTC = Coils select lines(0-5), coils mux(6-7)
 PORTD = LEDs (0-1), serial comm(2-3), current mux(4-7)
 PORTE = serial comm(0-1), fpks mux(2-4), voltage mux(5-7)
 PORTF = ADCs
 PORTG = LED's (0-1), coils mux(2), Not used(3-4) 
*/

#include <mega128.h>
#include <stdlib.h>
#define _ALTERNATE_GETCHAR_ // we don't want to use standard getchar()
#define _ALTERNATE_PUTCHAR_ // we don't want to use standard putchar() 
#include <stdio.h> 
#include <delay.h>  
#include "7segtest.c"       // 7-segment display driver
#define begin {             // convention
#define end } 

// enum's of all the possible things we want to measure/toggle
enum{fpks,coils,volcur,toggle};
enum{mux8cur,mux8vol,coilxcur,coilycur,coilzcur,coilzvol,coilyvol,coilxvol};
enum{x,y,z};
enum{plus, minus}; 
enum{current, voltage};
enum{ohm5,ohm100k,DNC2,DNC3,ohm25,ohm200,ohm1k,ohm10};
enum{aerofin2,aerofin1,NC2,NC3,heater,gps,CDH,gpsmem};
enum{kill2gnd,kill2bat,kil11gnd,kill1bat,flightbat,flightgnd};

// function declarations
void test_ckt(int test,char sel,char sel2,char load); // test a component
void run_coil(char sel, char sel2,char typ); // test a coil
void run_mux8(char sel,char sel2,char load); // test something hooked up to mux8
void run_mux2(void); // test something hooked up to mux2

void set_load(char sel); // select load
float calc_adc(char val, char pin);   // compute ADC value into current or voltage
char read_adc(char pin);  // read desired ADC
void set_line(char pin); // toggle a select line
void initialize(void);

void put_cmd(char c);   // second serial port putchar
char get_cmd(void);     // second serial port getchar
void menu(void);        // main menu                
void print_description(char descrip); // extra info about tests
void print_endtest(void);          // show test complete


// RECEIVE VARIABLES
#define rx_buff_size 60
char rx_buff[rx_buff_size + 1];
char rx_read_index;
char rx_write_index;
char rx_count;
bit  rx_overflow;

#define rx_buff_size1 60
char rx_buff1[rx_buff_size1 + 1];
char rx_read_index1;
char rx_write_index1;
char rx_count1;
bit  rx_overflow1;

// TRANSMIT VARIABLES
#define tx_buff_size 10
char tx_buff[tx_buff_size + 1];
char tx_read_index;
char tx_write_index;
char tx_count;
bit  tx_ready;
int temp_count;

#define tx_buff_size1 10
char tx_buff1[tx_buff_size1 + 1];
char tx_read_index1;
char tx_write_index1;
char tx_count1;
bit  tx_ready1;
int temp_count1;

// global admin variables
// admin_state switches through menu
// temp1-4 set up the tests
// ready indicates whether the test is ready to go
// testnum is an index for the default program
// no_comp is a flag that sets whether a delay is used, or a keystroke from the user
char admin_state,temp1,temp2,temp3,temp4,ready,testnum,no_comp;

// Counters (seconds, generic, milliseconds)
int s, count,ms; 

// time base
interrupt [TIM0_OVF] void timing(void)
begin
    count--;
    if(count == 0) {
        ms++;
        count = 32;
        }
    if(ms >= 1000) {
        s++;
        ms = 0;
        }
end

// recieve serial 0
interrupt [USART0_RXC] void isr_uart_rxc(void)
begin
   char c;
   c = UDR0;                                                               
   
   rx_buff[rx_write_index] = c;         // put new char in our rx buffer
   
   if (++rx_write_index > rx_buff_size)
       rx_write_index = 0;        // wrap the pointer
       
   if (++rx_count > rx_buff_size)    // check for overflow
   begin
      rx_count = rx_buff_size;        // size can't be bigger than buffer
      rx_overflow = 1;            // be aware that error has occured!
   end 
   
end // end isr_uart_rxc 

// transmit serial 0
interrupt [USART0_TXC] void isr_uart_txc(void)
begin 
  if (tx_count != 0)
  begin
      if (tx_ready == 1)
      begin
          tx_ready = 0;
          if (++tx_read_index > tx_buff_size)
              tx_read_index = 0;
            tx_count--;
       end
       if (tx_count != 0)
       begin
         UDR0 = tx_buff[tx_read_index];
         if(++tx_read_index > tx_buff_size)
                 tx_read_index = 0;
        tx_count--; 
       end
    
  end 
  UCSR0B |= 0x40;    // reset interrupt flag 
temp_count++;
end 

// takes care of recieve for second serial port
interrupt [USART1_RXC] void isr_uart1_rxc(void)
begin
char c;
   c = UDR1;                                                               
   
   rx_buff1[rx_write_index1] = c;             // put new char in our rx buffer
   
   if (++rx_write_index1 > rx_buff_size1)
       rx_write_index1 = 0;            // wrap the pointer
       
   if (++rx_count1 > rx_buff_size1)        // check for overflow
   begin
      rx_count1 = rx_buff_size1;            // size can't be bigger than buffer
      rx_overflow1 = 1;                // be aware that error has occured!
   end 
end 

// takes care of transmission for second serial port
interrupt [USART1_TXC] void isr_uart1_txc(void)
begin
if (tx_count1 != 0)
  begin
      if (tx_ready1 == 1)
      begin
          tx_ready1 = 0;
          if (++tx_read_index1 > tx_buff_size1)
              tx_read_index1 = 0;
            tx_count1--;
       end
       if (tx_count1 != 0)
       begin
         UDR1 = tx_buff1[tx_read_index1];
         if(++tx_read_index1 > tx_buff_size1)
                 tx_read_index1 = 0;
        tx_count1--; 
       end
end 

UCSR1B |= 0x40;    // reset interrupt flag 
temp_count1++;
end

// non-blocking getchar to hyperterm  
char getchar(void)
begin 
    char c;                           // Local variable used for return value
    while (rx_count == 0) ;         // We have a character already here
    begin
       c = rx_buff[rx_read_index];        // Read from receive buffer
       if(++rx_read_index > rx_buff_size)   // Wrap around buffer if needed
           rx_read_index = 0;
       if(rx_count)                // Decrease number of characters in buffer
                  rx_count--;
    end
    
    return c;
end 

// non-blocking putchar to hyperterm
void putchar(char c)
begin
    char tx_init = 0;
    while(tx_count > (tx_buff_size - 1)) ;    // prevents running around on buffer. 
                        // potential system hesitation (blocking)
                        // but errors are worse.
                        // if widespread, we can increase size of buffer
           if (tx_count == 0)
        tx_init = 1;            // if nothing in tx buffer, we need to start tx up again
        
    tx_buff[tx_write_index++] = c;        // push char into buffer
    
    if (tx_write_index > tx_buff_size)    // wrap around buffer
              tx_write_index = 0;
    
    tx_count++;                // we have another entry in tx buffer    
    
          if (tx_init == 1)
          begin
        tx_ready = 1;
        UDR0 = c;
         end    
       
end 

// send commands to the power board mcu
void put_cmd(char c)
begin
char tx_init1 = 0;
    while(tx_count1 > (tx_buff_size1 - 1)) ;// prevents running around on buffer. 
                        // potential system hesitation (blocking)
                        // but errors are worse.
                        // if widespread, we can increase size of buffer
           if (tx_count1 == 0)
        tx_init1 = 1;            // if nothing in tx buffer, we need to start tx up again
        
    tx_buff1[tx_write_index1++] = c;        // push char into buffer
    
    if (tx_write_index1 > tx_buff_size1)    // wrap around buffer
              tx_write_index1 = 0;
    
    tx_count1++;                // we have another entry in tx buffer    
    
          if (tx_init1 == 1)
          begin
        tx_ready1 = 1;
        UDR1 = c;
         end    
       
end 

// get commands from the power board mcu
char get_cmd(void)
begin
    char c;                           // Local variable used for return value
    s = 0;
    while (rx_count1 == 0) {
    }                     // We have a character already here
    begin
       c = rx_buff1[rx_read_index1];    // Read from receive buffer
       if(++rx_read_index1 > rx_buff_size1) // Wrap around buffer if needed
           rx_read_index1 = 0;
       if(rx_count1)            // Decrease number of characters in buffer
                  rx_count1--;
    end
    return c;
end  

// primary test vehicle, calls all other tests based upon user inputs
void test_ckt(int test,char sel,char sel2,char load)
begin
switch(test)
begin   
    case fpks:
        run_mux2();
    break;
    case coils:
        run_coil(sel, sel2,load);
    break;
    case volcur:
        run_mux8(sel,sel2,load);
    break; 
    case toggle:
        set_line(sel);
    break;
end
end

// measure the voltages and currents of the coils in both directions
// methodology: set the select lines, toggle led's, ready the appropriate
// values and display
void run_coil(char sel, char sel2,char typ)
begin
char val;
float displayval,conv;
int lcdval;
lcd_4digit_clear();
// run coil in plus direction 
if(sel2 == plus){ 
    PORTG = 4;
    PORTC.7 = 0;
    PORTC.6 = 0;
    if(sel == x){
    PORTC.4 = 1;
    PORTC.2 = 0;
    PORTC.0 = 0;
    if(typ == current){val = read_adc(coilxcur); displayval = calc_adc(val,coilxcur);
        print_description(16);
        PORTG = 3;
        PORTD = 2;}
    if(typ == voltage){val = read_adc(coilxvol); displayval = calc_adc(val,coilxvol);
        print_description(15);
        PORTG = 5;
        PORTD = 3;}
    } 
    if(sel == y){
    PORTC.4 = 0;
    PORTC.2 = 1;
    PORTC.0 = 0;
    if(typ == current){val = read_adc(coilycur); displayval = calc_adc(val,coilycur);
        PORTG = 3;
        PORTD = 2;}
    if(typ == voltage){val = read_adc(coilyvol); displayval = calc_adc(val,coilyvol);
        PORTG = 5;
        PORTD = 3;}
    }
    if(sel == z){
    PORTC.4 = 0;
    PORTC.2 = 0;
    PORTC.0 = 1;
    if(typ == current){val = read_adc(coilzcur); displayval = calc_adc(val,coilzcur);
        PORTG = 3;
        PORTD = 2;}
    if(typ == voltage){val = read_adc(coilzvol); displayval = calc_adc(val,coilzvol);
        PORTG = 5;
        PORTD = 3;}
    }
}
if(sel2 == minus){
          PORTG = 2;
          PORTC.7 = 1;
          PORTC.6 = 1;
          if(sel == x){
          PORTC.5 = 1;
    PORTC.3 = 0;
    PORTC.1 = 0;
    if(typ == current){val = read_adc(coilxcur); displayval = calc_adc(val,coilxcur);
        print_description(16);
        PORTG = 3;
        PORTD = 2;}
    if(typ == voltage){val = read_adc(coilxvol); displayval = calc_adc(val,coilxvol);
        print_description(15);
        PORTG = 5;
        PORTD = 3;}
          }
    if(sel == y){
    PORTC.5 = 0;
    PORTC.3 = 1;
    PORTC.1 = 0;
    if(typ == current){val = read_adc(coilycur); displayval = calc_adc(val,coilycur);
        PORTG = 3;
        PORTD = 2;}
    if(typ == voltage){val = read_adc(coilyvol); displayval = calc_adc(val,coilyvol);
        PORTG = 5;
        PORTD = 3;}
    }
    if(sel == z){
    PORTC.5 = 0;
    PORTC.3 = 0;
    PORTC.1 = 1;
    if(typ == current){val = read_adc(coilzcur); displayval = calc_adc(val,coilzcur);
        PORTG = 3;
        PORTD = 2;}
    if(typ == voltage){val = read_adc(coilzvol); displayval = calc_adc(val,coilzvol);
        PORTG = 5;
        PORTD = 3;}
    }
}
if(typ == current) {
conv = displayval * 100; 
lcdval = (int) conv;
delay_ms(200);
lcd_4digit_int(lcdval);
printf("\n\r The current is: %2.2f A",displayval);
if(no_comp) delay_ms(1000);
else  getchar();
PORTD = 2; }
if(typ == voltage) {
conv = displayval * 100; 
lcdval = (int) conv;
delay_ms(200);
lcd_4digit_int(lcdval);
printf("\n\r The voltage is: %2.2f V",displayval);
if(no_comp) delay_ms(1000);
else getchar();
PORTG = 1;}
ready = 0;
end

// set up the voltage/current readings and take them. Then display the values.
// Current measure set voltage and load and checks for current  ( < .210 A)
// Voltage measure disables the load mux and checks the voltage ( < 2.8 V)
void run_mux8(char sel,char sel2,charar load)
begin
char val,flag=0;
float displayval,conv;
int lcdval;
lcd_4digit_clear();
switch(sel)
begin
    case mux8cur: 
    PORTB.0 = 0;
    PORTD.7 = 0; 
    PORTG = 3;
    // set select lines to pick appropriate voltage/load
    if (sel2 == aerofin2){PORTE.5 = 0; PORTE.6 = 0; PORTE.7 = 0; print_description(9);}
    if (sel2 == aerofin1){PORTE.5 = 1; PORTE.6 = 0; PORTE.7 = 0; print_description(10);}
    if (sel2 == heater){PORTE.5 = 0; PORTE.6 = 0; PORTE.7 = 1; print_description(11);}
    if (sel2 == gps){PORTE.5 = 1; PORTE.6 = 0; PORTE.7 = 1; print_description(12);}
    if (sel2 == CDH){PORTE.5 = 0; PORTE.6 = 1; PORTE.7 = 1;}
    if (sel2 == gpsmem){PORTE.5 = 1; PORTE.6 = 1; PORTE.7 = 1; print_description(14);}
    set_load(load);
    val = read_adc(sel);
    displayval = calc_adc(val,sel);
    if((displayval > .05)||(displayval < .02)) PORTD = 2;
    else PORTD = 1;
    // convert value to be displayed to LCD
    conv = displayval * 100; 
    lcdval = (int) conv; 
    delay_ms(200);
    lcd_4digit_int(lcdval);
    printf("\n\r The current is: %2.2f A",displayval);
    ready = 0; ; 
    if(no_comp) delay_ms(1000);
    else getchar();
    break;
    case mux8vol:
    PORTB.0 = 0;
           PORTD.7 = 1;
           PORTD.0 = 1;
           PORTD.1 = 1;
           // set select lines to pick appropriate voltage
    if (sel2 == aerofin2){PORTE.5 = 0; PORTE.6 = 0; PORTE.7 = 0; print_description(9);}
    if (sel2 == aerofin1){PORTE.5 = 1; PORTE.6 = 0; PORTE.7 = 0; print_description(10);}
    if (sel2 == heater){PORTE.5 = 0; PORTE.6 = 0; PORTE.7 = 1; print_description(11);}
    if (sel2 == gps){PORTE.5 = 1; PORTE.6 = 0; PORTE.7 = 1; print_description(12);}
    if (sel2 == CDH){PORTE.5 = 0; PORTE.6 = 1; PORTE.7 = 1;}
    if (sel2 == gpsmem){PORTE.5 = 1; PORTE.6 = 1; PORTE.7 = 1; print_description(14);}
    set_load(load);
    val = read_adc(sel);
    displayval = calc_adc(val,sel);
    if((displayval > 2.0) || (displayval < 0.2)){ PORTG = 1;flag = 1;}
    else {
        PORTG = 2;
        printf("\n\rVoltage Below acceptable range");
    }
    // convert value to be displayed to LCD
    conv = displayval * 100; 
    lcdval = (int) conv; 
    delay_ms(200);
    if(flag = 1) lcd_4digit_int(lcdval);
    else lcd_4digit_error(lcdval);
    printf("\n\r The voltage is: %2.2f V",displayval);
    flag = 0;
    ready = 0;
    if(no_comp) delay_ms(1000);
    else getchar();
    break;
end
// Disable muxes
PORTB.0 = 1;
PORTD.7 = 1;
end 
 
// cycle through all the fpks combinations and see if a voltage appears
void run_mux2(void)
begin 
print_description(1);
PORTE.2 = 0;
PORTE.3 = 0;
PORTE.4 = 0;
run_mux8(mux8vol, CDH, ohm200);                 
print_endtest();

print_description(2);
PORTE.2 = 0;
PORTE.3 = 0;
PORTE.4 = 1;
run_mux8(mux8vol, CDH, ohm200);   
print_endtest();
print_description(3);

PORTE.2 = 0;
PORTE.3 = 1;
PORTE.4 = 0;
run_mux8(mux8vol, CDH,ohm200); 
print_endtest();
print_description(4);

PORTE.2 = 0;
PORTE.3 = 1;
PORTE.4 = 1;     
run_mux8(mux8vol, CDH,ohm200);
print_endtest();
print_description(5);

PORTE.2 = 1;
PORTE.3 = 0;
PORTE.4 = 0;
run_mux8(mux8vol, CDH,ohm200); 
print_endtest();
print_description(6);

PORTE.2 = 1;
PORTE.3 = 0;
PORTE.4 = 1;
run_mux8(mux8vol, CDH,ohm200); 
print_endtest();
print_description(7);

PORTE.2 = 1;
PORTE.3 = 1;
PORTE.4 = 0;
run_mux8(mux8vol, CDH,ohm200);
print_endtest(); 
print_description(8);

PORTE.2 = 1;
PORTE.3 = 1;
PORTE.4 = 1; 
run_mux8(mux8vol, CDH,ohm200); 
print_endtest();  
lcd_4digit_dash();
ready = 0;
end 

// set up multiplexer to connect to the correct load
void set_load(char sel)
begin
switch(sel)
begin
    case ohm5:
         PORTD.4 = 0;
    PORTD.5 = 0;
    PORTD.6 = 0;
         break;
    case ohm10:
         PORTD.4 = 1;
    PORTD.5 = 0;
    PORTD.6 = 1;
        break;
    case ohm25:
        PORTD.4 = 0;
    PORTD.5 = 0;
    PORTD.6 = 1;
       break;
    case ohm200:
       PORTD.4 = 0;
    PORTD.5 = 1;
    PORTD.6 = 1;
        break;
    case ohm1k:
       PORTD.4 = 1;
    PORTD.5 = 1;
    PORTD.6 = 1;
       break;
    case ohm100k:
       PORTD.4 = 1;
    PORTD.5 = 0;
    PORTD.6 = 0;
        break;
end
end

// calculate the acutal voltage/current values taking into account gain
float calc_adc(char val, char pin)
begin
float displayval,tempval;
switch(pin)
begin       
    case mux8cur:
    displayval = (float) (val / 255.0 * 5.0 / 50.0 * 11);
    break; 
    
    case mux8vol:
        tempval = (float) (val / 255.0 * 5.0 * 2.022);
    if(tempval <= 2.7) displayval = tempval;
    else{
        printf("\n\rInput Voltage above specified input range\r\n");
        displayval = tempval;
    }
    break; 
    
    case coilxcur:
        displayval = (float) (val / 255.0 * 5.0 / 50.0 * 135.0);
    break; 
    
    case coilycur:
        displayval = (float) (val / 255.0 * 5.0 / 50.0 * 135.0);
    break; 
    
    case coilzcur:
        displayval = (float) (val / 255.0 * 5.0 / 50.0 * 135.0);
    break; 
    
    case coilzvol:
    displayval = (float) (val / 255.0 * 5.0)+.5+.5;
    break; 
    
    case coilyvol:
        displayval = (float) (val / 255.0 * 5.0)+.35+.35; // adjust for two transistor drops
    break;  
    
    case coilxvol:
    displayval = (float) (val / 255.0 * 5.0)+.55+.55; // adjust for two transistor drops
    break; 
end
return displayval;
end     

// set up and read the adc values
// Methodology: set ADMUX to appropriate ADC
// Do a dummy measurement to take care of transients
// Do the real measurement (make sure to delay)
char read_adc(char pin)
begin
char val = 0;
switch(pin)
begin
    case mux8cur:
    delay_ms(50);
    ADMUX = 0b01100000;  // select correct adc
    ADCSRA.6 = 1;        // start conversion
          delay_us(300);       // wait for conversion to finish
    ADCSRA.6 = 1;
    delay_us(300);
    val=ADCH;            // read value 
    break; 
    
    case mux8vol:
    delay_ms(50);
    ADMUX = 0b01100001;  // repeat above process
    ADCSRA.6 = 1;        // start conversion
          delay_us(300);       // wait for conversion to finish
    ADCSRA.6 = 1;
    delay_us(300);
    val = ADCH;
           break; 
           
    case coilxcur:
    ADMUX = 0b01100010;
    ADCSRA.6 = 1;        // start conversion
          delay_us(300);       // wait for conversion to finish
    ADCSRA.6=1;
    delay_us(300);
    val = ADCH;
           break; 
           
    case coilycur:
          ADMUX = 0b01100011;
          ADCSRA.6 = 1;        // start conversion
          delay_us(300);       // wait for conversion to finish
          ADCSRA.6=1;
          delay_us(300);
    val = ADCH;
          break; 
          
    case coilzcur:
           ADMUX = 0b01100100;
    ADCSRA.6 = 1;        // start conversion
          delay_us(300);       // wait for conversion to finish
    ADCSRA.6=1;  
    delay_us(300);
    val = ADCH;
          break; 
          
    case coilzvol:
         ADMUX = 0b01100101;
         ADCSRA.6 = 1;        // start conversion
          delay_us(300);       // wait for conversion to finish
         ADCSRA.6=1; 
         delay_us(300);
    val = ADCH;
         break; 
         
    case coilyvol:
          ADMUX = 0b01100110;
          ADCSRA.6 = 1;        // start conversion
          delay_us(300);       // wait for conversion to finish
          ADCSRA.6=1; 
          delay_us(300);
    val = ADCH;
         break; 
         
    case coilxvol:
         ADMUX = 0b01100111;
         ADCSRA.6 = 1;        // start conversion
          delay_us(300);       // wait for conversion to finish
         ADCSRA.6=1; 
         delay_us(300);
    val = ADCH;
         break;
end
return val;
end

// Toggles select lines that connect up to the power board
void set_line(char pin)
begin 
if (pin == aerofin2) PORTB.6 = 1;
if (pin == aerofin1) PORTB.4 =1;
if (pin == heater) PORTB.5 = 1;
if (pin == gpsmem) PORTB.7 = 1;
printf("\n\r press a key to continue \n\r");
getchar();
PORTB.4 = 0;
PORTB.5 = 0;
PORTB.6 = 0;
PORTB.7 = 0;
ready = 0;
end 

// Main menu 
// Steps through all the options that the board can measure
// Sets temporary values to set up the tests as specified
void menu(void)
begin 
char input; // user input storage and default program index
// main menu 22 cases total
PORTG = 0;
PORTD = 0;
switch(admin_state)
begin   // startup screen
    case 0:
    lcd_4digit_dash();
    printf("\n\r1. Test flight pin/kill switch\r\n");
    printf("2. Test coils\r\n"); 
    printf("3. Measure voltage on various pins\r\n");
    printf("4. Measure current on various pins\r\n");
    printf("5. Run Default Program\r\n");
    printf("6. Toggle select lines\r\n");
    printf("7. Send a command to the Power board\r\n");
    printf("Your choice: ");
    admin_state = 1;
    s = 0;
    break;
    
    // get user input
    case 1:
    if(s >= 60) {admin_state = 2; no_comp = 1;}
    if(rx_count > 0)
        input = getchar();
    if((input > 48) && (input < (48 + 8))){
        putchar(input);
        if (input == 49)
                   admin_state = 3;  
            if (input == 50)
                   admin_state = 4;  
               if (input == 51)
                   admin_state = 5;
        if (input == 52)
            admin_state = 6;
        if (input == 53){
            admin_state = 2;
            testnum = 0;
            } 
        if (input == 54){
            admin_state = 9;
        }
        if (input == 55){
            admin_state = 23; 
            printf("\n\rType single character command to power board\r\n");
        }
    }
    else if((input >=7)){
        printf("\n\rTry again");
        delay_ms(10);
        admin_state = 0;
    }  
    break; 
    
    // default program
    case 2: 
    ready = 0;
    if(testnum == 0){
        printf("\n\rRunning default program...\r\n");
        temp1 = fpks;
        temp2 = 0;
        temp3 = 0;
        temp4 = 0;
        ready = 1;
        testnum = 1; 
        admin_state = 2;
    }
    else if(testnum == 1){
         temp1 = coils;
        temp2 = x;
        temp3 = plus;
        temp4 = voltage;
        ready = 1;
        testnum = 2; 
        admin_state = 2;
    }
    else if(testnum == 2){  
            temp1 = coils;
        temp2 = x;
        temp3 = minus;
        temp4 = current;
        ready = 1;
        testnum = 3; 
        admin_state = 2;
    }
    else if(testnum == 3){
               temp1 = volcur;
        temp2 = voltage;
        temp3 = aerofin2;
        temp4 = ohm5;
        ready = 1;
        testnum = 4; 
        admin_state = 2;
    }
    else if(testnum == 4){
        temp1 = volcur;
        temp2 = current;
        temp3 = aerofin2;
        temp4 = ohm5;
        ready = 1;
        testnum = 5; 
        admin_state = 2;
        }
        else if(testnum == 5){
            printf("\n\rDefault program done\n\r");
            printf("Hit any key to continue\n\r");
            admin_state = 2;
            testnum = 6;
        }
        else if(testnum == 6){
            if(no_comp) delay_ms(1000);
        else getchar();
            admin_state = 0;
            ready = 0;
            no_comp = 0; 
            testnum = 0;
        }
    break;
    
    // flight-pin kill switch selected
    case 3:
    temp1 = fpks;
    printf("\n\rThis test will toggle flight pin and kill switch lines");
    printf("\n\rIf the output matches the statements then the test was a success");
    printf("\n\rIf not then the test failed, enjoy.");
    printf("\n\rHit a key to proceed");
    temp2 = 0;
    temp3 = 0;
    temp4 = 0; 
    admin_state = 8;
    break; 
    
    // coils selected
    case 4:
    temp1 = coils;
    printf("\n\rWhich coil do you want to test?\r\n");
    printf("1. X\r\n");
    printf("2. Y\r\n");
    printf("3. Z\r\n");
    printf("Your choice: ");
    admin_state = 14;
    break; 
    
    // voltage measurement selected
    case 5:
    temp1 = volcur;
    temp2 = mux8vol;
    printf("\n\rWhich value do you want to test?\r\n");
    printf("1.Aerofin2\r\n");
    printf("2.Aerofin1\r\n");
    printf("3.Heater\r\n");
    printf("4.GPS\r\n");
    printf("5.CDH\r\n");
    printf("6.GPSMEM\r\n");
    printf("Your choice: ");
    admin_state = 7;
    break; 
    
    // current measurement selected
    case 6:
    temp1 = volcur;
    temp2 = mux8cur;
    printf("\n\rWhich value do you want to test?\r\n");
    printf("1.Aerofin2\r\n");
    printf("2.Aerofin1\r\n");
    printf("3.Heater\r\n");
    printf("4.GPS\r\n");
    printf("5.CDH\r\n");
    printf("6.GPSMEM\r\n");
    printf("Your choice: ");
    admin_state = 7;
    break; 
    
    // get more user specifications on voltage/current measurement
    case 7:
    if(rx_count > 0)
        input = getchar();
    if((input > 48) && (input < (48 +7))){
        putchar(input);
        if (input == 49){
                temp3 = aerofin2;
                temp4 = ohm5;
                }
            if (input == 50){
                temp3 = aerofin1;
                temp4 = ohm5;
                } 
               if (input == 51){
                   temp3 = heater;
                   temp4=ohm25;
                   }
        if (input == 52){
                temp3 = gps;
                temp4 = ohm10;
                }
        if (input == 53){
                temp3 = CDH;
                temp4 = ohm200;
                }
        if (input == 54){
        
            temp3 = gpsmem;
            temp4 = ohm1k;
            }
          admin_state = 10;
        }
    else if(input >=7){
        printf("\n\rTry again");
        delay_ms(10);
        admin_state = 6;
    }  
    break; 
                        
    // wait for user input before proceeding with fpks test
    case 8:
         if(rx_count > 0) input = getchar();
         ready = 1;
         admin_state = 19;
    break; 
    
    // Select Toggle lines
    case 9: 
        temp1 = toggle;
        printf("\n\rWhich do you want to toggle?\r\n");
        printf("1. Aerofin2\r\n");
        printf("2. Heater\r\n");
        printf("3. Aerofin1\r\n");
        printf("4. GPS Mem\r\n");
        printf("Your choice: ");
        admin_state = 20;
    break;
    
    // tell the user what they picked
    case 10:
       //    printf("\n\r %c You chose: ",temp1,"",temp2,"",temp3,"",temp4,"\r\n"); 
    ready = 1;
    admin_state = 19;
    break;
    
    // get coil specifications
    case 14:
    if(rx_count > 0)
        input = getchar();
    if((input > 48) && (input < (48 +4))){
           putchar(input);
           if(input == 49) temp2 = x;
           if(input == 50) temp2 = y;
           if(input == 51) temp2 = z;
           admin_state = 15;
           } 
    else if(input >= 7){
    printf("\n\rTry again");
            delay_ms(10);
         admin_state = 4;
    }
    break;
     
    // get more coil specs
    case 15:
    printf("\n\rWhich direction do you want to run the coil?\r\n");
    printf("1. +\r\n");
    printf("2. -\r\n");
    printf("Your choice: ");
    admin_state = 16;
    break;
    
    // more coil specs
    case 16:
    if(rx_count > 0)
        input = getchar();
    if((input > 48) && (input < (48 +3))){
        putchar(input);
        if(input == 49) temp3 = plus;
        if(input == 50) temp3 = minus;
        admin_state = 17;
    }
    else if(input >= 7){
    printf("\n\rTry again");
        delay_ms(10);
        admin_state = 15;
    } 
    break;
    
    //  more coil specs
    case 17:
    printf("\n\rMeasure: \n\r");
    printf("1. Current\r\n");
    printf("2. Voltage\r\n");
    printf("Your choice: "); 
    admin_state = 18;
    break;
    
    // more coil specs
    case 18:
    if(rx_count > 0){
        input = getchar();}
    if((input > 48) && (input < (48 +3))){
        putchar(input);
        if(input == 49) temp4 = current;
        if(input == 50) temp4 = voltage;
        ready = 1;
        admin_state = 19;
    }
    else if(input >= 7){
    printf("\n\rTry again");
        delay_ms(10);
        admin_state = 17;
    } 
    break;
    
    // prompt user input after test is complete
    case 19:
    printf("\n\r Hit any key to continue");
    admin_state = 21; 
    break;
    case 20:
    if(rx_count > 0){input = getchar();}
    if((input > 48) && (input < (48 +5))){
        putchar(input);
        if(input == 49) temp2 = aerofin2;
        if(input == 50) temp2 = heater;
        if(input == 51) temp2 = aerofin1;
        if(input == 52) temp2 = gpsmem;
        ready = 1; 
        temp3 = 0;
        temp4 = 0;
        admin_state = 19;
    } 
    else if(input >= 7){
    printf("\n\rTry again");
        delay_ms(10);
        admin_state = 9;
    } 
    break;
    
    // get user input and reset system
    case 21:
    if(rx_count > 0){
        input = getchar();
    ready = 0;
    temp1 = 0;
    temp2 = 0;
    temp3 = 0;
    temp4 = 0;
    PORTD = 3;
    PORTG = 3;
    lcd_4digit_dash();
    admin_state = 0; 
    s=0;
    }
    break; 
    
    // Serial comm test
        case 22:
             delay_ms(50);
            while(rx_count1 > 0){
                input = get_cmd();
                putchar(input);  
            } 
                admin_state = 19;
        break;
        // input to serial comm tests
        case 23:
            input = getchar();
            put_cmd(input); 
            put_cmd(0x0d);
            admin_state = 22;
        break;
end
end 

// Prints description of test that is running
void print_description(char descrip)
begin          
        printf("\r\n");
        printf("\r\n");
        printf("\r\n");
        printf("\r\n");
        printf("-----------START OF TEST-----------\r\n");                    
        lcd_4digit_marked(descrip);
switch(descrip)

    begin
    case 1:
    begin  

        printf("Description:\r\n");
        printf("Starting Test Code -01-\r\n");
        printf("Kill Switch Flight Pin Function\r\n");
        printf("\r\n");
        printf("Applied:\r\n");
        printf("Kill Switch 1:  Power GND\r\n");
        printf("Kill Switch 2:  Power GND\r\n");
        printf("Flight Pin  1:    Power GND\r\n");
        printf("\r\n");
    end
    break;     
    case 2:
    begin  

        printf("Description:\r\n");
        printf("Starting Test Code -02-\r\n");
        printf("Kill Switch Flight Pin Function\r\n");
        printf("\r\n");
        printf("Applied:\r\n");
        printf("Kill Switch 1:  Power GND\r\n");
        printf("Kill Switch 2:  Power GND\r\n");
        printf("Flight Pin  1:    Power VBatt\r\n");
        printf("\r\n");
    end
    break;    
    case 3:
    begin  
        printf("Description:\r\n");
        printf("Starting Test Code -03-\r\n");
        printf("Kill Switch Flight Pin Function\r\n");
        printf("\r\n");
        printf("Applied:\r\n");
        printf("Kill Switch 1:  Power GND\r\n");
        printf("Kill Switch 2:  Power VBatt\r\n");
        printf("Flight Pin  1:    Power GND\r\n");
        printf("\r\n");
    end
    break;       
    case 4:
    begin  
        printf("Description:\r\n");
        printf("Starting Test Code -04-\r\n");
        printf("Kill Switch Flight Pin Function\r\n");
        printf("\r\n");
        printf("Applied:\r\n");
        printf("Kill Switch 1:  Power GND\r\n");
        printf("Kill Switch 2:  Power VBatt\r\n");
        printf("Flight Pin  1:    Power VBatt\r\n");
        printf("\r\n");
    end
    break;                                     
    
    case 5:
    begin  
        printf("Description:\r\n");
        printf("Starting Test Code -05-\r\n");
        printf("Kill Switch Flight Pin Function\r\n");
        printf("\r\n");
        printf("Applied:\r\n");
        printf("Kill Switch 1:  Power VBatt\r\n");
        printf("Kill Switch 2:  Power GND\r\n");
        printf("Flight Pin  1:    Power GND\r\n");
        printf("\r\n");
    end
    break;                                
    
    case 6:
    begin  
        printf("Description:\r\n");
        printf("Starting Test Code -06-\r\n");
        printf("Kill Switch Flight Pin Function\r\n");
        printf("\r\n");
        printf("Applied:\r\n");
        printf("Kill Switch 1:  Power VBatt\r\n");
        printf("Kill Switch 2:  Power GND\r\n");
        printf("Flight Pin  1:    Power VBatt\r\n");
        printf("\r\n");
    end
    break;
    case 7:
    begin  
        printf("Description:\r\n");
        printf("Starting Test Code -07-\r\n");
        printf("Kill Switch Flight Pin Function\r\n");
        printf("\r\n");
        printf("Applied:\r\n");
        printf("Kill Switch 1:  Power VBatt\r\n");
        printf("Kill Switch 2:  Power VBatt\r\n");
        printf("Flight Pin  1:    Power GND\r\n");
        printf("\r\n");
    end
    break;
    case 8:
    begin  
        printf("Description:\r\n");
        printf("Starting Test Code -08-\r\n");
        printf("Kill Switch Flight Pin Function\r\n");
        printf("\r\n");
        printf("Applied:\r\n");
        printf("Kill Switch 1:  Power VBatt\r\n");
        printf("Kill Switch 2:  Power VBatt\r\n");
        printf("Flight Pin  1:    Power VBatt\r\n");
        printf("\r\n");
    end               
    break;
    case 9:
        printf("\r\nMeasuring aerofin2\r\n");
        printf("Test code -09-\r\n");
        printf("A = 0 B = 0 C= 0");
    break;
    case 10:
        printf("\r\nMeasuring aerofin1\r\n");
        printf("Testcode -10-\r\n");
        printf("A= 0 B= 0 C= 1");
    break;
    case 11:
         printf("\r\nMeasuring Heater\r\n");
        printf("Testcode -11-\r\n");
        printf("A= 1 B= 0 C= 0");
    break;
    case 12: 
        printf("\r\nMeasuring gps\r\n");
        printf("Testcode -12-\r\n");
        printf("A= 1 B= 0 C= 1"); 
    break;
    case 13:
        printf("\r\nMeasuring CDH\r\n");
        printf("Testcode -13-\r\n");
        printf("A= 1 B= 1 C= 0");
    break;
    case 14:
        printf("\r\nMeasuring gpsmem\r\n");
        printf("Testcode -14-\r\n");
        printf("A= 1 B= 1 C= 1"); 
    break;
    case 15: 
        printf("\n\rMeasuring Coil X voltage");
        printf("\n\r Testcode -15-\r\n");  
    break;
    case 16:
        printf("\n\rMeasureing Coil X current");
        printf("\n\r Testcode -16-\r\n");
    break;
    default:                                   
        begin
        printf("WARNING: NO DESCRIPTION FOUND FOR TEST\r\n");
        printf("\r\n");
        end
    end
        printf("\r\n");
        printf("Test Result Output:\r\n");
end                                                               

void print_endtest(void)
begin
        printf("\r\n------------END OF TEST---------------\r\n");                    
end

// MAIN - run menu, get user input, run test_ckt
void main(void)
begin        
char test;
char sel, sel2,load;    
initialize(); // Initialize
lcd_init();   // Init lcd
lcd_4digit_dash(); // Put dashes on lcd
lcd_4digit_help();
printf("\n\rWelcome please choose your operation\r\n"); 
while(1)
begin   
    menu();
    if (ready){
        test = temp1;
        sel = temp2;
               sel2 = temp3;
               load = temp4;
        test_ckt(test, sel,sel2,load);
    }
end
end

// Init 
void initialize(void)
begin      
UCSR0B = 0xD8;    // enables ALL interrupts
UBRR0L = 51;    // 9600 Baud @ 16MHz XTAL
UCSR1B = 0xD8;  // enables interrupts
UBRR1L = 51;    // 9600 Baud


DDRB = 0xff;     // 8:1 mux select lines out
DDRD = 0xff;     // 8:1 mux select lines out
DDRG = 0xff;     // led's
PORTD = 3;       // led's off
PORTG = 3;       // led's off


PORTB.0 = 1;     // Disable 8:1 mux
PORTD.7 = 1;     // Disable 8:1 mux
TCCR0 = 1;         // turn on the clock
TIMSK = 1;      // overflow interrupt

testnum = 0;
count = 32;     // timer init for ms accuracy
temp1 = 0;      // temps to set test parameters
temp2 = 0;
temp3 = 0;
temp4 = 0;
ready = 0;      // is the test set up?
s = 0;          // seconds
ms = 0;         // milliseconds 
ADCSRA = 0b10000111; // adc setup
// turn on interrupts
#asm               
           sei
#endasm 
end

 


Cornell University - ECE 476 Final Project
Bryan Doyle & Michael Austin - Spring 2005