Пример #1
0
/**
 * \brief Updates or turns off the signal to the induction element
 *
 * Converts a wattage value to a signal period. Higher wattage means
 * shorter period, which means more induced power if this was a
 * physical stove top.
 *
 * \param wattage value representing the desired power level.
 */
static void ovenctl_actuate_induction_element(uint8_t wattage)
{
	if (wattage == 0) {
		tc_write_clock_source(&OVEN_FREQ_TC, TC_CLKSEL_OFF_gc);
	} else {
		tc_write_clock_source(&OVEN_FREQ_TC, TC_CLKSEL_DIV256_gc);
	}

	OVEN_FREQ_TC.CCABUF = 512 / wattage;
}
Пример #2
0
/**
 * \brief Initialize Timer/Counters used to simulate oven actuation signal
 *
 * TCC0 is set up to generate a dual variable frequency signal with dead-time
 * insertion using the AWEX module. This is similar to how heating elements are
 * actuated in real induction ovens. Its output is on pin C2 which is marked as
 * RXD on header J1.
 *
 * TCC1 is set up to capture a frequency signal via a pin change event using the
 * XMEGA Event System. Its input is pin C4 which is marked as SS on header J1.
 */
void main_init_tc(void)
{
    /* Set up timer for PWM output, used to actuate cooking element */
    tc_enable(&OVEN_FREQ_TC);

    /* Configures the waveform generator in Frequency generation mode */
    tc_set_wgm(&OVEN_FREQ_TC, TC_WG_FRQ);

    /* Configures the CCA level. This controls frequency generation */
    tc_write_cc(&OVEN_FREQ_TC, TC_CCA, FREQ_TIMER_PERIOD_INIT / 2);

    /* Enables and configures the deadtime of AWEX channel B outputs */
    tc_awex_enable_ccb_deadtime(&AWEXC);

    tc_awex_set_dti_high(&AWEXC, FREQ_TIMER_PERIOD_INIT / 4);
    tc_awex_set_dti_low(&AWEXC, FREQ_TIMER_PERIOD_INIT / 4);

    /* Output of AWEX channel B is on pins C2 and C3 */
    tc_awex_set_output_override(&AWEXC, 0x0C);

    /* Make sure that the output is initially turned off */
    tc_write_clock_source(&OVEN_FREQ_TC, TC_CLKSEL_OFF_gc);

    /* Set up timer for input capture for the simulation to read "real"
     * power
     */
    tc_enable(&OVEN_FREQ_CAPT_TC);
    /* Select Event Channel 1 as input to the timer, and perform frequency
     * capture.
     */
    tc_set_input_capture(&OVEN_FREQ_CAPT_TC, TC_EVSEL_CH1_gc,
                         TC_EVACT_FRQ_gc);
    /* Enable Capture Channel A */
    tc_enable_cc_channels(&OVEN_FREQ_CAPT_TC, TC_CCAEN);

    /* Make sure pin C4 is configured for input and sensing on rise and fall
     * and pin C2 is configured for output.
     */
    ioport_configure_pin(J1_PIN4, IOPORT_DIR_INPUT | IOPORT_BOTHEDGES);
    ioport_configure_pin(J1_PIN2, IOPORT_DIR_OUTPUT);

    /* Turn on power to the event system */
    PR.PRGEN &= ~PR_EVSYS_bm;
    /* Use pin C4 as input to Event Channel 1 */
    EVSYS.CH1MUX = EVSYS_CHMUX_PORTC_PIN4_gc;

    /* Turn on timer used for input capture */
    tc_write_clock_source(&OVEN_FREQ_CAPT_TC, TC_CLKSEL_DIV256_gc);
}
Пример #3
0
/**
 * \brief Turn off power to simulated oven plate, disable tests
 *
 * This function modified the simulation input to zero (zero watts, no
 * power input), then turns off the periodic tests and disables monitoring of
 * them.
 *
 * \param *wattage Pointer to the value representing desired power level
 * \param *power Pointer to the value representing whether the desired power
 * level should be applied to the induction element.
 */
