/***************************************************************************
 ************************ ENTRYPOINT AND MAIN LOOP *************************
 **************************************************************************/
int main(void)
{
	// Init MCU
	init_application();

#ifdef CONFIG_TEST
	// Branch to welcome screen
	test_mode();
#else
	/* clear whole scren */
	display_clear(0, 0);
#endif

	/* Init modules */
	mod_init();

	/* main loop */
	while (1) {
		/* Go to LPM3, wait for interrupts */
		_BIS_SR(LPM3_bits + GIE);
		__no_operation();

		/* service watchdog on wakeup */
		#ifdef USE_WATCHDOG
			// Service watchdog (reset counter)
			WDTCTL = (WDTCTL & 0xff) | WDTPW | WDTCNTCL;
		#endif

		/* check if any driver has events pending */
		check_events();

		/* check for button presses, drive the menu */
		check_buttons();
	}
}
示例#2
0
int main (void)
{
	ioinit(); //Boot up defaults
	
	while(1)
	{
		check_buttons(); //See if we need to set the time or snooze
		check_alarm(); //See if the current time is equal to the alarm time
	}
	
    return(0);
}
示例#3
0
int main(void) {
    Init();
    syd_message("Line Counter");


    FrontLED(ON);

    while (1) {
        check_buttons();
        calibrate();
        count();
    }
    
    return 0;
}
示例#4
0
void		events_menu(int *play, int *end, int *window)
{
  SDL_Event	ev;
  int		pos[2];

  ev.type = 0;
  pos[0] = 1550;
  pos[1] = 1850;
  SDL_PollEvent(&ev);
  if ((ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_ESCAPE) || ev.type == SDL_QUIT)
    *end = 1;
  if (ev.type == SDL_MOUSEMOTION || (ev.type == SDL_MOUSEBUTTONDOWN && ev.button.button == SDL_BUTTON_LEFT))
    {
      if (ev.button.x > pos[0] && ev.button.x < pos[1])
	check_buttons(&ev, play, end, window);
      else
	*window = 0;
    }
}
示例#5
0
int main(void)
{
	char out_buf[20+1];
	measured_val[0]=0;
	measured_val[1]=0;
	init_dac();
	lcd_init(LCD_DISP_ON);
	init_kbd();
	set_val[0]=15;set_val[1]=50; // 150mA and 5V
	if (eeprom_read_byte((uint8_t *)0x0) == 19){
		// ok magic number matches accept values
		set_val[1]=eeprom_read_word((uint16_t *)0x04);
		set_val[0]=eeprom_read_word((uint16_t *)0x02);
	}
	// I2C also called TWI
	i2c_init(3,1,0);
	sei();
	i2c_send_data("on");
	init_analog();
	while (1) {

		// current
		measured_val[0]=adc_i_to_disp(getanalogresult(0));
		set_val_adcUnits[0]=disp_i_to_adc(set_val[0]);
		set_target_adc_val(0,set_val_adcUnits[0]);
		// voltage
		measured_val[1]=adc_u_to_disp(getanalogresult(1),measured_val[0]);
		set_val_adcUnits[1]=disp_u_to_adc(set_val[1])+disp_i_to_u_adc_offset(measured_val[0]);
		set_target_adc_val(1,set_val_adcUnits[1]);

		// voltage
		lcd_clrscr();
		int_to_ascii(measured_val[1],out_buf,1,1);
		lcd_puts(out_buf);
		lcd_puts("V ");
		int_to_ascii(set_val[1],out_buf,1,1);
		lcd_putc('[');
		lcd_puts(out_buf);
		lcd_putc(']');
		if (!is_current_limit()){
			// put a marker to show which value is currenlty limiting
			lcd_puts("<-");
		}

		// current
		lcd_gotoxy(0,1);
		int_to_ascii(measured_val[0],out_buf,2,0);
		lcd_puts(out_buf);
		lcd_puts("A ");
		int_to_ascii(set_val[0],out_buf,2,0);
		lcd_putc('[');
		lcd_puts(out_buf);
		lcd_putc(']');
		if (is_current_limit()){
			// put a marker to show which value is currenlty limiting
			lcd_puts("<-");
		}
		//dbg
		//int_to_ascii(is_dacval(),out_buf,0,0);
		//lcd_puts(out_buf);
		check_i2c_interface();

		// the buttons must be responsive but they must not 
		// scroll too fast if pressed permanently
		if (check_buttons()==0){
			// no buttons pressed
			delay_ms(100);
			bpress=0;
			check_i2c_interface();
			check_buttons();
			delay_ms(150);
		}else{
			// button press
			if (bpress > 11){
				// somebody pressed permanetly the button=>scroll fast
				delay_ms(10);
				check_i2c_interface();
				delay_ms(40);
			}else{
				bpress++;
				delay_ms(100);
				check_i2c_interface();
				delay_ms(150);
			}
		}
	}
	return(0);
}
示例#6
0
/**
 * Application specific background code.
 * 
 * Executed once per main loop cycle.
 */
