本帖最后由 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)
#include "mcc_generated_files/mcc.h"
uint16_t PWMCount = 0;
uint16_t BreathingCount = 0;
uint16_t PWMDutyCount = 1;
uint8_t Direction = 0;
/*
Main application
*/
void Breathing_cb(void)
{
TCA0_ClearOverflowInterruptFlag();
TCA0_WriteTimer(0);
PWMCount++;
BreathingCount++;
if(PWMCount == PWMDutyCount)
{
LED_SetHigh();
}
if(PWMCount >= 350)
{
LED_SetLow();
PWMCount = 0;
}
if(BreathingCount >= 500 && Direction == 0)
{
BreathingCount = 0;
PWMDutyCount++;
if(PWMDutyCount >= 349)
{
Direction = 1;
}
}
if(BreathingCount >= 500 && Direction == 1)
{
BreathingCount = 0;
PWMDutyCount--;
if(PWMDutyCount == 1)
{
Direction = 0;
}
}
}
int main(void)
{
/* Initializes MCU, drivers and middleware */
SYSTEM_Initialize();
LED_SetDigitalOutput();
LED_SetHigh(); //LED熄灭
TCA0.SINGLE.PER = 10; //10us定时中断一次
TCA0_WriteTimer(0);
TCA0.SINGLE.INTCTRL = 1 << TCA_SINGLE_OVF_bp;
TCA0_SetOVFIsrCallback(Breathing_cb);
while (1){
}
}
⑬效果如下:
测试源码:
AVR_BreathingLED.X.zip
(180.1 KB)
|