While ago I found project that emulates candle flame using attiny13 MCU and two colored LED (red/yellow) or alternately two separate LED’s. Project author used method of randomly turning on and off LED’s and it looked relatively OK but it was a bit hard for eyes. I decided to make it from scratch using PWM (Pulse wide modulation) that randomly changes light intensity instead of turning on and off completely LED’s. It resulted in much more realistic candle flame simulation. It’s handy for making romantic atmosphere without fear of setting fire 😉
LED’s are connected through 100Ω resistor to PB0 and PB1 attiny13 pins (pin 5 and 6). Attiny is powered from 5V source. Program is written in Bascom. One thing that should be done while programming attiny is to change lfuse from 6A to 7A so that MCU clock is changed from default 1.2MHz to 9.6MHz and that also changes PWM speed so there is no visible blinking of LED’s due to low PWM frequency.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
$regfile = "attiny13.dat" $crystal = 9600000 $hwstack = 32 $swstack = 16 $framesize = 8 Config Watchdog = 256 Start Watchdog Tccr0a = &B10100001 Tccr0b = &B00000011 Config Pinb.0 = Output Config Pinb.1 = Output Do Reset Watchdog Pwm0a = Rnd(255) Pwm0b = Rnd(255) Waitms 100 Loop |
And here is source and compiled hex file for download
pwmsveca.bas
pwmsveca.hex
So helpful ,Thanks!
Please explain about register values ( Tccr0a & Tccr0b ) .
Waveform Generation Mode:
Phase Correct PWM mode (8 bit PWM).
Clock source: I/O Clock / 64
Clear OC0A on Compare Match when up-counting.
Set OC0A on Compare Match when down-counting.
Clear OC0B on Compare Match when up-counting.
Set OC0B on Compare Match when down-counting.
To be honest that part is something I found on Bascom forum and it was while ago.