此内容由EEWORLD论坛网友姜先生、初一原创,如需转载或用于商业用途需征得作者同意并注明出处
题目要求:不得使用 数组 以及 延时函数,用 定时器
#include <STC12C5A60S2.H>//该款单片机的头文件
unsigned int Count_disturb = 0;//记录中断次数
unsigned int model = 0;//switch语句中事件变量
unsigned char i = 0, j = 0;//双循环控制变量
void Int_T0(void) interrupt 1//中断函数
{
TH0 = -9;
Count_disturb++;
}
void main()//主函数
{
P0M0 = 0xff;//推挽输出,加强电流
TMOD = 0x01;//定时器0选用工作方式1,16位软件重装载初值方式
TH0 = -9;//默认TL0 = 0,注意:寄存器只能存 非负数,此处编译后,自动转化为TH0 = 65536 - 9
ET0 = 1;//以下三行均为启动 定时器 的固定操作
EA = 1;
TR0 = 1;
while (1)//主循环
{
switch (model)
{
case 0:
if (Count_disturb == 100)//数字可任意改变,中断一次为2.5ms
{
P0 = 0x01 << i++;//一般流水灯
i = i & 7;//一共8个灯,& 7 为了让8个灯依次点亮后 重复流水
Count_disturb = 0;//赋0,进入下一次计时
j++;//进入下一次循环的条件j == 7,由此确立,可任意更改数值
}
break;
//以下各事件处理 与 事件0 类似
case 1:
if (Count_disturb == 100)
{
P0 = 0x80 | (0x01 << i++);
if (i == 6)
{
j = 7;
}
i = i & 7;
Count_disturb = 0;
}
break;
case 2:
if (Count_disturb == 100)
{
P0 = 0xC0 | (0x01 << i++);
if (i == 5)
{
j = 7;
}
i = i & 7;
Count_disturb = 0;
}
break;
case 3:
if (Count_disturb == 100)
{
P0 = 0xE0 | (0x01 << i++);
if (i == 4)
{
j = 7;
}
i = i & 7;
Count_disturb = 0;
}
break;
case 4:
if (Count_disturb == 100)
{
P0 = 0xF0 | (0x01 << i++);
if (i == 3)
{
j = 7;
}
i = i & 7;
Count_disturb = 0;
}
break;
case 5:
if (Count_disturb == 100)
{
P0 = 0xF8 | (0x01 << i++);
i = i & 7;
Count_disturb = 0;
if (i == 2)
{
j = 7;
}
}
break;
case 6:
if (Count_disturb == 100)
{
P0 = 0xFC | (0x01 << i++);
i = i & 7;
Count_disturb = 0;
if (i == 1)
{
j = 7;
}
}
break;
case 7:
if (Count_disturb == 100)
{
P0 = 0xFE;
Count_disturb = 0;
j = 7;
}
break;
case 8:
if (Count_disturb == 100)
{
P0 = 0xFF;
Count_disturb = 0;
j = 7;
}
break;
case 9:
do
{
if (Count_disturb == 100)
{
P0 = ~P0;
Count_disturb = 0;
i++;
}
} while (i <= 4);
j = 7;
i = 0;
break;
}
//第二重循环
if (j == 7)
{
model++;
j = 0;
if (model == 10)
{
model = 0;
}
}
}
}
引用: zwk34 发表于 2019-11-19 08:20 谢谢分享,要是加个简单的注释就更好了,便于初学者借鉴。
好的,谢谢建议,已经添加
引用: 姜先生、初一 发表于 2019-11-19 11:52 好的,谢谢建议,已经添加
呵呵,热心人呀。