#include <stc8a8kd4.h>//代替reg52.h
void HPWM_Init();
void HPWM_Set(char PWMchannel, unsigned int zkb);
void PortMode()
{
P0M1 = 0; P0M0 = 0; //设置为准双向口
P1M1 = 0; P1M0 = 0; //设置为准双向口
P2M1 = 0; P2M0 = 0; //设置为准双向口
P3M1 = 0; P3M0 = 0; //设置为准双向口
P4M1 = 0; P4M0 = 0; //设置为准双向口
P5M1 = 0; P5M0 = 0; //设置为准双向口
P6M1 = 0; P6M0 = 0; //设置为准双向口
P7M1 = 0; P7M0 = 0; //设置为准双向口
}
void Delay100ms() //@11.0592MHz
{
unsigned char i, j, k;
i = 6;
j = 157;
k = 59;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
//*****************************************main
void main()
{
PortMode();
HPWM_Init();
HPWM_Set(2, 0);
HPWM_Set(7, 0);
while(1)
{
HPWM_Set(2, 100);
HPWM_Set(7, 700);
Delay100ms();
HPWM_Set(2, 800);
HPWM_Set(7, 200);
Delay100ms() ;
}
}
///
void HPWM_Init()
{
PWMSET=0x01;
//0:是扩展RAM,XRAM
P_SW2=0x80;//必须加这句,将PSW2寄存器的bit7位置1
PWMCKS=0x00;//设置PWM周期=系统时钟=11.0592Mhz;
PWMC=1000; //设置PWM周期为1000个PWM时钟、、大概11K频率
//假设周期为T=cycle*clock,clock可以是系统分频后的脉冲,也可以是定时器2溢出时钟
// |------|
// ____| |_____
PWM2T1 = 0x0100; //100H的地方输出低电平
PWM2T2 = 0x0500; //当计数到500H的时候,输出高电平
PWM7T1 = 0x0100; //100H的地方输出低电平
PWM7T2 = 0x0500; //当计数到500H的时候,输出高电平
PWM2CR = 0x90; //端口切换,P62,1001 0000
PWM7CR = 0x90; //P67输出PWM,1001 0000
P_SW2 =0x00;
PWMCFG = 0x01;
}
//******************************************
/
void HPWM_Set(char PWMchannel, unsigned int zkb)
{
unsigned int CycleTime;
PWMSET=0x01;
P_SW2=0x80;
if(zkb>950)zkb=950;//约定最大
switch(PWMchannel)
{
case 2:
PWM2T1 =zkb; //
PWM2T2 = 1;//当计数到CYCLE的时候,会归0
break;
case 7:
PWM7T1 = zkb;//当计数到CYCLE的时候,会归0
PWM7T2 = 1; //
break;
default:break;
}
P_SW2=0x00;
PWMCFG = 0x01;
}
8kd4pwm
最新推荐文章于 2024-09-11 16:57:23 发布