你这不是中断优先级的问题,是程序结构的问题,给你改了,你试试。
- #include <reg52.h>
- #define u8 unsigned char
- #define u16 unsigned int
- sbit PWM = P2^1;
- sbit KEY4 = P3^3;
- sbit led1 = P2^5;
- sbit led2 = P2^6;
- sbit led3 = P2^7;
- u8 mode = 0;
- u8 gduty;
- void InitTimer0(void)
- {
- TMOD = 0x01;
- TH0 = 0xFC;
- TL0 = 0x18;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void Delay_ms(u16 t)
- {
- u16 i,j;
- for(i=t;i>0;i--)
- for(j=110;j>0;j--);
- }
- void main()
- {
- InitTimer0();
- while(1)
- {
- if(!KEY4)
- {
- Delay_ms(10);
- if(!KEY4)
- {
- mode = ++mode%4;
- switch(mode)
- {
- case 0: led1=1;led2=1;led3=1;gduty=0;break;//占空比%100
- case 1: led1=0;led2=0;led3=0;gduty=2;break;//占空比%50
- case 2: led1=0;led2=0;led3=1;gduty=3;break;//占空比%25
- case 3: led1=0;led2=1;led3=1;gduty=4;break;//占空比%0
- }
- while(!KEY4);
- }
- }
- }
- }
- void Timer0Interrupt(void) interrupt 1
- {
- static u8 time = 0;
- TH0 = 0xFC;
- TL0 = 0x18;
- time++;
- if(time >= 4)
- time = 0;
- if(time >= gduty) //占空比
- PWM = 0;
- else
- PWM = 1;
- }
复制代码 |