Close

Attempt 1 - C Code without Library functions

A project log for Pong in AVR

Final project of the AVR Architecture Course

paulson-k-antonyPaulson K Antony 03/01/2021 at 11:260 Comments

Initial idea was to look at the generated assembly code and copy the required lines into my assembly program.

But since the original C code depended heavily on Arduino library functions, many unnecessary assembly lines were generated.

So rewrote the whole code to make user-defined function/ code for the library functions like SPI.transfer( ), SPI.begin( ), digitalWrite( ) and so on. Also, removed all small or unnecessary loops.

This can be done by direct PORT and Register Manipulation.

#define lo8(x) ((x)&0xff)
#define hi8(x) ((x)>>8)

uint8_t playerPaddle = 2;
uint8_t compPaddle = 2;
uint8_t ballx = 6;
uint8_t bally = 4;
int8_t speedx = 1;
int8_t speedy = -1;
boolean gameOver = false;

uint16_t buffer = 0 ; 

Single-Byte Global Variables

void spiTransfer(uint8_t data)
{
  SPDR = data;
  while(!(SPSR & (1<<SPIF)));
}

User-Defined SPI.Transfer

bitClear(PORTB, 2);
spiTransfer(i);
spiTransfer(lo8(buffer));
spiTransfer(i);
spiTransfer(hi8(buffer));
bitSet(PORTB, 2);

Single SPI Transaction

//SPI.begin( ), pinMode( ), digitalWrite( )
DDRB = bit(5)|bit(3)|bit(2);
PORTB = bit(2);
SPCR = 0b01010000;
DDRD = 0;
PORTD = bit(7)|bit(6) ;

//digitalRead( )
if(!bitRead(PIND, LB))
    movePaddleLeft();
if(!bitRead(PIND, RB))
    movePaddleRight();

 Direct Port/ Register Manipulation

The assembly code generated was way more simpler and straightforward and gave me an idea of how to approach the assembly code.

Unfortunately, I got cocky and wrote the whole code in one go without testing. Random LEDs started lighting up on the LED matrix and wasted hours trying to find out the errors.

Gave up and started from scratch

Discussions