/*
这是我学习Flag的一个例程,参考论坛上的相关例程,在此感谢相关的作者。
因为我的串口有点问题,故通过助学板上的按键来发Flag。
初始化设置: 时钟配置为 XTL12M_EN
CPU时钟频率为12MHz
外设时钟配置为PWM01_S : 外部 4~24MHz 晶振使能
外设时钟配置为PWM01_EN : 选上
GPIOB的多功能管教选择:PB.14 INT0,PB.15 INT1
端口模式:PB.14 输入,PB.15 输入
去抖使能:PB.14 和PB.15 选上
PA.12选择多功能管脚选择PWM0
主要完成功能:产生1000Hz的波形
按按键Key1可以把波形的占空比减小
按按键Key2可以把波形的占空比加大
*/
#include "LOOK_Key_PWM.h"
#define LOOK_H 0
#if LOOK_H == 0
#include "NUC1xx.h"
#include "NUC1xxM051Seriescfg.h"
#else
#define __HAVE_GPIO
#include <nuc120re3an.h>
using namespace nuvoton;
#endif
flag_t Flag(0);
class Keyboard_t : public interrupt_t {
public:
__INLINE__ Keyboard_t();
protected:
bool isr(int vector);
void dsr(int vector, uintptr_t count);
};
// Keyboard_t 构造函数
__INLINE__ Keyboard_t::Keyboard_t()
{
attach(EINT0_IRQn);
attach(EINT1_IRQn);
#if LOOK_H == 0
GPIOBs.IEN.Regs = (1 << Pin15) | (1 << Pin14); // 开启中断
#else
GPIOB.IEN(0)
.IF_EN15(1)
.IF_EN14(1); // 开启中断
#endif
vector_t::enable(EINT0_IRQn);
vector_t::enable(EINT1_IRQn);
}
// Keyboard_t 中断服务例程
bool Keyboard_t::isr(int vector)
{
#if LOOK_H == 0
GPIOBs.ISRC.Regs = GPIOBs.ISRC.Regs; // 清中断 flag
#else
//注意:下句被优化了
// GPIOB.ISRC = GPIOB.ISRC; // 清中断 flag
//注意:下句不能被优化,特别注意()是建立缓存
GPIOB.ISRC = GPIOB.ISRC(); // 清中断 flag
#endif
return true;
}
// Keyboard_t 中断滞后服务例程
void Keyboard_t::dsr(int vector, uintptr_t count)
{
if (vector == EINT0_IRQn)//Key2
{
Flag.do_set_bits(0b01);
}
else if (vector == EINT1_IRQn)//Key1
{
Flag.do_set_bits(0b10);
}
}
Keyboard_t Key; // 创建Key对象
// 任务类 task_LOOK_Key_PWM_t 的例程
void task_LOOK_Key_PWM_t::routine()
{
// TODO: 在此编写 task_LOOK_PWM_t 例程的内容
uint8_t PWM0_Duty_Cycle = 50; //PWM初始化占空比为50
//Enable PWM engine clock and reset PWM
#if LOOK_H == 0
SYSCLKs.CLKSEL1.Bits.TMR0_S = 0b000; //外部12M晶振
PWM0s.PCR.Bits.CH0INV = 0; //反向关闭
PWM0s.PCR.Bits.CH0MOD = 1; //自动重载
PWM0s.PPR.Bits.CP01 = 1; //预分频 1
PWM0s.CSR.Bits.CSR0 = 0b100; //分频系数 1
uint16_t u16Duty = 12000000/((1+1)*1*1000);//PWM 频率 1000HZ
PWM0s.CNR0.Regs = u16Duty-1; //PWM 频率 = PWMxy_CLK/(prescale+1)*(clock divider)/(CNR+1);
PWM0s.CMR0.Regs = u16Duty*PWM0_Duty_Cycle/100-1; //占空比 = (CMR+1)/(CNR+1).
PWM0s.POE.Bits.PWM0 = 1; //PWM0输出使能
//PA.12作为PWM0 使用LOOK进行配置
PWM0s.PCR.Bits.CH0EN = 1; // 使能PWM功能
#else
SYSCLK.CLKSEL1().TMR0_S = tmrs_t::XTL12M; //外部12M晶振
PWMA.PCR()
.CH0INV(0) //反向关闭
.CH0MOD(1); //自动重载
PWMA.PPR().CP01 = 1; //预分频 1
PWMA.CSR().CSR0 = 0b100; //分频系数 1
uint16_t u16Duty = 12000000/((1+1)*1*1000);//PWM 频率 1000HZ
PWMA.CNR0 = u16Duty-1; //PWM 频率 = PWMxy_CLK/(prescale+1)*(clock divider)/(CNR+1);
PWMA.CMR0 = u16Duty*PWM0_Duty_Cycle/100-1; //占空比 = (CMR+1)/(CNR+1).
PWMA.POE().PWM0 = 1; //PWM0输出使能
//PA.12作为PWM0 使用LOOK进行配置
PWMA.PCR().CH0EN = 1; // 使能PWM功能
#endif
while (true) {
// TODO: 在此编写 task_LOOK_PWM_t 例程的内容
int flag = Flag.wait(0b011, flag_t::ANY_CONSUME);
switch (flag)
{
case 0x01:
//占空比增加10%
if (PWM0_Duty_Cycle!=100)
{
PWM0_Duty_Cycle +=5;
#if LOOK_H == 0
PWM0s.CMR0.Regs = u16Duty*PWM0_Duty_Cycle/100-1; //更新占空比
#else
PWMA.CMR0 = u16Duty*PWM0_Duty_Cycle/100-1; //更新占空比
#endif
}
break;
case 0x02:
//占空比减少10%
if (PWM0_Duty_Cycle!=0)
{
PWM0_Duty_Cycle -=5;
#if LOOK_H == 0
if (PWM0_Duty_Cycle!=0)
PWM0s.CMR0.Regs = u16Duty*PWM0_Duty_Cycle/100-1; //更新占空比
else PWM0s.CMR0.Regs = 0;
#else
if (PWM0_Duty_Cycle!=0)
PWMA.CMR0 = u16Duty*PWM0_Duty_Cycle/100-1; //更新占空比
else PWMA.CMR0 = 0;
#endif
}
break;
}
}
}
#ifdef LOOK_SCHEDULING_PRIORITY
instantiate::task<task_LOOK_Key_PWM_t, LOOK_STACK_SIZE> task_LOOK_Key_PWM(0);
#else
instantiate::task<task_LOOK_Key_PWM_t, LOOK_STACK_SIZE> task_LOOK_Key_PWM;
#endif
|