- #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;
- }
- }
|