static void ovenctl_turn_off_plate(uint8_t *wattage, bool *power)
{
	*wattage = 0;
	*power = false;

	/* Turn off and stop monitoring temperature test */
	tc_write_clock_source(&OVEN_PERIODIC_TEMPTEST_TC, TC_CLKSEL_OFF_gc);
	tc_reset(&OVEN_PERIODIC_TEMPTEST_TC);
	classb_intmon_set_state(TEMP_SANITY_TEST, M_DISABLE);

	/* Turn off and stop monitoring periodic Class B tests. */
	tc_write_clock_source(&OVEN_PERIODIC_CLASSB_TC, TC_CLKSEL_OFF_gc);
	tc_reset(&OVEN_PERIODIC_CLASSB_TC);
	classb_intmon_set_state(PER_CLASSB_TESTS, M_DISABLE);
}
Пример #4
0
/**
 * \brief Run tests and turn on power to simulated oven plate
 *
 * This functions runs some class B tests, reinitializes Timer/Counters, ADC and
 * DAC used by the oven, then turns on and starts monitoring of periodic tests.
 */
static void ovenctl_turn_on_plate(void)
{
	/* Store current DAC value since the analog IO test is destructive */
	uint16_t dac_val = DACB.CH0DATA;

	/* Run tests -- classb_error is updated if any of them fail */
	oven_classb_run_tests();

	/* Initialize the ADC and DAC modules, as well as the Timer/Counters
	 * we use to emulate real world application.
	 */
	main_init_adc_dac();
	DACB.CH0DATA = dac_val;
	main_init_tc();

	/* Enable and set up timer for periodic temperature checking */
	tc_enable(&OVEN_PERIODIC_TEMPTEST_TC);
	/* Set timer to overflow every 600ms: 24MHz / (1024 * 14063) = 0.6s */
	tc_write_clock_source(&OVEN_PERIODIC_TEMPTEST_TC, TC_CLKSEL_DIV1024_gc);
	tc_write_period(&OVEN_PERIODIC_TEMPTEST_TC, 14062);

	/* Set up temperature check as interrupt callback function, then enable
	 * interrupts
	 */
	tc_set_overflow_interrupt_callback(&OVEN_PERIODIC_TEMPTEST_TC,
			ovenctl_periodic_temperature_sanity_check);
	tc_set_overflow_interrupt_level(&OVEN_PERIODIC_TEMPTEST_TC,
			TC_OVFINTLVL_LO_gc);

	/* Enable monitoring of the periodic temperature check */
	classb_intmon_set_state(TEMP_SANITY_TEST, M_ENABLE);

	/* Enable and set up timer for periodic execution of Class B tests */
	tc_enable(&OVEN_PERIODIC_CLASSB_TC);
	/* Set timer to overflow every second: 24MHz / (1024 * 23438) = 1s */
	tc_write_clock_source(&OVEN_PERIODIC_CLASSB_TC, TC_CLKSEL_DIV1024_gc);
	tc_write_period(&OVEN_PERIODIC_CLASSB_TC, 23437);
	/* Set up periodic class B test as interrupt callback function, then
	 * enable interrupts
	 */
	tc_set_overflow_interrupt_callback(&OVEN_PERIODIC_CLASSB_TC,
			ovenctl_periodic_classb_tests);
	tc_set_overflow_interrupt_level(&OVEN_PERIODIC_CLASSB_TC,
			TC_OVFINTLVL_LO_gc);

	/* Enable monitoring of the periodic temperature check */
	classb_intmon_set_state(PER_CLASSB_TESTS, M_ENABLE);
}
Пример #5
0
/**
 * \b avrInitSystemTickTimer
 *
 * Initialise the system tick timer. Uses the AVR's timer1 facility.
 *
 * @return None
 */
