[活动专区] 【AT-START-F425测评】呼吸灯

[复制链接]
 楼主| zhouminjie 发表于 2022-3-27 22:17 | 显示全部楼层 |阅读模式
#申请原创# 感谢这次能被选中测评雅特力开发板,因为疫情原因被隔离,板子一直不在身边没有及时完成测评,后面加油补上吧。之前的公司项目一直主要使用的ST的方案,国产的32位机暂时只接触过MM32,正好趁这次机会尝试下AT32;
这次先用开发板实现LED呼吸灯。
首先上官网查芯片资料,AT官网:https://www.arterytek.com/cn/index.jsp  做的比较好,芯片相关资料丰富,方便用户查找。
下载F425的DS、RM、BSP、keil5版本的Pack。
在keil5的pack installer中安装F425的keil版本pack,安装完成后如下:
1648387166(1).png
新建工程,拷贝BSP压缩包中libraries到工程中,新建其他.c.h文件:
2.png
参考AT例程,编写hal_led.c代码如下:
  1. gpio_type *led_gpio_port[LED_NUM]               = {LEDR_GPIO, LEDY_GPIO, LEDG_GPIO}; //ledIO端口数组
  2. unsigned short led_gpio_pin[LED_NUM]            = {LEDR_PIN, LEDY_PIN, LEDG_PIN}; //ledIO引脚数组
  3. crm_periph_clock_type led_gpio_crm_clk[LED_NUM] = {LEDR_GPIO_CRM_CLK, LEDY_GPIO_CRM_CLK, LEDG_GPIO_CRM_CLK}; //ledIO端口时钟数组

  4. /**
  5.          @函数名   at32_led_init
  6.    @描述     ledIO口初始化配置
  7.    @参数     led-R、Y、G
  8.    @返回值   无
  9.    @注意     无
  10.   */
  11. static void at32_led_init(led_type led)
  12. {
  13.     gpio_init_type gpio_init_struct;

  14.     /* enable the led clock */
  15.     crm_periph_clock_enable(led_gpio_crm_clk[led], TRUE);

  16.     /* set default parameter */
  17.     gpio_default_para_init(&gpio_init_struct);

  18.     /* configure the led gpio */
  19.     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  20.     gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  21.     gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  22.     gpio_init_struct.gpio_pins = led_gpio_pin[led];
  23.     gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  24.     gpio_init(led_gpio_port[led], &gpio_init_struct);

  25. }

  26. /**
  27.          @函数名   at32_led_on
  28.    @描述     ledIO口设置低电平
  29.    @参数     led-R、Y、G
  30.    @返回值   无
  31.    @注意     无
  32.   */
  33. void at32_led_on(led_type led)
  34. {
  35.     if(led > (LED_NUM - 1))
  36.         return;

  37.     if(led_gpio_pin[led])
  38.         gpio_bits_reset(led_gpio_port[led], led_gpio_pin[led]);
  39. }

  40. /**
  41.          @函数名   at32_led_off
  42.    @描述     ledIO口设置高电平
  43.    @参数     led-R、Y、G
  44.    @返回值   无
  45.    @注意     无
  46.   */
  47. void at32_led_off(led_type led)
  48. {
  49.     if(led > (LED_NUM - 1))
  50.         return;

  51.     if(led_gpio_pin[led])
  52.         gpio_bits_set(led_gpio_port[led], led_gpio_pin[led]);
  53. }

  54. /**
  55.          @函数名   at32_led_toggle
  56.    @描述     ledIO口电平取反
  57.    @参数     led-R、Y、G
  58.    @返回值   无
  59.    @注意     无
  60.   */
  61. void at32_led_toggle(led_type led)
  62. {
  63.     if(led > (LED_NUM - 1))
  64.         return;

  65.     if(led_gpio_pin[led])
  66.         led_gpio_port[led]->odt ^= led_gpio_pin[led];
  67. }

  68. /**
  69.          @函数名   hal_ledInit
  70.    @描述     led初始化配置
  71.    @参数     无
  72.    @返回值   无
  73.    @注意     无
  74.   */
  75. void hal_ledInit(void)
  76. {
  77.     at32_led_init(LEDR);
  78.     at32_led_init(LEDY);
  79.     at32_led_init(LEDG);
  80.     at32_led_off(LEDR);
  81.     at32_led_off(LEDY);
  82.     at32_led_off(LEDG);
  83. }
