单片机C源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.流水灯

#include
void delay();
void main()
{
P1=0x0c;
P1_2=0;
P0=0;
while(1)
{ P0_4=1;
delay();
P0_4=0;
delay();

P0_5=1;
delay();
P0_5=0;
delay();

P0_6=1;
delay();
P0_6=0;
delay();

P0_7=1;
delay();
P0_7=0;
delay();

}
}
void delay()
{
unsigned int i=0,j=0;
for(i=1000;i>0;i--)
for(j=100;j>0;j--);


}



1.用函数实现流水灯

#include
#include
void delay()
{
unsigned char i,j;
for(i=500;i>0;i--)
for(j=500;j>0;j--);
}

void main()
{
unsigned char temp;
P0=0;
P1=0x0c;
P1_2=0;
temp=0x11;
P0=temp;
while(1)
{
delay();
delay();
delay();
temp=_crol_(temp,1);
P0=temp;

} }



1.用数组实现流水灯

#include
void delay();
void main()
{
P1=0x0c;
P1_2=0;
P0=0;
while(1)
{ int i;
unsigned char Tab[4]={0x10,0x20,0x40,0x80};
for(i=0;i<4;i++)
{ P0=Tab[i];
delay();
P0=Tab[i];
delay();
}
} }
void delay()
{
unsigned int i=0,j=0;
for(i=1000;i>0;i--)
for(j=100;j>0;j--);
}



2.显示学号

#include
void delay(unsigned int i);
void main()
{P0=0;P1=0x0c;
P0=0x7f;
P1_1=1;
P1_1=0;

P0=0x3f;
P1_0=1;
P1_0=0;
delay(1);

P0=0xbf;
P1_1=1;
P1_1=0;

P0=0x6f;
P1_0=1;
P1_0=0;
delay(1);

P0=0xdf;
P1_1=1;
P1_1=0;

P0=0x5b;
P1_0=1;
P1_0=0;
delay(1);

P0=0xef;
P1_1=1;
P1_1=0;

P0=0x06;
P1_0=1;
P1_0=0;
delay(1);

P0=0xf7;
P1_1=1;
P1_1=0;

P0=0x3f;
P1_0=1;
P1_0=0;
delay(1);

P0=0xfb;
P1_1=1;
P1_1=0;

P0=0x4f;
P1_0=1;
P1_0=0;
delay(1);

P0=0xfd;
P1_1=1;
P1_1=0;

P0=0x6d;
P1_0=1;
P1_0=0;
delay(1);

P0=0xfe;
P1_1=1;
P1_1=0;

P0=0x6f;
P1_0=1;
P1_0=0;
delay(1);
}

void delay(unsigned int i)
{unsigned int j;
for(i;i>0;i--)
for(j=124;j>0;j--);
}



3.按键加

#include

#define uint unsigned int
#define uchar unsigned char

sbit Key_ce=P1^3;
sbit Seg_ce=P1^0;
sbit Dig_ce=P1^1;

