打印

关于pwm原理实现渐变的led灯求助!!!

[复制链接]
2685|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
lc7745166|  楼主 | 2011-2-19 19:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一下是我写的程序,调试通过,但是实现不了!!!
main.c:
#include<reg52.h>
#include"diy.h"
#include"timer.h"
void main()
{
timeinit();
while(1)
{
  

}

}


timer.c:

#include<reg52.h>
#include"diy.h"
#define LED_PWM_MAX 166
#define LED_PWM_MIN 0
#define LED P1
#define LED_ON LED=0X00
#define LED_OFF LED=0XFF
static uint8 Time_Counter=0;
static uint8 LED_Direction=0;
static uint8 LED_PWM_Counter=0;
void timeinit()
{
TMOD=0X01;
TH0=(65535-200)/256;
TL0=(65535-200)%256;
EA=1;
ET0=1;
TR0=1;
}
void time0() interrupt 1
{
static uint8 PWM_Counter=0;
TH0=(65535-200)/255;
TL0=(65535-200)%255;
if((LED_Direction==0)&&(++Time_Counter>=50))
{
  Time_Counter=0;
  
  if(LED_PWM_Counter>LED_PWM_MAX)
  {
   LED_PWM_Counter=LED_PWM_MAX;
   LED_Direction=1;
  
  }
  if(LED_PWM_Counter<=LED_PWM_MAX)
  {
   LED_PWM_Counter++;
  
  }
  
}
if((LED_Direction==1)&&(++Time_Counter>=50))
{
   Time_Counter=0;
   if(LED_PWM_Counter>=LED_PWM_MIN)
   {
    LED_PWM_Counter--;
   
   }
   if(LED_PWM_Counter<LED_PWM_MIN)
   {
    LED_PWM_Counter=LED_PWM_MIN;
   LED_Direction=0;
     
   
   }

}
  PWM_Counter=LED_PWM_Counter;
  if(PWM_Counter>0)
  {
    LED_ON;
   PWM_Counter--;
     
  }
  else
  {
   LED_OFF;
  }
}

diy.h:

#ifndef _DIY_H_
#define _DIY_H_
typedef unsigned char uint8;
typedef unsigned int uint16;
#endif

timer.h

#ifndef _TIMER_H_
#define _TIMER_H_
extern void timeinit();
#endif

还请各位指教,不胜感激!!!

相关帖子

沙发
coody| | 2011-2-20 16:39 | 只看该作者
PWM驱动LED灰度最简单容易的就是分场扫描。

比如做8~16个LED的256级灰度PWM控制,一个定时器就可以了。

如果MCU速度足够快或者使用硬件SPI,则用HC595可以驱动超过100个LED的灰度。只需要3个IO

使用特权

评论回复
板凳
触觉的爱| | 2011-2-20 20:44 | 只看该作者
TMOD=0X01;
TH0=(65535-200)/256;
TL0=(65535-200)%256;
EA=1;
ET0=1;
TR0=1;
}
void time0() interrupt 1
{
static uint8 PWM_Counter=0;
TH0=(65535-200)/255;
TL0=(65535-200)%255;
:)

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

8

帖子

1

粉丝