Tiny Pong: More fun with ATtiny45 and VGA
I’m still waiting for my cheap Bluetooth module from China which will serve as an input interface for my scoreboard project. In the meantime, I’ll show you how to convert your ATtiny microcontroller into a Pong game (with no input so far).
ATtiny45 +-----------------+ | | IN ----| 1 (PB5) (VCC) 8 |---- +5V 22pf | | +--||----+------| 2 (PB3) (PB2) 7 |---- HSYNC | [ ] XTAL| | +--||----+------| 3 (PB4) (PB1) 6 |---- VSYNC | 22pf | | +---------------| 4 (GND) (PB0) 5 |---- RGB | | | GND +-----------------+
Now the RGB is connected to PB0, and there is a good reason for this. I’m still using the same technique of storing what I want to render in registers but instead of 4, this time I’m using 15 so I can achieve an horizontal resolution of 120 by 96 to make the pixels somehow squared. Now, to be able to walk trough the 120 bits and turn the RGB pin on/off accordingly (and evenly) I needed to crop the code, removing the loops (so you will see a lot of similar code in the part that renders the line) and the conditional skip now replaced by an “add with carry” after the shift into a temporary register that will be used with “out” which is less expensive than “sbi” and “cbi”.
So, in terms of code optimization, this:
; r1 bit 0 cbr r16, 1 lsl r1 adc r16, r22 out PORT, r16 ... repeated 120 time (8 times per bit and 15 times per register)
Is better than:
ldi r16, 0x08 line44: rol r8 brcc rgboff44 nop ; RGB on sbi PORT, RGB ; sbi = 2 clocks rjmp cont44 rgboff44: ; RGB off cbi PORT, RGB ; cbi = 2 clocks nop nop cont44: dec r16 nop nop nop nop nop brne line44
There are also other parts of the code that might be of interest. For example, I’ve use LFSR to add some pseudo-random variables to the ball direction and the paddle “computer” movements. Also, I’ve used the Bresenham’s line algorithm to determine the ball position.
The missing part, is still the input. I’m not sure how this will work with only one pin available, but I guess I’ll work out something with the Bluetooth module and one of the synch signals (if even possible).
I’ve tried to add some intro screen or “splash”, but the program memory is so small that I’ve quickly exceeded the 4096 available bytes.
Download the Tiny Pong 1.0 source code and Enjoy!