单片机源程序如下:
- #ifndef __CAIDENGSHU_H__
- #define __CAIDENGSHU_H__
- #include <STC/STC12C5A60S2.h>
- #include <intrins.h>
- void WaterLampToCol19();
- void WaterLampToCol20();
- void WaterLampToCol21();
- void WaterLampToCol22();
- void WaterLampToCol23();
- void WaterLampToCol24();
- void WaterLampToCol25();
- void WaterLampToCol26();
- void WaterLampToCol27();
- void WaterLampToCol28();
- void WaterLampToCol29();
- void WaterLampToCol30();
- void WaterLampToCol31();
- void WaterLampToCol32(); //蓝光整段,,彩虹后面往前面流水,整段从左往右 七彩效果
- void WaterLampToCol33(); //蓝光整段,,彩虹后面往前面流水,整段从右往左
- void WaterLampToCol34(); //整段不发光,中间往两边流水,加两边向中间流水,循环三次
- void WaterLampToCol35(); //彩虹波浪流水,分许多8个为一组的小段
- void WaterLampToCol36(); //8个为一组的小段,,重点是变化的彩虹波浪
- void WaterLampToCol37(); //每七段一节,红色流水,左右来回
- void WaterLampToCol41(); // 每八段一节,蓝色流水,左右来回
- void WaterLampToCol42(); // 每七段一节,蓝色流水,左右来回
- void WaterLampToCol43(); // 每七段一节,黄色流水,左右来回
- void WaterLampToCol44(); // 每七段一节,粉色流水,左右来回
-
- void WaterLampToCol38(); //八灯一节,,七彩流水跑马效果
- void WaterLampToCol39(); //蓝光整段,,彩虹从左到右流过去 真正彩虹效果
- void WaterLampToCol40(); //整段不发光,中间往两边流水,加两边向中间流水,循环三次
- void WaterLampToCol45(); //水滴向上升腾效果 绿光
- void WaterLampToCol46(); //水滴向上升腾效果 红光
- void WaterLampToCol47(); //水滴向上升腾效果 蓝光
- #endif
复制代码
- #include "main.h"
- #include <STC/STC12C5A60S2.h>
- #include <intrins.h> //因为要用到左右移函数,所以加入这个头文件
- #include "caidengshu.h"
- unsigned long WsDat[nWs];
- //#define MAIN_Fosc 24000000L //定义主时钟
- #define Black 0x000000 // 黑色
- sbit LED=P0^0;
-
- unsigned char q;
- unsigned char h;
-
-
- void WS_SET(unsigned long color,unsigned char num);//灯条开指定的灯
- void WS_SET_Water_lamp(unsigned long color,unsigned int time);//流水灯不连接
- void WS_SET_Water_lamp_two(unsigned long color,unsigned int time);//流水灯连接
- void WS_SET_Water_lamp_two1(unsigned long color,unsigned int time);
- void Breathing_lamp(unsigned long color,unsigned int time);//呼吸灯-仅支持三原色
- void Breathing_lamp_Alternate(unsigned int time);//三色渐变交替
- void yigedianliushui(unsigned long color,unsigned int time); //一个IC流水到左边
- void WaterLampToCol2(unsigned long _uColor, unsigned int _uTime);
- void WS_L_SetAll();
- void WaterLampToCol3(unsigned long _uColor, unsigned int _uTime);
- void WaterLampToCol12(unsigned long _uColor0,unsigned long _uColor1, unsigned int _uTime);
- void WaterLampToCol11( unsigned int uTime);
- void WaterLampToCol18( unsigned int uTime);
- void qingling();
- void WaterLampToCol11( unsigned int uTime);
- void yigedianliushui10(unsigned int time);
- void Delay1s() //@24.000MHz
- {
- unsigned char i, j;
- _nop_();
- _nop_();
- i = 24;
- j = 84;
- do
- {
- while (--j);
- } while (--i);
- }
- void Delay1ms() //@24.000MHz
- {
- unsigned char i, j;
- _nop_();
- _nop_();
- i = 24;
- j = 84;
- do
- {
- while (--j);
- } while (--i);
- }
- void delay_ms(unsigned int i)
- {
- while(i--)
- {
- Delay1ms();
- }
- }
- void Delay20ms() //@24.000MHz
- {
- unsigned char i, j, k;
- i = 2;
- j = 211;
- k = 231;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- void main () //主函数
- {
-
- unsigned int x;
-
- qingling();
- P0M0=0X00;
- P0M1=0X00;
-
-
- // q=1;
- // ColorToColor(0X000000,0XFF0000);
- // ColorToColor(0XFF0000,0xFFFF00);
- // ColorToColor(0xFFFF00,0Xffffff);
- // ColorToColor(0Xffffff,0X00ffff);
- // ColorToColor(0X00ffff,0X0000ff);
- // ColorToColor(0X0000ff,0X000000);
- // ColorToColor(0X000000,0X00FF00);
- // ColorToColor(0X00FF00,0x0FFFF0);
- // ColorToColor(0x0FFFF0,0Xffffff);
- // ColorToColor(0Xffffff,0XFF00ff);
- // ColorToColor(0XFF00ff,0XF0000f);
- // ColorToColor(0XF0000f,0X000000);
- while(1)
- {
-
- Breathing_lamp_Alternate(5);//三色渐变交替
- delay_ms(1000);
-
- x=3;
- while(x--)
- {
- WaterLampToCol45(); //水滴向上升腾效果 绿光
- }
-
- x=3;
- while(x--)
- {
- WaterLampToCol46(); //水滴向上升腾效果 红光
- }
-
- x=3;
- while(x--)
- {
- WaterLampToCol47(); //水滴向上升腾效果 蓝光
- }
-
-
- WaterLampToCol38(); //八灯一节,,七彩流水跑马效果
- WaterLampToCol39(); //蓝光整段,,彩虹从左到右流过去 真正彩虹效果
- WaterLampToCol40(); //整段不发光,中间往两边流水,加两边向中间流水,循环三次
- WaterLampToCol27(); //粉光里流水三个蓝光
- delay_ms(200);
-
- x=10;
- while(x--) //七彩模式灯
- {
- WaterLampToCol19();
- delay_ms(200);
- WaterLampToCol20();
- delay_ms(200);
- WaterLampToCol21();
- delay_ms(200);
- WaterLampToCol22();
- delay_ms(200);
- WaterLampToCol23();
- delay_ms(200);
-
- WaterLampToCol25();
- delay_ms(200);
-
- WaterLampToCol25();
- delay_ms(200);
-
- WaterLampToCol26();
- delay_ms(200);
- }
-
- WaterLampToCol34(); //整段不发光,中间往两边流水,加两边向中间流水,循环三次
-
- x=3;
- while(x--)
- {
- WaterLampToCol36(); //8个为一组的小段,,重点是变化的彩虹波浪
- }
-
- WaterLampToCol37(); // 每七段一节,红色流水,左右来回
- WaterLampToCol41(); // 每八段一节,蓝色流水,左右来回
- WaterLampToCol42(); // 每七段一节,蓝色流水,左右来回
- WaterLampToCol43(); // 每七段一节,黄色流水,左右来回
- WaterLampToCol44(); // 每七段一节,粉色流水,左右来回
-
- x=3; //黄绿红交替变换闪烁
- while(x--)
-
- {
- WaterLampToCol28();
- }
-
- x=3;
- while(x--)
- {
- WaterLampToCol29();
- }
- x=3;
- while(x--)
- {
- WaterLampToCol30();
- }
-
- WS_SET_Water_lamp_two(0x00ff00,100); //左边往右边黄光流水
-
-
- WS_SET_Water_lamp_two1(0x00ff00,100); //右边往左边黄光流水
-
-
-
-
-
- /* WaterLampToCol3(0x10ff50 ,100);
- delay_ms(1000);
- WaterLampToCol2(0x10ff50,100);
- delay_ms(1000);
- WS_SET_Water_lamp_two(0x10ff50,100);//流水灯连接
- delay_ms(1000);
- yigedianliushui(0x10ff50,100);
- delay_ms(1000);
- WS_SET_Water_lamp_two1(0x10ff50,100);//流水灯连接
- delay_ms(1000);
- Breathing_lamp(0x10ff50,100);
- delay_ms(1000);
- DisP_R_G_B(20);
- delay_ms(1000); */
-
- }
- }
- void yigedianliushui10(unsigned int time) //粉光里面黄光流水
- {
- unsigned char j,x,m;
- unsigned long led,xishu,xishu1;
- led=0x01;
- xishu=1;
- xishu1=0;
-
- for(x=0;x<LED_N;x++)
- {
- for(j=12;j>0;j--)
- {
- led=_crol_(led,1);
- for(m=0;m<8;m++)
- {
- xishu1=xishu*2;
- xishu1=led/xishu;
- if(xishu1==1)
- {
- DisP_Color(0x20ff50);
- }
- else
- {
- DisP_Color(0x20ff00);
- }
- }
- }
- reset();
- delay_ms(time);
- }
- }
- void delay(unsigned int time) //延时
- {
-
- unsigned int i,j,k;
- for(j=0;j<time;j++)
- for(i=0;i<time;i++)
- for(k=0;k<time;k++)
- Delay();
- }
- void Delay() //@24.000MHz
- {
- _nop_();
- }
- void reset(void)
- {
- int i;
- LED=0;
- for(i=0;i<50;i++)
- {
- Delay();
- }
- }
- void T0(void)
- {
- LED=1;
- LED=0;
- Delay();
- }
- void T1(void)
- {
- LED=1;
- Delay();
- LED=0;
- }
- void qingling()
- {
- unsigned char i;
- for(i=0;i<nWs;i++)
- {
- DisP_Color(WsDat[0x000000]);
- }
- reset();
- }
- void DisP_Color(unsigned long dat) //控制一个灯
- { unsigned int i;
- for(i=0;i<24;i++)
- {
- if(0x800000 == (dat & 0x800000)) T1();
- else T0();
- dat<<=1; //左移一位
- }
- }
- void DisP_ALLColor(unsigned long dat) //控制所有灯
- {
- int j;
- for(j=0;j<LED_N;j++) DisP_Color(dat);
- reset();
- }
- void DisP_R_G_B(unsigned int j) //RGB渐变
- {
- unsigned long i=0;
- while(i<0xffffff)
- {
- if(i<0xff) DisP_ALLColor(i++);
- else if(i<0xffff)
- {
- i=i+0x100;
- DisP_ALLColor(i);
- }
- else if(i<0xffffff)
- {i=i+0x10000;
- DisP_ALLColor(i);
- }
- delay(j);
- }
- while(i>0)
- {
- if(i>0xffff00) DisP_ALLColor(i--);
- else if(i>0xff0000) {i=i-0x100; DisP_ALLColor(i);}
- else if(i>0) {i=i-0x10000;DisP_ALLColor(i);}
- delay(j);
- }
- reset();
- }
-
-
- unsigned char abs0(int num)
- {
- if(num>0) return num;
-
- num = -num;
- return (unsigned char) num;
- }
-
- unsigned long ColorToColor(unsigned long color0, unsigned long color1)
- {
- unsigned char Red0, Green0, Blue0; // 起始三原色
- unsigned char Red1, Green1, Blue1; // 结果三原色
- int RedMinus, GreenMinus, BlueMinus; // 颜色差(color1 - color0)
- unsigned char NStep; // 需要几步
- float RedStep, GreenStep, BlueStep; // 各色步进值
- unsigned long color; // 结果色
- unsigned char i;
-
- // 绿 红 蓝 三原色分解
- Red0 = color0>>8;
- Green0 = color0>>16;
- Blue0 = color0;
-
- Red1 = color1>>8;
- Green1 = color1>>16;
- Blue1 = color1;
-
- // 计算需要多少步(取差值的最大值)
- RedMinus = Red1 - Red0;
- GreenMinus = Green1 - Green0;
- BlueMinus = Blue1 - Blue0;
-
- NStep = ( abs0(RedMinus) > abs0(GreenMinus) ) ? abs0(RedMinus):abs0(GreenMinus);
- NStep = ( NStep > abs0(BlueMinus) ) ? NStep:abs0(BlueMinus);
-
- // 计算出各色步进值
- RedStep = (float)RedMinus / NStep;
- GreenStep = (float)GreenMinus / NStep;
- BlueStep = (float)BlueMinus / NStep;
-
- // 渐变开始
- for(i=0; i<NStep; i++)
- {
- Red1 = Red0 + (char)(RedStep * i);
- Green1 = Green0 + (char)(GreenStep * i);
- Blue1 = Blue0 + (char)(BlueStep * i);
-
- color = (((unsigned long)Green1<<16)&0XFF0000) | ((Red1<<8)&0XFF00) | Blue1&0XFF; // 合成 绿红蓝
- WsDat[i] = color;
- DisP_ALLColor(color) ;
-
- delay(15) ; // 渐变速度
- }
-
-
- return color;
- }
- void WS_SET(unsigned long color,unsigned char num) //
- {
- unsigned char j;
- for(j=0;j<LED_N;j++)
- {
- if(j<num)
- {
- DisP_Color(color);
- }
- else
- {
- DisP_Color(Black);
- }
- }
- reset();
- }
- void WS_SET_Water_lamp(unsigned long color,unsigned int time)
- {
- unsigned char j,x;
- for(x=0;x<LED_N;x++)
- {
- for(j=0;j<LED_N;j++)
- {
- if(j==x)
- {
- DisP_Color(color);
- }
- else
- {
- DisP_Color(0x20ff00);
- }
- }
- reset();
- delay_ms(time);
- }
- }
- void WS_SET_Water_lamp_two(unsigned long color,unsigned int time) //左边往右边流水
- {
- unsigned char j,x;
- for(x=0;x<nWs;x++)
- {
- for(j=0;j<nWs;j++)
- {
- if(j<x)
- {
- DisP_Color(color);
- }
- else
- {
- DisP_Color(0x9f9f9f);
- }
- }
- reset();
- delay_ms(time);
- }
- }
- void WS_SET_Water_lamp_two1(unsigned long color,unsigned int time) //右边往左边流水
- {
- unsigned char j,x;
- for(x=0;x<nWs;x++)
- {
- for(j=nWs;j>0;j--)
- {
- if(j<x)
- {
- DisP_Color(color);
- }
- else
- {
- DisP_Color(0x9f9f9f);
- }
- }
- reset();
- delay_ms(time);
- }
- }
- void yigedianliushui(unsigned long color,unsigned int time) //一个IC流水到左边
- {
- unsigned char j,x,m;
- unsigned long led,xishu,xishu1;
- led=0x01;
- xishu=1;
- xishu1=0;
- for(x=0;x<LED_N;x++)
- {
- for(j=12;j>0;j--)
- {
- led=_crol_(led,1);
- for(m=0;m<8;m++)
- {
- xishu1=xishu*2;
- xishu1=led/xishu;
- if(xishu1==1)
- {
- DisP_Color(color);
- }
- else
- {
- DisP_Color(0x9f9f9f);
- }
- }
- }
- reset();
- delay_ms(time);
- }
- }
- void Breathing_lamp(unsigned long color,unsigned int time)
- {
- unsigned char j,x,r,g,b,move;
- unsigned long color0,color1;
- r=color>>8;
- g=color;
- b=color>>16;
- if(r!=0)
- {
- color0=r;move=8;
- }
- if(g!=0)
- {
- color0=g;move=0;
- }
- if(b!=0)
- {
- color0=b;move=16;
- }
- for(x=0;x<127;x++)
- {
- color1=color0<<move;
- for(j=0;j<LED_N;j++)
- {
- DisP_Color(color1);
-
- }
- color0=color0-2;
- reset();
- delay_ms(time);
- }
- for(x=0;x<127;x++)
- {
- color1=color0<<move;
- for(j=0;j<LED_N;j++)
- {
- DisP_Color(color1);
-
- }
- color0=color0+2;
- reset();
- delay_ms(time);
- }
- }
- void Breathing_lamp_Alternate(unsigned int time)
- {
- unsigned char j,x,num,move;
- unsigned long color0,color1;
- for(num=0;num<3;num++)
- {
- color0=0;
- move=8*num;
- for(x=0;x<127;x++)
- {
- color1=color0<<move;
- for(j=0;j<LED_N;j++)
- {
- DisP_Color(color1);
- }
- color0=color0+2;
- reset();
- delay_ms(time);
- }
- for(x=0;x<127;x++)
- {
- color1=color0<<move;
- for(j=0;j<LED_N;j++)
- {
- DisP_Color(color1);
-
- }
- color0=color0-2;
- reset();
- delay_ms(time);
- }
- }
- }
- /*
- *********************************************************************************************************
- * 函 数 名: WaterLampToCol2
- * 功能说明: 单组灯移位显示
- * 形 参:_uColor 点亮颜色
- * _uTime 移位速度
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void WaterLampToCol2(unsigned long _uColor, unsigned int _uTime)
- {
- unsigned char i, j;
- unsigned long Red2,Green2, Blue2; // 三原色
-
- Red2 = _uColor>>8;
- Green2 = _uColor>>16;
- Blue2 = _uColor;
-
- for (i = 0; i < nWs; i++)
- {
- for (j = 0; j < i; j++)
- {
- // Red2 = Red2 + 0x22;
- //Green2 = Green2 + 0x22;
- Blue2 = Blue2 + 0x22;
- _uColor = Green2<<16 | Red2<<8 | Blue2;
-
- // _uColor = _uColor + 0xfffff*i;
- WsDat[j] = _uColor;
- }
- WS_L_SetAll();
- delay_ms(_uTime);
- // memset(WsDat, 0, sizeof(WsDat));
- }
- }
- /*
- *********************************************************************************************************
- * 函 数 名: WaterLampToCol3
- * 功能说明: 从中点向两端移动
- * 形 参:_uColor 点亮颜色
- * _uTime 移位速度
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void WaterLampToCol3(unsigned long _uColor, unsigned int _uTime)
- {
- unsigned char i;
-
- for (i = nWs/2; i < nWs; i++)
- {
- _uColor = _uColor + 0xfffff*i;
- WsDat[i] = _uColor;
- WsDat[nWs/2 -(i - nWs/2)] = _uColor;
- WS_L_SetAll();
- delay_ms(_uTime);
- }
- }
- void WS_L_SetAll()
- {
- unsigned char j;
-
- for (j=0; j<nWs; j++)
- {
- DisP_Color(WsDat[j]); // j 级联个数/ 0
- }
- reset();
- }
- /*
- *********************************************************************************************************
- * 函 数 名: WaterLampToCol12(可用)
- * 功能说明: 从后向前移动(跳一个)
- * 形 参:_uColor 点亮颜色
- * _uTime 移位速度
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void WaterLampToCol12(unsigned long _uColor0,unsigned long _uColor1, unsigned int _uTime)
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有程序51hei提供下载:
caideng - 七彩灯带 各种 104IC.zip
(142.88 KB, 下载次数: 565)
|