通过这个程序来了解基础的单片机语言
单片机源程序如下:
- /*
- 呼吸灯
- 灯在P3.0引脚
- */
- #include <REG51.h>
- sbit Pin = P3^0;
- void Delay(unsigned int a)
- {
- while( a-- != 0)
- {
- }
- }
- main()
- {
- int i, delaytime;
- bit mark = 0;
- int bright = 0;
- while(1)//for(;;)
- {
- if(bright == 0)
- Delay(50000); //灭到尽保持500ms
- if(bright == 255 || bright == 0)
- mark = !mark; //亮暗标志,1逐渐变亮,0逐渐变暗
- if(mark == 1)
- {
- bright++;
- delaytime = 9;
- }
- else
- {
- bright--;
- delaytime = 4;
- }
- for(i = 0; i < 255; i++)
- {
- if(i < bright)
- {
- Pin = 0;
- Delay(delaytime);
- }
- else
- {
- Pin = 1;
- Delay(delaytime);
- }
- }
- }
- }
复制代码
所有资料51hei提供下载:
breath_lamp.rar
(12.84 KB, 下载次数: 19)
|