The water lamp, er, passes by silently
It is mainly recorded for your reference.
These three methods are implemented in C language. The three methods are as follows:
1. Implemented through bit operations
2. Implementation through displacement
3. Realize through Cyclic Displacement
// The specific implementation of the source code must be combined with the corresponding hardware circuit. This Code is based on the circuit, as shown at the end. If you are lucky enough to be referenced, Please adjust the hardware circuit
1. bitwise operations.
The code is long but easy to understand.
The implementation code is as follows:
# Include
Sbit led0 = p1 ^ 0; sbit led1 = p1 ^ 1; sbit led2 = p1 ^ 2; sbit led3 = p1 ^ 3; sbit led4 = p1 ^ 4; sbit led5 = p1 ^ 5; sbit led6 = p1 ^ 6; sbit led7 = p1 ^ 7; void delayms (unsigned int XMS) // refer to the delay function {unsigned int I, j; for (I = XMS; I> 0; I --) for (j = 120; j> 0; J --); // use 12 MHz crystal oscillator} void main () {While (1) {led0 = 0; delayms (100); led0 = 1; led1 = 0; delayms (100): lde1 = 1; led2 = 0; delayms (100): lde2 = 1; led3 = 0; delayms (100): lde3 = 1; led4 = 0; delayms (100): lde4 = 1; led5 = 0; delayms (100): lde5 = 1; led6 = 0; delayms (100): lde6 = 1; led7 = 0; delayms (100): lde7 = 1 ;}}
We can see that each statement is basically fixed.
2. Implementation through displacement
Thanks to the bitwise operation in C language, you can use the displacement operation to realize the flow lamp.
The Code is as follows:
This is only able to achieve the effect of a flow lamp.
#include <reg52.h>void delayms(unsigned int xms){ unsigned int i,j; for(i=xms;i>0;i--) for(j=120;j>0;j--);}void main(){P1 = 0xfe;delayms(400);while(1) { unsigned int i; for(i=0;i<8;i++) { P1<<=1; P1=P1|0x01; delayms(400); } }}
After watching the video, I changed it to the loop effect. The main change is to re-assign the initial value for each loop.
#include <reg52.h>void delayms(unsigned int xms){ unsigned int i,j; for(i=xms;i>0;i--) for(j=120;j>0;j--);}void main(){unsigned int i;DS1302 =0;while(1) { P1 = 0xfe; delayms(400); i=0; for(;i<8;i++) { P1<<=1; P1=P1|0x01; delayms(400); } }}
3. Realize through Cyclic Displacement.
This implementation is relatively new for me. Although I know the Cyclic Displacement, it has never been implemented.
#include <reg52.h>void delayms (unsigned int xms);void main(){unsigned char a,b;P1 = 0xfe; while(1){a = P1>>7;b = P1<<1;P1 = a|b;delayms(500);}}void delayms(unsigned int xms){unsigned int i,j;for(i = xms; i > 0; i--)for(j = 120; j > 0; j--);}
In addition to logical implementation, you can also directly call the function implementation. The called function is included in the intrins. h header file.
The Code is as follows:
#include <reg52.h>#include <intrins.h> void delayms(unsigned int xms){unsigned int i,j;for(i=xms;i>0;i--)for(j=120;j>0;j--);}void main(){P1 = 0xfe;while(1){P1 = _crol_(P1,1);delayms(400);}}
I feel that the Cyclic Displacement implemented by myself is the best. Calling a function is the most concise, but it is not a self-implemented loop function after all: P
LED circuit diagram:
This is to buy others development board HJ-C52.