今天我们来讨论一下我们如何利用单片机做出一些炫彩的效果,而昨天好像是七夕节,很多人都把它当做了情人节,那么我们来做一个浪漫的“I Love u”;今天我就把代码写出来,而不再是截图挂出来。效果图我也就拍出来了,你们如果有单片机可以自己复制代码下来看一些效果。(其实这一次的代码有一点跨度大了,毕竟前面我并没有说的很深入,现在一下子又变得看不懂了,不过慢慢来,我会在以后的博客中把后边的讨论写详细一点)代码如下,注释我来写了一部分,但是不全,我会慢慢把它补齐。
效果大致如我以下描述:首先,LED灯慢慢的像流水灯一样一个接一个亮起来;而后是点阵从靠近LED灯的那一排开始亮起来,就像蛇一样游动,第一排是从第一个亮完最后一个,从第二排接着第一排的最后一个那侧开始亮(就像蛇游动)一直到最后一排;接下来是数码管,数码管只有那一个点“.“亮,像流水灯一样亮起来;前面效果都完之后所有的LED灯,点阵,数码管都会全部亮起来;点阵就会显示“I love u”那句话了,不过不是同时显示,成一个先后顺序。
(如果代码和注释中有误,请指出,我会第一时间和你沟通并改正,谢谢)
#include <reg52.h>
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
//延时函数
void delay(unsigned int t){
unsigned int i;
for(i=0;i<t;i++);
}
//左移函数
void change1(){
unsigned int j;
for(j=0;j<8;j++){
P0=~(0x01<<j);
delay(10000);
}
}
//右移函数
void change2(){
unsigned int j;
for(j=0;j<8;j++){
P0=~(0x80>>j);
delay(3000);
}
}
//LED流水灯的依次点亮
void y6(unsigned int j){
ENLED=0;
ADDR3=1;
ADDR2=1;
ADDR1=1;
ADDR0=0;
if(j==1){
change1();
}else{
change2();
}
}
//点阵的点亮
void y0_5(unsigned int i){
unsigned int j;
ENLED=0;
ADDR3=0;
switch(i){
case 0: ADDR2=1; ADDR1=1; ADDR0=1; change2(); break;
case 1: ADDR2=1; ADDR1=1; ADDR0=0; change1(); break;
case 2: ADDR2=1; ADDR1=0; ADDR0=1; change2(); break;
case 3: ADDR2=1; ADDR1=0; ADDR0=0; change1(); break;
case 4: ADDR2=0; ADDR1=1; ADDR0=1; change2(); break;
case 5: ADDR2=0; ADDR1=1; ADDR0=0; change1(); break;
case 6: ADDR2=0; ADDR1=0; ADDR0=1; change2(); break;
case 7: ADDR2=0; ADDR1=0; ADDR0=0; change1(); break;
}
}
//数码管的点亮
void y0_s(unsigned int i,unsigned int wk){
unsigned int j;
ENLED=0;
ADDR3=1;
switch(i){
case 0: ADDR2=1; ADDR1=0; ADDR0=1; break;
case 1: ADDR2=1; ADDR1=0; ADDR0=0; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=1; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=0; break;
case 4: ADDR2=0; ADDR1=0; ADDR0=1; break;
case 5: ADDR2=0; ADDR1=0; ADDR0=0; break;
}
if(wk==1){
P0=0x00;
}else{
P0=0x7f;
}
}
//数码管的全部点亮
void alllion6(unsigned int wk){
unsigned int j;
ENLED=0;
ADDR3=1;
for(j=0;j<6;j++){
switch(j){
case 0: ADDR2=1; ADDR1=0; ADDR0=1; break;
case 1: ADDR2=1; ADDR1=0; ADDR0=0; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=1; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=0; break;
case 4: ADDR2=0; ADDR1=0; ADDR0=1; break;
case 5: ADDR2=0; ADDR1=0; ADDR0=0; break;
}
if(wk==1){
P0=0x00;
}else{
P0=0x7f;
}
delay(100);
}
}
//点阵全部点亮
void alllion8(){
unsigned int j;
ENLED=0;
ADDR3=0;
for(j=0;j<8;j++){
switch(j){
case 0: ADDR2=1; ADDR1=1; ADDR0=1; P0=0x00; break;
case 1: ADDR2=1; ADDR1=1; ADDR0=0; P0=0x00; break;
case 2: ADDR2=1; ADDR1=0; ADDR0=1; P0=0x00; break;
case 3: ADDR2=1; ADDR1=0; ADDR0=0; P0=0x00; break;
case 4: ADDR2=0; ADDR1=1; ADDR0=1; P0=0x00; break;
case 5: ADDR2=0; ADDR1=1; ADDR0=0; P0=0x00; break;
case 6: ADDR2=0; ADDR1=0; ADDR0=1; P0=0x00; break;
case 7: ADDR2=0; ADDR1=0; ADDR0=0; P0=0x00; break;
}
delay(100);
}
}
//LED全部点亮
void alllionled(){
ENLED=0;
ADDR3=1;
ADDR2=1;
ADDR1=1;
ADDR0=0;
P0=0x00;
}
//“I love U”中的“I”
void alllioni(){
unsigned int j;
ENLED=0;
ADDR3=0;
for(j=0;j<8;j++){
switch(j){
case 0: ADDR2=1; ADDR1=1; ADDR0=1; P0=0x00; break;
case 1: ADDR2=1; ADDR1=1; ADDR0=0; P0=0x00; break;
case 2: ADDR2=1; ADDR1=0; ADDR0=1; P0=0xe7; break;
case 3: ADDR2=1; ADDR1=0; ADDR0=0; P0=0xe7; break;
case 4: ADDR2=0; ADDR1=1; ADDR0=1; P0=0xe7; break;
case 5: ADDR2=0; ADDR1=1; ADDR0=0; P0=0xe7; break;
case 6: ADDR2=0; ADDR1=0; ADDR0=1; P0=0x00; break;
case 7: ADDR2=0; ADDR1=0; ADDR0=0; P0=0x00; break;
}
delay(100);
}
}
//“I love U”中的“love”
void alllionl(){
unsigned int j;
ENLED=0;
ADDR3=0;
for(j=0;j<8;j++){
switch(j){
case 0: ADDR2=1; ADDR1=1; ADDR0=1; P0=0xe7; break;
case 1: ADDR2=1; ADDR1=1; ADDR0=0; P0=0xc3; break;
case 2: ADDR2=1; ADDR1=0; ADDR0=1; P0=0x81; break;
case 3: ADDR2=1; ADDR1=0; ADDR0=0; P0=0x00; break;
case 4: ADDR2=0; ADDR1=1; ADDR0=1; P0=0x00; break;
case 5: ADDR2=0; ADDR1=1; ADDR0=0; P0=0x00; break;
case 6: ADDR2=0; ADDR1=0; ADDR0=1; P0=0x99; break;
case 7: ADDR2=0; ADDR1=0; ADDR0=0; P0=0xff; break;
}
delay(100);
}
}
//“I love U”中的“u”
void allliony(){
unsigned int j;
ENLED=0;
ADDR3=0;
for(j=0;j<8;j++){
switch(j){
case 0: ADDR2=1; ADDR1=1; ADDR0=1; P0=0x81; break;
case 1: ADDR2=1; ADDR1=1; ADDR0=0; P0=0x00; break;
case 2: ADDR2=1; ADDR1=0; ADDR0=1; P0=0x3c; break;
case 3: ADDR2=1; ADDR1=0; ADDR0=0; P0=0x3c; break;
case 4: ADDR2=0; ADDR1=1; ADDR0=1; P0=0x3c; break;
case 5: ADDR2=0; ADDR1=1; ADDR0=0; P0=0x3c; break;
case 6: ADDR2=0; ADDR1=0; ADDR0=1; P0=0x3c; break;
case 7: ADDR2=0; ADDR1=0; ADDR0=0; P0=0x3c; break;
}
delay(100);
}
}
main(){
while(1){
unsigned int wk=0;
unsigned int wc=0;
unsigned int j;
y6(1);//流水LED灯
for(j=0;j<8;j++){
y0_5(j);//点阵循环点亮
}
for(j=0;j<6;j++){
y0_s(j,0);//数码管
delay(10000);
}
//全亮
for(j=0;j<200;j++){
alllion6(1);
delay(20);
alllion8();
delay(20);
alllionled();
delay(20);
}
//I love you
for(wk=0;wk<200;wk++){
alllioni();
delay(50);
alllionled();
delay(50);
alllion6(1);
delay(50);
}
for(wk=0;wk<200;wk++){
alllionl();
delay(50);
alllionled();
delay(50);
alllion6(1);
delay(50);
}
for(wk=0;wk<200;wk++){
allliony();
delay(50);
alllionled();
delay(50);
alllion6(1);
delay(50);
}
}
}