void init_DAC(void){
  spi_set_endianess(drv_dac_1,true);
  spi_cs_hi(drv_dac_1);
// Output range = +10V
  write_DAC(0x0C,0x0001);
// Power
  write_DAC(0x10,0x0015);
// Control
  write_DAC(0x19,0x0006);
  write_DAC(0x00,0x8000);
  write_DAC(0x02,0x8000);
}
Beispiel #2
0
/****************************************************************************
 *  FUNCTION: main() - local routine                                        *
 ****************************************************************************/
int main(int argc, char *argv[])
    {
    int key_entered, Ch;

    intro();

    /* Set all 16 buffers to 0 */
    for (Ch = 0; Ch < 4; Ch++)
	   OUTPORT(base + (Ch * 2), Cal(0x000, Ch));

    inb(base+0xA);    // set automatic update mode
    inb(base+0XF);    // release zero latch

    do
	{
	CLRSCR();
	write_DAC();
	CPRINTF("\n\nWould you like to output another value (Y or N)?\n");
	key_entered = GETCH();
	} while((key_entered != 'N') && (key_entered != 'n'));

    /* Set all 16 buffers to 0 */
    for (Ch = 0; Ch < 4; Ch++)
	   OUTPORT(base + (Ch * 2), Cal(0x000, Ch));

    CLRSCR();
    PUTS("DA12-16 Sample1 complete.");
    } /* end main */
