void TimerE0_init(void)
{
    tc_write_clock_source(&TCE0,TC_CLKSEL_DIV256_gc);
    tc_set_wgm(&TCE0,TC_WG_SS);
    tc_write_period(&TCE0,0x00FF);
    tc_set_direction(&TCE0,TC_UP);
    tc_enable_cc_channels(&TCE0,TC_CCAEN);
    tc_enable_cc_channels(&TCE0,TC_CCBEN);
    tc_enable(&TCE0);
};
Ejemplo n.º 2
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);
}
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);
};
Ejemplo n.º 4
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);

}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
0
int main(void)
{
	pmic_init();
	board_init();
	sysclk_init();
	sleepmgr_init();

	cpu_irq_enable();

#if (BOARD == XMEGA_A3BU_XPLAINED)
	/* The status LED must be used as LED2, so we turn off
	 * the green led which is in the same packaging. */
	ioport_set_pin_high(LED3_GPIO);
#endif

	/*
	* Unmask clock for TIMER_EXAMPLE
	*/
	tc_enable(&TIMER_EXAMPLE);

	/*
	* Configure interrupts callback functions for TIMER_EXAMPLE
	* overflow interrupt, CCA interrupt and CCB interrupt
	*/
	tc_set_overflow_interrupt_callback(&TIMER_EXAMPLE,
			example_ovf_interrupt_callback);
	tc_set_cca_interrupt_callback(&TIMER_EXAMPLE,
			example_cca_interrupt_callback);
	tc_set_ccb_interrupt_callback(&TIMER_EXAMPLE,
			example_ccb_interrupt_callback);

	/*
	* Configure TC in normal mode, configure period, CCA and CCB
	* Enable both CCA and CCB channels
	*/

	tc_set_wgm(&TIMER_EXAMPLE, TC_WG_NORMAL);
	tc_write_period(&TIMER_EXAMPLE, TIMER_EXAMPLE_PERIOD);
	tc_write_cc(&TIMER_EXAMPLE, TC_CCA, TIMER_EXAMPLE_PERIOD / 2);
	tc_write_cc(&TIMER_EXAMPLE, TC_CCB, TIMER_EXAMPLE_PERIOD / 4);
	tc_enable_cc_channels(&TIMER_EXAMPLE,(enum tc_cc_channel_mask_enable_t)(TC_CCAEN | TC_CCBEN));

	/*
	* Enable TC interrupts (overflow, CCA and CCB)
	*/
	tc_set_overflow_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO);
	tc_set_cca_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO);
	tc_set_ccb_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO);

	/*
	* Run TIMER_EXAMPLE at TIMER_EXAMPLE_PERIOD(31250Hz) resolution
	*/
	tc_set_resolution(&TIMER_EXAMPLE, TIMER_EXAMPLE_PERIOD);

	do {
		/* Go to sleep, everything is handled by interrupts. */
		sleepmgr_enter_sleep();
	} while (1);
}
Ejemplo n.º 7
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);
}
Ejemplo n.º 8
0
void backlight_start_pwm()
{
	// Backlight PWM init
	// Unmask clock for TIMER_EXAMPLE
	tc_enable(&BACKLIGHT_TIMER);
	// Configure TC in PWM Single Slope PWM
	BACKLIGHTPORT.REMAP |= PORT_TC0B_bm;
	tc_set_wgm(&BACKLIGHT_TIMER, TC_WG_SS);
	backlight_set_pwm(0);
	tc_enable_cc_channels(&BACKLIGHT_TIMER,TC_CCBEN);
	// Run TC at 2MHz clock resolution
	tc_set_resolution(&BACKLIGHT_TIMER, BACKLIGHT_TIMER_FREQUENCY);
	backlight_set_pwm(BACKLIGHT_TIMER_COUNT);
}
Ejemplo n.º 9
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);
}
Ejemplo n.º 10
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);
}
Ejemplo n.º 11
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;
    }
}
Ejemplo n.º 12
0
static void qdec_enabled_tc_freq(qdec_config_t *config)
{
	volatile uint8_t *evsys_chctrl, *evsys_chctrl_freq;

	/* Configuration of frequency calculation */
	Assert(config->event_channel != config->freq_opt.event_channel);
	if (config->index.enabled) {
		Assert((config->event_channel + 1)
				!= config->freq_opt.event_channel);
	}

#if XMEGA_E

	/* Channel must be < 4, because QDec channel is 0
	 * and EVSYS.DFCTRL enables filter per event group */
	Assert(config->freq_opt.event_channel < 4);
#endif

	/* In event channel enable digital filter as QDec event channel */
#if XMEGA_E
	if (EVSYS.DFCTRL & EVSYS_PRESCFILT_CH04_gc) {
		if (config->freq_opt.event_channel == 1) {
			EVSYS.DFCTRL |= EVSYS_PRESCFILT_CH15_gc;
		} else if (config->freq_opt.event_channel == 2) {
			EVSYS.DFCTRL |= EVSYS_PRESCFILT_CH26_gc;
		} else {
			EVSYS.DFCTRL |= EVSYS_PRESCFILT_CH37_gc;
		}
	}

#endif
	evsys_chctrl_freq = &EVSYS.CH0CTRL + config->freq_opt.event_channel;
	evsys_chctrl = &EVSYS.CH0CTRL + config->event_channel;
	*evsys_chctrl_freq = *evsys_chctrl & EVSYS_DIGFILT_gm;

	/* Configure event channel for frequency calculation */
	qdec_evsys_pin_2_chmux(config->port, config->pins_base,
			config->freq_opt.event_channel);

	/* Configure TC to capture frequency
	 * Load timer period register
	 * Enable capture on CCA channel
	 * Select timer clock source
	 */
	tc_enable(config->freq_opt.timer);
	tc_set_input_capture(config->freq_opt.timer,
			(TC_EVSEL_t)(TC_EVSEL_CH0_gc
			+ config->freq_opt.event_channel),
			TC_EVACT_FRQ_gc);
	tc_write_count(config->freq_opt.timer, 0);
	tc_write_period(config->freq_opt.timer, 0xFFFF);
	tc_enable_cc_channels(config->freq_opt.timer, TC_CCAEN);
	tc_set_resolution(config->freq_opt.timer,
			(config->freq_opt.unit / 1000) / config->revolution);
	config->freq_opt.coef
			= (((uint64_t)tc_get_resolution(config->freq_opt.timer) * 1000)
			/ config->freq_opt.unit)
			* 4
			/ config->revolution;
	config->freq_opt.last_freq = 0; /* Initialize frequence to 0Hz */
}
Ejemplo n.º 13
0
int main (void)
{
	En_RC32M();

	//Enable LowLevel & HighLevel Interrupts
	PMIC_CTRL |= PMIC_HILVLEN_bm | PMIC_LOLVLEN_bm |PMIC_MEDLVLEN_bm;

	PORT_init();
	TimerD0_init();
	TimerC0_init();
	TimerE1_init();
	USARTE0_init();
	ADCA_init();
	//wdt_enable();

	// Globally enable interrupts
	sei();

	//Address[0]=Address[0] + RobotID ;

	//// NRF Initialize
	NRF_init () ;
	
	while(1)
	  {  
		    asm("wdr");
		   // BUZZER
		    //adc = adc +(adc_get_unsigned_result(&ADCA,ADC_CH0)-adc)*0.01;
		   adc = adc_get_unsigned_result(&ADCA,ADC_CH0);
		    if (adc<=2250)//10 volt
			Buzzer_PORT.OUTSET = Buzzer_PIN_bm;
		    else if(shoot_alarm_flg && (charge_count>=30000))
		    Buzzer_PORT.OUTSET = Buzzer_PIN_bm;
		    else
		    Buzzer_PORT.OUTCLR = Buzzer_PIN_bm;
			
			//SHOOT
			PORTC_OUTCLR=KCK_SH_PIN_bm;
			if((KCK_Ch_Limit_PORT.IN & KCK_Ch_Limit_PIN_bm)>>KCK_Ch_Limit_PIN_bp)
			{
				full_charge=1;
				tc_disable_cc_channels(&TCC0,TC_CCDEN);
				charge_count=0;
			}
			else
			{
				if((flg || flg_sw)==0)
				{
					tc_enable_cc_channels(&TCC0,TC_CCDEN);
				}
			}
			if (charge_flg)//full_charge 
			{
				if (Robot_D[RobotID].KCK )
				{
					if( KCK_Sens || (Robot_D[RobotID].KCK%2))
					{
						flg = 1;
					}
				}
			}
			if (KCK_DSH_SW)//bazi vaghta begir nagir dare
			{
				flg_sw = 1;
			}
			if (free_wheel >= 500 )
			{
				NRF_init();
			}
			if(KCK_Sens)
			LED_Green_PORT.OUTSET = LED_Green_PIN_bm;
			else
			LED_Green_PORT.OUTCLR = LED_Green_PIN_bm;
		   // //motor test
		   //switch(flag2sec)
			  //{ case 200:
			   //// M.Setpoint=1000;
			   //Robot_D[RobotID].M0b  = 0xE8;//low
			   //Robot_D[RobotID].M0a  = 0X03;//high
			   //break;
			   //
			   //case 400:
			   ////M.Setpoint=2000;
			   //Robot_D[RobotID].M0b  = 0xD0;//low
			   //Robot_D[RobotID].M0a  = 0X07;//high
			   //break;
			   //
			   //case 600:
			   ////M.Setpoint=500;
			   //Robot_D[RobotID].M0b  = 0xF4;//low
			   //Robot_D[RobotID].M0a  = 0X01;//high
			   //break;
			   //
			   //case 800:
			   ////M.Setpoint=4000;
			   //Robot_D[RobotID].M0b  = 0xA0;//low
			   //Robot_D[RobotID].M0a  = 0X0F;//high
			   //break;
			   //
			   //case 1000:
			   ////M.Setpoint=1000;
			   //Robot_D[RobotID].M0b  = 0xE8;//low
			   //Robot_D[RobotID].M0a  = 0X03;//high
			   //break;
			   //
			   //case 1200:
			   ////M.Setpoint=500;
			   //Robot_D[RobotID].M0b  = 0xF4;//low
			   //Robot_D[RobotID].M0a  = 0X01;//high
			   //break;
			   //
			   //case 1400:
			   ////M.Setpoint=-500;
			   //Robot_D[RobotID].M0b  = 0x0C;//low
			   //Robot_D[RobotID].M0a  = 0XFE;//high
			   //break;
			   //
			   //case 1600:
			   ////M.Setpoint=400;
			   //Robot_D[RobotID].M0b  = 0x90;//low
			   //Robot_D[RobotID].M0a  = 0X01;//high
			   //break;
			   //
			   //case 1800:
			   ////M.Setpoint=350;
			   //Robot_D[RobotID].M0b  = 0x5E;//low
			   //Robot_D[RobotID].M0a  = 0X01;//high
			   //break;
			   //
			   //case 2000:
			   ////M.Setpoint=340;
			   //Robot_D[RobotID].M0b  = 0x54;//low
			   //Robot_D[RobotID].M0a  = 0X01;//high
			   //break;
			   //
			   //case 2200:
			   ////M.Setpoint=330;
			   //Robot_D[RobotID].M0b  = 0x4A;//low
			   //Robot_D[RobotID].M0a  = 0X01;//high
			   //break;
			   //
			   //case 2600:
			   ////M.Setpoint=100;
			   //Robot_D[RobotID].M0b  = 0x64;//low
			   //Robot_D[RobotID].M0a  = 0X00;//high
			   //break;
			   //
			   //case 2800:
			   ////M.Setpoint=50;
			   //Robot_D[RobotID].M0b  = 0x32;//low
			   //Robot_D[RobotID].M0a  = 0X00;//high
			   //break;
			   //
			   //case 3000:
			   ////M.Setpoint=1000;
			   //Robot_D[RobotID].M0b  = 0xE8;//low
			   //Robot_D[RobotID].M0a  = 0X03;//high
			   //break;
			   //
			   //case 3200:
			   ////M.Setpoint=-50;
			   //Robot_D[RobotID].M0b  = 0xCE;//low
			   //Robot_D[RobotID].M0a  = 0XFF;//high
			   //flag2sec=0;
			   //break;
	  //
			   //}
		 //Robot_D[RobotID].M0b  = 0xD0;//0X18;//-1000//01;//low37121
		 //Robot_D[RobotID].M0a  = 0x07;//0XFC;//high
		 //Robot_D[RobotID].M1b  = 0XE8;//2000//ghalat17325
		 //Robot_D[RobotID].M1a  = 0X03;
		 //Robot_D[RobotID].M2b  = 0XDC;//1000//low13703
		 //Robot_D[RobotID].M2a  = 0X05;//high
		 //Robot_D[RobotID].M3b  = 0xF4;//3000//32;//ghalat30258
		 //Robot_D[RobotID].M3a  = 0X01;//76;
		    
			////SEND TEST DATA TO FT232
			//char str1[20];
		    //uint8_t count1 = sprintf(str1,"%d\r",adc);
			//
			//for (uint8_t i=0;i<count1;i++)
			//{
				//usart_putchar(&USARTE0,str1[i]);
				//
			//}
			//usart_putchar(&USARTE0,'a');
			
	  }