void avrInitSystemTickTimer ( void )
{
  /*
  * Unmask clock for TCC1
  */
  tc_enable(&TCC1);
  
  /*
  * Configure interrupts callback functions for CCA interrupt
  */
  tc_set_cca_interrupt_callback(&TCC1,
      cca_interrupt_callback);
      
  /*
  * Configure TC in normal mode, configure period, CCA
  * Enable CCA channel
  */
  tc_set_wgm(&TCC1, TC_WG_NORMAL);
  tc_write_period(&TCC1, AVR_CPU_HZ / 256 / SYSTEM_TICKS_PER_SEC);
  tc_write_cc(&TCC1, TC_CCA, AVR_CPU_HZ / 256 / SYSTEM_TICKS_PER_SEC / 2);
  tc_enable_cc_channels(&TCC1,(enum tc_cc_channel_mask_enable_t)(TC_CCAEN));
  
  /*
  * Enable TC interrupts (overflow, CCA and CCB)
  */
  tc_set_cca_interrupt_level(&TCC1, TC_CCAINTLVL_LO_gc);
  
  /*
  * Run TCC1 at AVR_CPU_HZ / 256
  */
  tc_write_clock_source(&TCC1, TC_CLKSEL_DIV256_gc);

}
Пример #6
0
/*! \brief  to initialiaze hw timer
 */
uint8_t tmr_init(void)
{
	uint8_t timer_multiplier;

	tc_enable(TIMER);

	tc_set_overflow_interrupt_callback(TIMER,
			(tc_callback_t)tc_ovf_callback);

	/*initialize timer in waveform generator - Normal mode */
	tc_set_wgm(TIMER, TC_WG_NORMAL);

	tc_write_period(TIMER, TIMER_PERIOD);
	/* select clock division as 1 */
	tc_write_clock_source(TIMER, TC_CLKSEL_DIV1_gc);

	tc_set_overflow_interrupt_level(TIMER, TC_INT_LVL_HI);

	tc_set_cca_interrupt_callback(TIMER, (tc_callback_t)tc_cca_callback);

	tc_enable_cc_channels(TIMER, TC_CCAEN);

	tc_set_cca_interrupt_level(TIMER, TC_INT_LVL_OFF);

	/* calculate how faster the timer with current clk freq compared to
	 * timer with 1Mhz */
	timer_multiplier = sysclk_get_peripheral_bus_hz(TIMER) / DEF_1MHZ;

	return timer_multiplier;
}
static void init_timer_isr(void)
{
	tc_enable(&TCC0);
	tc_write_period(&TCC0, TIMER_PERIOD);
	tc_write_clock_source(&TCC0, TC_CLKSEL_DIV8_gc);
	tc_set_cca_interrupt_level(&TCC0, PMIC_LVL_LOW);
	tc_set_cca_interrupt_callback(&TCC0, example_cca_interrupt_callback);
}
void init_timer_isr( void )
{
  tc_enable (&TCC0);
  tc_write_period (&TCC0, (TICKS_PER_MS * qt_measurement_period_msec));
  tc_write_clock_source (&TCC0, TC_CLKSEL_DIV8_gc);
  tc_set_cca_interrupt_level (&TCC0, PMIC_LVL_LOW);
  tc_set_cca_interrupt_callback(&TCC0, example_cca_interrupt_callback);
}
Пример #9
0
/**
 * Initialize trace
 */
