int main() { usart_init(9600UL); usart_write_string_line("Timers example.."); set_as_output(DDRB, PB0); test_timer0(); forever { } }
void spindle_init() { // #ifdef VARIABLE_SPINDLE // // // Configure variable spindle PWM and enable pin, if requried. On the Uno, PWM and enable are // // combined unless configured otherwise. // SPINDLE_PWM_DDR |= (1<<SPINDLE_PWM_BIT); // Configure as PWM output pin. // TCCRA_REGISTER = TCCRA_INIT_MASK; // Configure PWM output compare timer // TCCRB_REGISTER = TCCRB_INIT_MASK; // #ifdef CPU_MAP_ATMEGA2560 // OCRA_REGISTER = OCRA_TOP_VALUE; // Set the top value for 16-bit fast PWM mode // SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); // Configure as output pin. // SPINDLE_DIRECTION_DDR |= (1<<SPINDLE_DIRECTION_BIT); // Configure as output pin. // #else // Otherwise 328p // #ifdef USE_SPINDLE_DIR_AS_ENABLE_PIN // SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); // Configure as output pin. // #else // SPINDLE_DIRECTION_DDR |= (1<<SPINDLE_DIRECTION_BIT); // Configure as output pin. // #endif // #endif // // #else // // // Configure no variable spindle and only enable pin. // SPINDLE_ENABLE_DDR |= (1<<SPINDLE_ENABLE_BIT); // Configure as output pin. // SPINDLE_DIRECTION_DDR |= (1<<SPINDLE_DIRECTION_BIT); // Configure as output pin. // // #endif #ifdef VARIABLE_SPINDLE //setup to control RC ESC, 20ms period, 0.5-1.5ms pulsewidth RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); #ifdef STANDARD_GRBL GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); //Reset init structure GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructInit( &TIM_TimeBaseStructure ); // Reset init structure TIM_TimeBaseStructure.TIM_Prescaler = 100 - 1; // 100 MHz / 100 = 1 MHz TIM_TimeBaseStructure.TIM_Period = 20000 - 1; // 1 MHz / 20000 = 50 Hz (20 ms) TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCStructInit( &TIM_OCInitStructure ); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Pulse = 1000; TIM_OC1Init(TIM3, &TIM_OCInitStructure); #else GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); //Reset init structure GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM3); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructInit( &TIM_TimeBaseStructure ); // Reset init structure TIM_TimeBaseStructure.TIM_Prescaler = 100 - 1; // 100 MHz / 100 = 1 MHz TIM_TimeBaseStructure.TIM_Period = 20000 - 1; // 1 MHz / 20000 = 50 Hz (20 ms) TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCStructInit( &TIM_OCInitStructure ); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Pulse = 1000; TIM_OC3Init(TIM3, &TIM_OCInitStructure); #endif TIM_Cmd( TIM3, ENABLE ); #else set_as_output(SPINDLE_DIR); set_as_output(SPINDLE_EN); #endif spindle_stop(); }