1、编写C51代码,实现流水灯字变花型:
/***********************************************************
* 实验功能 : LED实现流水灯花型控制
*************************************************************/
# include<reg51.h>
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
timer0() interrupt 1
{
static unsigned int tt;
tt++;
if(tt==10)
tt=0;
}
void run(int *a,int len)
{
unsigned int k;
for(k=0;k<len;k++){
P0 = a[k];
delay(100*len);
}
}
void main(void)
{
int a[]={0XFF,0XFE,0XFC,0XF8,0XF0,0XE0,0XC0,0X80,0X00};
int b[]={0X01,0X03,0X7,0X0F,0X1F,0X3F,0X7F,0XFF};
int c[]={0X00,0X18,0X3C,0X7E,0XFF};
int d[]={0XE7,0XC3,0X81,0X00};
TMOD=0x02;
TH0=0x06;
TL0=0x06;
TR0=1;
ET0=1;
EA=1;
while(1){
run(a,9);
run(b,8);
run(c,5);
run(d,4);
}
}
2、实现八个LED的三级亮度变化:
#include"reg51.h"
unsigned int scale;
void main(void)
{
unsigned int n;
TMOD=0x02;
TH0=0x06;
TL0=0x06;
TR0=1;
ET0=1;
EA=1;
while(1)
{
for(n=0;n<50000;n++);
scale+=4;
if(scale>=10)
scale=0;
}
}
/*********************************************************
程序名称:T0中断服务程序
程序功能:程序中从tt=0开始到scale为低电平,从scale开始到tt=10为高电平;由于scale是变量,所以改变scale就可以改变占空比。
**********************************************************/
timer0() interrupt 1
{
static unsigned int tt;
tt++;
if(tt>=10)
{
tt=0;
P0=0xFF;
}
if(scale==tt)
P0=0x00;
}
3、动态驱动八位数码管,显示两人学号:
#include<reg51.h>
#define GPIO_DIG P0
void Delay10ms(unsigned int c);
unsigned char code DIG_CODE_KANG[] = {0X3F,0X6D,0X06,0X66,0X7F,0X06,0X4F,0X6F};
unsigned char code DIG_CODE_ZHANG[] = {0X3F,0X6D,0X06,0X66,0X7F,0X06,0X66,0X3F};
unsigned char code choose[] = {0X01,0X02,0X04,0X08,0X10,0X20,0X40,0X80};
void main(void)
{
unsigned char i = 0;
unsigned int n = 0;
while(1){
P0 = 0X00;
Delay10ms(500);
n = i = 0;
while(1)
{
P2 = ~choose[i];
GPIO_DIG = DIG_CODE_KANG[i];
i++;
n++;
Delay10ms(1);
if(i == 8)
i = 0;
if(n == 300)
break;
}
n = i = 0;
P0 = 0X00;
Delay10ms(500);
while(1)
{
P2 = ~choose[i];
GPIO_DIG = DIG_CODE_ZHANG[i];
i++;
n++;
Delay10ms(1);
if(i == 8)
i = 0;
if(n == 300)
break;
}
}
}
void Delay10ms(unsigned int c)
{
unsigned char a, b;
for (;c>0;c--)
for (b=5;b>0;b--)
for (a=130;a>0;a--);
}
4、用四位数码管实现0~9999计数器:
/***********************************************************
* 实验功能 : 4位数码管实现0~9999的计数器
*************************************************************/
#include<reg51.h>
void Delay10ms(unsigned int c);
void show(int choose_bit,int number);
unsigned char code TIMER[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
unsigned char code choose[] = {0X01,0X02,0X04,0X08};
void main(void)
{
unsigned char i = 0, j = 0, k = 0, m = 0;
while(1)
{
for(i=0;i<10;i++)
for(j=0;j<10;j++)
for(k=0;k<10;k++)
for(m=0;m<10;m++){
show(0,i);
show(1,j);
show(2,k);
show(3,m);
}
}
}
void Delay10ms(unsigned int c)
{
unsigned char a, b;
for (;c>0;c--)
for (b=5;b>0;b--)
for (a=130;a>0;a--);
}
void show(int choose_bit,int number)
{
P2 = ~choose[choose_bit];
P0 = TIMER[number];
delay(1);
}