PWM怎么写进去
#include <stc15.h>
#include <intrins.h>
#include "onewire.h"
unsigned char a[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0x7f,0xbf,0xff,0xc6};
unsigned char b[8]={0},n=1;
char dao=50;
unsigned int count=0;
unsigned int count2=0;
unsigned char su=1;
unsigned char temp;
bit flag = 0;
void BUZZ()//初始化
{
P2=(P2&0x1f)|0x80;
P0=0xff;
P2=(P2&0x1f)|0xc0;
P0=0xff;
P2=(P2&0x1f)|0xe0;
P0=0xff;
P2=(P2&0x1f)|0xa0;
P0=0x00;
}
void SMG()//数码管 段选 位选
{
static int i=0;
P2=(P2&0x1f)|0xc0;
P0=0x01<<i;
P2&=0x1f;
P2=(P2&0x1f)|0xe0;
P0=a[b[i]];
P2&=0x1f;
i=(i+1)%8;
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;//自己写的//======
EA = 1;
}
void Delay2ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 22;
j = 128;
do
{
while (--j);
} while (--i);
}
void SMGxianshi()//数码管显示
{
if(n==1)
{
b[0]=17;
b[1]=1;
b[2]=17;
b[3]=18;
b[4]=0;
b[5]=dao/100;
b[6]=dao%100/10;
b[7]=dao%10;
}
else if(n==2)
{
b[0]=17;
b[1]=2;
b[2]=17;
b[3]=18;
b[4]=0;
b[5]=dao/100;
b[6]=dao%100/10;
b[7]=dao%10;
}
else if(n==3)
{
b[0]=17;
b[1]=3;
b[2]=17;
b[3]=18;
b[4]=0;
b[5]=dao/100;
b[6]=dao%100/10;
b[7]=dao%10;
}
}
void SMGxianshi2()
{
P2=(P2&0x1f)|0x80;
P0=0Xff;
b[0]=17;
b[1]=4;
b[2]=17;
b[3]=18;
b[4]=18;
b[5]=temp/10%10;
b[6]=temp%10;
b[7]=19;
}
void Read_Temp()
{
unsigned char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay2ms();
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
Delay2ms();
LSB = Read_DS18B20();
MSB = Read_DS18B20();
temp = (MSB << 8) | LSB;
if((temp & 0xf000) == 0x0000)
{
temp >>= 4;
}
}
void Delay20ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 1;
j = 216;
k = 35;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void key()//按键
{
if(P33==0)
{
Delay20ms();
if(P33==0)
{
n++;
dao=50;
if(n>3)
{
n=1;
}
}
}
while(P33==0);
if(P32==0)
{
Delay20ms();
if(P32==0)
{
switch(su)
{
case 1 :dao=0;su=2; break;
case 2 :dao=60;su=3;break;
case 3 :dao=120;su=1;break;
}
}
}
while(P32==0);
if(P31==0)
{
Delay20ms();
if(P31==0)
{
dao=0;
}
}
while(P31==0);
if(P30==0)
{
Delay20ms();
if(P30==0)
{
flag = ~ flag;
}
}while(P30 == 0);
}
void ServiceTimer0()interrupt 1//定时服务函数
{
if(flag == 0)
{
SMG();
SMGxianshi();
}
else if(flag == 1)
{
SMG();
SMGxianshi2();
}
count++;
if(count%1000==0)
{
dao--;
if(dao<0)
{
dao=0;
}
}
}
void main()
{
BUZZ();
Timer0Init();
while(1)
{
Read_Temp();
key();
}
}