- #include "at32f425_board.h"
- #include "at32f425_clock.h"
- uint16_t timer_period = 0;
- uint16_t channel_pulse_r = 0, channel_pulse_g = 0, channel_pulse_b = 0, channel_pulse_c = 0, channel_pulse_w = 0;
- //pwmR D11 PA7 TMR14_CH1 红光
- //pwmG D6 PB10 TMR2_CH3 绿光
- //pwmB D3 PB3 TMR2_CH2 蓝光
- //pwmC D5 PB4 TMR3_CH1 冷白
- //pwmW D9 PC7 TMR3_CH2 暖白
- void timer_pwm_init(void)
- {
- crm_clocks_freq_type crm_clocks_freq_struct = {0};
- gpio_init_type gpio_init_struct = {0};
- tmr_output_config_type tmr_output_struct;
- /* enable tmr14/tmr2/tmr3/gpiob clock */
- crm_periph_clock_enable(CRM_TMR14_PERIPH_CLOCK, TRUE);
- crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE);
- crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
- crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
- crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
- crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
- gpio_init_struct.gpio_pins = GPIO_PINS_7;
- 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_3 | GPIO_PINS_4 | GPIO_PINS_10;
- 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_init_struct.gpio_pins = GPIO_PINS_7;
- 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(GPIOC, &gpio_init_struct);
- gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE7, GPIO_MUX_4);//pwmR
- gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE3, GPIO_MUX_2);
- gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE4, GPIO_MUX_1);//pwmC
- gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE10, GPIO_MUX_2);
- gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE7, GPIO_MUX_0);//pwmW
- /* get system clock */
- crm_clocks_freq_get(&crm_clocks_freq_struct);
- /* compute the value to be set in arr regiter to generate signal frequency at 17.57 khz */
- timer_period = (crm_clocks_freq_struct.sclk_freq / 10000 ) - 1;
-
- tmr_base_init(TMR14, timer_period, 0);
- tmr_cnt_dir_set(TMR14, TMR_COUNT_UP);
-
- tmr_base_init(TMR2, timer_period, 0);
- tmr_cnt_dir_set(TMR2, TMR_COUNT_UP);
-
- tmr_base_init(TMR3, timer_period, 0);
- tmr_cnt_dir_set(TMR3, TMR_COUNT_UP);
- /* channel configuration in output mode */
- 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;
-
- /* TMR14 channel 1 pwmR */
- tmr_output_channel_config(TMR14, TMR_SELECT_CHANNEL_1, &tmr_output_struct);
- tmr_channel_value_set(TMR14, TMR_SELECT_CHANNEL_1, 0);
-
- /* TMR2 channel 3 pwmG */
- tmr_output_channel_config(TMR2, TMR_SELECT_CHANNEL_3, &tmr_output_struct);
- tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, 0);
-
- /* TMR2 channel 2 pwmB */
- tmr_output_channel_config(TMR2, TMR_SELECT_CHANNEL_2, &tmr_output_struct);
- tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_2, 0);
-
- /* TMR3 channel 1 pwmC */
- tmr_output_channel_config(TMR3, TMR_SELECT_CHANNEL_1, &tmr_output_struct);
- tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, 0);
- /* TMR3 channel 2 pwmW */
- tmr_output_channel_config(TMR3, TMR_SELECT_CHANNEL_2, &tmr_output_struct);
- tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, 0);
-
- /* output enable */
- tmr_output_enable(TMR14, TRUE);
- tmr_output_enable(TMR2, TRUE);
- tmr_output_enable(TMR3, TRUE);
-
- /* enable tmr */
- tmr_counter_enable(TMR14, TRUE);
- tmr_counter_enable(TMR2, TRUE);
- tmr_counter_enable(TMR3, TRUE);
- }
- //r:0~100
- //g:0~100
- //b:0~100
- //c:0~100
- //w:0~100
- void set_rgbcw(int r,int g,int b,int c,int w)
- {
- channel_pulse_r = (uint16_t)(((uint32_t) r * (timer_period - 1)) / 100);
- channel_pulse_g = (uint16_t)(((uint32_t) g * (timer_period - 1)) / 100);
- channel_pulse_b = (uint16_t)(((uint32_t) b * (timer_period - 1)) / 100);
- channel_pulse_c = (uint16_t)(((uint32_t) c * (timer_period - 1)) / 100);
- channel_pulse_w = (uint16_t)(((uint32_t) w * (timer_period - 1)) / 100);
-
- /* TMR14 channel 1 pwmR */
- tmr_channel_value_set(TMR14, TMR_SELECT_CHANNEL_1, channel_pulse_r);
-
- /* TMR2 channel 3 pwmG */
- tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, channel_pulse_g);
-
- /* TMR2 channel 2 pwmB */
- tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_2, channel_pulse_b);
-
- /* TMR3 channel 1 pwmC */
- tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, channel_pulse_c);
-
- /* TMR3 channel 2 pwmW */
- tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, channel_pulse_w);
- }
- int main(void)
- {
- system_clock_config();
-
- at32_board_init();
-
- uart_print_init(115200);
- /* turn led2/led3/led4 on */
- at32_led_on(LED2);
- at32_led_on(LED3);
- at32_led_on(LED4);
- timer_pwm_init();
-
- printf("AT-START-F425 PWM test!\r\n");
-
- at32_led_toggle(LED2);
- at32_led_toggle(LED3);
- at32_led_toggle(LED4);
- while(1)
- {
- for(int i=0;i<=100;i++)
- {
- set_rgbcw(i,0,0,0,0);
- delay_ms(10);
- }
- for(int i=100;i>=0;i--)
- {
- set_rgbcw(i,0,0,0,0);
- delay_ms(10);
- }
-
- for(int i=0;i<=100;i++)
- {
- set_rgbcw(0,i,0,0,0);
- delay_ms(10);
- }
- for(int i=100;i>=0;i--)
- {
- set_rgbcw(0,i,0,0,0);
- delay_ms(10);
- }
-
- for(int i=0;i<=100;i++)
- {
- set_rgbcw(0,0,i,0,0);
- delay_ms(10);
- }
- for(int i=100;i>=0;i--)
- {
- set_rgbcw(0,0,i,0,0);
- delay_ms(10);
- }
-
- for(int i=0;i<=100;i++)
- {
- set_rgbcw(0,0,0,i,0);
- delay_ms(10);
- }
- for(int i=100;i>=0;i--)
- {
- set_rgbcw(0,0,0,i,0);
- delay_ms(10);
- }
-
- for(int i=0;i<=100;i++)
- {
- set_rgbcw(0,0,0,0,i);
- delay_ms(10);
- }
- for(int i=100;i>=0;i--)
- {
- set_rgbcw(0,0,0,0,i);
- delay_ms(10);
- }
- }
- }
驱动效果:
视频:
https://www.bilibili.com/video/BV1mU4y1o7fB?share_source=copy_web
总结:雅特力AT-START-F425开发板体验十分优秀,官网资料丰富,固件库例程丰富,上手简单,API简洁易用,给国产点赞。