To achieve random variation of LEDs behavior, like candle flicker, we need to generate random numbers. Embedded systems (like ATtiny13) generally use some hardware based pseudo-random number generators like linear feedback shift registers (LFSRs).
This code is use 32-bit Galois LFSR (Linear-feedback shift register) random number generator. The bit positions that affect the next state are called the taps (32, 31, 29 and 1). Bits that are not taps are shifted one position to the right unchanged. The taps, on the other hand, are XORed with the output bit before they are stored in the next position. The new output bit is the next input bit. The effect of this is that when the output bit is zero, all the bits in the register shift to the right unchanged, and the input bit becomes zero. When the output bit is one, the bits in the tap positions all flip (if they are 0, they become 1, and if they are 1, they become 0), and then the entire register is shifted to the right and the input bit becomes 1.
Based on the random values generated by the LFSR, random of LEDs are switched on. Between two port updates, a random amount of delay is executed. The value of the delay time is also derived from the LFSR value. The seed value of the LFSR is set to 1.
CircuitCode source: "tinyAVR Microcontroller Project for the Evil Genius" page 40-41
/*
* "tinyAVR Microcontroller Project for the Evil Genius" page 40-41
*/
#include <avr/io.h>
#include <util/delay.h>
int main(void){
unsigned long lfsr = 1;
unsigned char temp;
DDRB = 0xff; // makes the all pins an output
while(1){
lfsr = (lfsr >>1) ^ (-(lfsr & 1u) & 0xd0000001u); /* taps 32 31 29 1 */
temp = (unsigned char) lfsr; //take lower
DDRB = ~temp; //Declare those pins as output where temp has zero
PORTB = temp; //Give the value of 0 to the pins declared output
temp = (unsigned char) (lfsr >> 24);
_delay_loop_2(temp<<7);
}
}
Programming ATtiny13 with Arduino Uno
Box design
Comments
Please log in or sign up to comment.