新手看了《手把手教你学51单片机》仿着写的下到板子 小灯常亮
端口没有问题 找了两天不知道问题出在哪里 来求助了
#include<reg52.h>
sbit LED=P2^7;
unsigned char H_RH=0;
unsigned char H_RL=0;
unsigned char L_RH=0;
unsigned char L_RL=0;
void ConfigPWM(unsigned int FR,unsigned char DC);
void AdjustDC(unsigned char dc);
void main()
{
EA=1;
ConfigPWM(100,10);
TH1=0x3C;
TL1=0xBC;
ET1=1;
TR1=1;
while(1);
}
void ConfigPWM(unsigned int FR,unsigned char DC)
{
unsigned long per;
unsigned int high,low;
per=(12000000/12)/FR;
high=(per*DC)/100;
low=per-high;
high=65536-high+12;
low=65536-low+12;
H_RH=(unsigned char)(high>>8);
H_RL=(unsigned char)high;
L_RH=(unsigned char)(low>>8);
L_RL=(unsigned char)low;
TMOD&=0x0F;
TMOD|=0x10;
TMOD&=0xF0;
TMOD|=0x01;
TH0=H_RH;
TL0=H_RL;
ET0=1;
TR0=1;
LED=1;
}
void AdjustDC(unsigned char dc)
{
unsigned int high,low;
high=(10000*dc)/100;//10000=((12000000/12)/100)
low=10000-high;
high=65536-high+12;
low=65536-low+12;
H_RH=(unsigned char)(high>>8);
H_RL=(unsigned char)high;
L_RH=(unsigned char)(low>>8);
L_RL=(unsigned char)low;
}
void interruptTimer0() interrupt 1
{
if(LED==1)
{
TH0=L_RH;
TL0=L_RL;
LED=0;
}
else
{
TH0=H_RH;
TL0=H_RL;
LED=1;
}
}
void interruptTimer1() interrupt 3
{
unsigned char code DC[13]={5, 18, 30, 41, 51, 60, 68, 75, 81, 86, 90, 93, 95};
static unsigned char i=0;
static bit flag=0;
TH1=0x3C;
TL1=0xBC;
AdjustDC(DC[i]);
if(flag==0)
{
i++;
if(i==12){flag=1;}
}
else
{
i--;
if(i==0){flag=0;}
}
}
|