本帖最后由 wang0225 于 2011-8-18 12:58 编辑
/*
初始化设置: 时钟配置为 XTL12M_EN
CPU时钟频率为12MHz
外设时钟配置为PWM01_S : 外部 4~24MHz 晶振使能
外设时钟配置为PWM01_EN : 选上
GPIOB的多功能管教选择:PB.14 INT0,PB.15 INT1
端口模式:PB.10 输出 ,PB.14 输入,PB.15 输入
去抖使能:PB.14 和PB.15 选上
输出值:PB.10 低
PA.12选择多功能管脚选择PWM0
主要完成功能:测试下新塘的的PWM的输出反向寄存器,刚开始按key2输出占空比位20%的1K的方波,按科key1输出占空比位80%的1K的方波。
按按键Key1可以通过示波器看到占空比位80%的1K的方波
按按键Key2可以通过示波器看到占空比位20%的1K的方波
为保证按键确实按下,添加一个蜂鸣器的任务。
*/
#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(0b101);
}
else if (vector == EINT1_IRQn)//Key1
{
Flag.do_set_bits(0b110);
}
}
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 = 20; //PWM初始化占空比为20
//Enable PWM engine clock and reset PWM
#if LOOK_H == 0
SYSCLKs.CLKSEL1.Bits.TMR0_S = 0b000; //外部12M晶振
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.PCR.Bits.CH0MOD = 1; //自动重载
PWM0s.POE.Bits.PWM0 = 1; //PWM0输出使能
#else
SYSCLK.CLKSEL1().TMR0_S = tmrs_t::XTL12M; //外部12M晶振
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.PCR()
.CH0MOD(1); //自动重?
PWMA.POE().PWM0 = 1; //PWM0输出使能
#endif
while (true) {
// TODO: 在此编写 task_LOOK_PWM_t 例程的内容
int flag = Flag.wait(0b001, flag_t::ANY_CONSUME);
if(flag==0b001)
{
#if LOOK_H == 0
PWM0s.PCR.Bits.CH0INV = 0; //反向关闭
PWM0s.PCR.Bits.CH0EN = 1; // 使能PWM功能
#else
PWMA.PCR()
.CH0INV(0) //反向关闭
PWMA.PCR().CH0EN = 1; // 使能PWM功能
#endif
}
else{
#if LOOK_H == 0
PWM0s.PCR.Bits.CH0EN = 0; // 禁止PWM功能
#else
PWMA.PCR().CH0EN = 0; // 禁止PWM功能
#endif
}
}
}
// 任务类 task_LOOK_Key_PWM1_t 的例程
void task_LOOK_Key_PWM1_t::routine()
{
// TODO: 在此编写 task_LOOK_PWM_t 例程的内容
uint8_t PWM0_Duty_Cycle = 20; //PWM初始化占空比为20
#if LOOK_H == 0
SYSCLKs.CLKSEL1.Bits.TMR0_S = 0b000; //外部12M晶振
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.PCR.Bits.CH0MOD = 1; //自动重载
PWM0s.POE.Bits.PWM0 = 1; //PWM0输出使能
#else
SYSCLK.CLKSEL1().TMR0_S = tmrs_t::XTL12M; //外部12M晶振
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.PCR()
.CH0MOD(1); //自动重载
PWMA.POE().PWM0 = 1; //PWM0输出使能
#endif
while (true) {
// TODO: 在此编写 task_LOOK_PWM_t 例程的内容
int flag = Flag.wait(0b010, flag_t::ANY_CONSUME);
if(flag==0b010)
{
#if LOOK_H == 0
PWM0s.PCR.Bits.CH0INV = 1; //反向打开
PWM0s.PCR.Bits.CH0EN = 1; // 使能PWM功能
#else
PWMA.PCR()
.CH0INV(1) //反向打开
PWMA.PCR().CH0EN = 1; // 使能PWM功能
#endif
}
else{
#if LOOK_H == 0
PWM0s.PCR.Bits.CH0EN = 0; // 禁止PWM功能
#else
PWMA.PCR().CH0EN = 0; // 禁止PWM功能
#endif
}
}
}
// 任务类 task_BEEP_t 的例程
void task_BEEP_t::routine()
{
// TODO: 在此编写 task_BEEP_t 例程的内容
while (true) {
// TODO: 在此编写 task_BEEP_t 例程的内容
int flag = Flag.wait(0b100, flag_t::ANY_CONSUME);
if (flag==4)
{
for (uint32_t i = 0; i < 6; i ++){//蜂鸣器响三次
#if LOOK_H == 0
GPIOBs.DOUT.Bits.Pin10 ^= 1;//蜂鸣器响PB10=1,不响PB10=0
#else
GPIOB.DOUT().DOUT10 ^= 1;
#endif
delay(LOOK_TICKS_PER_SEC / 20);//断续间隔
}
#if LOOK_H == 0
GPIOBs.DOUT.Bits.Pin10 = 0;
#else
GPIOB.DOUT().DOUT10(0);
#endif
}
}
}
#ifdef LOOK_SCHEDULING_PRIORITY
instantiate::task<task_LOOK_Key_PWM_t, LOOK_STACK_SIZE> task_LOOK_Key_PWM(0);
instantiate::task<task_LOOK_Key_PWM1_t, LOOK_STACK_SIZE> task_LOOK_Key_PWM1(1);
instantiate::task<task_BEEP_t, LOOK_STACK_SIZE> task_BEEP(2);
#else
instantiate::task<task_LOOK_Key_PWM_t, LOOK_STACK_SIZE> task_LOOK_Key_PWM;
instantiate::task<task_LOOK_Key_PWM1_t, LOOK_STACK_SIZE> task_LOOK_Key_PWM1;
instantiate::task<task_BEEP_t, LOOK_STACK_SIZE> task_BEEP;
#endif
照片晚点传上!:) |