void hf_trace_init() {
    tc_enable(&TCC0);
    tc_set_overflow_interrupt_callback(&TCC0, tc_wrap);
    tc_set_wgm(&TCC0, TC_WG_NORMAL);
    tc_write_period(&TCC0, 65535);
    tc_set_overflow_interrupt_level(&TCC0, TC_INT_LVL_LO);
    tc_write_clock_source(&TCC0, TC_CLKSEL_DIV8_gc);
}
void TimerE1_init(void)
{
    tc_write_clock_source(&TCE1,TC_CLKSEL_DIV256_gc);
    tc_set_wgm(&TCE1,TC_WG_SS);
    tc_write_period(&TCE1,0x00FF);
    tc_set_direction(&TCE1,TC_UP);
    tc_enable_cc_channels(&TCE1,TC_CCAEN);
    tc_enable(&TCE1);
};
Пример #11
0
void app_touch_init(void)
{
#ifdef QTOUCH_STUDIO_MASKS
	SNS_array[0][0] = 0x50;
	SNS_array[0][1] = 0x00;
	SNS_array[1][0] = 0x00;
	SNS_array[1][1] = 0x00;

	SNSK_array[0][0] = 0xA0;
	SNSK_array[0][1] = 0x00;
	SNSK_array[1][0] = 0x00;
	SNSK_array[1][1] = 0x00;
#endif

	/* Configures the sensors as keys and assigns the channel numbers.
	 * The sensor is wired up with SNS=PF6 and SNSK=PF7
	 * When using "pin reconfigurability" this will result in channel 0
	 * because it is the first and only channel that is used.
	 * For the standard qtouch library setup we would need to use
	 * channel 3 since we are using the last two pins on the port.
	 */
	qt_enable_key(CHANNEL_0, NO_AKS_GROUP, 10u, HYST_6_25);
	qt_enable_key(CHANNEL_1, NO_AKS_GROUP, 10u, HYST_6_25);

	qt_init_sensing();

	/* This will fill the default threshold values in the configuration
	 * data structure. But User can change the values of these parameters.
	 */
	qt_config_data.qt_di              = DEF_QT_DI;
	qt_config_data.qt_neg_drift_rate  = DEF_QT_NEG_DRIFT_RATE;
	qt_config_data.qt_pos_drift_rate  = DEF_QT_POS_DRIFT_RATE;
	qt_config_data.qt_max_on_duration = DEF_QT_MAX_ON_DURATION;
	qt_config_data.qt_drift_hold_time = DEF_QT_DRIFT_HOLD_TIME;
	qt_config_data.qt_recal_threshold = DEF_QT_RECAL_THRESHOLD;
	qt_config_data.qt_pos_recal_delay = DEF_QT_POS_RECAL_DELAY;

	/* Initialize the timer counter */
	tc_enable(&TCC0);
	tc_write_period(&TCC0, TIMER_PERIOD);
	tc_write_clock_source(&TCC0, TC_CLKSEL_DIV8_gc);
	tc_set_cca_interrupt_level(&TCC0, PMIC_LVL_LOW);
	tc_set_cca_interrupt_callback(&TCC0, app_touch_tc_interrupt_callback);

	/*
	 * Set up callback function. This function is called after the library
	 * has made capacitive measurements, but before it has processed them.
	 * The user can use this hook to apply filter functions to the measured
	 * signal values.(Possibly to fix sensor layout faults)
	 */
	qt_filter_callback = NULL;

#ifdef _DEBUG_INTERFACE_
	QDebug_Init();
#endif
	sleepmgr_lock_mode(SLEEPMGR_IDLE);
}
void TimerD0_init(void)
{
	tc_write_clock_source(&TCD0,TC_CLKSEL_DIV256_gc);
	tc_set_wgm(&TCD0,TC_WG_NORMAL);
	tc_set_overflow_interrupt_level(&TCD0,TC_INT_LVL_MED);
	tc_write_period(&TCD0,TIMERD0_PER);
	tc_set_direction(&TCD0,TC_UP);
	tc_enable(&TCD0);
};
Пример #13
0
void XBee_Init()
{
	tc_enable(&(XBEE_TIMER)); // timer used for callback functionality
	tc_set_overflow_interrupt_callback(&(XBEE_TIMER), SendTelemetry); // sets up a function to callback when it's time to run
	tc_set_wgm(&(XBEE_TIMER), TC_WG_NORMAL); // sets the waveform generation
	tc_write_period(&(XBEE_TIMER), XBEE_HZ);  // this should run at 2 HZ
	tc_set_overflow_interrupt_level(&(XBEE_TIMER), TC_INT_LVL_MED); // low priority 0x01
	tc_write_clock_source(&(XBEE_TIMER), TC_CLKSEL_DIV1024_gc); // set clock prescaler to 1024
}
Пример #14
0
void init_MS5611_callback()
{
	tc_enable(&(MS5611_TIMER)); // timer used for callback functionality
	tc_set_overflow_interrupt_callback(&(MS5611_TIMER), ms5611_altitude); // sets up a function to callback when it's time to run
	tc_set_wgm(&(MS5611_TIMER), TC_WG_NORMAL); // sets the waveform generation
	tc_write_period(&(MS5611_TIMER), MS5611_HZ);
	tc_set_overflow_interrupt_level(&(MS5611_TIMER), TC_INT_LVL_LO); // medium priority 0x02
	tc_write_clock_source(&(MS5611_TIMER), TC_CLKSEL_DIV1024_gc); // set clock prescaler to 1024
}
void TimerC0_init(void)
{
	tc_write_clock_source(&TCC0,TC_CLKSEL_DIV64_gc);//1
	tc_set_wgm(&TCC0,TC_WG_SS);
	tc_write_period(&TCC0,0x77);//0x01DFF
	tc_set_direction(&TCC0,TC_UP);
	tc_enable_cc_channels(&TCC0,TC_CCAEN);
	tc_enable_cc_channels(&TCC0,TC_CCBEN);
	tc_enable(&TCC0);
	tc_write_cc(&TCC0,TC_CCA,0x5D);
};
Пример #16
0
/**
 * \brief Start a PWM channel
 *
 * This function enables a channel with a given duty cycle.
 *
 * \param *config           Pointer to the PWM configuration struct
 * \param duty_cycle_scale  Duty cycle as a value between 0 and 100.
 */
