// 函数变量 Period 值和输出频率之间 关系表
// SYSCLKOUT = 60 MHz 40 MHz
// --------------------------------------
// Period Frequency Frequency
// 1000 60 kHz 40 kHz
// 800 75 kHz 50 kHz
// 600 100 kHz 67 kHz
// 500 120 kHz 80 kHz
// 250 240 kHz 160 kHz
// 200 300 kHz 200 kHz
// 100 600 kHz 400 kHz
// 50 1.2 Mhz 800 kHz
// 25 2.4 Mhz 1.6 MHz
// 20 3.0 Mhz 2.0 MHz
// 12 5.0 MHz 3.3 MHz
// 10 6.0 MHz 4.0 MHz
// 9 6.7 MHz 4.4 MHz
// 8 7.5 MHz 5.0 MHz
// 7 8.6 MHz 5.7 MHz
// 6 10.0 MHz 6.6 MHz
// 5 12.0 MHz 8.0 MHz
/***************************************************************************************************
**Description ** 头文件
***************************************************************************************************/
#include "DSP2802x_Device.h" // DSP2802x 头文件
#include "DSP2802x_Examples.h" // DSP2802x Examples Headerfile
#include "DSP2802x_EPwm_defines.h"
//---------------------------------------------------------------
#define KEY1 GpioDataRegs.GPBDAT.bit.GPIO33
#define KEY2 GpioDataRegs.GPBDAT.bit.GPIO32
void Scan_Key(void); //扫描键盘
void Manage_Key1(void); //按键1处理程序
void Manage_Key2(void); //按键2处理程序
void HRPWM4_Config(int);
Uint16 i,j, DutyFine, n,update;
unsigned int a=50;
/***************************************************************************************************
**Function: ** main
**Description: ** 主函数
**Input: ** Null
**Output: ** Null
**Others: ** Null
***************************************************************************************************/
Uint32 temp;
void main(void)
{
InitSysCtrl();
InitSpiaGpio();
InitEPwm4Gpio();
DINT;
update =1;
DutyFine =0;
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
EDIS;
HRPWM4_Config(2400);
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
while (update ==1)
{
for(DutyFine =1; DutyFine <256 ;DutyFine ++)
{
EPwm4Regs.CMPA.half.CMPAHR = DutyFine << 8;
for (i=0;i<10000;i++){}
}
if((KEY1==0)|(KEY2==0))//扫描是否按键按下
Scan_Key();
EPwm4Regs.CMPB=24*a;
}
}
/***************************************************************************************************
**Function: ** HRPWM1_Config(period)
**Description: ** 初始化HRPWM模块
**Input: ** period
**Output: ** Null
**Others: ** Null
***************************************************************************************************/
void HRPWM4_Config(period)
{
EPwm4Regs.TBCTL.bit.PRDLD = TB_IMMEDIATE;
EPwm4Regs.TBPRD = period-1;
EPwm4Regs.CMPA.half.CMPA = period / 2;
EPwm4Regs.CMPA.half.CMPAHR = (1 << 8);
EPwm4Regs.CMPB = 0;
EPwm4Regs.TBPHS.all = 0;
EPwm4Regs.TBCTR = 0;
EPwm4Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;
EPwm4Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm4Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;
EPwm4Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;
EPwm4Regs.TBCTL.bit.CLKDIV = TB_DIV1;
EPwm4Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
EPwm4Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;
EPwm4Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm4Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm4Regs.AQCTLA.bit.ZRO = AQ_CLEAR;
EPwm4Regs.AQCTLB.bit.ZRO = AQ_CLEAR;
EPwm4Regs.AQCTLB.bit.CBU = AQ_SET;
EALLOW;
EPwm4Regs.HRCNFG.all = 0x0;
EPwm4Regs.HRCNFG.bit.CTLMODE = HR_CMP;
EPwm4Regs.HRCNFG.bit.HRLOAD = HR_CTR_ZERO;
EDIS;
}
void Scan_Key(void)
{
unsigned int i;
for(i=0;i<10000;i++); //键盘消抖动
if(KEY1==0)
Manage_Key1();
else if(KEY2==0)
Manage_Key2();
while((KEY1==0)|(KEY2==0));
}
void Manage_Key1(void)
{
if(a==0)
{
a=0;
}
else a--;
}
void Manage_Key2(void)
{
if(a==100)
{
a=100;
}
else a++;
}
评论1