#include <reg52.h>
#include <function.h>//详见第六章第8讲
#include <timer.h> //详见第八章第11讲
#include <lcd.h> //详见第十一章第3讲
#include <adc.h> //详见第十四章
sbit PWMOUT=P1^7;
u8 flag20ms = 0;
u8 PWM_VAL = 0;
void
main()
{
u8 val;
u8 str[10];
EA = 1;
InitLcd1602();
TIM1_Init(20000,6);
TIM0_Init(100,9);
LcdShowStr(6, 0,
"AIN0"
);
while
(1)
{
if
(flag20ms)
{
flag20ms = 0;
val = GetADCValue(0);
ValueToString(str, val);
LcdShowStr(6, 1, str);
PWM_VAL=val/14+5;
}
}
}
void
TIM0_IRQHandler() interrupt 1
{
static
u8 pwm=0;
TH0 = T0RH;
TL0 = T0RL;
pwm++;
if
(pwm>=200)pwm=0;
if
(pwm<PWM_VAL)PWMOUT=1;
else
PWMOUT=0;
}
void
TIM1_IRQHandler() interrupt 3
{
TH1 = T1RH;
TL1 = T1RL;
flag20ms = 1;
}