#include "stc8g.h"
#include <intrins.h>
#include <stdlib.h>
#include <stdio.h>
//******************************说明:使用22.1184Mhz;
//******************************说明:使用22.1184Mhz;
//******************************说明:使用22.1184Mhz;
//******************************说明:使用22.1184Mhz;
//定时器0,闪烁的定时器
//定时器1,预留
//定时器2作为串口1de 波特率发生器
//定时器3作为预留
//定时器4,作为系统时钟,上电运行之后,的时钟,参考arduino的millis函数,读取的就是定时器的时钟;
#define byte unsigned char
//************************************************************************全局变量
unsigned long Global_System_Tim0_Ms=0;
bit busy;
//***variables***variables***variables***variables***variables***variables***variables***variables
//These variables are used by multiplexing and Bit Angle Modulation Code
int shift_out;//used in the code a lot in for(i= type loops
unsigned char anode[8];//byte to write to the anode shift register, 8 of them, shifting the ON level in each byte in the array
//This is how the brightness for every LED is stored,
//Each LED only needs a 'bit' to know if it should be ON or OFF, so 64 Bytes gives you 512 bits= 512 LEDs
//Since we are modulating the LEDs, using 4 bit resolution, each color has 4 arrays containing 64 bits each
unsigned char red0[64], red1[64], red2[64], red3[64];
byte blue0[64], blue1[64], blue2[64], blue3[64];
byte green0[64], green1[64], green2[64], green3[64];
//notice how more resolution will eat up more of your precious RAM
int level=0;//keeps track of which level we are shifting data to
int anodelevel=0;//this increments through the anode levels
int BAM_Bit, BAM_Counter=0; // Bit Angle Modulation variables to keep track of things
//These variables can be used for other things
unsigned long start;//for a millis timer to cycle through the animations
int globalRed, globalGreen, globalBlue;
byte messageDrawScrollArray[15];
//***********************************************************************宏定义位显示
#define LongToBin(n) \
(\
((n >> 21) & 0x80) | \
((n >> 18) & 0x40) | \
((n >> 15) & 0x20) | \
((n >> 12) & 0x10) | \
((n >> 9) & 0x08) | \
((n >> 6) & 0x04) | \
((n >> 3) & 0x02) | \
((n ) & 0x01) \
)
#define B(n) LongToBin(0x##n##l)
//*******************************位操作
//取低字节
#define lowByte(w) ((w) & 0xff)
//取高字节
#define highByte(w) ((w) >> 8)
//读一个bit
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
//写一个bit
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
//设置一个bit
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
//清空一个bit
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
//生成相应bit
#define bit(b) (1 << (b))
//******************************************位操作结束
//***********************************************************引脚设置
sbit LEDtestpin=P1^7;
sbit HC595_LATCH = P1^2;//latch pin or rck pin STCP ss 595pin12
sbit HC595_OE = P1^1;//oe blank pin 595pin13
sbit HC595_dat=P1^3;
sbit HC595_sck=P1^5;
// 0data of 74hc595 pin14 ---->mosi P13
//clk of 595 shcp 595 pin11 ---->sclk P15
//P_SW1 = 0x00; //SS/P1.2, MOSI/P1.3, MISO/P1.4, SCLK/P1.5
#define ESPI 0x02
//******************************************************************引脚设置结束
//****************************************************************************************************************函数声明
void portmode();
void Delay500ms(); //@22.1184MHz
void delay(int tms); //@22.1184MHz
void Delay1ms() //@22.1184MHz
{
unsigned char i, j;
i = 29;
j = 183;
do
{
while (--j);
} while (--i);
}
void Delay10us() //@22.1184MHz
{
unsigned char i;
_nop_();
_nop_();
i = 71;
while (--i);
}
//SPI函数
// 0data of 74hc595 pin14 ---->mosi P13
//clk of 595 shcp 595pin11 ---->sclk P15
void SendTo595( unsigned char byteData)
{
char i=0;
for(;i<8;i++)
{
P13 = byteData>>7;
//0000 0001 >>1 0000 0000
//0000 0001 <<1 0000 0010
byteData= byteData<<1;
HC595_sck = 0;
Delay10us();
HC595_sck = 1;
}
HC595_LATCH = 0; //p10
Delay10us();
HC595_LATCH =1;
HC595_OE=0;
}
void SpiTransfer(unsigned char transferbyteTemp);
//定时器0,获取系统的时间,10ms为单位,
void Timer0Init(void) ; //1毫秒@22.1184MHz
unsigned long Get_millis(void);
//UART1 发送串口数据,字符串
void Uart1_Init(void); //115200bps@22.1184Mhz
void UartSendStr(char *p); //串口发送字符串
void UART1_SendData(char dat);//串口发送单个字节
//随机函数
int randlimit(int limitmaxnum);//随机函数,产生0-limitmaxnum
int randbetween(int minnum,int maxnum);//随机函数,产生minnum -- maxnum之间。
//定时器1,产生LED灯闪烁的时间间隔,产生约8000hZ的频率
//125us进入一次定时器中断。定时时间是125us。
void Timer1Init(void); //125微秒@22.1184MHz
//LED闪烁函数
void clean();
void Timer1Init(void); //125微秒@22.1184MHz
void test();
void upDown (unsigned int iterations);
unsigned char strlen(unsigned char *str);
float map(float val,float I_Min,float I_Max,float O_Min,float O_Max);
void shift_all_layers(int layerShift);
void draw_snake(int ii,int iiterations,int aa, int bb, int cc, int dd, int ee, int ff);
void snakes(int iterations);
void wipe_out();
//**************************************************************************************************************函数声明结束
unsigned char reverse_bit(unsigned char num)
{
unsigned char i;
bit x;
unsigned char new_num = 0;
for (i = 0; i < 8; i++)
{
x = num & 1; //取出最后一位
new_num <<= 1; //新数左移
new_num =new_num | x; //把刚取出的一位加到新数
num >>= 1; //原数右移,准备取第二位
}
return new_num;
}
void main()
{
// int c;
unsigned char keycnt=0;
unsigned char keyflag=0;
long starttime=0;
busy = 0;
portmode();
Timer0Init();
Uart1_Init();
Timer1Init();
EA=1;//打开总中断
//TR0=1;
// srand(1);
// while(1)
// {
// if(P54==0)
// {
// printf("hello1\r\n");
// break;
// }
// }
printf("h1\r\n");
Delay500ms();
// printf("h2\r\n");
// Delay500ms();
// printf("h3\r\n");
// Delay500ms();
//
// SPCTL = 0x50; //使能SPI主机模式
// SPSTAT = 0xc0; //清中断标志
// IE2 = ESPI; //使能SPI中断
// HC595_LATCH = 1;
// busy = 0;
// HC595_LATCH =1;
// HC595_OE =0;
// LEDtestpin=0;Delay500ms();
// LEDtestpin=1;Delay500ms();
//
// LEDtestpin=0;Delay500ms();
// LEDtestpin=1;Delay500ms();
// printf("hello22\r\n");
// test();
// Delay500ms();Delay500ms();Delay500ms();Delay500ms();Delay500ms();Delay500ms();
// clean();
// Delay500ms();Delay500ms();Delay500ms();Delay500ms();Delay500ms();Delay500ms();
// test();
// Delay500ms();Delay500ms();Delay500ms();Delay500ms();Delay500ms();Delay500ms();
// clean();
// Delay500ms();Delay500ms();Delay500ms();Delay500ms();Delay500ms();Delay500ms();
while(1)
{
if(P54==0)
{
delay(10);
if(P54==0)
{
while(P54==0);
printf("keypress=%bd\r\n",keycnt);
if(keycnt==0)keycnt=1;
else if(keycnt==1)keycnt=0;
keyflag=1;
}
}
if(keycnt==0&&keyflag==1)
{
printf("test\r\n");
//test();
keyflag=0;
HC595_OE=1;
//0点亮,1不亮
//第R,先发
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
//G,
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
SendTo595(reverse_bit(~B(00000000)));
//B,
//B-----C
//A-----D
SendTo595(reverse_bit(~B(10110100)));//立方体的A角--D角
SendTo595(reverse_bit(~B(10000001)));
SendTo595(reverse_bit(~B(10001111)));
SendTo595(reverse_bit(~B(11001111)));
SendTo595(reverse_bit(~B(11111111)));
SendTo595(reverse_bit(~B(11000001)));
SendTo595(reverse_bit(~B(10100001)));
SendTo595(reverse_bit(~B(11101100)));//立方体的B角--C角
//层
SendTo595(0x80);
HC595_LATCH=1;
Delay10us();
HC595_LATCH=0;
HC595_OE=0;
}
else if(keycnt==1&&keyflag==1)
{
printf("clean\r\n");
//clean();
keyflag=0;
HC595_OE=1;
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
SendTo595(0x00);
// for(shift_out=0; shift_out<8; shift_out++)
// SendTo595(red0[shift_out]);
// for(shift_out=0; shift_out<+8; shift_out++)
// SendTo595(green0[shift_out]);
// for(shift_out=0; shift_out<+8; shift_out++)
// SendTo595(blue0[shift_out]);
//
// SendTo595(anode[0]);//finally, send out the anode level byte
HC595_LATCH=1;
Delay10us();
HC595_LATCH=0;
HC595_OE=0;
// HC595_OE=1;
// SendTo595(0x00);
// for(shift_out=0; shift_out<8; shift_out++)
// SendTo595(red0[shift_out]);
// for(shift_out=0; shift_out<+8; shift_out++)
// SendTo595(green0[shift_out]);
// for(shift_out=0; shift_out<+8; shift_out++)
// SendTo595(blue0[shift_out]);
//
// SendTo595(anode[0]);//finally, send out the anode level byte
// HC595_LATCH=1;
// _nop_();_nop_();_nop_();_nop_();
// HC595_LATCH=0;
// HC595_OE=0;
}
//upDown(10);
//snakes(20);
//wipe_out();
}
}
//***********************************************************************子函数
//端口设置函数
void portmode(void)
{
P0M0=0x00;P0M1=0x00;
P1M0=0x00;P1M1=0x00;
P2M0=0x00;P2M1=0x00;
P3M0=0x00;P3M1=0x00;
P4M0=0x00;P4M1=0x00;
P5M0=0x00;P5M1=0x00;
P6M0=0x00;P6M1=0x00;
P7M0=0x00;P7M1=0x00;
}
void delay(int tms) //@22.1184MHz
{
unsigned char i, j;
i = 29;
j = 183;
while(--tms)
{
while (--j);
} while (--i);
}
//**********************************************************************SPI中断
void SPI_Isr() interrupt 9
{
SPSTAT = 0xc0; //清中断标志
HC595_LATCH = 1; //拉高从机的SS管脚
busy = 0;
// LED = !LED; //测试端口
}
//********************************************************************SPI传输函数
//输入参数:参数1:传输的字节
void SpiTransfer(unsigned char transferbyteTemp)
{
busy = 1;
HC595_LATCH = 0; //拉低从机SS管脚
SPDAT = transferbyteTemp; //发送测试数据
while (busy);
}
//定时器初始化函数,用于计算单片机开机之后运行的时间,10ms统计一次;
void Timer0Init(void) //10毫秒@22.1184MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初始值
TH0 = 0xF8; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 0; //定时器0开始计时
ET0=1; //使能定时器中断
}
void TM0_Isr() interrupt 1
{
// P10 = !P10; //测试端口
Global_System_Tim0_Ms++;
if(Global_System_Tim0_Ms>2000000000UL)//2,147,483,648
{
Global_System_Tim0_Ms=0;
}
}
//*******************************获取当前的时间
unsigned long Get_millis(void)
{
return Global_System_Tim0_Ms;
}
//串口1,用定时器2产生波特率115200,定时器2产生,
void Uart1_Init(void) //115200bps@22.1284000MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0xFC; //设置定时初始值
T2H = 0xFF; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
}
//重写putchar函数,可以用printf函数
char putchar(char c)
{
UART1_SendData(c);
return c;
}
//UART1 发送串口数据,一个字节
void UART1_SendData(char dat)
{
ES=0; //关串口中断
SBUF=dat;
while(TI!=1); //等待发送成功
TI=0; //清除发送中断标志
ES=1; //开串口中断
}
//UART1 发送串口数据,字符串
void UartSendStr(char *p)
{
while (*p)
{
UART1_SendData(*p++);
}
}
//延时500ms
void Delay500ms() //@22.1184MHz
{
unsigned char i, j, k;
i = 57;
j = 27;
k = 112;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
//*************************************************************随机函数
int randlimit(int limitmaxnum)
{
return rand()%limitmaxnum;
}
int randbetween(int minnum,int maxnum)
{
return rand() %(maxnum-minnum+1)+minnum;
}
//*******************************************************************************LED代码开始
void Timer1Init(void) //125微秒@22.1184MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x33; //设置定时初值
TH1 = 0xF5; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 0; //定时器1开始计时
}
void LED(int level, int row, int column, unsigned char red, unsigned char green, unsigned char blue)
{
//****LED Routine****LED Routine****LED Routine****LED Routine
int whichbyte;
int wholebyte;
//This is where it all starts
//This routine is how LEDs are updated, with the inputs for the LED
//location and its R G and B brightness levels
// First, check and make sure nothing went beyond the limits,
//just clamp things at either 0 or 7 for location, and 0 or 15 for brightness
if(level<0)level=0;
if(level>7)level=7;
if(row<0)row=0;
if(row>7)row=7;
if(column<0)column=0;
if(column>7)column=7;
if(red<0)red=0;
if(red>15)red=15;
if(green<0)green=0;
if(green>15)green=15;
if(blue<0)blue=0;
if(blue>15)blue=15;
//There are 512 LEDs in the cube, so when we write to level 2, column 5, row 4, that needs to be translated into a number from 0 to 511
//This looks confusing, I know...
whichbyte = (int)(((level*64)+(row*8)+column)/8);
// The first level LEDs are first in the sequence, then 2nd level, then third, and so on
//the (level*64) is what indexes the level's starting place, so level 0 are LEDs 0-63, level 1 are LEDs 64-127, and so on
//The column counts left to right 0-7 and the row is back to front 0-7
//This means that if you had level 0, row 0, the bottom back row would count from 0-7,
//so if you looked down on the cube, and only looked at the bottom level
// 00 01 02 03 04 05 06 07
// 08 09 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
//Then, if you incremented the level, the top right of the grid above would start at 64
//The reason for doing this, is so you don't have to memorize a number for each LED, allowing you to use level, row, column
//Now, what about the divide by 8 in there?
//...well, we have 8 bits per byte, and we have 64 bytes in memory for all 512 bits needed for each LED, so
//we divide the number we just found by 8, and take the integ7er of it, so we know which byte, that bit is located
//confused? that's ok, let's take an example, if we wanted to write to the LED to the last LED in the cube, we would write a 7, 7, 7
// giving (7*64)+(7*8)=7 = 511, which is right, but now let's divide it by 8, 511/8 = 63.875, and take the int of it so, we get 63,
//this is the last byte in the array, which is right since this is the last LED
// This next variable is the same thing as before, but here we don't divide by 8, so we get the LED number 0-511
wholebyte=(level*64)+(row*8)+column;
//This will all make sense in a sec
//This is 4 bit color resolution, so each color contains x4 64 byte arrays, explanation below:
bitWrite(red0[whichbyte], wholebyte-(8*whichbyte), bitRead(red, 0));
bitWrite(red1[whichbyte], wholebyte-(8*whichbyte), bitRead(red, 1));
bitWrite(red2[whichbyte], wholebyte-(8*whichbyte), bitRead(red, 2));
bitWrite(red3[whichbyte], wholebyte-(8*whichbyte), bitRead(red, 3));
bitWrite(green0[whichbyte], wholebyte-(8*whichbyte), bitRead(green, 0));
bitWrite(green1[whichbyte], wholebyte-(8*whichbyte), bitRead(green, 1));
bitWrite(green2[whichbyte], wholebyte-(8*whichbyte), bitRead(green, 2));
bitWrite(green3[whichbyte], wholebyte-(8*whichbyte), bitRead(green, 3));
bitWrite(blue0[whichbyte], wholebyte-(8*whichbyte), bitRead(blue, 0));
bitWrite(blue1[whichbyte], wholebyte-(8*whichbyte), bitRead(blue, 1));
bitWrite(blue2[whichbyte], wholebyte-(8*whichbyte), bitRead(blue, 2));
bitWrite(blue3[whichbyte], wholebyte-(8*whichbyte), bitRead(blue, 3));
//Are you now more confused? You shouldn't be! It's starting to make sense now. Notice how each line is a bitWrite, which is,
//bitWrite(the byte you want to write to, the bit of the byte to write, and the 0 or 1 you want to write)
//This means that the 'whichbyte' is the byte from 0-63 in which the bit corresponding to the LED from 0-511
//Is making sense now why we did that? taking a value from 0-511 and converting it to a value from 0-63, since each LED represents a bit in
//an array of 64 bytes.
//Then next line is which bit 'wholebyte-(8*whichbyte)'
//This is simply taking the LED's value of 0-511 and subracting it from the BYTE its bit was located in times 8
//Think about it, byte 63 will contain LEDs from 504 to 511, so if you took 505-(8*63), you get a 1, meaning that,
//LED number 505 is is located in bit 1 of byte 63 in the array
//is that it? No, you still have to do the bitRead of the brightness 0-15 you are trying to write,
//if you wrote a 15 to RED, all 4 arrays for that LED would have a 1 for that bit, meaning it will be on 100%
//This is why the four arrays read 0-4 of the value entered in for RED, GREEN, and BLUE
//hopefully this all makes some sense?
}
void TM1_Isr() interrupt 3 using 1
{
// P10 = !P10; //测试端口
//This routine is called in the background automatically at frequency at 8000hz
//In this code, this is called every 125us, giving each level in the cube 124us of ON time
//There are 8 levels, so we have a maximum brightness of 1/8,
//since the level must turn off before the next level is turned on
//The frequency of the multiplexing is then 124us*8=992us, or 1/992us= about 1kHz
//The first thing we do is turn all of the LEDs OFF, by writing a 1 to the blank pin
//Note, in my bread-boarded version, I was able to move
//this way down in the cube, meaning that the OFF time was minimized
//do to signal integrity and parasitic capcitance, my rise/fall times,
//required all of the LEDs to first turn off, before updating
//otherwise you get a ghosting effect on the previous level
//添加代码
//关闭所有的LED灯,发送关闭LED灯的命令,
HC595_OE=1;
SendTo595(0x00);
//This is 4 bit 'Bit angle Modulation' or BAM, There are 8 levels, so when a '1' is written to the color brightness,
//each level will have a chance to light up for 1 cycle, the BAM bit keeps track of which bit we are modulating out of the 4 bits
//Bam counter is the cycle count, meaning as we light up each level, we increment the BAM_Counter
if(BAM_Counter==8)
BAM_Bit++;
else
if(BAM_Counter==24)
BAM_Bit++;
else
if(BAM_Counter==56)
BAM_Bit++;
BAM_Counter++;//Here is where we increment the BAM counter
switch (BAM_Bit){//The BAM bit will be a value from 0-3, and only shift out the arrays corresponding to that bit, 0-3
//Here's how this works, each case is the bit in the Bit angle modulation from 0-4,
//Next, it depends on which level we're on, so the byte in the array to be written depends on which level, but since each level contains 64 LED,
//we only shift out 8 bytes for each color
case 0:
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(red0[shift_out]);
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(green0[shift_out]);
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(blue0[shift_out]);
break;
case 1:
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(red1[shift_out]);
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(green1[shift_out]);
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(blue1[shift_out]);
break;
case 2:
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(red2[shift_out]);
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(green2[shift_out]);
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(blue2[shift_out]);
break;
case 3:
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(red3[shift_out]);
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(green3[shift_out]);
for(shift_out=level; shift_out<level+8; shift_out++)
SendTo595(blue3[shift_out]);
//Here is where the BAM_Counter is reset back to 0, it's only 4 bit, but since each cycle takes 8 counts,
//, it goes 0 8 16 32, and when BAM_counter hits 64 we reset the BAM
if(BAM_Counter==120)
{
BAM_Counter=0;
BAM_Bit=0;
}
break;
}//switch_case
SendTo595(anode[anodelevel]);//finally, send out the anode level byte
// PORTL |= 1<<5;//Latch pin HIGH
// PORTL &= ~(1<<5);//Latch pin LOW
// PORTL &= ~(1<<4);//Blank pin LOW to turn on the LEDs with the new data
HC595_LATCH=1;
_nop_();_nop_();_nop_();_nop_();
HC595_LATCH=0;
HC595_OE=0;
anodelevel++;//inrement the anode level
level = level+8;//increment the level variable by 8, which is used to shift out data, since the next level woudl be the next 8 bytes in the arrays
if(anodelevel==8)//go back to 0 if max is reached
anodelevel=0;
if(level==64)//if you hit 64 on level, this means you just sent out all 63 bytes, so go back
level=0;
// pinMode(blank_pin, OUTPUT);//moved down here so outputs are all off until the first call of this function
//***MultiPlex BAM END***MultiPlex BAM END***MultiPlex BAM END***MultiPlex BAM END***MultiPlex BAM END***MultiPlex BAM END***MultiPlex BAM END
}
// ********* CLEAN UP ***************
void clean()
{
int ii, jj, kk;
for(ii=0; ii<8; ii++)
for(jj=0; jj<8; jj++)
for(kk=0; kk<8; kk++)
LED(ii,jj,kk,0,0,0);
}
//函数test
void test()
{
int i=0;
int OnOff=0;
for ( i=0;i<8;i++){
OnOff = 1;
LED (i,7,0, OnOff * 15, OnOff * 15, OnOff * 15);
LED (i,7,0, OnOff * 15, OnOff * 15, OnOff * 15);
}//endfor
} // end test
void upDown (unsigned int iterations){
unsigned int fillArray[64];
unsigned int gArray[8] = {15, 13, 11, 9, 7, 5, 3, 0};
unsigned int bArray[8] = {0, 3, 5, 7, 9, 11, 13, 15};
int i=0;
int x =0;
int j=0;
int r=0;
int c =0;
int k=0;
int reps=0;
for ( i=0;i<64;i++){
x = randlimit(2);
if (x==1){
fillArray[i] = 7;}
else {
fillArray[i] = 0;}
} // end i
for ( j=0;j<64;j++){ // randon fill top and bottom layer
r = j / 8;
c = j - (r * 8);
LED (fillArray[j] ,r , c , 0, gArray[fillArray[j]], bArray[fillArray[j]]);
} // end j
for ( reps=0;reps<iterations;reps++){
x = randlimit(64);
r = x / 8;
c = x - (r * 8);
if (fillArray[x] == 0){
for ( k=1;k<8;k++){
LED (k-1, r , c , 0,0,0);
LED (k, r , c , 0, gArray[k], bArray[k]);
delay (25);
}
}
else {
for ( k=6;k>=0;k--){
LED (k+1, r , c , 0,0,0);
LED (k, r , c , 0, gArray[k], bArray[k]);
delay (25);
}
} // end else
} // end reps
clean();
} // end upDown
//得到字符串的长度(字节)
//*str:字符串指针
//返回值:字符串的长度
unsigned char strlen(unsigned char *str)
{
unsigned char len=0;
while(1)
{
if(*str=='\0')break;//拷贝完成了.
len++;
str++;
}
return len;
}
//https://wenku.baidu.com/view/5c069166ab956bec0975f46527d3240c8547a175.html
//map函数
float map(float val,float I_Min,float I_Max,float O_Min,float O_Max)
{
return (((val-I_Min)*((O_Max-O_Min)/(I_Max-I_Min)))+O_Min);
}
//函数:shift_all_layers
void shift_all_layers(int layerShift){
int startLayer = 0;
int x;
int y;
int arrayTo;
int arrayFrom ;
if (layerShift == 1){
startLayer = 6;
}
if (layerShift == -1){
startLayer = 1;
}
for ( x=0;x<7;x++){ //repeat copy row 7 time
for ( y=0;y<8;y++){
arrayTo = (startLayer * 8) + y + (8 * layerShift);
arrayFrom = (startLayer * 8) + y;
red0[arrayTo] = red0[arrayFrom];
red1[arrayTo] = red1[arrayFrom];
red2[arrayTo] = red2[arrayFrom];
red3[arrayTo] = red3[arrayFrom];
green0[arrayTo] = green0[arrayFrom];
green1[arrayTo] = green1[arrayFrom];
green2[arrayTo] = green2[arrayFrom];
green3[arrayTo] = green3[arrayFrom];
blue0[arrayTo] = blue0[arrayFrom];
blue1[arrayTo] = blue1[arrayFrom];
blue2[arrayTo] = blue2[arrayFrom];
blue3[arrayTo] = blue3[arrayFrom];
}
startLayer = startLayer - layerShift;
}
}// end shift_all_layers
//***************函数:蛇
void draw_snake(int ii,int iiterations,int aa, int bb, int cc, int dd, int ee, int ff)
{;
LED(7,aa,bb,0,15,0);
LED(7,aa+1,bb,0,15,0);
LED(7,aa+1,bb+1,0,15,0);
LED(7,aa,bb+1,0,15,0);
if (ii>iiterations/3){
LED(7,cc,dd,0,0,15);
LED(7,cc+1,dd,0,0,15);
LED(7,cc+1,dd+1,0,0,15);
LED(7,cc,dd+1,0,0,15);
}
if (ii>iiterations/3*2){
LED(7,ee,ff,15,0,0);
LED(7,ee+1,ff,15,0,0);
LED(7,ee+1,ff+1,15,0,0);
LED(7,ee,ff+1,15,0,0);
}}// end draw_snake
void snakes(int iterations)
{
int delayTime = 60; // change this variable to slow down or speed up animation
int i;
int x;
int y;
int a = randbetween(1,3);
int b = randbetween(1,3);
int c = randbetween(3,5);
int d = randbetween(3,5);
int e = randbetween(5,7);
int f = randbetween(5,7);
for ( i=0;i<iterations;i++){
for( x=0;x<8;x++){ //Clear level 7
for( y=0;y<8;y++){
LED(7,x,y,0,0,0);
}}
//https://blog.csdn.net/weixin_43894786/article/details/105358975 map函数
a = a + map(randlimit(3),0,2,-1,1);
c = c + map(randlimit(3),0,2,-1,1);
e = e + map(randlimit(3),0,2,-1,1);
if(a<0)a=0;
if(a>5)a=5;
if(c<0)c=0;
if(c>5)c=5;
if(e<0)e=0;
if(e>5)e=5;
draw_snake(i,iterations,a,b,c,d,e,f);
shift_all_layers(-1); // shift all levels down 1
delay(delayTime);
b = b + map(randlimit (3),0,2,-1,1);
d = d + map(randlimit (3),0,2,-1,1);
f = f + map(randlimit (3),0,2,-1,1);
if(b<0)b=0;
if(b>5)b=5;
if(d<0)d=0;
if(d>5)d=5;
if(f<0)f=0;
if(f>5)f=5;
draw_snake(i,iterations,a,b,c,d,e,f);
shift_all_layers(-1); // shift all levels down 1
delay(delayTime);
}}// End Snake
//函数波浪
void wipe_out(){//*****wipe_out*****wipe_out*****wipe_out*****wipe_out*****wipe_out*****wipe_out*****wipe_out*****wipe_out
int xxx=0, yyy=0, zzz=0;
int fx=randlimit(8), fy=randlimit(8), fz=randlimit(8), direct, fxm=1, fym=1, fzm=1, fxo=0, fyo=0, fzo=0;
int ftx=randlimit(8), fty=randlimit(8), ftz=randlimit(8), ftxm=1, ftym=1, ftzm=1, ftxo=0, ftyo=0, ftzo=0;
int select, rr, gg, bb, rrt, ggt, bbt;
for(xxx=0; xxx<8; xxx++){
for(yyy=0; yyy<8; yyy++){
for(zzz=0; zzz<8; zzz++){
LED(xxx, yyy, zzz, 0, 0, 0);
}}}
select=randlimit(3);
if(select==0){
rr=randbetween(1, 16);
gg=randbetween(1, 16);
bb=0;}
if(select==1){
rr=randbetween(1, 16);
gg=0;
bb=randbetween(1, 16);}
if(select==2){
rr=0;
gg=randbetween(1, 16);
bb=randbetween(1, 16);}
select=randlimit(3);
if(select==0){
rrt=randbetween(1, 16);
ggt=randbetween(1, 16);
bbt=0;}
if(select==1){
rrt=randbetween(1, 16);
ggt=0;
bbt=randbetween(1, 16);}
if(select==2){
rrt=0;
ggt=randbetween(1, 16);
bbt=randbetween(1, 16);}
start=Get_millis();
while(Get_millis()-start<10000){
//fx=random(8); fy=random(8); fz=random(8);
LED(fxo, fyo, fzo, 0, 0, 0);
LED(fxo, fyo, fzo+1, 0, 0, 0);
LED(fxo, fyo, fzo-1, 0, 0, 0);
LED(fxo+1, fyo, fzo, 0, 0, 0);
LED(fxo-1, fyo, fzo, 0, 0, 0);
LED(fxo, fyo+1, fzo, 0, 0, 0);
LED(fxo, fyo-1, fzo, 0, 0, 0);
LED(ftxo, ftyo, ftzo, 0, 0, 0);
LED(ftxo, ftyo, ftzo+1, 0, 0, 0);
LED(ftxo, ftyo, ftzo-1, 0, 0, 0);
LED(ftxo+1, ftyo, ftzo, 0, 0, 0);
LED(ftxo-1, ftyo, ftzo, 0, 0, 0);
LED(ftxo, ftyo+1, ftzo, 0, 0, 0);
LED(ftxo, ftyo-1, ftzo, 0, 0, 0);
LED(ftx, fty, ftz, rr, gg, bb);
LED(ftx, fty, ftz+1, rr, gg, bb);
LED(ftx, fty, ftz-1, rr, gg, bb);
LED(ftx+1, fty, ftz, rr, gg, bb);
LED(ftx-1, fty, ftz, rr, gg, bb);
LED(ftx, fty+1, ftz, rr, gg, bb);
LED(ftx, fty-1, ftz, rr, gg, bb);
LED(fx, fy, fz, rrt, ggt, bbt);
LED(fx, fy, fz+1, rrt, ggt, bbt);
LED(fx, fy, fz-1, rrt, ggt, bbt);
LED(fx+1, fy, fz, rrt, ggt, bbt);
LED(fx-1, fy, fz, rrt, ggt, bbt);
LED(fx, fy+1, fz, rrt, ggt, bbt);
LED(fx, fy-1, fz, rrt, ggt, bbt);
delay(10);
fxo=fx;
fyo=fy;
fzo=fz;
ftxo=ftx;
ftyo=fty;
ftzo=ftz;
direct=randlimit(3);
if(direct==0)
fx= fx+fxm;
if(direct==1)
fy= fy+fym;
if(direct==2)
fz= fz+fzm;
if(fx<0){
fx=0; fxm=1;}
if(fx>7){
fx=7; fxm=-1;}
if(fy<0){
fy=0; fym=1;}
if(fy>7){
fy=7; fym=-1;}
if(fz<0){
fz=0; fzm=1;}
if(fz>7){
fz=7; fzm=-1;}
direct=randlimit(3);
if(direct==0)
ftx= ftx+ftxm;
if(direct==1)
fty= fty+ftym;
if(direct==2)
ftz= ftz+ftzm;
if(ftx<0){
ftx=0; ftxm=1;}
if(ftx>7){
ftx=7; ftxm=-1;}
if(fty<0){
fty=0; ftym=1;}
if(fty>7){
fty=7; ftym=-1;}
if(ftz<0){
ftz=0; ftzm=1;}
if(ftz>7){
ftz=7; ftzm=-1;}
}//while
for(xxx=0; xxx<8; xxx++){
for(yyy=0; yyy<8; yyy++){
for(zzz=0; zzz<8; zzz++){
LED(xxx, yyy, zzz, 0, 0, 0);
}}}
}//wipeout
//*****************************************************************LED代码结束
888测试知道顺序能不能反转
最新推荐文章于 2024-05-22 17:19:37 发布