void display();
//延时函数
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=124;j>0;j--);
}
//共阴数码管显示段
uchar code Seg_tab[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
//主函数
void main()
{
P0=0;
P1=0x0c; //初始化
P0=0x0e;
Dig_ce=1;
Dig_ce=0; //第一行拉低
// P0=0x0f;

while(1)
{
display();
}

}
//显示函数
void display()
{
uint shi,ge,num;

Key_ce=0; //开键盘使能
P0=0;
Seg_ce=1;
Seg_ce=0; //清数码管显示 !
if(P0_0==0)
{
delay(50);
if(P0_0==0)
{
num++;
shi=num/10; //十位
ge=num%10; //个位
if(num==33)
{
num=0;
}
}




}

while(!P0_0);//松手检测

Key_ce=1;//关键盘
P0=Seg_tab[shi];
Seg_ce=1;
Seg_ce=0;
P0=0xfd;
Dig_ce=1;
Dig_ce=0;
delay(5);

P0=Seg_tab[ge];
Seg_ce=1;
Seg_ce=0;
P0=0xfe;
Dig_ce=1;
Dig_ce=0;
delay(5);
}



3.后四位显示矩阵键盘

#include
#include

#define uchar unsigned char
#define uint unsigned int

uchar temp1,temp2, keynum,dig;

uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

void main()
{
char i,j; //申明变量,一定要在主函数开头写!!!
P0 = 0;
P1 = 0x0c;
while(1)
{
dig = 0xfe;
for(i=0;i<4;i++)
{
P0 = dig;P1_1=1; P1_1=0;delay(1);
P0 = 0;P1_0=1; P1_0=0;delay(1);
P1_3=0;
temp1=P0;
temp1=temp1&0x0f;
if(temp1!=0x0f)
{
delay(10);
temp2=P0;
temp2=temp2&0x0f;
if(temp1==temp2)
{
j=i; //注意不要写反拉!!!!!

switch(temp2)
{
case 0x0e:keynum=0;break;
case 0x0d:keynum=1;break;
case 0x0b:keynum=2;break;
case 0x07:keynum=3;break;
default:break;
}
while(temp2!=0x0f)
{
temp2=P0;
temp2=temp2&0x0f;
}

}

}
P1_3 = 1;
P0 = table[keynum+j*4];
P1_0 = 1; P1_0 = 0;
delay(5);
dig = _crol_(dig,1);


}

}
}



3.加学号

#include

unsigned char seg_tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //段
unsigned char diq_tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //位
unsigned int num=0;

void delay(unsigned int x)
{
unsigned int i,j;
for(i=x;i>0;i--) ;
for(j=124;j>0;j--) ;
}

void display(unsigned int data_d )
{
unsigned int d1,d2;
d1=data_d/10;
d2=data_d%10;
P0=seg_tab[d1];
P1_0=1;
P1_0=0;
P0=diq_tab[1];
P1_1=1;
P1_1=0;
delay(10);
P0=seg_tab[d2];
P1_0=1;
P1_0=0;
P0=diq_tab[0];
P1_1=1;
P1_1=0;
delay(1);


}

void main()
{
P0=0;
P1=0x0c;
while(1)
{
P0=0;P1_0=1;P1_0=0;
P0=0x0e;P1_1=1;P1_1=0;
P1_3=0;
if(P0_0==0)
{
delay(10);
if(P0_0==0)
{
num++;
}
while(!P0_0)
{
delay(1);
while(!P0_0);
}
if(num==76)
{
num=0;
}
}
P1_3=1;
display(num);
}




3.键盘控制LED闪亮

#include
void delay(unsigned int i);
sbit led_ce=P1^2;
sbit dig_ce=P1^1;
sbit key_ce=P1^3;
void main()
{bit stat=0;
P0=0;
P1=0x0c;
led_ce=0;
while(1)
{P0_4=stat;
dig_ce=1;
P0=0x0e;
dig_ce=0;
key_ce=0;
if(P0_0==0)
{delay(10);
if(P0_0==0)
{stat=~stat;}
key_ce=1;}
}
}

void delay(unsigned int i)
{unsigned int j;
for(i;i>0;i--)
for(j=124;j>0;j--);}




3.键盘扫描

#include
void delay(unsigned int i);
void keyscan();
sbit key_ce=P1^3;
sbit led_ce=P1^2;
sbit dig_ce=P1^1;
sbit seg_ce=P1^0;
unsigned char knum[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,

0x7c,0x39,0x5e,0x79,0x71};
void main()
{P0=0;
P1=0x0c;

while(1)
{keyscan();}
}

void delay(unsigned int i)
{unsigned int j;
for(i;i>0;i--)
for(j=124;j>0;j--);
}

void keyscan()
{unsigned char temp1,temp2,num;
P0=0xfe;
dig_ce=1;
dig_ce=0;
P0=0; seg_ce=1; seg_ce=0;
key_ce=0;
temp1=P0;
temp1=P0&0x0f;
if(temp1!=0x0f)
{
delay(10);
temp2=P0;temp2=temp2%0x0f;
if(temp1==temp2)
{
switch(temp2)
{case 0x0e:num=0;break;
case 0x0d:num=1;break;
case 0x0b:num=2;break;
case 0x07:num=3;break;
default:break;}
while(temp2!=0x0f)
{temp2=P0;
temp2=temp2&0x0f;}
}
}


key_ce=1;
P0=knum[num];seg_ce=1;seg_ce=0;
P0=0xfe;dig_ce=1;dig_ce=0;
delay(1);}



4.定时自加1

#include
#define uint unsigned int
#define uchar unsigned char
sbit wela=P1^1;
sbit dula=P1^0;
uchar num;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void init() //初始化
{ P1=0x08;
P0=0x00;
TMOD=0x01; //定时器0工作在方式1
TH0=(65535-30000)/256; //定时器装初值,此为30ms初值
TL0=(65535-30000)%256;
TR0=1; EA=1;ET0=1; }
void delay(uint z)
{ uint x,y;
for(x=500;x>0;x--)
for(y=z;y>0;y--);
}void main()
{ wela=1;
P0=0x00;
wela=0;
while(1)
{for(num=0;num<10;num++)
{ dula=1;
P0=table[num];
dula=0;
delay(100);
}
}
}



4.循环显示0至9

#include

unsigned char code segtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f} ;
unsigned int num=0,dat=0;

void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}