void app_background (void)
{
    check_buttons();
}
示例#7
0
TypeScreen game_rank_screen()
{
	const int num_of_buttons = 1;
	int i, pos;
	const int num_of_ops = 1;
	ArrowOp vet_arrow_type[num_of_ops];
	JHI_Image but_up;
	JHI_Image but_down;
	JHI_Image but_up_red;
	JHI_Image but_down_red;
	JHI_Image *but_up_pointer[num_of_ops];
	JHI_Image *but_down_pointer[num_of_ops];
	JHI_Text * text_pointer[num_of_ops];
	JHI_Text * text_pointer_name[num_of_ops];
	JHI_Text level;
	JHI_Text level_name;
	JHI_Font f1,f2;
	TypeScreen te = IS_CLOSE;
	JHI_Image back;
	Button back_title[num_of_buttons];
	JHI_MouseSt mouse;
	JHI_Effect ef1;
	JHI_Point2d init_arrow_pos;

	jhi_load_image_with_transparent_color(&but_up, BUTTON_SET_UP, BLUE);
	jhi_load_image_with_transparent_color(&but_down, BUTTON_SET_DOWN, BLUE);
	jhi_load_image_with_transparent_color(&but_up_red, BUTTON_SET_RED_UP, BLUE);
	jhi_load_image_with_transparent_color(&but_down_red, BUTTON_SET_RED_DOWN, BLUE);
	jhi_load_effect(&ef1, CLICK_BOTTON_SOUND);
	jhi_load_font(&f2, MONSTER_FONT, 40);
	jhi_load_font(&f1, MONSTER_FONT, 30);
	jhi_load_image(&back, BACK_FILE_PATH);
	jhi_set_fps_timer(32);

	jhi_init_text(&level);
	jhi_init_text(&level_name);
	jhi_set_text(&f1, &level, BLACK, "Dificuldade:");
	jhi_set_text(&f1, &level_name, RED, levels[get_rank_game_level()]);
	text_pointer[0] = &level;
	text_pointer_name[0] = &level_name;

	vet_arrow_type[0] = RANK_LEVEL;

	for (i = 0; i < num_of_ops; i++)
	{
		but_up_pointer[i] = &but_up;
		but_down_pointer[i] = &but_down;
	}


	jhi_init_mouse(&mouse);
	init_button(&back_title[0], &f1, "Retornar", BUTTON_TYPE_1);

	back_title[0].pos.x = jhi_get_central_pos(jhi_get_width_window(), jhi_get_height_window(), jhi_get_image_width(&(back_title[0].img)),
			jhi_get_image_height(&(back_title[0].img))).x - 250;

	init_arrow_pos = jhi_get_point(jhi_get_central_pos(jhi_get_width_window(), jhi_get_height_window(),
			jhi_get_image_width(&but_up), 0).x - 120, 20);

	while(jhi_get_close_window() != JHI_CLOSE)
	{
		jhi_timer_start();
		jhi_update();
		jhi_clean();

		jhi_draw_image(&back, jhi_get_point(0, 0));
		draw_rank(&f2, get_rank_game_level());
		draw_button(&back_title[0], jhi_get_point(back_title[0].pos.x, 500));
		draw_arrows(but_up_pointer, but_down_pointer, num_of_ops,
				 init_arrow_pos, text_pointer, text_pointer_name);


		jhi_init_mouse(&mouse);

		for (i = 0; i < jhi_get_number_of_events(); i++)
		{
			mouse = jhi_get_mouse_status(i);
		}

		if (mouse.mouse_event != MOUSE_NOT_EVENT)
		{

			HANDLE_ARROWS;
			pos = check_buttons(mouse, back_title, num_of_buttons);
			if (pos == 0) {te = IS_TITLE; jhi_play_effect(&ef1, 2000); break;}
		}

		jhi_wait_time();
	}

	jhi_free_font(&f1);
	jhi_free_font(&f2);
	jhi_free_image(&back);
	free_button(&back_title[0]);
	jhi_free_effect(&ef1);
	jhi_free_image(&but_up);
	jhi_free_image (&but_down);
	jhi_free_image(&but_up_red);
	jhi_free_image(&but_down_red);
	jhi_free_text(&level);
	jhi_free_text(&level_name);

	return te;
}
示例#8
0
int main(void) {
	m_clockdivide(0);
	m_bus_init();
	setup_timer_3();
	m_rf_open(chan,TX_add,p_length);
	setup_pins();
	if (debug_ADC){ setupUSB();}

	long VLeftSum=0;
	long HLeftSum=0;
	long VRightSum=0;
	long HRightSum=0;
//	int  VLeftSum=0;
//	int  HLeftSum=0;
//	int  VRightSum=0;
//	int  HRightSum=0;


	int cnt_RB = 0;
	int cnt_LB = 0;


	/////////////////////////////////////////////////////////////////////////////////
	//
	// give me a half second to go from switching on the m2 to holding the config buttons.
	//
	//////////////////////////////////////////////////////////////////////////////////

	m_green(1);
	int timer_3_cnt=0;
	int Calibration = 50;
	for (timer_3_cnt=0 ; timer_3_cnt< Calibration ; ){
		if(check(TIFR3,OCF3A)){		 ///// timer 3 runs at 100Hz
			set(TIFR3,OCF3A );
			timer_3_cnt++;
		}
	}

	m_red(ON);

	for (timer_3_cnt=0 ; timer_3_cnt<Calibration ; ){
		if(check(TIFR3,OCF3A)){		 ///// timer 3 runs at 100Hz
			set(TIFR3,OCF3A );
			timer_3_cnt++;


		update_ADC(0,0,0,0);

		check_buttons();
		if (send_buffer[1]==1){ cnt_RB++ ;}
		if (send_buffer[0]==1){ cnt_LB++ ;}

		VLeftSum  += *(int*)(&send_buffer[2]);
		HLeftSum  += *(int*)(&send_buffer[6]);
		VRightSum += *(int*)(&send_buffer[4]);
		HRightSum += *(int*)(&send_buffer[8]);

//		if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}
		if (debug_ADC){debug_ADC_sums( timer_3_cnt,  VRightSum, VLeftSum,  HLeftSum,  HRightSum );}
		}
	}

	// if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}

	int VLeftOffset = (int)(VLeftSum/Calibration);
	int HLeftOffset = (int)(HLeftSum/Calibration);
	int VRightOffset= (int)(VRightSum/Calibration);
	int HRightOffset= (int)(HRightSum/Calibration);

	int VLeftOffset = 512;//(int)(VLeftSum/Calibration);
	int HLeftOffset = 512;//(int)(HLeftSum/Calibration);
	int VRightOffset= 512;//(int)(VRightSum/Calibration);
	int HRightOffset= 512;//(int)(HRightSum/Calibration);

	bool L_bump  = ( cnt_LB > Calibration/2 );
	bool R_bump = ( cnt_RB > Calibration/2 );


	if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}
	if (debug_ADC){debug_ADC_vals( Calibration,   VRightSum, VLeftSum,  HLeftSum,  HRightSum );}
	m_green(0);
	m_red(OFF);

	set_drive_mode( L_bump , R_bump );

	while (1){
		check_buttons();
		update_ADC( VLeftOffset,HLeftOffset,VRightOffset,HRightOffset );
		TX_comm();
		if (debug_ADC){deal_with_new();}
	}
}
示例#9
0
TypeScreen game_input_name_player_screen()
{
	JHI_Image back;
	Button input_name;
	Button char_input;
	Button char_input2;
	Button play[1];
	JHI_Effect ef1;
	const int num_of_buttons = 1;
	int cur_player = 0;
	JHI_MouseSt mouse;
	JHI_KeyboardSt key;
	TypeScreen te = IS_CLOSE;
	int i, pos;
	char flag = 'z' + 2;
	char cprint = 'z' + 2;
	char c = 'a';
	char ch[2];
	char name[20];
	char pname[10];
	JHI_Font f1, f2;
	Player *player_pointer;

	jhi_load_font(&f1, REGULAR_FONT,30);
	jhi_load_font(&f2, MONSTER_FONT, 30);
	jhi_load_image(&back, BACK_FILE_PATH);
	jhi_load_effect(&ef1, CLICK_BOTTON_SOUND);

	jhi_set_fps_timer(32);

	memset(name, 0, sizeof(name));
	memset(pname, 0, sizeof(pname));
	strncpy(name, "Seu Nome ", sizeof(name));

	init_button(&char_input, &f1, "a", BUTTON_TYPE_1);
	init_button(&char_input2, &f1, "a", BUTTON_TYPE_2);
	init_button(&input_name, &f1, name, BUTTON_TYPE_1);
	init_button(&play[0], &f1, "Jogar", BUTTON_TYPE_1);

	play[0].pos.x = jhi_get_central_pos(jhi_get_width_window(), jhi_get_height_window(),
				jhi_get_image_width(&(play[0].img)), jhi_get_image_height(&(play[0].img))).x;

	while(jhi_get_close_window() != JHI_CLOSE)
	{
		jhi_timer_start();
		jhi_update();
		jhi_clean();

		char_input.pos.x = 100;
		char_input.pos.y = 200;
		jhi_draw_image(&back, jhi_get_point(0, 0));
		draw_button(&play[0], jhi_get_point(play[0].pos.x, 500));
		mouse.mouse_event = MOUSE_NOT_EVENT;
		key.key_event = KEYBOARD_NOT_EVENT;

		for (i = 0; i < jhi_get_number_of_events(); i++)
		{
			mouse = jhi_get_mouse_status(i);
			key = jhi_get_keyboard_status(i);

		}

		if (mouse.mouse_event != MOUSE_NOT_EVENT)
		{
			cprint = check_input(&char_input, mouse, &flag, f1.length);

			pos = check_buttons(mouse, play, num_of_buttons);

			if (pos == 0 && strlen(pname) > 0) {
				player_pointer = get_player_by_index(cur_player);
				init_player(player_pointer, &f2, pname, 0);
				jhi_play_effect(&ef1, 2000);

				if (get_number_of_players() == -1)
				{
					player_pointer = get_player_by_index(1);
					init_player(player_pointer, &f2, "CPU", 0);
					te = IS_PLAY3;
					break;
				}

				if (cur_player + 1 == get_number_of_players() && get_number_of_players() == 1)
				{
					te = IS_PLAY;
					break;
				}
				if (cur_player + 1 == get_number_of_players() && get_number_of_players() == 2)
				{
					te = IS_PLAY2;
					break;
				}
				if (cur_player + 1 == get_number_of_players()-1 && get_number_of_players() == 3)
				{
					te = IS_PLAY4;
					player_pointer = get_player_by_index(2);
					init_player(player_pointer, &f2, "CPU", 0);
					break;
				}
				memset(name, 0, sizeof(name));
				memset(pname, 0, sizeof(pname));
				strncpy(name, "Seu Nome ", sizeof(name));
				cur_player++;
				free_button(&input_name);
				init_button(&input_name, &f1, name, BUTTON_TYPE_1);
			}

		}

		change_button_text(&input_name, name);

		set_button_size(&input_name, strlen(name)*jhi_get_image_width(&(char_input.img)),
					jhi_get_image_height(&(char_input.img)));

		draw_button(&input_name, jhi_get_point(jhi_get_central_pos(jhi_get_width_window(), jhi_get_height_window(),
				jhi_get_image_width(&(input_name.img)), jhi_get_image_height(&(input_name.img))).x, 100));

		while(c != 'z' + 1)
		{

			memset(ch, 0, sizeof(ch));
			memcpy(ch, &c, sizeof(c));

			if (c == flag)
			{
				change_button_text(&char_input2, ch);
				draw_button(&char_input2, char_input.pos);
			}
			else
			{
				change_button_text(&char_input, ch);
				draw_button(&char_input, char_input.pos);
			}

			if (c == cprint)
			{

				if (strlen(name) <= 19)
				{
					strncat(name, &c, sizeof(char));
					strncat(pname, &c, sizeof(char));
				}
				cprint = 'z' + 2;
			}

			char_input.pos.x += 3 * f1.length;


			if (c == 'h' || c == 'p' || c =='x')
			{
				char_input.pos.x = 100;
				char_input.pos.y += 3 * f1.length;
			}

			c++;

		}

		c = 'a';

		jhi_wait_time();
	}

	jhi_stop_music();
	jhi_free_image(&back);
	free_button(&input_name);
	free_button(&char_input);
	free_button(&char_input2);
	free_button(&play[0]);
	jhi_free_effect(&ef1);
	free_title_music();
	jhi_free_font(&f1);
	jhi_free_font(&f2);

	if (te == IS_CLOSE)
	{
		player_pointer = get_player_by_index(0);
		free_player(player_pointer);
	}

	return te;

}
示例#10
0
文件: main.c 项目: mirdej/tvtools
// ==============================================================================
// - main
// ------------------------------------------------------------------------------
int main(void)
{

	// ------------------------- Initialize Hardware
	//PORTB: LEDs
	
	DDRB = 0x0f;
	PORTB = 0x00;

	// PORTC: Buttons
	DDRC 	= 0x00;		// set all pins to input
	PORTC 	= 0x0f;		// pullups
		
	// PORTD: 4066 control, interrupt, rx/tx, USB
	DDRD = 0xf2;
	
	// timer 0	-> millisecond clock
	ticks 	= 0;
	TCCR0A 	= (1 << WGM01);									// CTC
	TCCR0B 	= (0 << CS02) | (1 << CS01) | (0 << CS00);		// 8 prescaler @ 20Mhz -> 2'500'000 Hz
	TIMSK0 	= (1 << OCIE0A);								// enable interrupt
	OCR0A	= 249;											// 10000 Hz -> 10 ticks / Millisecond

	// init UART
	UCSR0B = ( 1 << UCSZ02); 						
	UCSR0C = /*( 1 << UPM1) | */( 1 << UCSZ01) | ( 1 << UCSZ00 );		// Even Parity, 9 bit, 1 stop bit
	UBRR0H = 0;
	UBRR0L = 249; 												// 5000 baud (F_CPU/16/BAUD - 1)

	// enable Pinchange Interrupt on PC5 -> PCINT13
	PCICR = (1 << PCIE1);
    PCMSK1 = (1 << PCINT13);
	

	usbDeviceConnect();
	usbReset();
    usbInit();

	wdt_enable(WDTO_1S);	// enable watchdog timer

	sei();
	
	tx_state = idle_tx;
	
	UCSR0B |= (1 << TXEN0); // turn on uart
	
	select_cam(0);
	
	unsigned long tt;
	unsigned char idx;
	
	// ------------------------- Main Loop
	while(1) {
        wdt_reset();
		usbPoll();			// see if there's something going on on the usb bus
        check_uart();	
        
        if (millis()-lastTime >= 80) {
        	 check_buttons();
        }
	}
	return 0;
}
示例#11
0
void main(void)
{

	int i=0;					// Loop Variable
	timeout_ctr	= 0	;

//############################ Initialisation ###############################//

						// _SOURCE_
	DeviceInit();		// DeviceInit.c
	InitGpio();			// GpioInit.c
	InitPieVectTable();	// PieVect.c
	InitPieCtrl();		// PieCtrl.c

	InitSci();			// Serial.c
	init_lcd_menu();	// LcdMenu.c
	lcd_init();			// LcdDriver.c

	SineRefInit();		// SineRefInit.c
	AdcInit();			// AdcInit.c
	PwmInit();			// PwmInit.c

	State.all				= 0;
	State.bit.Start			= 1;
	PeripheralEn.bit.Buttons= 1;	// Allow button checks

	EnableInterrupts();				// PieCtrl.c
	DisableInverter();				// Ensure inverter is disabled on start-up

	lcd_position(0x5,0x0);
	lcd_puts("Kingfisher");				// Print introductory message on top line
	lcd_position(0x0,0x1);				// Start Cursor From First Line
	lcd_puts("  Hydro Controller    ");	// Print introductory message on bottom line

	asm(" CLRC INTM, DBGM");			// Enable global interrupts and real-time debug

	CpuTimer0Regs.PRD.all =  COUNTER0_PRD;	// Initialise CPU timer0
	CpuTimer1Regs.PRD.all =  COUNTER1_PRD;	// Initialise CPU timer0



//############################# Polling Loop ################################//

	while(1)
	{

	//#######################################################################//
	//									STATES 								 //
	//#######################################################################//
	//
	//############################ START STATE ##############################//
		if(State.bit.Start)
		{
			asm("NOP");
		}

	//######################### EXCITATION STATE ############################//
		if (State.bit.Excitation)
		{
			data_out.bit.led_ctrl_green	= 1;
			data_out.bit.led_ctrl_red	= 0;

			if ( (AdcSignal.V_HVDC < 50) && (AdcSignal.Shaft_Velocity > 2000) && !(State.bit.Fault) )
			{
//				PeripheralEn.bit.Buttons= 0;

				timeout_ctr++;
				if (timeout_ctr > 3)
				{
					State.all		= 0;
					State.bit.Fault	= 1;
					timeout_ctr		= 0;
				}
				else
				{
					data_out.bit.startup = 1;
					shift_data_out();
					if (CpuTimer0Regs.TCR.bit.TIF == 1)
					{
						for (i=1;i<=2;i++)						// wait 2 seconds
						{
							CpuTimer0Regs.TCR.bit.TIF	= 1;	// Clear flag
							while(!CpuTimer0Regs.TCR.bit.TIF);
						}
					}
					data_out.bit.startup = 0;
					shift_data_out();
					if (CpuTimer0Regs.TCR.bit.TIF == 1)
					{
						for(i=1;i<=3;i++)						// wait 3 seconds
						{
							CpuTimer0Regs.TCR.bit.TIF	= 1;	// Clear flag
							while(!CpuTimer0Regs.TCR.bit.TIF);
						}
					}
				}
			}
			else if ( (AdcSignal.V_HVDC >= 50) && (AdcSignal.V_HVDC < 250) )
			{
				timeout_ctr			= 0;
				State.all			= 0;
				State.bit.Excited	= 1;
				// Perhaps change LCD Screen to message saying "Slowly increase turbine speed to increase voltage."
			}
			else if ( (AdcSignal.V_HVDC > 250) )
			{
				timeout_ctr					= 0;		// Resets the timeout counter for excitation
				State.all					= 0;
				State.bit.StartupSequence	= 1;
			}
		}

	//############################ EXCITED STATE ############################//
		if (State.bit.Excited)
		{
			if (AdcSignal.V_HVDC > 250)
			{
				timeout_ctr					= 0;		// Resets the timeout counter for excitation
				State.all					= 0;
				State.bit.StartupSequence	= 1;
			}
			else
			{
				// !!!!! ADD SOME CODE FOR THIS CONDITION !!!!!
			}
		}

	//############################# FAULT STATE #############################//
		if (State.bit.Fault)
		{
			if (PeripheralEn.bit.Inverter)
			{
				DisableInverter();
				data_out.bit.led_dump1		= 0;
				data_out.bit.led_dump2		= 0;
				data_out.bit.led_inverter	= 0;
				data_out.bit.led_update		= 0;
			}
			data_out.bit.protect_trig	= 1;

			data_out.bit.led_ctrl_green	= 0;
			data_out.bit.led_ctrl_red	= 1;
			if ( !(LED_Fault_Ctr++ % 16384) )
			{
				data_out.bit.led_dump1		= ~(data_out.bit.led_dump1);
				data_out.bit.led_dump2		= ~(data_out.bit.led_dump2);
				data_out.bit.led_inverter	= ~(data_out.bit.led_inverter);
				data_out.bit.led_update		= ~(data_out.bit.led_update);
			}
		}

	//########################### DC STABLE STATE ###########################//
		if (State.bit.DcStable)
		{
			if ( !(PeripheralEn.bit.Inverter) )	EnableInverter();
			data_out.bit.led_ctrl_green	= 1;
			data_out.bit.led_ctrl_red	= 0;
		}

	//############################## TEST STATE #############################//
		if (State.bit.Test)
		{

		}

	//########################### TURNED OFF STATE ##########################//
		if (State.bit.TurnOff)
		{
			data_out.bit.led_ctrl_green	= 0;
			data_out.bit.led_ctrl_red	= 0;
			data_out.bit.led_dump1		= 0;
			data_out.bit.led_dump2		= 0;
			data_out.bit.led_inverter	= 0;
			data_out.bit.led_update		= 0;
		}

	//#######################################################################//
	//								FLAGS 									 //
	//#######################################################################//
	//
	//######################## SCREEN UPDATE FLAG #######################//
		if (flag_bc)
		{
			menu_update_lcd();
			flag_bc = 0;
		}
		asm(" nop");

	//########################### READ IN DATA FLAG #########################//
		if (flag.bit.DataIN)
		{
			shift_data_in();
			check_buttons();
			flag.bit.DataIN = 0;
		}

	//########################## SEND OUT DATA FLAG #########################//
		if (flag.bit.DataOUT)
		{
			shift_data_out();
			flag.bit.DataOUT = 0;
		}

	//######################## HVDC VOLTAGE REGULATION ######################//
		if (flag.bit.V_HVDC)
		{
			if ( !(State.bit.TurnOff) && !(State.bit.Fault) )
			{
				V_DcError	= V_DC_REF - AdcSignal.V_HVDC;			// Calculate the DC error from the reference
				DutyError	= 0.75*K_DC*V_DcError + 0.25*DutyError;	// Modify the dump load duty cycle with respect to the DC error

				if (V_DcError > 0)		// If the DC voltage is less than the reference, dump no power
				{
					EPwm3Regs.CMPA.half.CMPA= 0;
					EPwm3Regs.CMPB			= 0;
				}
				else if (DutyError > DUMP1_DUTY_MAX)
				{
					DutyFeedback			= 1.0*(DUMP1_DUTY_MAX)*DUMP_PRD;
					EPwm3Regs.CMPA.half.CMPA= DutyFeedback;
					DutyFeedback			= 1.0*(DutyError - DUMP1_DUTY_MAX)*DUMP_PRD;
					EPwm3Regs.CMPB			= DutyFeedback;
				}
				else
				{
					DutyFeedback			= 1.0*(DutyError)*DUMP_PRD;
					EPwm3Regs.CMPA.half.CMPA= DutyFeedback;
					EPwm3Regs.CMPB			= 0;
				}
			}else		// If there is a fault or the system is off, dump all power
			{
				EPwm3Regs.CMPA.half.CMPA	= (DUMP1_DUTY_MAX)*DUMP_PRD;	// Dump all power!
				EPwm3Regs.CMPB				= DUMP_PRD;	// Dump all power!
			}

			flag.bit.V_HVDC	= 0;		// Reset the flag
		}


	//###################### SHAFT VELOCITY MEASUREMENT #####################//
		if (ShaftVelocCtr >= 1000)										// Updates the measured velocity every 1000*(1/EPWM8) = 0.5s
		{
			AdcSignal.Shaft_Velocity	= shaftspeedtest; //0.8*(ShaftPulseCtr/0.5)*60 + 0.2*AdcSignal.Shaft_Velocity;	// Where 1 pulse = 1 rotation
			ShaftPulseCtr				= 0;	// { Reset counters
			ShaftVelocCtr				= 0;	// {
		}

	//################### OUTPUT AC VOLTAGE FREQUENCY MEASURE ################//
		if (V_AcOutPrdCtr >= 10)
		{
			AdcSignal.V_AcOut_Freq	= (SYSCLK/ADC_SAMP_PRD+1)*(V_AcOutPrdCtr/FreqOutDivCtr);	// Approximate frequency over at least 10 periods
			AcOutFreqError			= AC_OUT_FREQ - AdcSignal.V_AcOut_Freq;	// Find frequency error
			V_AcOutReqFreq			= V_AcOutReqFreq + 0.05*AcOutFreqError;	// Frequency feedback loop
			sinGen.freq				= V_AcOutReqFreq*(BIT31)*(2*BIT32*INVERTER_PWM_PRD)*(1/(sinGen.step_max*SYSCLK));	// Alter reference sine wave frequency
			V_AcOutPrdCtr			= 0	;	// }
			FreqOutDivCtr			= 0	;	// } Reset counters
		}

	//####################### INVERTER CONTROL #######################//
		if (flag.bit.Inverter)
		{
/* BROKEN	InverterMaxDuty	= V_AC_OUT_REF*(1.0/(AdcSignal.V_HVDC));	// Find the maximum duty cycle necessary to create Vacoutpeak=325V
			DutyScaleMax	= (BIT16*InverterMaxDuty-BIT15)*30.51850948e-6;	// 30.51850948e-6 = 1/(BIT15-1)

			if ( !(State.bit.InvSoftStart) )
			{
				DutyScaleError	= DutyScaleMax - DutyScale;
				DutyScale	= DutyScale + 0.01*DutyScaleError;
				if (DutyScale < 0.2)
				{
				DutyScale	= 0.2;
				ADD SOME CODE IN CASE IT IS DANGEROUS TO GO BELOW 0.2
				}
			}
*/

			// ----- Toggle the onboard LED to show inverter control is active
			if (GPIO34_count >= 4000)						// toggle_time = GPIO34_count_limit/fpwm
			{
				GpioDataRegs.GPBTOGGLE.bit.GPIO34	= 1;	// Toggle the pin
				GPIO34_count	= 0;						// Reset the count
			}

			flag.bit.Inverter	= 0;
		}

	//################### INPUT AC VOLTAGE FREQUENCY MEASURE ################//
		if (V_AcInPrdCtr >= 10)
		{
			AdcSignal.V_AcIn_Freq	= 0.8*((SYSCLK/ADC_SAMP_PRD+1)*(V_AcInPrdCtr/FreqInDivCtr)) + 0.2*(AdcSignal.V_AcIn_Freq);	// Approximate frequency over at least 10 periods
			genSlip					= 100*((AdcSignal.V_AcIn_Freq*60) - (AdcSignal.Shaft_Velocity))/(AdcSignal.V_AcIn_Freq*60);	// Calculates slip: s= 100%*((ns-nr)/ns)
			V_AcInPrdCtr			= 0	;	// }
			FreqInDivCtr			= 0	;	// } Reset counters
		}


/*		if(SendADCResult)
		{
			SendADCResult = 0;

			SerialSendStr("Voltage: ");
			SerialSendInt((int)V_DC_measured);
			SerialSendStr("   PWM1: ");
			SerialSendInt((int)(EPwm3Regs.CMPA.half.CMPA*100.0/(DUMP_PRD*1.0)));
			SerialSendCR();
			SerialSendStr("   PWM2: ");
			SerialSendInt((int)((EPwm3Regs.CMPB*100.0)/(DUMP_PRD*1.0)));
			SerialSendCR();
		}*/

	}

}