[PIC®/AVR®/dsPIC®产品] 【CuriosityNano测评报告】AVR128DB48+LED呼吸灯

[复制链接]
 楼主| zhouminjie 发表于 2021-4-5 16:15 | 显示全部楼层 |阅读模式
本帖最后由 zhouminjie 于 2021-4-21 08:26 编辑

收到板子快半个月了,之前没接触过AVR单片机,趁着节假日学习下AVR,开始点灯
①下载安装MPLAB X IDE开发环境,安装XC8编译工具,安装MPLAB代码配置器MCC
打开MPLAB

②新建工程File—>New Project...

③选择Microchip Embedded—>Standalone Projects—>Next

④Device选择AVR128DB48、Tool选择AVR128DB48 Curiosity Nano

⑤编译工具选择XC8

⑥设置工程名,存储路径,Encoding设置GB18030(中文注释就不会显示为乱码)

⑦Tools—>Embedded—>MCC,打开代码配置器

⑧配置System Module,Oscillator Frequency Options设置16MHz system clock

⑨配置Pin Module,右击Pin Manager: Package View中芯片引脚PB3,设置为GPIO output

⑩添加TCA0定时器外设,使能定时器TCA0,Clock Selection设置System Clock/16,使能溢出中断

⑪配置Interrupt Manager,使能全局中断

⑫配置完成后,点击Generate生成main.c,在main.c中增加LED代码,定时器设置为10us中断一次,呼吸频率200Hz,5ms(500*10us)
  1. #include "mcc_generated_files/mcc.h"

  2. uint16_t PWMCount = 0;
  3. uint16_t BreathingCount = 0;
  4. uint16_t PWMDutyCount = 1;
  5. uint8_t Direction = 0;

  6. /*
  7.     Main application
  8. */
  9. void Breathing_cb(void)
  10. {
  11.     TCA0_ClearOverflowInterruptFlag();
  12.     TCA0_WriteTimer(0);

  13.     PWMCount++;
  14.     BreathingCount++;
  15.     if(PWMCount == PWMDutyCount)
  16.     {
  17.         LED_SetHigh();
  18.     }
  19.     if(PWMCount >= 350)
  20.     {
  21.         LED_SetLow();
  22.         PWMCount = 0;
  23.     }
  24.     if(BreathingCount >= 500 && Direction == 0)
  25.     {
  26.         BreathingCount = 0;
  27.         PWMDutyCount++;
  28.         if(PWMDutyCount >= 349)
  29.         {
  30.             Direction = 1;
  31.         }
  32.     }
  33.     if(BreathingCount >= 500 && Direction == 1)
  34.     {
  35.         BreathingCount = 0;
  36.         PWMDutyCount--;
  37.         if(PWMDutyCount == 1)
  38.         {
  39.             Direction = 0;
  40.         }
  41.     }
  42. }

  43. int main(void)
  44. {
  45.     /* Initializes MCU, drivers and middleware */
  46.     SYSTEM_Initialize();
  47.    
  48.     LED_SetDigitalOutput();
  49.     LED_SetHigh(); //LED熄灭
  50.     TCA0.SINGLE.PER = 10; //10us定时中断一次
  51.     TCA0_WriteTimer(0);
  52.     TCA0.SINGLE.INTCTRL = 1 << TCA_SINGLE_OVF_bp;
  53.     TCA0_SetOVFIsrCallback(Breathing_cb);
  54.    
  55.     while (1){
  56.     }
  57. }

⑬效果如下:

测试源码:






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

33

主题

140

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部

33

主题

140

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部