コード例 #1
0
ファイル: powerSystem.c プロジェクト: tyharbert/SeniorDesign
// main function; program starts here
void main(void) 
{
    unsigned char data = 0;
    unsigned int battery_voltage = 0; // ADC reading of battery voltage
    
    ports_initialize();    // set up ports 
    ADC_initialize();      // configure ADC
    SPI_initialize();      // set up SPI module
    sleep_initialize();    // set up sleep mode
    ISR_initialize();      // set up ISR
    
    while(1)
    {
        SLEEP(); // sleep PIC until WDT expires
        
        time++; // increment time (+16s)
        
        battery_voltage = ADC_read(0x09); // read battery voltage
        
        if ( time >= 1 ) // 1*16s=16s
        {        
            time = 0; // reset time
            if(battery_voltage >= 690) //690 = 11v
            {
                LATCbits.LATC4 = 1; // turn relay on
                
                SPI_enable(); // turn on SPI module
            
                while( data != 0xDA ) // wait for shutdown command to be received
                {
                    while(!SSP1STATbits.BF) // wait till data is in the buffer
                    {
                        CLRWDT();
                    }
                    data = SSP1BUF; // read data outside
                }
            
                data = 0x00;
            
                SPI_disable(); // turn off SPI module
                
                __delay_ms(10000); // wait for R Pi to shutdown
            
                LATCbits.LATC4 = 0; // turn relay off
            }  
        }
    }    
}
コード例 #2
0
ファイル: main.c プロジェクト: tgross35/ENGR100_PROJECT
int main(void) {
//	int i, j;
	int sine_table[NTABLE], cosine_table[NTABLE];
	int mseq_table[CYCLES * NTABLE * MSEQ_LENGTH];
	float size;
	float s;
//	float aI, aQ;
//	float uI[M];                // Store last M samples of yI
//	float uQ[M];                // Store last M samples of yQ
//	float vI, vQ;
//	float xI, xQ;
//	float w = 0;
//	float wq = 0;
	int spb = MSEQ_LENGTH * CYCLES * NTABLE;
	int last_samples[spb * MSG_LEN];
	int i = 0;

	USART_initialize(3000000, 1); 	// set baud rate to 3,000,000

	DAC_initialize(FS); 			// set sample rate
	DAC_ready_flag = 0; 		// clear the DAC ready flag
	DAC_start();				// start the DAC running
	ADC_initialize(FS);
	ADC_start();
	ADC_ready_flag = 0; 		// clear the ADC ready flag
	int mseq[MSEQ_LENGTH] = { 1, 0, 1, 0, 1, 0, 1 };
	int msg_table[MSG_LEN * CYCLES * NTABLE];
	int recieved[MSG_LEN];
	//char message[MSG_LEN] = "testing";
	int msg[MSG_LEN] = { 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0 };
	size = (float) (NTABLE / TABCYCLES);
	for (i = 0; i < NTABLE; i++) {
		sine_table[i] = (int) floor(32767 * sin(2.0 * PI * i / (float) size));
		cosine_table[i] = (int) floor(32767 * cos(2.0 * PI * i / (float) size));
	}
	for (i = 0; i < spb; i++) {
		mseq_table[i] =
				(mseq[i / (NTABLE * CYCLES)]) ?
						-sine_table[i % NTABLE] : cosine_table[i % NTABLE];
	}
	for (i = 0; i < CYCLES * NTABLE * MSG_LEN; i++) {
		msg_table[i] =
				(msg[i / (CYCLES * NTABLE)]) ?
						-sine_table[i % NTABLE] : sine_table[i % NTABLE];
	}
	for (i = 0; i < MSG_LEN; i++) {
		recieved[i] = 0;
	}

	i = 0;
	int m = 0;
	int strength = 0;
	int maxstrength = 0;
	for (i = 0; i < spb; i++) {
		last_samples[i] = 0;
	}
	int cycles = 0;
	int seconds = 0;
	int state = 0; // 0: listening for mseq
				   // 4: initializing collection of packet data
				   // 5: collecting packet data
				   // 6: decoding & printing data
	while (1) {

		if (ADC_ready_flag != 0 && DAC_ready_flag != 0) {
			ADC_ready_flag = 0;
			DAC_ready_flag = 0;

			if (state == 0) {
				DAC1_value = mseq_table[m];
				s = ADC1_value + 15000;                   // PIN 35A - Grab ADC value
				last_samples[m] = s;

				strength = 0;
				for (i = 0; i < spb; i++) { // Slow?
					strength +=
							(last_samples[(m + 1 + i) % spb] * mseq_table[i])
									/ 32767;
				}
				strength /= 100;
				m++;
				m = m % spb;
				cycles++;
				cycles = cycles % FS;
				if (strength > 7500)
					printf("Convolution: %i \n", strength);
				if (strength > 7500) {
					printf("CAUGHT MSEQ\n");
					state = 4;
					m = 0;
				}
				if (strength > maxstrength) {
					maxstrength = strength;
					printf("Max Strength: ");
					printf("%i", maxstrength);
					printf("\n");
				}
				if (cycles == 0) {
					seconds++;
					printf("[O3] Seconds elapsed: ");
					printf("%i", seconds);
					printf("\n");
				}

			} else if (state == 4) {
				if (m == -1) {
					m = 0;
					state = 5;
					last_samples[m] = ADC1_value + 15000;
					m = 1;
					DAC1_value = msg_table[m];
				} else {
					m = -1;
				};
				DAC1_value = msg_table[0];
			} else if (state == 5) {
				s = ADC1_value + 15000;
				last_samples[m] = s;
				int zero_s = 0;
				int one_s = 0;
				if (((m + 1) % (CYCLES * NTABLE)) == 0) {
					zero_s = 0;
					one_s = 0;
					for (i = (((m + 1) - (CYCLES * NTABLE))); i <= m; i++) {
						one_s += ((last_samples[i]) / 100)
								* ((-sine_table[i % NTABLE]) / 100);
						zero_s += ((last_samples[i]) / 100)
								* ((sine_table[i % NTABLE]) / 100);
					}
					printf("zero strength: %i\n", zero_s);
					printf("one strength: %i\n", one_s);
					if (zero_s > one_s) {
						printf("got 0\n");
						recieved[((m + 1) / (CYCLES * NTABLE)) - 1] += 0;
					} else {
						printf("got 1\n");
						recieved[((m + 1) / (CYCLES * NTABLE)) - 1] += 1;
					}
				}
				if (m == CYCLES * NTABLE * MSG_LEN)
					state = 6;
				m++;
				DAC1_value = msg_table[m];
			} else if (state == 6) {
				if (m < 20000)
					m++;
				else {
					state = 4;
					for (i = 0; i < MSG_LEN; i++) {
						printf("%i ", recieved[i]);
					}
					printf("\n");
				}
			}
		}
	}
}