|
发表于 2022-11-6 04:59:25
|
显示全部楼层
- #include <reg52.h>
- sbit LED1 = P3^6;
- sbit LED2 = P3^7;
- unsigned char x,y; //这里三个x,y,z 用于控制后面的PWM频率的;
- unsigned char z = 10;
- void delay(unsigned int a) //延迟;
- {
- unsigned int i,j;
- while(--a)
- {
- i = 2;
- j = 199;
- while(--i)
- {
- while(--j);
- }
- }
- }
- void timer0Init()
- {
- TMOD = 0x02; // 模式 2;
- TH0 = 0xFF;
- TL0 = 0xFF;
- TR0 = 1; //开打内部定时器,1 = Enable, 0 = Disable;
- ET0 = 1; //打开T0中断,1 = Enable, 0 = Disable;
- EA = 1;
- }
- void rf() //这里,让x值爬上去,再让x值掉下来,给后面中断使用;
- {
- for(x=0;x<z;x++)
- {
- delay(2);
- }
- EA = 0; //这里起共5行,让灯灭一下,看起来效果好点点。
- LED1 = 1;
- LED2 = 1;
- delay(200);
- EA = 1;
- for(x=z;x>0;x--)
- {
- delay(2);
- }
- delay(10);
- }
- void main()
- {
- timer0Init();
- delay(10);
- while(1)
- {
- rf();
- }
- }
- void timer0() interrupt 1
- {
- y++;
- if(y<=x){LED1 = 1;LED2 = 1;}
- else{LED1 = 0;LED2 = 0;y = 0;}
- }
复制代码 |
|