void to_timer() interrupt 1
{
num++;
TH0 = (65536 - 1000)/256;
TL0 = (65536 - 1000)%256;

}
void display(unsigned int dat)
{
P0 = segtab[dat];
P1_0 = 1;
P1_0 = 0;
delay(1);
P0 = 0xfe;
P1_1 = 1;
P1_1 = 0;
delay(1);


}
void main()
{
P0 = 0;
P1 = 0x0c;
TMOD = 0x01;
EA = 1;ET0 = 1;
TH0 = (65536 - 1000)/256;
TL0 = (65536 - 1000)%256;
TR0 = 1;
while(1)
{
if(num == 666)
{
num = 0;
display(dat);
dat++;
}
if(dat == 10)
{
dat = 0;
}
}

}



4.中断定时30ms控制D0闪烁

#include

void main()
{
P0 = 0;
P1 = 0x0c;
P1_2 = 0;
TMOD = 0x01;
EA = 1;ET0 = 1;
TH0 =(65536 - 30000)/256;
TL0 =(65536 - 30000)%256;
TR0 = 1;
while(1);
}
void to_timer() interrupt 1
{
P0_4 = ~P0_4 ;
TH0 =(65536 - 30000)/256;
TL0 =(65536 - 30000)%256;
}



4.中断定时300ms控制D0闪烁

#include

unsigned int num = 0;

void main()
{
P0 = 0;
P1 = 0x0c;
P1_2 = 0;
TMOD = 0x01;
EA = 1;ET0 = 1;
TH0 = (65536 - 30000)/256;
TL0 = (65536 - 30000)%256;
TR0 =1;
while(1)
{
if(num == 10)
{
num = 0;
P0_4 = ~P0_4;
}
}
}
void to_int0() interrupt 1
{
num++;
TH0 = (65536 - 30000)/256;
TL0 = (65536 - 30000)%256;

}




4.自加2

#include

unsig

ned char seg_tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //段
unsigned char diq_tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //位
unsigned int num=0,data_d=0;

void delay(unsigned int x)
{
unsigned int i,j;
for(i=x;i>0;i--) ;
for(j=124;j>0;j--) ;
}

void display(unsigned int data_d )
{
unsigned int d1,d2;
d1=data_d/10;
d2=data_d%10;
P0=seg_tab[d1];
P1_0=1;
P1_0=0;
P0=diq_tab[1];
P1_1=1;
P1_1=0;
delay(1);
P0=seg_tab[d2];
P1_0=1;
P1_0=0;
P0=diq_tab[0];
P1_1=1;
P1_1=0;
delay(1);


}

void T0_timer()interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
num++;
}
void main()
{
P0=0;
P1=0x0c;
P1_0=0;P1_1=0;//端口要初始化
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
display(data_d);
if(num==100)
{
data_d++;
num=0;
}
if(data_d==60)
{
data_d=0;
}

}
}



4.自减1

#include
#define uint unsigned int
#define uchar unsigned char

void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=124;j>0;j--);
}
uchar code table[]={ 0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void display(uint sumis);
sbit dula=P1^0;
sbit wela=P1^1;
uint num,shi,ge,sum=60;
void main()
{

P0=0;
P1=0x0c;
TMOD=0x01;
TH0=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
display(sum);
}

}

void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL1=(65536-50000)%256;
num++;
if(num==20)
{
num=0;
sum--;
if(sum==0)
{
sum=60;
}

}

}
void display(uint sumis)
{
shi=sumis/10;
ge=sumis%10;
P0=0xfd;
wela=1;
wela=0;

P0=table[shi];
dula=1;
dula=0;
delay(5);

P0=0xfe;
wela=1;
wela=0;

P0=table[ge];
dula=1;
dula=0;
delay(5);
}



学号+按键加