void pwm_start(struct pwm_config *config, uint8_t duty_cycle_scale)
{
    /* Set given duty cycle */
    pwm_set_duty_cycle_percent(config, duty_cycle_scale);
    /* Set correct TC period */
    tc_write_period(config->tc, config->period);
    /* Enable CC channel for this TC */
    tc_enable_cc_channels(config->tc, config->cc_mask);
    /* Enable TC by setting correct clock prescaler */
    tc_write_clock_source(config->tc, config->clk_sel);
}
Пример #17
0
uint8_t shutter_cont(double freq){
    tc_write_clock_source(&SHUTTER_TC, TC_CLKSEL_OFF_gc);
    if (freq > 30 || freq < 0.23842) {
        return 1;
    }

    else{
        tc_enable(&SHUTTER_TC);
        tc_set_wgm(&SHUTTER_TC, TC_WG_FRQ);
        tc_enable_cc_channels(&SHUTTER_TC, TC_CCBEN);
        
        tc_write_clock_source(&SHUTTER_TC, TC_CLKSEL_DIV64_gc);

        uint16_t temp_div = ceil((1/(2*freq))*F_CPU/65536);
        uint16_t divider = 0;
        
        if (temp_div <= 64){
            tc_write_clock_source(&SHUTTER_TC,TC_CLKSEL_DIV64_gc);
            divider = 64;
        }
        else if (temp_div <= 256){
            tc_write_clock_source(&SHUTTER_TC,TC_CLKSEL_DIV256_gc);
            divider = 256;
        }
        else if (temp_div <= 1024){
            tc_write_clock_source(&SHUTTER_TC,TC_CLKSEL_DIV1024_gc);
            divider = 1024;
        }
        else{
            printf("#ERR: Frequency/ADC rate is too low\n");
            return 0;
        }

        SHUTTER_TC.CCA = ((uint16_t)((F_CPU/divider)/(2*freq)))-1; //f=1/(2*(CCA+1)*f_clk)
    
        return 0;
    }
}
Пример #18
0
void app_cpu_load_init(void)
{
	/* Reset counters */
	app_cpu_load_time_actif = 0;
	app_cpu_load_time_sleep = 0;
	/* Start Timer counter used to monitor CPU timing */
	tc_enable(&TCC1);
	tc_write_clock_source(&TCC1, TC_CLKSEL_DIV256_gc); /* 24MHz / 256 */
	tc_set_direction(&TCC1, TC_UP);
	/* Display static background */
	gfx_mono_draw_string(DISPLAY_CPU_LOAD_TEXT,
			DISPLAY_CPU_LOAD_TEXT_POS_X,
			DISPLAY_CPU_LOAD_TEXT_POS_Y,
			&sysfont);
}
Пример #19
0
static void qdec_enabled_tc(qdec_config_t *config)
{
	/* Configure TC for quadrature decode for event action and
	 * event channel selection:
	 * - Set event source and event action as per sent parameters
	 * - Load Period register of TC with number of counts for single
	 *   revolution
	 * - Write clock value and start timer
	 */
	tc_enable(config->timer);
	tc_set_input_capture(config->timer,
			(TC_EVSEL_t)(TC_EVSEL_CH0_gc + config->event_channel),
			TC_EVACT_QDEC_gc);
	tc_write_count(config->timer, 0);
	tc_write_period(config->timer, config->revolution - 1);
	tc_write_clock_source(config->timer, TC_CLKSEL_DIV1_gc);
}
Пример #20
0
static void prvSetupTimerInterrupt(void) {
    // Use TCC0 as a tick counter. If this is to be changed, change ISR as well
    tc_enable(&TCC0);
    tc_set_wgm(&TCC0, TC_WG_NORMAL);

    // Select the clock source and prescale by 64
    tc_write_clock_source(&TCC0, TC_CLKSEL_DIV64_gc);

    //set period of counter
    tc_write_period(&TCC0, configCPU_CLOCK_HZ / configTICK_RATE_HZ / 64 - 1);

    //enable interrupt and set low level
    //tc_set_overflow_interrupt_callback(&TCC0, tickTimer);
    tc_set_overflow_interrupt_level(&TCC0, TC_INT_LVL_LO);

    cpu_irq_enable();
}
Пример #21
0
static void init_timer_isr( void )
{
	tc_enable(&TCD0);

	/* We divide the peripheral 2MHz clock by 2 to get 1MHz*/
	tc_write_clock_source(&TCD0, TC_CLKSEL_DIV2_gc);

	/* Set Compare A interrupt to low level */
	tc_set_cca_interrupt_level(&TCD0, TC_INT_LVL_LO);

	/* 1000 counts is 1ms at 1MHz input clock */
	tc_write_period (&TCD0, 1000 * qt_measurement_period_msec);

	/* Handling callback */
	tc_set_cca_interrupt_callback(&TCD0, touch_timer_callback);

	/* Enable CCA */
	tc_enable_cc_channels(&TCD0, TC_CCAEN);
}
Пример #22
0
/*! \brief  to initialiaze hw timer
 */
