stc89c52,,双PWM

95 篇文章 5 订阅
#include <reg52.h>

sbit LPWM0 = P1^2;
sbit LPWM1 = P1^3;
sbit RPWM0 = P1^4;
sbit RPWM1 = P1^6;
unsigned char flag1S=0;
//全局变量
unsigned int LPwm0Cnt=0;
unsigned int LPwm1Cnt=0;
unsigned int LPWM0Set=100;
unsigned int LPWM1Set=100;

unsigned int RPwm0Cnt=0;
unsigned int RPwm1Cnt=0;
unsigned int RPWM0Set=100;
unsigned int RPWM1Set=100;

void delay(int tms)   //20ms
{
	int i,j;
	for(i=0;i<tms;i++)
	{
	  for(j=0;j<5;j++);
	}
}

void main()
{
		TMOD=0x01; //定时器 0 工作在模式 1,16 位定时模式
		TH0=(65535-100)/256; //定时 10us
		TL0=(65535-100)%256;
		ET0=1;//打开定时器0中断开关
		EA=1;//打开总中断开关
		TR0=1;//打开定时器
	
	while(1)
	{
	    LPWM0Set =0;
			//LPWM1Set =25;
			delay(5);//d20--103ms  d10--53ms
		  LPWM0Set =25;
			//LPWM1Set =0;
			delay(5);
	}
    
			
	
		while(1)
    { //正转 

			LPWM0Set =0;
			LPWM1Set =25;
			
			RPWM0Set =0;
			RPWM1Set =25;
			delay(5);
			
			LPWM0Set =0;
			LPWM1Set =0;
			
			RPWM0Set =0;
			RPWM1Set =0;
			delay(5);
			
			
			LPWM0Set =95;
			LPWM1Set =0;
			
			RPWM0Set =95;
			RPWM1Set =0;
			delay(5);
			
			
			LPWM0Set =0;
			LPWM1Set =0;
			
			RPWM0Set =0;
			RPWM1Set =0;
			delay(5);
			
			}
}
/* 定时器0中断函数 */
void tm0_isr() interrupt 1 using 1
{
    
		TH0=0xFF;//10us 进来一次中断
		TL0=0xCC;
	  LPwm0Cnt++;
		LPwm1Cnt++;
		RPwm0Cnt++;
		RPwm1Cnt++;

		//决定PWM的周期,也就是频率,
		if(LPwm0Cnt>=50)
		{
			LPwm0Cnt=0;
		}
		if(LPwm1Cnt>=50)
		{
			LPwm1Cnt=0;
		}
		//输出PWM
		if(LPwm0Cnt<LPWM0Set)//1s=1000ms 20ms
		{
			LPWM0=1;
		}
		else
		{
			LPWM0=0;
		}
		//输出PWM
		if(LPwm1Cnt<LPWM1Set)//1s=1000ms 20ms
		{
			LPWM1=1;
		}
		else
		{
			LPWM1=0;
		}
		
			//决定PWM的周期,也就是频率,
		if(RPwm0Cnt>=50)
		{
			RPwm0Cnt=0;
		}
		if(RPwm1Cnt>=50)
		{
			RPwm1Cnt=0;
		}
		//输出PWM
		if(RPwm0Cnt<RPWM0Set)//
		{
			RPWM0=1;
		}
		else
		{
			RPWM0=0;
		}
		//输出PWM
		if(RPwm1Cnt<RPWM1Set)//
		{
			RPWM1=1;
		}
		else
		{
			RPWM1=0;
		}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值