#include <stc8.h> #define c 30 void delay(unsigned int ms) { unsigned int i,j; for(i=ms;i>0;i--) { for(j=147;j>0;j--); } } void pwm(unsigned char k) { unsigned char n; for(n=0;n<c;n++) { P3=0X00; delay(k); P3=0XFF; delay(c-k); } } void main() { //³õʼ»¯´úÂë P3 = 0x00; //ÉèÖÃÖÐ¶Ï EA = 1; while (1) { //Ö÷Ñ»·´úÂë unsigned char t; for (t = 1; t < c; t++) { pwm(t); } for (t = c - 1; t > 0; t--) { pwm(t); } delay(2000); } } 优化一下代码
时间: 2024-04-08 13:32:41 浏览: 173
STC8 寄存器头文件
#include <stc8.h>
void delay(unsigned int ms)
{
unsigned int i, j;
for (i = ms; i > 0; i--)
{
for (j = 147; j > 0; j--);
}
}
void pwm(unsigned char k)
{
unsigned char n;
for (n = 0; n < k; n++)
{
P3 = 0x00;
delay(k);
P3 = 0xFF;
delay(c - k);
}
}
void main()
{
// 初始化引脚
P3 = 0x00;
// 设置中断
EA = 1;
while (1)
{
unsigned char t;
// 主循环
for (t = 1; t < c; t++)
{
pwm(t);
}
for (t = c - 1; t > 0; t--)
{
pwm(t);
}
delay(2000);
}
}
阅读全文