单片机定时器学习
一、代码图片与仿真模拟图
#include <reg51.h>/*定义KEY*/
sbit KEY1 = P1^0;
sbit KEY2 = P1^1;void Delay100ms() //@12.000MHz延时100ms
{unsigned char i, j;i = 195;j = 138;do{while (--j);} while (--i);
}//判断KEY值并执行程序
void Judgefunction (unsigned char KEY1,unsigned char KEY2)
{unsigned char i;//常亮if (KEY1==1&&KEY2==1){P2=0x00;}//跑马灯if (KEY1==0){for(i=0;i<8;i++){P2=~(0x01<<i); Delay100ms();}}//闪烁if (KEY2==0){P2=0x00;Delay100ms();P2=0xFF;Delay100ms();}
}
//主函数
void main()
{while(1){Judgefunction (KEY1,KEY2);}
}
二、跑马灯与定时器
首先通过工具设计延时函数,注意选择晶振频率和单片机的频率设置
再在仿真中添加跑马灯