赞
踩
main函数
- int main(void)
- {
- /* 系统时钟配置 */
- system_clock_config();
- /* LED 延时函数等初始化 */
- at32_board_init();
- /* 获取系统时钟 */
- crm_clocks_freq_get(&crm_clocks_freq_struct);
- /* 点亮 LED2/LED3/LED4 */
- at32_led_on(LED2);
- at32_led_on(LED3);
- at32_led_on(LED4);
- /* 开启 TMR1 时钟 */
- crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
- /* 配置定时器 TMRx_DIV 寄存器和 TMRx_PR 寄存器 */
- /* systemclock/24000/10000 = 1hz */
- tmr_base_init(TMR1, 9999, (crm_clocks_freq_struct.ahb_freq / 10000) - 1);
- /*配置定时器为向上计数方向,如果选择向上计数也可以不配置该语句,
- 因为 TMR 默认就是向上计数模式 */
- tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);
- /* 开启定时器溢出中断 */
- tmr_interrupt_enable(TMR1, TMR_OVF_INT, TRUE);
- /* 开启 NVIC 溢出中断 */
- nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
- nvic_irq_enable(TMR1_OVF_TMR10_IRQn, 0, 0);
- /* 开启定时器计数 */
- tmr_counter_enable(TMR1, TRUE);
- clkout_config();
- while(1)
- {
-
- }
- }
- void TMR1_OVF_TMR10_IRQHandler(void)
- {
- /* 判断溢出标志位是否置起 */
- if(tmr_flag_get(TMR1, TMR_OVF_FLAG) == SET)
- {
- /* 增加应用程序 */
- at32_led_toggle(LED3);
- tmr_flag_clear(TMR1, TMR_OVF_FLAG);
- }
- }
- int main(void)
- {
- system_clock_config();
- /* 初始化板载设备 */
- at32_board_init();
-
- /* get system clock */
- crm_clocks_freq_get(&crm_clocks_freq_struct);
- /* turn led2/led3/led4 on */
- at32_led_on(LED2);
- at32_led_on(LED3);
- at32_led_on(LED4);
- /* 打开 tmr1/gpioa/gpiob 时钟 */
- crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
- crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
- crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
- /* 配置 TMR1 输出管脚 */
- gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10 | GPIO_PINS_11;
- gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
- gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
- gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_init(GPIOA, &gpio_init_struct);
- gpio_init_struct.gpio_pins = GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15;
- gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
- gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
- gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_init(GPIOB, &gpio_init_struct);
- gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE8, GPIO_MUX_1);
- gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_1);
- gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_1);
- gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE11, GPIO_MUX_1);
- gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE13, GPIO_MUX_1);
- gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE14, GPIO_MUX_1);
- gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE15, GPIO_MUX_1);
- /* tmr1 初始化 ---------------------------------------------------
- generate 7 pwm signals with 4 different duty cycles:
- prescaler = 0, tmr1 counter clock = apb2_freq *2
- the objective is to generate 7 pwm signal at 17.57 khz:
- - tim1_period = (apb2_freq * 2 / 17570) - 1
- the channel 1 and channel 1n duty cycle is set to 50%
- the channel 2 and channel 2n duty cycle is set to 37.5%
- the channel 3 and channel 3n duty cycle is set to 25%
- the channel 4 duty cycle is set to 12.5%
- the timer pulse is calculated as follows:
- - channelxpulse = dutycycle * (tim1_period - 1) / 100
- ----------------------------------------------------------------------- */
- /* compute the value to be set in arr regiter to generate signal frequency at 17.57 khz */
- timerperiod = ((crm_clocks_freq_struct.apb2_freq * 2) / 17570 ) - 1;
- /* compute ccr1 value to generate a duty cycle at 50% for channel 1 and 1n */
- channel1pulse = (uint16_t) (((uint32_t) 5 * (timerperiod - 1)) / 10);
- /* compute ccr2 value to generate a duty cycle at 37.5% for channel 2 and 2n */
- channel2pulse = (uint16_t) (((uint32_t) 375 * (timerperiod - 1)) / 1000);
- /* compute ccr3 value to generate a duty cycle at 25% for channel 3 and 3n */
- channel3pulse = (uint16_t) (((uint32_t) 25 * (timerperiod - 1)) / 100);
- /* compute ccr4 value to generate a duty cycle at 12.5% for channel 4 */
- channel4pulse = (uint16_t) (((uint32_t) 125 * (timerperiod- 1)) / 1000);
- /* 配置 TMR 为向上计数模式 */
- tmr_base_init(TMR1, timerperiod, 0);
- tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);
- /* 配置通道 1/2/3/4 */
- tmr_output_default_para_init(&tmr_output_struct);
- tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_B;
- tmr_output_struct.oc_output_state = TRUE;
- tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_LOW;
- tmr_output_struct.oc_idle_state = TRUE;
- tmr_output_struct.occ_output_state = TRUE;
- tmr_output_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
- tmr_output_struct.occ_idle_state = FALSE;
- /* channel 1 */
- tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_1, &tmr_output_struct);
- tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_1, channel1pulse);
- /* channel 2 */
- tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_2, &tmr_output_struct);
- tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_2, channel2pulse);
- /* channel 3 */
- tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_3, &tmr_output_struct);
- tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_3, channel3pulse);
- /* channel 4 */
- tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_4, &tmr_output_struct);
- tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_4, channel4pulse);
- /*TMR1 输出总开关打开 */
- tmr_output_enable(TMR1, TRUE);
- /*使能 TMR1 */
- tmr_counter_enable(TMR1, TRUE);
- while(1)
- {}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。