#include
void delay(int i);
void display();
void xuehao();
sbit key=P1^3;
sbit duan=P1^0;
sbit wei=P1^1;
int ge,shi,num;
int table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void main()
{
num=0;
P0=0;
P1=0x0c;
wei=1;P0=0x0e;wei=0;
xuehao();
while(1)
{
display();
}
}
void delay(int i)
{
int j;
for(i;i>0;i--)
for(j=124;j>0;j--);
}
void display()
{
key=0;
duan=1;P0=0;duan=0;
if(P0_0==0)
{
delay(10);
if(P0_0==0)
{
num++;
shi=num/10;
ge=num%10;
if(num==45)
{
num=0;
}
}

}
while(!P0_0);
key=1;

wei=1;P0=0xfd;wei=0;
duan=1;P0=table[shi];duan=0;
delay(10);
duan=1;P0=0;duan=0;

wei=1;P0=0xfe;wei=0;
duan=1;P0=table[ge];duan=0;
delay(10);
duan=1;P0=0;duan=0;
}


void xuehao()
{
unsigned int k;
for(k=100;k>0;k--)
{

P1_1=1;
P0=0x7f;
P1_1=0;

P1_0=1;
P0=0x3f;
P1_0=0;
delay(1);
P0=0xff; //0 P0=0xff; 消隐

P1_1=1;
P0=0xbf;
P1_1=0;

P1_0=1;
P0=0x6f;
P1_0=0;
delay(1);
P0=0xff; //9

P1_1=1;
P0=0xdf;
P1_1=0;

P1_0=1;
P0=0x5b;
P1_0=0;
delay(1);
P0=0xff; //2

P1_1=1;
P0=0xef;
P1_1=0;

P1_0=1;
P0=0x06;
P1_0=0;
delay(1);
P0=0xff; //1

P1_1=1;
P0=0xf7;
P1_1=0;

P1_0=1;
P0=0x3f;
P1_0=0;
delay(1);
P0=0xff; //0

P1_1=1;
P0=0xfb;
P1_1=0;

P1_0=1;
P0=0x4f;
P1_0=0;
delay(1);
P0=0xff; //3

P1_1=1;
P0=0xfd;
P1_1=0;

P1_0=1;
P0=0x6d;
P1_0=0;
delay(1);
P0=0xff; //5

P1_1=1;
P0=0xfe;
P1_1=0;

P1_0=1;
P0=0x4f;
P1_0=0;
delay(1);
P0=0xff; //3
}
}




综合(K0加,K1减1,K2归0)

#include
#define uint unsigned int
#define uchar unsigned char
sbit Key_ce=P1^3;
sbit dula=P1^0; //数码管段选 相当于Sec_ce
sbit wela=P1^1; //数码管位选 相当于原理图的Dig_ce 根据自己习惯写
void display(uint sum); //数码管显示函数
void keyscan(); //键盘扫描程序
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=124;j>0;j--);
}
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71}; //共阴数码管显示段
uint sum;
void main()
{
P0=0;
P1=0x0c;
P0=0x0e;
wela=1;
wela=0;
P0=0x0f; //把P0拉高,这样在后面方便检查P0哪个值变低了
TMOD=0x01;
TH0=(65536-50000)/256; // 装初值,50ms
TL0=(65536-50000)%256; //
EA=1;
ET0=1;
while(1)
{
keyscan(); // 键盘扫描程序
display(sum); // 显示子程序
}

}
void keyscan()
{


Key_ce=0;
P0=0; //这行和下两行代码很重要,没有的话数码管没反应的,必须写
dula=1;
dula=0;
if(P0_0==0) //按下加一
{
delay(80); //消除抖动
if(P0_0==0)
{
sum++;
if(sum==61) //这计数到60后清零
{
sum=0;
}
while(!P0_0); //判断是否松手,消除松手抖动
}
}
if(P0_1==0) //减一
{
delay(80);
if(P0_1==0)
{
sum--;
if(sum==-1)
{
sum=60;
}
while(!P0_0);
}
}
if(P0_2==0) //清理程序
{
delay(80);
if(P0_2==0)
{
sum=0;
}
while(!P0_0); //消除松手抖动
}
if(P0_3==0) //按下后开启或关闭定时器,每一秒自动加1;
{
delay(80);
if(P0_3==0)
{
while(!P0_0);
TR0=~TR0; // 开启或关闭定时器

}
}

Key_ce=1;


}
void display(uint sum) //显示子程序
{
uint shi,ge;
shi=sum/10;
ge=sum%10;
P0=0xfd;
wela=1;
wela=0;
P0=table[shi];
dula=1;
dula=0;
delay(5);

P0=0xfe;
wela=1;
wela=0;
P0=table[ge];
dula=1;
dula=0;
delay(5);
}
void T0

_timer() interrupt 1 //P0_3口定时器自动加一中断
{
uint num;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;

if(num==20) //num==20时 20*50ms=1秒
{
num=0;
sum++;
if(sum==61)
sum=0;




}

}












相关文档
最新文档