C51入門1,跑馬燈和LED實(shí)驗(yàn)
剛開始接觸的是C51,自己以前寫的程序,在自己的實(shí)驗(yàn)板上實(shí)現(xiàn)的,現(xiàn)在整理一下留做紀(jì)念哈?。?!
實(shí)驗(yàn)一:一個(gè)LED燈閃爍
//一個(gè)LED燈閃爍實(shí)驗(yàn)
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//頭文件
#include
//端口設(shè)置
sbit P10=P1^0;
char code dx516[3] _at_ 0x003b;
//主函數(shù)
void main(void)
{ uint i;
P10=0;
while(1)
{
P10=~P10;//取反
for (i=0;i<20000;i++);//延時(shí)
}
}
實(shí)驗(yàn)二:跑馬燈實(shí)驗(yàn)
//跑馬燈實(shí)驗(yàn)
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#include
//端口設(shè)置
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
char code dx516[3] _at_ 0x003b;
//主函數(shù)
void main(void)
{ uint i;
while(1)
{
P13=1;P10=0;//相鄰端口取反
for (i=0;i<40000;i++);//延時(shí)
P10=1;P11=0;
for (i=0;i<40000;i++);
P11=1;P12=0;
for (i=0;i<40000;i++);
P12=1;P13=0;
for (i=0;i<40000;i++);
}
}
實(shí)驗(yàn)三:一個(gè)按鍵控制一個(gè)LED的亮和滅
//一個(gè)按鍵控制LED
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#include
//端口設(shè)置
sbit P10=P1^0;
sbit K1=P3^2;
sbit K2=P3^5;
char code dx516[3] _at_ 0x003b;
//主函數(shù)
void main(void)
{
while(1)
{
if (!K1)//按一下
{
P10=0;//亮
}
if (!K2)//再按一下
{
P10=1;//滅
}
}
}
帶有去抖動(dòng)的程序:
//去抖動(dòng)的按鍵控制LED燈實(shí)驗(yàn)
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#include
//端口設(shè)置
sbit P10=P1^0;//LED燈
sbit K1=P3^2;//按鍵
char code dx516[3] _at_ 0x003b;
//主函數(shù)
void main(void)
{
bit light;
uint i;
while(1)
{
if (!K1)//確認(rèn)按鍵按下
{
for (i=0;i<1000;i++);//延時(shí)
light=~light;//燈取反
while(!K1);//去抖動(dòng)的作用
for (i=0;i<1000;i++);
}
P10=light;
}
}