谁有用51单片机一个定时器输出PWM脉冲 频率一定,占空比可调的吗,给个程序啊?
1个回答
展开全部
#include<reg52.h>
sbit P2_0=P2^0;
unsigned char n;
void main()
{
TMOD=0X01;
P2_0=0;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
ET0=1;
EA=1;
TR0=1;
while(1);
}
void t0_init() interrupt 1 using 1
{
//P2_0=!P2_0;
n++;
if(n==10)
n=0;
if(n==4)
P2_0=0;
if(n==0)
P2_0=1;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
}
;=============================================================
;版权所有,请勿复制 本程序最多可调节占空比的范围为1~255
;=============================================================
ORG 0000H
LJMP DEFINE
ORG 0100H
DEFINE:
MOV P0,#80H
MOV P1,#00H
MOV P2,#00H
MOV P3,#00H
MOV R1,#1
MAIN:
JNB P0.7,XIAODOU ;检测按键
XIAODOU:
LCALL DELAY1
JNB P0.7,SHANSHUO ;消抖,判断是否真正按下
MOV A,P1
CPL A
MOV P1,A ;P1口闪烁
MOV A,R1 ;按键次数传递给 R2
MOV R2,A
TWOYS: LCALL DELAY
DJNZ R2,TWOYS ;调用延时的次数为按键的次数 R2
AJMP MAIN
SHANSHUO:
MOV A,R1 ;按键每按下一次 ,累加器A自增1 结果放入R1中 记录按键次数
INC A
MOV R1,A
SONGSHOU: LCALL DELAY1 ;松手检测 没松手一直执行延时子程序
JNB P0.7,SONGSHOU
LJMP MAIN
RET
DELAY1:
MOV R4,#80
L3: MOV R5,#150
DJNZ R5,$
DJNZ R4,L3
RET
DELAY:
MOV R4,#250
L2: MOV R5,#250
DJNZ R5,$
DJNZ R4,L3
RET
END
sbit P2_0=P2^0;
unsigned char n;
void main()
{
TMOD=0X01;
P2_0=0;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
ET0=1;
EA=1;
TR0=1;
while(1);
}
void t0_init() interrupt 1 using 1
{
//P2_0=!P2_0;
n++;
if(n==10)
n=0;
if(n==4)
P2_0=0;
if(n==0)
P2_0=1;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
}
;=============================================================
;版权所有,请勿复制 本程序最多可调节占空比的范围为1~255
;=============================================================
ORG 0000H
LJMP DEFINE
ORG 0100H
DEFINE:
MOV P0,#80H
MOV P1,#00H
MOV P2,#00H
MOV P3,#00H
MOV R1,#1
MAIN:
JNB P0.7,XIAODOU ;检测按键
XIAODOU:
LCALL DELAY1
JNB P0.7,SHANSHUO ;消抖,判断是否真正按下
MOV A,P1
CPL A
MOV P1,A ;P1口闪烁
MOV A,R1 ;按键次数传递给 R2
MOV R2,A
TWOYS: LCALL DELAY
DJNZ R2,TWOYS ;调用延时的次数为按键的次数 R2
AJMP MAIN
SHANSHUO:
MOV A,R1 ;按键每按下一次 ,累加器A自增1 结果放入R1中 记录按键次数
INC A
MOV R1,A
SONGSHOU: LCALL DELAY1 ;松手检测 没松手一直执行延时子程序
JNB P0.7,SONGSHOU
LJMP MAIN
RET
DELAY1:
MOV R4,#80
L3: MOV R5,#150
DJNZ R5,$
DJNZ R4,L3
RET
DELAY:
MOV R4,#250
L2: MOV R5,#250
DJNZ R5,$
DJNZ R4,L3
RET
END
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询