hal_timer.c代码如下:
  1. crm_clocks_freq_type crm_clocks_freq_struct = {0}; //系统频率时钟源数组

  2. /**
  3.          @函数名   hal_timerInit
  4.    @描述     timer1初始化,10us定时中断
  5.    @参数     无
  6.    @返回值   无
  7.    @注意     无
  8.   */
  9. void hal_timerInit(void)
  10. {
  11.     /* get system clock */
  12.     crm_clocks_freq_get(&crm_clocks_freq_struct);

  13.     /* enable tmr1 clock */
  14.     crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);

  15.     /* tmr1 configuration */
  16.     /* time base configuration */
  17.     /* systemclock.ahb_freq(96Mhz)/96/10 = 100khz(10us)*/ //(crm_clocks_freq_struct.ahb_freq / 1000000) - 1) = 96000000/1000000-1 = 96-1 =95
  18.     tmr_base_init(TMR1, (10 - 1), (crm_clocks_freq_struct.ahb_freq / 1000000) - 1);
  19.     tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);

  20.     /* overflow interrupt enable */
  21.     tmr_interrupt_enable(TMR1, TMR_OVF_INT, TRUE);

  22.     /* tmr1 overflow interrupt nvic init */
  23.     nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  24.     nvic_irq_enable(TMR1_BRK_OVF_TRG_HALL_IRQn, 0, 0);

  25.     /* enable tmr1 */
  26.     tmr_counter_enable(TMR1, TRUE);

  27. }
breathing_led.c代码如下:
  1. static unsigned short PWMCount = 0; //呼吸周期计数
  2. static unsigned short PWMDutyCount = 1; //呼吸周期内占空比计数
  3. static unsigned char Direction = 0; //呼吸方向

  4. /**
  5.          @函数名   Breathing
  6.    @描述     GPIO模拟实现呼吸灯,10us定时器中断函数中调用,呼(吸)周期4ms(呼吸共8ms),每执行一次,周期计数加1,
  7.                   呼(吸)周期内占空比计数加(减)1,周期计数=周期占空比计数,led点亮,周期计数>=400,led熄灭
  8.    @参数     无
  9.    @返回值   无
  10.    @注意     无
  11.   */
  12. void Breathing(void)
  13. {
  14.     PWMCount++;

  15.     if(PWMCount >= 400)
  16.     {
  17.         at32_led_off(LEDR);
  18.         at32_led_off(LEDY);
  19.         at32_led_off(LEDG);
  20.         PWMCount = 0;

  21.         if(Direction == 0)
  22.         {
  23.             PWMDutyCount++;

  24.             if(PWMDutyCount > 399)
  25.             {
  26.                 Direction = 1;
  27.             }
  28.         }
  29.         else
  30.         {
  31.             PWMDutyCount--;

  32.             if(PWMDutyCount <= 1)
  33.             {
  34.                 Direction = 0;
  35.             }
  36.         }
  37.     }
  38.     else if(PWMCount == PWMDutyCount)
  39.     {
  40.         at32_led_on(LEDR);
  41.         at32_led_on(LEDY);
  42.         at32_led_on(LEDG);
  43.     }
  44. }
调试编译下载hex,效果如下:
WeChat_20220327203155_ (3).gif
工程代码:
AT32F425_LED.zip (1.86 MB, 下载次数: 7)

hejun96 发表于 2022-4-7 09:33 | 显示全部楼层
楼主的这个做法比较好,但如果周期是4ms,也就是pwm的频率是250Hz了,对LED来说一般的频率是在1KHz左右会比较好,是不是能再改进下 ?
 楼主| zhouminjie 发表于 2022-4-10 12:50 | 显示全部楼层
感谢建议,我再改进下
 楼主| zhouminjie 发表于 2022-4-10 12:52 | 显示全部楼层
hejun96 发表于 2022-4-7 09:33
楼主的这个做法比较好,但如果周期是4ms,也就是pwm的频率是250Hz了,对LED来说一般的频率是在1KHz左右会比 ...

感谢建议,我再改进下
febgxu 发表于 2022-5-16 10:13 | 显示全部楼层
推荐使用pwm控制的  
touser 发表于 2022-5-16 11:22 | 显示全部楼层
AT-START-F425性能怎么样
tabmone 发表于 2022-5-16 14:05 | 显示全部楼层
为什么要使用延时呢?  
usysm 发表于 2022-5-16 16:31 | 显示全部楼层
呼吸灯做出来好看一些。  
您需要登录后才可以回帖 登录 | 注册

本版积分规则

33

主题

140

帖子

3

粉丝
快速回复 返回顶部 返回列表