【MCS-51】【C51】LED单灯PWM调光

        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函数在本程序中未使用,请不必关心

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值