uint8_t tmr_init(void)
{
	uint8_t timer_multiplier;

	tc_enable(TIMER);

	tc_set_overflow_interrupt_callback(TIMER, tc_ovf_callback);

	tc_set_mode(TIMER, NORMAL);

	tc_enable_ovf_int(TIMER);

	configure_tc_callback(TIMER);

	tc_disable_compa_int(TIMER);

	tc_write_clock_source(TIMER, TC_CLKSEL_DIV1_gc);

	timer_multiplier = sysclk_get_peripheral_bus_hz(TIMER) / DEF_1MHZ;

	return timer_multiplier;
}
Пример #23
0
/**
 * \brief Delay function which use a Timer Counter and can be aborted
 * SW1 pressed stop delay.
 *
 * \param delay_ms  delay in ms
 */
static void main_introduction_delay(uint16_t delay_ms)
{
    /* Initialization TC to manage a delay between each slide */
    tc_enable(&TCC1);
    tc_write_clock_source(&TCC1, TC_CLKSEL_DIV1024_gc); /* 24MHz / 1024 */
    tc_set_direction(&TCC1, TC_UP);
    while (delay_ms) {
        tc_write_count(&TCC1, 0);
        uint16_t delay_step = delay_ms;
        if (delay_step > 2800) {
            delay_step = 2500;
        }

        while (tc_read_count(&TCC1) <
                ((24000lu * delay_step) / 1024lu)) {
            if (main_introduction_is_exist()) {
                break;
            }
        }
        delay_ms -= delay_step;
    }
    tc_disable(&TCC1);
}
Пример #24
0
int main (void)
{
	sysclk_init();
	board_init();
	pmic_init();
	gfx_mono_init();
	adc_sensors_init();
	// Enable display backlight
	gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT);
	cpu_irq_enable();
	
	while(true){
		
		if(state==1){
			start_game();
			}else if(state==2){
			tc_enable(&TCC0);
			tc_set_overflow_interrupt_callback(&TCC0, sun_count);
			tc_set_wgm(&TCC0, TC_WG_NORMAL);
			tc_write_period(&TCC0, 13500);
			tc_set_overflow_interrupt_level(&TCC0, TC_INT_LVL_LO);
			tc_write_clock_source(&TCC0, TC_CLKSEL_DIV256_gc);
			
			tc_enable(&TCC1);
			tc_set_overflow_interrupt_callback(&TCC1, button_press);
			tc_set_wgm(&TCC1, TC_WG_NORMAL);
			tc_write_period(&TCC1, 62500);
			tc_set_overflow_interrupt_level(&TCC1, TC_INT_LVL_LO);
			tc_write_clock_source(&TCC1, TC_CLKSEL_DIV8_gc);
			
			gfx_mono_draw_string("SUN:   0", 0, 0, &sysfont);
			gfx_mono_draw_string(">", 0, cursor_position, &sysfont);
			gfx_mono_draw_string("Score:  0", 63, 0, &sysfont);
			
			randomPeta();
			
			char* score_string = NULL;
			uint16_t old_score = 0;
			
			for(j = 0; j <= 70; j++){
				
				if(sun_value > 10){
					
					lightsensor_measure();
					while (!lightsensor_data_is_ready()) {
						// Wait until the conversion is complete
					}
					if(lightsensor_get_raw_value() > 250){
						sun_value -= 10;
						sunBurst();
						gfx_mono_draw_filled_rect(12,8,114,24,GFX_PIXEL_CLR);
					}
				}
				

				if(score > old_score){
					sprintf(score_string, "%3d", score);
					gfx_mono_draw_string(score_string, 100, 0, &sysfont);
					old_score = score;
				}
				
				if(lose){
					state=3;
					break;
					}else if(zombie==0){
					state=4;
					break;
				}
				
				
				tampilkanPeta();
				tampilkanTembak();
				delay_ms(1000);
			}
			}else if(state==3){
			cpu_irq_disable();
			gfx_mono_draw_filled_rect(0,0,128,32,GFX_PIXEL_CLR);
			while(true){
				gfx_mono_draw_string("GAME OVER",36,8,&sysfont)	;
				gfx_mono_draw_string("You Lose",39,20,&sysfont)	;
			}
			}else if(state==4){
			cpu_irq_disable();
			gfx_mono_draw_filled_rect(0,0,128,32,GFX_PIXEL_CLR);
			while(true){
				gfx_mono_draw_string("GAME OVER",36,2,&sysfont)	;
				gfx_mono_draw_string("You Win",42,12,&sysfont)	;
				gfx_mono_draw_string("Score = ",30,22,&sysfont)	;
				char* score_string = NULL;
				sprintf(score_string, "%3d", score);
				gfx_mono_draw_string(score_string, 79, 22, &sysfont);
			}
		}
	}
	
}
Пример #25
0
void shutter_cont_stop(void){
    tc_write_clock_source(&SHUTTER_TC, TC_CLKSEL_OFF_gc);
    tc_set_wgm(&SHUTTER_TC, TC_WG_NORMAL);
    tc_disable(&SHUTTER_TC);
    ioport_set_pin_level(SHUTTER_PIN, 0);
}
Пример #26
0
/**
 * \brief Initialize PWM configuration struct and set correct I/O pin to output
 *
 * \param config Pointer to PWM configuration struct.
 * \param tc \ref pwm_tc_t "TC" to use for this PWM.
 * \param channel \ref pwm_channel_t "CC channel" to use for this PWM.
 * \param freq_hz Frequency to use for this PWM.
  */
