关于心形小灯的51程序设计
时间:10-02
整理:3721RD
点击:
本设计包含五种闪烁模式,分别为:全亮、闪烁、滚动、1/3补全、1/2补全和呼吸,通过两个外部中断控制不同模式的切换。#include<reg52.h>#include<stdio.h>#define uint unsigned int#define uchar unsigned charsbit key0 = P3^2;sbit key1 = P3^3;sbit mouth_up = P1^6;sbit mouth_down = P1^7;uint mode = 0;uchar stars[24][2] = {{0xfe,0x01},{0xfe,0x02},{0xfe,0x04},{0xfe,0x08},{0xfe,0x10},{0xfe,0x20},{0xfe,0x40},{0xfe,0x80},{0xfd,0x01},{0xfd,0x02},{0xfd,0x04},{0xfd,0x08},{0xfd,0x10},{0xfd,0x20},{0xfd,0x40},{0xfd,0x80},{0xfb,0x01},{0xfb,0x02},{0xfb,0x04},{0xfb,0x08},{0xfb,0x10},{0xfb,0x20},{0xfb,0x40},{0xfb,0x80}};uchar vm[8] = {0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};//uchar v[8]= {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//p2->v//uchar h[3]= {0xfe,0xfd,0xfb};//p0->hvoid delay(uint x,uint y)//单步执行7*E-4{ int i,j; for(i = 0;i<x;i++) for(j = 0;j<y;j++);}void delay_mms(unsigned int xms){ unsigned int x,y; for(x=xms;x>0;x--) for(y=20;y>0;y--);}/********************************//**************Define a point***************/void dpoint(uchar *array){ P1 = array[0]; P2 = array[1];}/**************Mod***************/void mode2(){ int time = 10; while(time--) { int i; for(i=0;i<24;i++){ if(i%2 == 0) dpoint(stars[i]); delay(5,14); } } time = 10; while(time--) { int i; for(i=0;i<24;i++){ if(i%2 != 0) dpoint(stars[i]); delay(5,14); } }}
void mode4(){ P1 = 0xf8; delay(50,142); P1 = 0xff; delay(50,120);}
void mod0()//全亮{ while(mode==0) { P1 = 0xf8; delay_mms(90); P1 = 0xff; delay_mms(10); }}
void mod1()//从中间向两边,两边向中间{ while(mode==1) { int i,j; int laps; for(i=11;i>=0;i--) { laps = i/2+1; while(laps--) { for(j=11;j>=i;j--) { dpoint(stars[j]); delay_mms(5); dpoint(stars[23-j]); delay_mms(5); } } } for(i=0;i<=11;i++) { laps = 6-i/2; while(laps--) { for(j=0;j<=i;j++) { dpoint(stars[j]); delay_mms(5); dpoint(stars[23-j]); delay_mms(5); } } } P2 = 0xff; for(i=0;i<3;i++) { mode4(); } }}
void mod2()//奇偶变换{ while(mode==2) { mode2(); }}void mod3()//顺时针补全+三分补全{ int i,j,k; while(mode==3) { for(i=0;i<24;i++) { int laps = 8-i/3; while(laps--) { for(k=0;k<=i;k++) { dpoint(stars[k]); delay_mms(5); } } } mode2(); P1 = 0xf8; for(j=0;j<=7;j++) { P2 = vm[j]; delay(500,91); } mode2(); }}
void mod4()//闪烁{ P2 = 0xff; while(mode==4) { mode4(); }}void mod5()//呼吸{ int i,j; P2 = 0xff; while(mode==5) { for(i=0;i<100;i++) { P1 = 0xf8; delay_mms(i); P1 = 0xff; delay_mms(100-i); } for(j=i;j>0;j--) { P1 = 0xf8; delay_mms(j); P1 = 0xff; delay_mms(100-j); } }}//************************************void pick_mod(uint mode){ if(mode==1) mod1(); if(mode==2) mod2(); if(mode==3) mod3(); if(mode==4) mod4(); if(mode==5) mod5(); else mod0();}//************************************int main(){IE = 0X85;// EA=1;EX0=1;EX1=1;TCON = 0X05;//0000 0101while(1){ pick_mod(mode);}return 0;}
void mode_change() interrupt 0{ if(key0==0) { delay(20,90); if(key0==0) { if(mode<5) mode++; else { mode = 0; } while(!key0); } }}void reset_led() interrupt 2{ if(key1==0) { delay(20,20); if(key1==0) { mode = 0; while(!key1); } }}
void mode4(){ P1 = 0xf8; delay(50,142); P1 = 0xff; delay(50,120);}
void mod0()//全亮{ while(mode==0) { P1 = 0xf8; delay_mms(90); P1 = 0xff; delay_mms(10); }}
void mod1()//从中间向两边,两边向中间{ while(mode==1) { int i,j; int laps; for(i=11;i>=0;i--) { laps = i/2+1; while(laps--) { for(j=11;j>=i;j--) { dpoint(stars[j]); delay_mms(5); dpoint(stars[23-j]); delay_mms(5); } } } for(i=0;i<=11;i++) { laps = 6-i/2; while(laps--) { for(j=0;j<=i;j++) { dpoint(stars[j]); delay_mms(5); dpoint(stars[23-j]); delay_mms(5); } } } P2 = 0xff; for(i=0;i<3;i++) { mode4(); } }}
void mod2()//奇偶变换{ while(mode==2) { mode2(); }}void mod3()//顺时针补全+三分补全{ int i,j,k; while(mode==3) { for(i=0;i<24;i++) { int laps = 8-i/3; while(laps--) { for(k=0;k<=i;k++) { dpoint(stars[k]); delay_mms(5); } } } mode2(); P1 = 0xf8; for(j=0;j<=7;j++) { P2 = vm[j]; delay(500,91); } mode2(); }}
void mod4()//闪烁{ P2 = 0xff; while(mode==4) { mode4(); }}void mod5()//呼吸{ int i,j; P2 = 0xff; while(mode==5) { for(i=0;i<100;i++) { P1 = 0xf8; delay_mms(i); P1 = 0xff; delay_mms(100-i); } for(j=i;j>0;j--) { P1 = 0xf8; delay_mms(j); P1 = 0xff; delay_mms(100-j); } }}//************************************void pick_mod(uint mode){ if(mode==1) mod1(); if(mode==2) mod2(); if(mode==3) mod3(); if(mode==4) mod4(); if(mode==5) mod5(); else mod0();}//************************************int main(){IE = 0X85;// EA=1;EX0=1;EX1=1;TCON = 0X05;//0000 0101while(1){ pick_mod(mode);}return 0;}
void mode_change() interrupt 0{ if(key0==0) { delay(20,90); if(key0==0) { if(mode<5) mode++; else { mode = 0; } while(!key0); } }}void reset_led() interrupt 2{ if(key1==0) { delay(20,20); if(key1==0) { mode = 0; while(!key1); } }}
很想看,不过要是小编能重新整理一下就更好了
是准备的比较匆忙,文件发的也比较零散电路比较简单,大部分设计是通过软件实现的
有兴趣可以交流一下