各位单片机高手:请教一个单片机控制发光二极管亮度的问题
我现在所用的芯片是AT89C2051芯片,发光二极管接在芯片的p1口,P1.0未接,共七个发光二极管,我想通过软件控制二极管的亮度渐变,不知道有没有办法实现,怎么样实现???
我找过很多关于PWM的调节,据说这个PWM能够实现我想要的,但本人不懂PWM的原理,所以也未能实现,请各们大哥多多指教!!
请教一个单片机控制发光二极管亮度的问题??
全部回复(15)
正序查看
倒序查看
现在还没有回复呢,说说你的想法
@gnlee
对于你来讲pwm调节就是调节再一个周期内led亮和灭的时间比
您好!我也刚学单片机,这是我用HT的单片机做的一个控制LED亮度的程序,由于我目前没有条件搭线,所以想请高手帮忙指点一下.
看看小弟还有那些不足的地方.呵呵我还是个学生呢.
#include ht46r47.inc
;Pb2控制占空比
;Pc1,Pc2输出PWM
;Pb1选择Pc1或Pc2
data.section 'data'
t0 db ?;The cycle of PWM pulse
t10 db ?;Open time
aaa db ?;Open time control
bbb db ?;Close time control
sov db ?;Open time end
bov db ?;close time end
code.section 'code'
org 00h
clr pbc
mov a,02h
mov pbc,a
jmp start
org 08h
jmp pwm
;+++++++++++++++++++++++++++++++++++++++++++++++
start:
mov a,3 ;cycle of PWM pulse
mov t0,a
mov aaa,a
mov a,1
set sov
clr bbb
clr t10
;+++++++++++++++++++++++++++++++++++++++++++++
main:
mov a,05h
mov intc,a
mov a,84h
mov tmrc,a;fsys=32768Hz,rtc,t0=fsys/32=0.5ms
mov a,0feh;The clock base this time
mov tmr,a
set tmrc.4
jmp $
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++
sz pb.1 ;判断键是否按下
jmp loop
call chou
loop:
sz sov
jmp next1
sz bov
jmp next2
jmp loop
next1:
inc t10
mov a,aaa
sub a,t10
mov sov,a
sz sov
reti
sz bov
jmp $1
set sov
jmp loop
$1:
cpl pc
set bov
reti
next2:
inc t20
mov a,bbb
mov bov,a
sz bov
reti
sz sov
jmp $1
jmp loop
$1:
cpl pc
set sov
reti
;+++++++++++++++++++++++++++++++++++++++++++++++++
chou proc
sdz aaa
jmp $1
jmp start
$1:
mov a,t0
sub a,aaa
mov bbb,a
ret
chou endp
end
看看小弟还有那些不足的地方.呵呵我还是个学生呢.
#include ht46r47.inc
;Pb2控制占空比
;Pc1,Pc2输出PWM
;Pb1选择Pc1或Pc2
data.section 'data'
t0 db ?;The cycle of PWM pulse
t10 db ?;Open time
aaa db ?;Open time control
bbb db ?;Close time control
sov db ?;Open time end
bov db ?;close time end
code.section 'code'
org 00h
clr pbc
mov a,02h
mov pbc,a
jmp start
org 08h
jmp pwm
;+++++++++++++++++++++++++++++++++++++++++++++++
start:
mov a,3 ;cycle of PWM pulse
mov t0,a
mov aaa,a
mov a,1
set sov
clr bbb
clr t10
;+++++++++++++++++++++++++++++++++++++++++++++
main:
mov a,05h
mov intc,a
mov a,84h
mov tmrc,a;fsys=32768Hz,rtc,t0=fsys/32=0.5ms
mov a,0feh;The clock base this time
mov tmr,a
set tmrc.4
jmp $
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++
sz pb.1 ;判断键是否按下
jmp loop
call chou
loop:
sz sov
jmp next1
sz bov
jmp next2
jmp loop
next1:
inc t10
mov a,aaa
sub a,t10
mov sov,a
sz sov
reti
sz bov
jmp $1
set sov
jmp loop
$1:
cpl pc
set bov
reti
next2:
inc t20
mov a,bbb
mov bov,a
sz bov
reti
sz sov
jmp $1
jmp loop
$1:
cpl pc
set sov
reti
;+++++++++++++++++++++++++++++++++++++++++++++++++
chou proc
sdz aaa
jmp $1
jmp start
$1:
mov a,t0
sub a,aaa
mov bbb,a
ret
chou endp
end
0
回复
提示
@dfstar
嘿嘿我做过一会,原理就是跟了3楼的你一样.控制一个周期内LED亮和灭的时间比.
OUTE BIT P1.7 ;单片机引脚定义
SDAT BIT P1.6
STRB BIT P1.5
SCLK BIT P1.4
ORG 0000H
AJMP MAIN
ORG 0080H
MAIN:
;============================
;============================
MOV P1,#0FFH
REDSHOW: ; 红色渐变
MOV R4,#80
ACALL COLOR1
ACALL LOOP0
ACALL COLOR1
ACALL LOOP2 ;将输入数据送入锁存器
DJNZ R4,$-8
ACALL START
ACALL LIGHTOUT
;============================
AJMP MAIN
START: ;计数器亮度控制函数
LCALL DELAY05S
CLR TF0
MOV R1,#LOW(65535-10000)
MOV R2,#HIGH(65535-10000)
MOV TL0,R1
MOV TH0,R2
MOV R3,#LOW(65535-1)
MOV R4,#HIGH(65535-1)
MOV TL1,R3
MOV TH1,R4
RET
LIGHTOUT: ;二极管亮度控制
LOOPREDB: ;;二极管由暗到暗明
SETB TR0
JNB TF0,$
CLR TR0
CLR TF0
CLR OUTE ;TIGER
SETB TR1
JNB TF1,$
CLR TR1
CLR TF1
SETB OUTE ;STOP
CLR C
MOV A,R1
ADD A,#4
MOV R1,A
MOV A,R2
ADDC A,#0
MOV R2,A
MOV TL0,R1
MOV TH0,R2
CLR C
MOV A,R3
SUBB A,#4
MOV R3,A
MOV A,R4
SUBB A,#0
MOV R4,A
MOV TL1,R3
MOV TH1,R4
CJNE R1,#0FFH,LOOPREDB
CJNE R2,#0FFH,LOOPREDB
CLR OUTE
LCALL DELAY2S
MOV TL0,R1
MOV TH0,R2
LOOPREDB1: ;;二极管由明到暗
SETB TR0
JNB TF0,$
CLR TR0
CLR TF0
CLR OUTE ;TIGER
SETB TR1
JNB TF1,$
CLR TR1
CLR TF1
SETB OUTE ;STOP
CLR C
MOV A,R1
SUBB A,#4
MOV R1,A
MOV A,R2
SUBB A,#0
MOV R2,A
MOV TL0,R1
MOV TH0,R2
CLR C
MOV A,R3
ADD A,#2
MOV R3,A
MOV A,R4
ADDC A,#0
MOV R4,A
MOV TL1,R3
MOV TH1,R4
CJNE R1,#0EFH,LOOPREDB1
CJNE R2,#0D8H,LOOPREDB1
SETB OUTE
RET
;======================================
COLOR0: ;不亮
ACALL LOOP0
ACALL LOOP0
ACALL LOOP0
RET
COLOR1: ;红色
ACALL LOOP0
ACALL LOOP0
ACALL LOOP1
RET
COLOR2: ;绿色
ACALL LOOP0
ACALL LOOP1
ACALL LOOP0
RET
COLOR3: ;蓝色
ACALL LOOP1
ACALL LOOP0
ACALL LOOP0
RET
COLOR4:
ACALL LOOP0
ACALL LOOP1
ACALL LOOP1
RET
COLOR5:
ACALL LOOP1
ACALL LOOP0
ACALL LOOP1
RET
COLOR6:
ACALL LOOP1
ACALL LOOP1
ACALL LOOP0
RET
COLOR7:
ACALL LOOP1
ACALL LOOP1
ACALL LOOP1
RET
;================================
LOOP0: ;输入数据
CLR SDAT
CLR SCLK
SETB SCLK
RET
LOOP1:
SETB SDAT
CLR SCLK
SETB SCLK
RET
LOOP2:
CLR STRB
SETB STRB
RET
;==================================
delay:
mov r7,#10
mov r6,#10
mov r5,#10
djnz r5,$
djnz r6,$-2
djnz r7,$-4
ret
;==============================
DELAY05S:
MOV R5,#3 ;4
MOV R6,#250 ;230
MOV R7,#250
DJNZ R7,$
DJNZ R6,$-4
DJNZ R5,$-6
RET
DELAY1S:
MOV R5,#3 ;4
MOV R6,#250 ;230
MOV R7,#250
DJNZ R7,$
DJNZ R6,$-4
DJNZ R5,$-6
RET
;===================
DELAY2S:
MOV R5,#10 ;4
MOV R6,#250 ;230
MOV R7,#250
DJNZ R7,$
DJNZ R6,$-4
DJNZ R5,$-6
RET
;===================
END
;============================
SDAT BIT P1.6
STRB BIT P1.5
SCLK BIT P1.4
ORG 0000H
AJMP MAIN
ORG 0080H
MAIN:
;============================
;============================
MOV P1,#0FFH
REDSHOW: ; 红色渐变
MOV R4,#80
ACALL COLOR1
ACALL LOOP0
ACALL COLOR1
ACALL LOOP2 ;将输入数据送入锁存器
DJNZ R4,$-8
ACALL START
ACALL LIGHTOUT
;============================
AJMP MAIN
START: ;计数器亮度控制函数
LCALL DELAY05S
CLR TF0
MOV R1,#LOW(65535-10000)
MOV R2,#HIGH(65535-10000)
MOV TL0,R1
MOV TH0,R2
MOV R3,#LOW(65535-1)
MOV R4,#HIGH(65535-1)
MOV TL1,R3
MOV TH1,R4
RET
LIGHTOUT: ;二极管亮度控制
LOOPREDB: ;;二极管由暗到暗明
SETB TR0
JNB TF0,$
CLR TR0
CLR TF0
CLR OUTE ;TIGER
SETB TR1
JNB TF1,$
CLR TR1
CLR TF1
SETB OUTE ;STOP
CLR C
MOV A,R1
ADD A,#4
MOV R1,A
MOV A,R2
ADDC A,#0
MOV R2,A
MOV TL0,R1
MOV TH0,R2
CLR C
MOV A,R3
SUBB A,#4
MOV R3,A
MOV A,R4
SUBB A,#0
MOV R4,A
MOV TL1,R3
MOV TH1,R4
CJNE R1,#0FFH,LOOPREDB
CJNE R2,#0FFH,LOOPREDB
CLR OUTE
LCALL DELAY2S
MOV TL0,R1
MOV TH0,R2
LOOPREDB1: ;;二极管由明到暗
SETB TR0
JNB TF0,$
CLR TR0
CLR TF0
CLR OUTE ;TIGER
SETB TR1
JNB TF1,$
CLR TR1
CLR TF1
SETB OUTE ;STOP
CLR C
MOV A,R1
SUBB A,#4
MOV R1,A
MOV A,R2
SUBB A,#0
MOV R2,A
MOV TL0,R1
MOV TH0,R2
CLR C
MOV A,R3
ADD A,#2
MOV R3,A
MOV A,R4
ADDC A,#0
MOV R4,A
MOV TL1,R3
MOV TH1,R4
CJNE R1,#0EFH,LOOPREDB1
CJNE R2,#0D8H,LOOPREDB1
SETB OUTE
RET
;======================================
COLOR0: ;不亮
ACALL LOOP0
ACALL LOOP0
ACALL LOOP0
RET
COLOR1: ;红色
ACALL LOOP0
ACALL LOOP0
ACALL LOOP1
RET
COLOR2: ;绿色
ACALL LOOP0
ACALL LOOP1
ACALL LOOP0
RET
COLOR3: ;蓝色
ACALL LOOP1
ACALL LOOP0
ACALL LOOP0
RET
COLOR4:
ACALL LOOP0
ACALL LOOP1
ACALL LOOP1
RET
COLOR5:
ACALL LOOP1
ACALL LOOP0
ACALL LOOP1
RET
COLOR6:
ACALL LOOP1
ACALL LOOP1
ACALL LOOP0
RET
COLOR7:
ACALL LOOP1
ACALL LOOP1
ACALL LOOP1
RET
;================================
LOOP0: ;输入数据
CLR SDAT
CLR SCLK
SETB SCLK
RET
LOOP1:
SETB SDAT
CLR SCLK
SETB SCLK
RET
LOOP2:
CLR STRB
SETB STRB
RET
;==================================
delay:
mov r7,#10
mov r6,#10
mov r5,#10
djnz r5,$
djnz r6,$-2
djnz r7,$-4
ret
;==============================
DELAY05S:
MOV R5,#3 ;4
MOV R6,#250 ;230
MOV R7,#250
DJNZ R7,$
DJNZ R6,$-4
DJNZ R5,$-6
RET
DELAY1S:
MOV R5,#3 ;4
MOV R6,#250 ;230
MOV R7,#250
DJNZ R7,$
DJNZ R6,$-4
DJNZ R5,$-6
RET
;===================
DELAY2S:
MOV R5,#10 ;4
MOV R6,#250 ;230
MOV R7,#250
DJNZ R7,$
DJNZ R6,$-4
DJNZ R5,$-6
RET
;===================
END
;============================
0
回复
提示
@zrz2008
OUTEBITP1.7 ;单片机引脚定义SDATBITP1.6STRBBITP1.5SCLKBITP1.4ORG0000HAJMPMAINORG0080HMAIN:;============================;============================MOVP1,#0FFHREDSHOW: ;红色渐变MOV R4,#80ACALL COLOR1ACALL LOOP0ACALL COLOR1ACALL LOOP2 ;将输入数据送入锁存器 DJNZ R4,$-8ACALLSTARTACALLLIGHTOUT;============================AJMPMAINSTART: ;计数器亮度控制函数 LCALL DELAY05S CLR TF0 MOV R1,#LOW(65535-10000) MOV R2,#HIGH(65535-10000) MOV TL0,R1 MOV TH0,R2 MOV R3,#LOW(65535-1) MOV R4,#HIGH(65535-1) MOV TL1,R3 MOV TH1,R4RETLIGHTOUT: ;二极管亮度控制LOOPREDB: ;;二极管由暗到暗明 SETB TR0 JNB TF0,$ CLR TR0 CLR TF0 CLR OUTE ;TIGERSETB TR1JNB TF1,$ CLR TR1 CLR TF1 SETB OUTE ;STOP CLR C MOV A,R1 ADD A,#4 MOV R1,A MOV A,R2 ADDC A,#0 MOV R2,A MOV TL0,R1 MOV TH0,R2CLR C MOV A,R3 SUBB A,#4 MOV R3,A MOV A,R4 SUBB A,#0 MOV R4,A MOV TL1,R3 MOV TH1,R4 CJNE R1,#0FFH,LOOPREDB CJNE R2,#0FFH,LOOPREDB CLR OUTE LCALL DELAY2S MOV TL0,R1 MOV TH0,R2LOOPREDB1: ;;二极管由明到暗 SETB TR0 JNB TF0,$ CLR TR0 CLR TF0 CLR OUTE ;TIGERSETB TR1JNB TF1,$ CLR TR1 CLR TF1 SETB OUTE ;STOP CLR C MOV A,R1 SUBB A,#4 MOV R1,A MOV A,R2 SUBB A,#0 MOV R2,A MOV TL0,R1 MOV TH0,R2CLR C MOV A,R3 ADD A,#2 MOV R3,A MOV A,R4 ADDC A,#0 MOV R4,A MOV TL1,R3 MOV TH1,R4 CJNE R1,#0EFH,LOOPREDB1 CJNE R2,#0D8H,LOOPREDB1SETB OUTERET;======================================COLOR0: ;不亮ACALLLOOP0ACALLLOOP0ACALLLOOP0RETCOLOR1: ;红色ACALLLOOP0ACALLLOOP0ACALLLOOP1RETCOLOR2: ;绿色ACALLLOOP0ACALLLOOP1ACALLLOOP0RETCOLOR3: ;蓝色ACALLLOOP1ACALLLOOP0ACALLLOOP0RETCOLOR4:ACALLLOOP0ACALLLOOP1ACALLLOOP1RETCOLOR5:ACALLLOOP1ACALLLOOP0ACALLLOOP1RETCOLOR6:ACALLLOOP1ACALLLOOP1ACALLLOOP0RETCOLOR7:ACALLLOOP1ACALLLOOP1ACALLLOOP1RET;================================LOOP0: ;输入数据 CLR SDAT CLR SCLK SETB SCLKRETLOOP1: SETB SDAT CLR SCLK SETB SCLKRETLOOP2: CLR STRB SETB STRBRET;==================================delay:movr7,#10movr6,#10movr5,#10djnzr5,$djnzr6,$-2djnzr7,$-4ret;==============================DELAY05S: MOV R5,#3 ;4 MOV R6,#250 ;230 MOV R7,#250 DJNZ R7,$ DJNZ R6,$-4 DJNZ R5,$-6 RETDELAY1S: MOV R5,#3 ;4 MOV R6,#250 ;230 MOV R7,#250 DJNZ R7,$ DJNZ R6,$-4 DJNZ R5,$-6 RET;===================DELAY2S: MOV R5,#10 ;4 MOV R6,#250 ;230 MOV R7,#250 DJNZ R7,$ DJNZ R6,$-4 DJNZ R5,$-6 RET;===================END;============================
以上是我的程序,根据各位的意思,基本上是实现了,我十分高兴,不过还有一个小小的问题,二极管很闪,很剌眼,该怎么办
0
回复
提示
@lanpxr
您好!我也刚学单片机,这是我用HT的单片机做的一个控制LED亮度的程序,由于我目前没有条件搭线,所以想请高手帮忙指点一下.看看小弟还有那些不足的地方.呵呵我还是个学生呢.#includeht46r47.inc;Pb2控制占空比;Pc1,Pc2输出PWM;Pb1选择Pc1或Pc2data.section'data't0 db?;ThecycleofPWMpulset10db?;Opentimeaaadb?;Opentimecontrolbbbdb?;Closetimecontrolsovdb?;Opentimeendbovdb?;closetimeendcode.section'code'org 00hclr pbcmov a,02hmov pbc,ajmp startorg 08hjmp pwm;+++++++++++++++++++++++++++++++++++++++++++++++start:mov a,3 ;cycleofPWMpulsemov t0,amov aaa,amov a,1set sovclr bbbclr t10;+++++++++++++++++++++++++++++++++++++++++++++main:mov a,05hmov intc,amov a,84hmov tmrc,a;fsys=32768Hz,rtc,t0=fsys/32=0.5msmov a,0feh;Theclockbasethistimemov tmr,aset tmrc.4jmp $;+++++++++++++++++++++++++++++++++++++++++++++++++++++++sz pb.1 ;判断键是否按下jmp loopcallchouloop:sz sovjmp next1sz bovjmp next2jmp loopnext1:inc t10mov a,aaasub a,t10mov sov,asz sovretisz bovjmp $1set sovjmp loop$1:cpl pcset bovretinext2:inc t20mov a,bbbmov bov,asz bovretisz sovjmp $1jmp loop$1:cpl pcset sovreti;+++++++++++++++++++++++++++++++++++++++++++++++++chouprocsdz aaajmp $1jmp start$1:mov a,t0sub a,aaamov bbb,aretchouendpend
嘿嘿 小弟也做过一样的东东
原理就是大家所说的 也通过电路实现了
不过还是想看看大家怎么做的
我看我得程序比较幼稚 像大家学习
我copy 了你的程序 回家看看
我也是个新手 学生
请大家多多指教
原理就是大家所说的 也通过电路实现了
不过还是想看看大家怎么做的
我看我得程序比较幼稚 像大家学习
我copy 了你的程序 回家看看
我也是个新手 学生
请大家多多指教
0
回复
提示