Beispiel #3
0
int main(void) {
	PORTE.DIRSET |= 0xFF;
	PORTD.DIRSET = 0x00;
	PORTD.PIN1CTRL = 0b00011010;
	PORTC.DIRSET |= 0x01;

	
	
  while(1)
  {
    /* Read the next incoming event. Usually this is a blocking function. */
   // EVENTS event = readEventFromMessageQueue();
	
	if((flag.HARDWARE_ERROR_FLAG == 1 || flag.OVERTEMP_FLAG == 1) && flag.DISCHARGED_FLAG == 0)
	{
		state = DISCHARGE;
	}   
	
	/* Finite State Machine */
	switch(state)
    {
		
	case INIT:
		init_clock();
		init();
		interrupt_init();
		UART_init();
		AC_init();
		init_DAC();
		init_GPIO_interrupt();
//		init_counter();
		
		// Start by using capbank 1
		flag.CAPBANK_FLAG = 0;
		PORTE.OUT = 0x00;
		//_delay_ms(4000);
		
		// comparator reference voltages
		
		// Desired voltage reference level expressed in Capacitor voltage
		// TODO: high_trigger_cap is based on user_settings.voltage_amplitude which is in percent but should be converted to absolute.

		double low_trigger_cap = 0.3;
		
		// Voltage divided, digital version of capacitor voltage
		uint16_t high_trigger_b;
		uint16_t low_trigger_b;

//		high_trigger_b = voltage_to_DAC(voltage_to_vdac(high_trigger_cap));


		low_trigger_b = voltage_to_DAC(low_trigger_cap);
		
		state = STANDBY;
        break;

	case STANDBY:
		PORTE.OUT = 0x01;

		if (flag.PRECHARGE_FLAG)
		{
			state = PRECHARGE;
// 			PORTE.OUT = 0x04;
// 			_delay_ms(3000);
			break;
		}
		
		else if(flag.WRITE_FLAG)
		{
			state = WRITE;
			//delay_ms(3000);
			break;
		}
		
		else if(flag.READ_FLAG)
		{
			state = READ;
			// PORTE.OUT = 0x05;
			//_delay_ms(3000);
			break;
		}
		
		else if(!(PORTD.IN & (1 << 1)))
		{
			PORTE.OUT = 0x06;
			//_delay_ms(3000);
			if(user_settings.mode == 0)
			{
				state = SINGLE_PULSE;
				break;
			}
			else if(user_settings.mode == 1)
			{
				state = PAIRED_PULSE;
				break;
			}
		}
		
		else
		{
			state = STANDBY;
			break;
		}
		break;
		
	case PRECHARGE:
		// Clear precharge flag
		flag.PRECHARGE_FLAG = 0;
		flag.DISCHARGED_FLAG = 0;
		
		PORTE.OUT = 0x03;
		//_delay_ms(3000);
		// Set AC trigger high
//		write_DAC(high_trigger_b);
		write_DAC(voltage_to_DAC(voltage_to_vdac(user_settings.voltage_amplitude * V_MAX)));	
		
			// Go to pulse state, single or paired depending on user settings
			if (user_settings.mode == 0) // single pulse mode
			{
				// Capbank 1
				if (flag.CAPBANK_FLAG == 0)
				{
					// Close precharge switch Capbank 1
					PRECHARGE1_ON;
					PORTC.OUT |= (1 << 0);
					// check if precharge reference triggered
					if(ACA.STATUS & (1 << 4) )
					{
						// TODO: Open precharge switch
						PRECHARGE1_OFF;
						PORTC.OUT &= ~(1 << 0);
						PORTE.OUT = 0x04;
						//_delay_ms(3000);
						
						// Send precharge ready ACK
						USARTF0.DATA = 0x06;
						
						state = STANDBY;
						break;
					}
				}
				
				// Capbank 2
				else if (flag.CAPBANK_FLAG == 1)
				{
					// Close precharge switch Capbank 2
					PRECHARGE2_ON;
					//PORTC.OUT |= (1 << 0);
					// check if precharge reference triggered
//					if(ACA.STATUS & (1 << 5) )
					if(ACA.STATUS & (1 << 4) )
					{
						// TODO: Open precharge switch
						PRECHARGE2_OFF;
						//PORTC.OUT &= ~(1 << 0);
						PORTE.OUT = 0x04;
						//_delay_ms(3000);
						
						// Send precharge ready ACK
						USARTF0.DATA = 0x06;
						
						state = STANDBY;
					}
				}
			}
			
			else if (user_settings.mode == 1) // paired pulse mode
			{
				PRECHARGE1_ON;
				//PORTC.OUT |= (1 << 0);
				// check if precharge reference capbank 1 triggered
				if (ACA.STATUS & (1 << 4) )
				{
					// Open precharge switch capbank 1
					PRECHARGE1_OFF;
					//PORTC.OUT &= ~(1 << 0);
					PORTE.OUT = 0x04;
					//_delay_ms(1500);
					
					PRECHARGE2_ON;
					//PORTC.OUT |= (1 << 0);
					// check if precharge reference capbank 2 triggered
//					if (ACA.STATUS & (1 << 5) )
					if (ACA.STATUS & (1 << 4) )
					{
						// Open precharge switch capbank 2
						PRECHARGE2_OFF;
						//PORTC.OUT &= ~(1 << 0);
						PORTE.OUT = 0x05;
						//_delay_ms(1500);
						
						// Send precharge ready ACK
						USARTF0.DATA = 0x06;
						state = STANDBY;
					}	
				}
			}
		
		else
		{
			PORTE.OUT = 0x07;
			//_delay_ms(1500);
			// stay in precharge if trigger level not reached
			state = PRECHARGE;
		}

		break;

	case READ:
		//_delay_ms(3000);
		PORTE.OUT = 0x03;
		// read stuff here
		// TODO: read not possible yet
		
		state = STANDBY;
		break;
		
	case WRITE:
		PORTE.OUT = 0x02;
		// write stuff here
		bufferWrite(&UART_buffer_write, USARTF0.DATA);
		// Get  char from RX buffer
		flag.WRITE_FLAG = 0;
		//_delay_ms(1500);
		
		state = STANDBY;
		break;


		
	case SINGLE_PULSE:
		// set reference for AC with DAC
		write_DAC(low_trigger_b);
		
		// single pulse here
		PORTE.OUT = 0x05;
		//_delay_ms(3000);
		

		if (flag.CAPBANK_FLAG == 0)
		{
			// Pulse using capbank 1
			single_pulse(CAPBANK1);
			// TODO: Wait for pulse to damp out
			_delay_ms(1);
		}
		
		else
		{
			// Pulse using capbank 2
			single_pulse(CAPBANK2);
			// TODO: Wait for pulse to damp out
			_delay_ms(1);
		}
		
		
		// Toggle capbank used
		flag.CAPBANK_FLAG ^= (1 << 0);
		break;
		
	case PAIRED_PULSE:
		// paired pulse here
		PORTE.OUT = 0x05;
		//_delay_ms(3000);
		// set reference for AC with DAC
		write_DAC(low_trigger_b);
		state = STANDBY;
		// Pulse using capbank 1
		single_pulse(CAPBANK1);
		
// 		_delay_us(50);
		delay_in_ms(user_settings.interstimulus_interval);
		// Pulse using capbank 2
		single_pulse(CAPBANK2);
		_delay_us(50);
		// TODO: Wait for pulse to damp out
		//_delay_ms(1);
		// When finished, go to precharge state again to wait for new pulse
		state = STANDBY;
		break;
		
	case DISCHARGE:
	PORTE.OUT = 0x08;
	//_delay_ms(3000);
	// set reference for AC with DAC
	write_DAC(low_trigger_b);
	DISCHARGE1_ON;
	DISCHARGE2_ON;
	
	// Discharge complete when both capbank voltages below AC reference
	//		if ( (ACA.STATUS & (1 << 4)) && (ACA.STATUS & (1 << 5)) )
	if ( !(ACA.STATUS & (1 << 4)))

	{
		DISCHARGE1_OFF;
		DISCHARGE2_OFF;
		flag.DISCHARGED_FLAG = 1;
		flag.HARDWARE_ERROR_FLAG = 0;
		flag.OVERTEMP_FLAG = 0;
// 		_delay_ms(5000);
		state = STANDBY;
	}
	
	else
	{
		// Keep discharging
		state = DISCHARGE;
	}
	break;

    }
  } 
}
void update_DAC(void){
  write_DAC(0x00,(2048+htoes(mosi_packet.aout_1))<<4);
  write_DAC(0x02,(2048+htoes(mosi_packet.aout_2))<<4);
}
void clear_DAC(void){
  write_DAC(0x00,0x8000);
  write_DAC(0x02,0x8000);
}