Ejemplo n.º 1
0
int main(void)
{
	pmic_init();
	board_init();
	sysclk_init();
	sleepmgr_init();
	cpu_irq_enable();

	/* Enables the Timer defined in conf_example.h : TCE0 in this example */
	tc_enable(&TIMER_EXAMPLE);

	/* Configures the interrupt level of CCA and CCB modules : low */
	tc_set_cca_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO);
	tc_set_ccb_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO);

	/* Configures the waveform generator of this Timer mode in NORMAL mode */
	tc_set_wgm(&TIMER_EXAMPLE, TC_WG_NORMAL);

	/* Declares the interrupt functions which will be called when CCA and CCB
	interrupts will occur */
	tc_set_cca_interrupt_callback(&TIMER_EXAMPLE,
			example_cca_interrupt_callback);
	tc_set_ccb_interrupt_callback(&TIMER_EXAMPLE,
			example_ccb_interrupt_callback);

	/* Configures the Timer period*/
	tc_write_period(&TIMER_EXAMPLE, TIMER_EXAMPLE_PERIOD);

	/* Configures the CCA and CCB levels*/
	tc_write_cc(&TIMER_EXAMPLE, TC_CCA, TIMER_EXAMPLE_PERIOD/2);
	tc_write_cc(&TIMER_EXAMPLE, TC_CCB, TIMER_EXAMPLE_PERIOD/2);

	/* Enables the CCA and CCB channels*/
	tc_enable_cc_channels(&TIMER_EXAMPLE,TC_CCAEN);
	tc_enable_cc_channels(&TIMER_EXAMPLE,TC_CCAEN);

	/* Configures the waveform genertaor in Dual Slope mode and Top*/
	tc_set_wgm(&TIMER_EXAMPLE,TC_WG_DS_T);

	/* Enables and configures the deadtime of CCA and CCB outputs*/
	tc_awex_enable_cca_deadtime(&AWEXE);
	tc_awex_enable_ccb_deadtime(&AWEXE);
	tc_awex_set_dti_high(&AWEXE, TIMER_EXAMPLE_PERIOD/6);
	tc_awex_set_dti_low(&AWEXE, TIMER_EXAMPLE_PERIOD/6);

	/* Outputs CCA and CCB on Port E0 and E1*/
	tc_awex_set_output_override(&AWEXE, 0x03);

	tc_set_resolution(&TIMER_EXAMPLE, 10000);

	do {
		/* Go to sleep, everything is handled by interrupts. */
		sleepmgr_enter_sleep();
	} while (1);
}
Ejemplo n.º 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);
}