[page]
Detailed production process: http://www.51hei.com/bbs/dpj-25725-1.html
Here is the source code:
#include
//#include
#define ONE_CYCLE_STEP 255
#ifdef AT89C2051_HEADER_FILE
#define PORT P1 //If using STC12C2052, use P1 as the output port
#else
#define PORT P0 //Otherwise use P0 port for output
#endif
#define LED_CNT 4 //Define the output port number 1-8 4 represents the use of 0-3 port output
#define SCRIPT_CNT 6 //Define the script number
//Firefly structure
struct Firefly{
unsigned int brightness; //maximum brightness value 0-255
unsigned int timeCell; //Time interval 0-65535
unsigned int count; //Run control variable: current count
unsigned char bn; //Run control variable: current brightness
};
//Define the execution script for each output port
// Each port will execute SCRIPT_CNT times, and then continue to execute in a loop
struct Firefly code fireFlyScript[][SCRIPT_CNT] = {
{{0,300,0,0},{255,400,0,0},{0,500,0,0},{100,600,0,0},{0,900,0,0},{0,500,0,0}},
{{255,300,0,0},{0,400,0,0},{0,500,0,0},{255,800,0,0},{0,700,0,0},{0,500,0,0}},
{{0,300,0,0},{0,400,0,0},{255,500,0,0},{50,1200,0,0},{0,300,0,0},{0,500,0,0}},
{{128,500,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}},
{{0,900,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}},
{{255,400,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}},
{{0,200,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}},
{{0,750,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}}
};
//define LED_CNT only fireflies
struct Firefly fireFlies[LED_CNT];
//Script counter for each firefly
unsigned char ffScript[LED_CNT];
//Pre-calculated cosine function table, PWM output simulation function curve is better
unsigned char code cosValue[]={
0,0,0,0,0,0,0,0,1,1,1,2,2,3,3,4,4,5,6,6,7,8,
9,10,11,11,12,13,14,16,17,18,19,20,21,23,24,
25,27,28,29,31,32,34,35,37,39,40,42,44,45,47,
49,50,52,54,56,58,60,62,63,65,67,69,71,73,75,
77,80,82,84,86,88,90,92,94,97,99,101,103,105,
107,110,112,114,116,119,121,123,125,127,130,
132,134,136,139,141,143,145,148,150,152,154,
156,158,161,163,165,167,169,171,173,175,178,
180,182,184,186,188,190,191,193,195,197,199,
201,203,205,206,208,210,211,213,215,216,218,
220,221,223,224,226,227,228,230,231,232,234,
235,236,237,238,239,241,242,243,244,244,245,
246,247,248,249,249,250,251,251,252,252,253,
253,254,254,254,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,254,254,254,
253,253,252,252,251,251,250,249,249,248,247,
246,245,244,244,243,242,241,239,238,237,236,
235,234,232,231,230,228,227,226,224,223,221,
220,218,216,215,213,211,210,208,206,205,203,
201,199,197,195,193,192,190,188,186,184,182,
180,178,175,173,171,169,167,165,163,161,158,
156,154,152,150,148,145,143,141,139,136,134,
132,130,128,125,123,121,119,116,114,112,110,
107,105,103,101,99,97,94,92,90,88,86,84,82,
80,77,75,73,71,69,67,65,64,62,60,58,56,54,52,
50,49,47,45,44,42,40,39,37,35,34,32,31,29,28,
27,25,24,23,21,20,19,18,17,16,14,13,12,11,11,
10,9,8,7,6,6,5,4,4,3,3,2,2,1,1,1,0,0,0,0,0,0,0,
0
};
//Timer interrupt count variable
unsigned int time0count_0;
//---------------------------------------
//Name: Timer 0 interrupt service routine
// :Timer0
//parameter:
//return:
//---------------------------------------
void Timer0(void) interrupt 1
{
unsigned long cell; //temporary variable for calculation
unsigned char i,p;
TL0=0xE8; //Re-initialize TL0
TH0=0xFF; //Re-assign the initial value to TH0
//After a cycle ends, check the script and the duty cycle of the next cycle
if(++time0count_0>ONE_CYCLE_STEP)
{
time0count_0 = 0;
//N channels of signal processing
for(i = 0;i
{
//After one script is executed, load the next script
if(++fireFlies[i].count>fireFlies[i].timeCell)
{
ffScript[i]++;
ffScript[i] = ffScript[i] % SCRIPT_CNT;
fireFlies[i] = fireFlyScript[i][ffScript[i]];
}
//Calculate the duty cycle and brightness of the next cycle
cell = fireFlies[i].count;
cell *=360;
cell /= fireFlies[i].timeCell;
fireFlies[i].bn = (cosValue[cell]*fireFlies[i].brightness)/255;
//Prepare to light up an LED
p=1<
//If the brightness value is not 0, light up
if(fireFlies[i].bn != 0)
{
PORT |= p;
}
else
{
PORT &= ~p;
}
}
}
//After each output port reaches the duty cycle, it outputs a low level to turn off the LED
for(i = 0; i
{
p=1<
if(time0count_0==fireFlies[i].bn)PORT &= ~p;
}
}
void main()
{
unsigned i;
// Turn off all LEDs
PORT=0;
//Initialize all fireflies and execute scripts
for(i = 0; i
{
fireFlies[i] = fireFlyScript[i][0];
ffScript[i] = 0;
}
//***Timer 0 initialization***
TMOD&=0xF0; //Clear the lower 4 bits of TMOD timer 0 control part
TMOD|=0x01; //Set timer 0 to mode 1
TL0=0xE8; //Set the initial value of timer 0 to the lower 8 bits
TH0=0xFF; //Set the initial value of timer 0 high 8 bits
TR0=1; //Start timer 0
ET0=1; //Timer0 interrupt enabled
//***Open global interrupt settings****
//Timer Timer0 is set to enable interrupts, and global interrupts need to be enabled here
EA=1; //Open global interrupt
while(1); // infinite loop, after the microcontroller is initialized, it will keep running this infinite loop
}
Previous article:Traffic Light and Its Simulation Based on 51 Single Chip Microcomputer
Next article:51 MCU serial port control relay
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
Guess you like
- AD5933 can measure impedance accurately, but cannot measure phase angle accurately
- The Basic Principles and Applications of Chirp Signals
- One of the 17 most used software by electronic engineers
- How to understand the reverse voltage parameter in OSRAM LED datasheet
- There is a step when turning on the power, what is the reason, and is it a big impact?
- Could you please tell me the bootstrap principle of IR2104 in the following circuit diagram (please write in detail, thank you)
- The company has launched a new medical product, but is still hesitant about the piracy in the market. Is there any good way to promote it?
- Today at 14:00, live broadcast with prizes: [In-depth and easy-to-understand wearable health monitoring]
- [RVB2601 Creative Application Development] Flowing Light Gradient Light
- 【Gravity:AS7341 Review】Color Temperature Perception Measurement 1: Unboxing