- #include<reg52.h>
- #include<intrins.h>
- #define uintunsigned int
- #define ucharunsigned char
- sbit pwm =P2^1;
- uintcount=0,timer1=0,value=0;
- bit DIR=1;
- void main()
- {
- TMOD=1;
- TH0=(65536-1)/256;
- TL0=(65536-1)%256;
- EA=1;//开总中断
- ET0=1;//开定时器0中断
- TR0=1;//TR0置位,启动定时器
- while(1)
- {
- if(timer1>value)
- {
- pwm=1;
- }
- else
- {
- pwm=0;
- }
- }
- }
- void time0()interrupt 1 using 1
- {
- TH0=(65536-1)/256;
- TL0=(65536-1)%256;
- timer1++;//timer1不是周期吗?却一直在变.
- count++;
- if(timer1>700)
- {
- timer1=0;
- }
- if(count>100)//为什么count要大于100才开始value的计数
- {
- count=0;
- if(DIR==1)
- {
- value++;
- }
- if(DIR==0)
- {
- value--;
- }
- }
- if(value==700)
- {
- value=0;
- }
- }
|