LED单灯PWM调光
申明:本程序绝对原创,请转载本博文注明出处:http://blog.csdn.net/fly928sky/article/details/7668673
1.本程序电路图,请参考http://blog.csdn.net/fly928sky/article/details/7645591
2.程序中PWM调光的LED灯为DS9
3.本程序相同流程对应A51程序,请参考http://blog.csdn.net/fly928sky/article/details/7668663
代码如下:
1.regs.h
#ifndef _REGS_H
#define _REGS_H
#include <reg52.h>
#include <intrins.h>
#define U8 unsigned char
#define U16 unsigned int
#define U32 unsigned long int
U8 code tableLightWater[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE};
U8 code tableFigureLamp[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,
0xFF,0x00,0x55,0xaa,0xFF,0x00,0x55,0xaa};
#endif
注意:其中tableLightWater[ ]和tableFigureLamp[ ]在本程序中未使用,请不必关心
2.led_pwm.c
#include "regs.h"
sbit Wei = P1^1;
sbit Duan = P1^0;
sbit Line = P1^3;
sbit LedEn = P1^2;
void init();
void delay(U16);
void delayMS(U16);
void main()
{
U16 PwmCycle,PwmLow;
PwmCycle=600;
PwmLow=0;
init();
LedEn=1;
while(1)
{
P0=0xFF;
delay(60000);
for(PwmLow=1;PwmLow<PwmCycle;PwmLow++) //逐渐变亮
{
P0=0x7F;
delay(PwmLow);
P0=0xFF;
delay(PwmCycle-PwmLow);
}
P0=0x7F;
for(PwmLow=PwmCycle-1;PwmLow>1;PwmLow--) //逐渐变暗
{
P0=0x7F;
delay(PwmLow);
P0=0xFF;
delay(PwmCycle-PwmLow);
}
}
}
void init() //初始化函数
{
Wei=1; //关闭数码管
P0=0xFF;
Wei=0;
Line=1; //关闭点阵
P0=0x00;
Line=0;
}
void delay(U16 t)
{
while(--t);
}
void delayMS(U16 z) //延时函数
{
U16 x,y;
for(x=z;x>0;x--)
for(y=124;y>0;y--);
}
注意:其中delayMS函数在本程序中未使用,请不必关心