一下是我写的程序,调试通过,但是实现不了!!!
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
还请各位指教,不胜感激!!! |