void pwm_init(struct pwm_config *config, enum pwm_tc_t tc,
              enum pwm_channel_t channel, uint16_t freq_hz)
{
    /* Number of channels for this TC */
    uint8_t num_chan = 0;
    UNUSED(num_chan);

    /* Set TC and correct I/O pin to output */
    /*
     * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
     */
    switch (tc) {
#if defined(TCC0)
    case PWM_TCC0:
        config->tc = &TCC0;
        PORTC.DIR |= (1 << (channel-1));
        num_chan = 4;
        break;
#endif
#if defined(TCC1)
    case PWM_TCC1:
        config->tc = &TCC1;
        PORTC.DIR |= (1 << (channel+3));
        num_chan = 2;
        break;
#endif
#if defined(TCD0)
    case PWM_TCD0:
        config->tc = &TCD0;
        PORTD.DIR |= (1 << (channel-1));
        num_chan = 4;
        break;
#endif
#if defined(TCD1)
    case PWM_TCD1:
        config->tc = &TCD1;
        PORTD.DIR |= (1 << (channel+3));
        num_chan = 2;
        break;
#endif

#if defined(TCE0)
    case PWM_TCE0:
        config->tc = &TCE0;
        PORTE.DIR |= (1 << (channel-1));
        num_chan = 4;
        break;
#endif
#if defined(TCE1)
    case PWM_TCE1:
        config->tc = &TCE1;
        PORTE.DIR |= (1 << (channel+3));
        num_chan = 2;
        break;
#endif

#if defined(TCF0)
    case PWM_TCF0:
        config->tc = &TCF0;
        PORTF.DIR |= (1 << (channel-1));
        num_chan = 4;
        break;
#endif
#if defined(TCF1)
    case PWM_TCF1:
        config->tc = &TCF1;
        PORTF.DIR |= (1 << (channel+3));
        num_chan = 2;
        break;
#endif
    default:
        Assert(false);
        break;
    }

    /* Make sure we are not given a channel number larger
       than this TC can handle */
    Assert(channel <= num_chan);
    config->channel = channel;

    /* Set the correct cc_mask */
    switch (channel) {
    case PWM_CH_A:
        config->cc_mask = TC_CCAEN;
        break;
    case PWM_CH_B:
        config->cc_mask = TC_CCBEN;
        break;
    case PWM_CH_C:
        config->cc_mask = TC_CCCEN;
        break;
    case PWM_CH_D:
        config->cc_mask = TC_CCDEN;
        break;
    default:
        Assert(false);
        break;
    }

    /* Enable peripheral clock for this TC */
    tc_enable(config->tc);

    /* Set this TC's waveform generator in single slope mode */
    tc_set_wgm(config->tc, TC_WG_SS);

    /* Default values (disable TC and set minimum period)*/
    config->period = 0;
    config->clk_sel = PWM_CLK_OFF;
    tc_write_clock_source(config->tc, PWM_CLK_OFF);

    /* Set the PWM frequency */
    pwm_set_frequency(config, freq_hz);
}