基于51单片机12c5a60s2的彩灯带流水,可调节亮度,可用手机进行控制,手动调节pwm,自动调节
调光基于12c5a60s2程序:
- /*********************************************************************************************************
- *********************************************************************************************************/
- #include<STC12C5A60S2.h>
- //#include<LCD1602.h>
- #include<PWM.h>
- #include<ADC.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define FSCLK 11059200
- #define Number 29
- unsigned char Receive,i;
- unsigned int n,flag;
- unsigned char Receive_table[15];
- #define RELOAD_COUNT 0xFA
-
- sbit LED0=P0^2;
- sbit DAI = P3^2;
- sbit k1=P2^0;
- sbit k2=P2^1;
- sbit k3=P2^2;
- sbit k4=P2^3;
- sbit k5=P2^4;
- sbit k6=P2^5;
- sbit k7=P2^6;
- sbit k8=P2^7;
- sbit p13=P1^3;
- uint speed = 0;
- uchar flag_clr = 0;
- uchar Table_R[8]={ 0, 0,255, 0,255,255,255,0};
- uchar Table_G[8]={255, 0, 0,255, 0,255,255,0};
- uchar Table_B[8]={ 0,255, 0,255,255, 0,255,0};
- uchar code Table[256] = {
- 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
- 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,
- 40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,
- 60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
- 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,
- 100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,
- 120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,
- 140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
- 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
- 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,
- 200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,
- 220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
- 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
- };
- unsigned char CCAPOH,ID;
- float res0,res1;
- /*********************************************************************************************************
- ** 函数功能 :延时函数
- ** 函数说明 :利用软件延时,占用CPU,经调试最小单位大约为1ms
- ** 入口参数 :time:需要延时的时间,单位ms
- ** 出口参数 :无
- *********************************************************************************************************/
- void Delay_ms(uint time)
- {
- uint i,j;
- for(i = 0;i < time;i ++)
- for(j = 0;j < 930;j ++);
- }
- void ms_delay(unsigned int t)
- {
- unsigned int i,j;
- for(i=t;i>0;i--)
- for(j=110;j>0;j--);
- }
- void us_delay(unsigned int t)
- {
- while(t--);
- }
- void rst()
- {
- uint s;
- DAI=0;
- for(s=0;s<1000;s++);
- {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
- }
- void Delay_Fash (uint x) // 1ms Delay
- {
- uint t;
- t = 0;
- while (x--)
- {
- for (t = 0; t < 5000; t++);
- }
- }
- void Delay(uint x) // 1ms Delay
- {
- uint t;
- t = 0;
- while (x--)
- {
- for (t = 0; t < 330; t++);
- }
- }
- /*uart初始化*/
- void Uart_Init()//使用独立的波特率发生器(STC12C560S2或带有独立波特率发生器//、//的单片机均可)
- {
- SCON=0x50; //设置为串行口以方式1工作,8位异步通讯,允许接收中断。
- PCON=0x80; //SMOD波特率选择位为1,SMOD=1.
- BRT=0xfa;//波特率9600 256-晶振频率/波特率x16=BRT
- AUXR=0x11; //T0、T1不12分频,速度是89C51的12倍,启动独立波特率//发生器,每个时钟计数一次
- //设置串口1的波特率发生器为独立波特率发生器,
- // AUXR1=0x80; //切换到P1口
- ES=1; //开启串口中断
- EA=1; //开启总中断
- }
- void Send_Uart(unsigned char value){//向WIFI模块发送数据
- ES=0;
- TI=0;
- SBUF=value;//发送完后TI变1
- while(!TI);
- TI=0;
- ES=1;
- }
- /*给esp8266发送指令*/
- void ESP8266_Set(unsigned char *puf)
- {
- while(*puf){
- Send_Uart(*puf);
- us_delay(5);
- puf++;
- }
- us_delay(5);
- Send_Uart('\r');
- us_delay(5);
- Send_Uart('\n');
- ms_delay(20000);
- }
- void code0()
- {
- DAI=1;
- _nop_();
- DAI=0;
- _nop_(); _nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
-
- void code1()
- {
- DAI=1;
- _nop_(); _nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- DAI=0;
- _nop_();_nop_(); _nop_(); _nop_();
- }
- //打开灯
- void bright()
- { unsigned int j;
- for(j=0;j<720;j++)code1();
- }
- //关闭灯函数
- void dark()
- { unsigned int j;
-
- for(j=0;j<720;j++)code0();
- }
- void send_2811_24bits(uchar dat1,uchar dat2,uchar dat3)
- {
- uchar a;
- // uchar b;
- for(a=0;a<8;a++) //dat1
- {
- if(dat1&0x80)
- {
- code1();
- }
- else
- {
- code0();
- }
- dat1=dat1<<1;
- }
- for(a=0;a<8;a++) //dat2
- {
- if(dat2&0x80)
- {
- code1();
- }
- else
- {
- code0();
- }
- dat2=dat2<<1;
- }
- for(a=0;a<8;a++) //dat3
- {
- if(dat3&0x80)
- {
- code1();
- }
- else
- {
- code0();
- }
- dat3=dat3<<1;
- }
- }
-
-
-
- void Jump()
- {
- uint i;
- uchar Colour = 0; //这个参数值域:0~7
- uchar Count = 0; //跳变次数 //这个参数值域:0~1
- speed = 150;
- while(1)
- { if(k5==0)
- {
- Delay_ms(5);
- if(k5==0)
- {
- return;
- ID=0;
- }
- }
- for(i=0;i<50;i++) //Number = 50
- {
- send_2811_24bits(Table_R[Colour],Table_G[Colour],Table_B[Colour]);
- }
- rst();
- Delay(speed);
-
- Colour++;
- if(Colour>=7)
- {
- Colour = 0;
- Count++;
- if(Count>1)
- {
- Count = 0;
- return;
- }
- }
- }
- }
- void Flow()
- {
- uint i,Stack;
- uchar Colour1,Colour2;
- Stack = 0;
- Colour1 = 0;
- Colour2 = 6;
-
- speed=5; //17
-
- while(1)
- { if(k5==0)
- {
- Delay_ms(5);
- if(k5==0)
- {
- return;
- ID=0;
- }
- }
- for(i=0;i<Number;i++)
- {
- if(i<Stack)
- {
- send_2811_24bits(Table_R[Colour1],Table_G[Colour1],Table_B[Colour1]);
- }
- else
- {
- send_2811_24bits(Table_R[Colour2],Table_G[Colour2],Table_B[Colour2]);
- }
- }
- rst();
- Delay_Fash(speed);
- Stack++;
-
- if(Stack>Number)
- {
- Stack = 1;
- Colour2 = Colour1;
- Colour1++;
- if(Colour1>6)
- {
- return;
- }
- }
- }
- }
- void Pass()
- {
- int i,Stack,Stack_Buf;
- uchar Pass_Sec;
- char Colour,Colour_Buf;
- uchar TIMES=0;
-
- Pass_Sec = 12;
- Colour_Buf = 0;
- Stack_Buf = 0;
- speed = 5;
- while(1)
- { if(k5==0)
- {
- Delay_ms(5);
- if(k5==0)
- {
- return;
- ID=0;
- }
- }
- Stack = Stack_Buf;
- Colour = Colour_Buf;
- for(i=0;i<Number;i++)
- {
- send_2811_24bits(Table_R[Colour],Table_G[Colour],Table_B[Colour]);
- Stack++;
- if(Stack > Pass_Sec)
- {
- Stack = 0;
- Colour++;
- if(Colour>6)
- {
- Colour=0;
- }
- }
- }
- rst();
- Delay_Fash(speed);
- Stack_Buf--;
- {
- if(Stack_Buf<0)
- {
- Stack_Buf = Pass_Sec;
- Colour_Buf--;
- if(Colour_Buf<0)
- {
- Colour_Buf = 6;
- }
- if(Colour_Buf==0)
- {
- TIMES++;
- if(TIMES>1)
- {
- return;
- }
- }
- }
- }
- }
- }
- void Case()
- {
- int i,Stack,Stack_Buf,TIMES;
- uchar Pass_Sec;
- char Colour,Colour_Buf;
- TIMES=0;
- Pass_Sec=7;
- Colour_Buf=0;
- Stack_Buf=0;
- speed=10;
- while(1)
- {
- if(k5==0)
- {
- Delay_ms(5);
- if(k5==0)
- {
- return;
- ID=0;
- }
- }
- Stack=Stack_Buf;
- Colour=Colour_Buf;
- for(i=0;i<Number;i++)
- {
- if(Stack<4)
- {
- send_2811_24bits(Table_R[Colour],Table_G[Colour],Table_B[Colour]);
- }
- else
- {
- send_2811_24bits(255,255,255);
- }
- Stack++;
- if(Stack>Pass_Sec)
- {
- Stack=0;
- }
- }
- rst();
- Delay_Fash(speed);
- Stack_Buf--;
- {
- if(Stack_Buf<0)
- {
- Stack_Buf=Pass_Sec;
- }
- }
- TIMES++;
- if(TIMES>120)
- {
- TIMES=0;
- Colour_Buf++;
- if(Colour_Buf>5)
- {
- return;
- }
- }
- }
- }
- void adchange()
- {
- res0=GetADCResult(ADC_CH0);
- CCAP0H =256-ADC_RES;
- res1=GetADCResult(ADC_CH1);
- }
- void key()
- {
- if(CCAP0H<255)
- {
- if(k1==0)
- {
- Delay_ms(5);
- if(k1==0)
- {
- CCAP0H++;
- }
- }
- }
-
- if( CCAP0H>0)
- {
- if(k2==0)
- {
- Delay_ms(5);
- if(k2==0)
- {
- CCAP0H--;
- }
- }
- }
- }
- void changecolour1()
- {unsigned char i;
- rst();
- for(i=0;i<30;i++)
- {
- if(k5==0)
- {
- Delay_ms(5);
- if(k5==0)
- {
- return;
- ID=0;
- }
- }
- send_2811_24bits(0,255,0);
- }
- }
- void changecolour2()
- {unsigned char i;
- rst();
- for(i=0;i<30;i++)
- {
- if(k5==0)
- {
- Delay_ms(5);
- if(k5==0)
- {
- return;
- ID=0;
- }
- }
- send_2811_24bits(0,0,255);
- }
- }
- void changecolour3()
- {unsigned char i;
- rst();
- for(i=0;i<30;i++)
- {
- if(k5==0)
- {
- Delay_ms(5);
- if(k5==0)
- {
- return;
- ID=0;
- }
- }
- send_2811_24bits(255,0,0);
- }
- }
-
- void shouji()
- {
- if(Receive=='0')
- {
- ID=6;
- CCAP0H=30;
- }
- if(Receive=='1')
- {
- ID=6;
- CCAP0H=60;
- }
- if(Receive=='2')
- {
- ID=6;
- CCAP0H=90;
- }
- if(Receive=='3')
- {
- ID=6;
- CCAP0H=120;
- }
- if(Receive=='4')
- {
- ID=6;
- CCAP0H=150;
- }
- if(Receive=='5')
- {
- ID=6;
- CCAP0H=180;
- }
- if(Receive=='6')
- {
- ID=6;
- CCAP0H=210;
- }
- if(Receive=='7')
- {
- ID=6;
- CCAP0H=254;
- }
- if(Receive=='d')
- {
- ID=6;
- CCAP0H=0;
- }
- }
- void main()
- {
- CCAP0H=254;
- ID=0;
- TMOD|=0X10;
- TH1=0XFC; //给定时器赋初值,定时1ms
- TL1=0X18;
- EA=1;
- TR1=1;
- ET1=1;
-
- Uart_Init();
- PCA_init();
- Timer0_init();
- ADC_Init(ADC_PORT0 | ADC_PORT1); //配置通道P1^0 ,P1^1 为AD采集口,如要配置其他其他口,直接用或运算加进初始化函数中
-
- ESP8266_Set("AT+CWMODE=3");//选择wifi应用模式,此时为STA模式
- ms_delay(3000);
- // ESP8266_Set("AT+RST");
- ESP8266_Set("AT+CWJAP=\"WIFI\",\"12345678\"");//连接网络
- ms_delay(15000);
- ESP8266_Set("AT+CIPMUX=0");//设置单连接模式,透传只能在单连接模式下进行
- ms_delay(5000);
- ESP8266_Set("AT+CIPMODE=1");//设置透传模式
- ms_delay(5000);
- ESP8266_Set("AT+CIPSTART=\"TCP\",\"10.50.84.160\",8080");//连接到服务器
- while(1)
- {
- shouji();
- switch(ID)
- {
- case(0):bright();key();break;
- case(1):bright();adchange();break;
- case(2):Flow();Case();Pass();Jump();break;
- case(3):changecolour1();break;
- case(4):changecolour2();break;
- case(5):changecolour3();break;
- case(6):bright();break;
- }
- }
- }
- void timer1() interrupt 3
- {
- if(k3==0)
- {
- Delay_ms(5);
- if(k3==0)
- {
- ID=1;
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
全部资料下载地址:
调光基于12c5a60s2.zip
(60.05 KB, 下载次数: 309)
|