本帖最后由 hotpower 于 2011-7-27 21:41 编辑
#include "main.h"
/*
本工程主要包含两个个任务:①普通类任务task_LOOK_SPI_PDMA_t:等待标志Flag_SPI低四位任意位置位 然后完成来自于uart0的任务;
②中断类任务uart0_t:uart 同步输出功能及接收功能
初始化设置: 时钟配置为 XTL12M_EN
CPU时钟频率为12MHz
UART时钟选择外部12MHZ,并使能UART0时钟
外设时钟配置为PWM01_S : 外部 4~24MHz 晶振使能
外设时钟配置为PWM01_EN : 选上
PB.0选择多功能管脚选择RXD0 PB.1选择多功能管脚选择TXD0
PA.12选择多功能管脚选择PWM0
主要完成功能:产生400Hz的波形
UART0 接收到字符‘U’或‘u’实现PWM占空比增加10%;
UART0 接收到字符‘D’或‘d’实现PWM占空比减少10%.
*/
flag_t Flag_PWM(0);
// 任务类 task_LOOK_PWM_t 的例程
void task_LOOK_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.APBCLK.Bits.PWM01_EN = 1; //PWM01时钟使能,配置文件设置好此句话不用,下面一样
//SYSs.IPRSTC2.Bits.PWM03_RST = 1; // PWM03复位
//SYSs.IPRSTC2.Bits.PWM03_RST = 0; //PWM03开始工作
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*400);//PWM 频率 400HZ
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.APBCLK().PWM01_EN = 1; //PWM01时钟使能
// SYS.IPRSTC2().PWM03_RST = 1; // PWM03复位
// SYS.IPRSTC2().PWM03_RST = 0; //PWM03开始工作
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*400);//PWM 频率 400HZ
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 例程的内容
uart0.puts("WM0当前设置占空比为:");
uart0.put_numeral(PWM0_Duty_Cycle);
uart0.puts("%\n");
int flag = Flag_PWM.wait(0b011, flag_t::ANY_CONSUME);
switch (flag)
{
case 0x01:
//占空比增加10%
if (PWM0_Duty_Cycle!=100)
{
PWM0_Duty_Cycle +=10;
#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 -=10;
#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_PWM_t, LOOK_STACK_SIZE> task_LOOK_PWM(0);
#else
instantiate::task<task_LOOK_PWM_t, LOOK_STACK_SIZE> task_LOOK_PWM;
#endif
|