diff options
author | Dominique Martinet <asmadeus@codewreck.org> | 2011-12-14 20:08:59 +0100 |
---|---|---|
committer | Dominique Martinet <asmadeus@codewreck.org> | 2011-12-14 20:08:59 +0100 |
commit | 82a1e49345a6c21ee938bc6ec44bc6e2353ad8ca (patch) | |
tree | 2a7d7be779228cc7459bcb77572ea2731c4f39c2 | |
parent | a5b31900f2cc9cb58db5f6144a8aae3ac2ab4616 (diff) |
-rw-r--r-- | init.c | 23 | ||||
-rw-r--r-- | init.h | 2 | ||||
-rw-r--r-- | pong.c | 79 | ||||
-rw-r--r-- | pong.h | 15 |
4 files changed, 94 insertions, 25 deletions
@@ -39,8 +39,8 @@ void Timer0_Init(void) { TR0=0; // don't run yet } -void wait500ns(){ - TL0 = 255-(SYSCLK/50000); // an edge every 20us; +void waitLCD(){ + TL0 = 255-(SYSCLK/100000); // an edge after 10us; TF0=0; TR0=1; while (!TF0); @@ -48,14 +48,14 @@ void wait500ns(){ } void LCD_Send() { - wait500ns(); - E=0; - wait500ns(); + waitLCD(); E=1; + waitLCD(); + E=0; } void LCD_Init() { - // reset LCD for a while, and reactivate it. + // reset LCD for a while, and reactivate it. CS1=1; CS2=1; @@ -66,7 +66,7 @@ void LCD_Init() { RST=1; - // DB=0xC0; + // DB=0xC0; RS=0; RW=0; DB=0b11000000; LCD_Send(); @@ -108,5 +108,14 @@ void Timer2_Init (void) TR2 = 1; // start Timer2 } +void ADC_Init(void) { + AD0TM=1; + REF0CN = 0x0e; // enable thermistance, ADC n' stuff + + AMX0P = 0x1E; // compare to thermistance + AMX0N = 0x1F; // compare to GND + ADC0CF = 3000<<3; // clock to sample stuff + ADC0CF &= ~0x04; // right-justify results (default) +} @@ -8,5 +8,7 @@ void Timer1_Init(); void Timer2_Init (void); void UART_Init(void); void SW_Init(void); +void ADC_Init(void); void LCD_Init(void); +void waitLCD(); void LCD_Send(); @@ -68,6 +68,10 @@ void main (void) { LCD_Init(); + ADC_Init(); + + initRand(); + InitGame(); while (1) { // spin forever @@ -110,6 +114,45 @@ void main (void) { } } +void initRand() { + unsigned char i = 0; + unsigned char seed = 0; + AD0EN=1; + AD0INT=0; + + for (i=0; i<8; i++) { + AD0BUSY=1; + while (!AD0INT); + seed |= ((ADC0L & 0x01) << i); + // printf("ADC0L: %x, ADC0H: %x\r\n", ADC0L, ADC0H); // leave anything that does a sleep + waitLCD(); + waitLCD(); + waitLCD(); + } + + AD0EN=0; + + printf("seed: %u\r\n", seed); + rand(seed); + + if (seed==255 || seed == 0) // means something went wrong with the ADC an' it wasn't exactly random + initRand(); +} + +char rand(char seed) { + static char myseed=42; + + if (seed != 0) + myseed = seed; + + if (myseed == 0) + myseed = 42; //we'd be stuck if it happens + + myseed *= 239; + + return(myseed); +} + void InitGame() { memset(&NextGame, 0, sizeof(NextGame)); @@ -118,8 +161,13 @@ void InitGame() NextGame.player2 = (X_MAX - SPLAYERHEIGHT)/ 2; NextGame.ball.x = X_MAX/ 2; NextGame.ball.y = Y_MAX/ 2; - NextGame.velocity.x = KEYINC; - NextGame.velocity.y = KEYINC; + // NextGame.velocity.x = (int)rand(0)*16; + NextGame.velocity.x = (((int)rand(0)*(KEYINC/4-1)) % (3*KEYINC) - 3*KEYINC/2); + if (rand(0) >= 0) { + NextGame.velocity.y = KEYINC; + } else { + NextGame.velocity.y = -KEYINC; + } memcpy(&OldGame, &NextGame, sizeof(NextGame)); @@ -137,27 +185,27 @@ void GameEngine() NextGame.ball.x += NextGame.velocity.x; NextGame.ball.y += NextGame.velocity.y; if (NextGame.ball.x <= absvelocityx || NextGame.ball.x >= X_MAX - absvelocityx) { - NextGame.velocity.x = -12*(NextGame.velocity.x/10); + NextGame.velocity.x = -NextGame.velocity.x; NextGame.ball.x += NextGame.velocity.x; - } + } if (NextGame.ball.y <= SPLAYERWIDTH) { if ((NextGame.ball.x >= NextGame.player1) && (NextGame.ball.x <= NextGame.player1 + SPLAYERHEIGHT)) { NextGame.velocity.y = -12*(NextGame.velocity.y/10); NextGame.ball.y += NextGame.velocity.y; NextGame.velocity.x = 12*(NextGame.velocity.x/10); - } else { + } else { InitGame(); } - } - if (NextGame.ball.y >= Y_MAX - SPLAYERWIDTH) { + } + if (NextGame.ball.y >= Y_MAX - SPLAYERWIDTH - SBALLWIDTH) { if ((NextGame.ball.x >= NextGame.player2) && (NextGame.ball.x <= NextGame.player2 + SPLAYERHEIGHT)) { NextGame.velocity.y = -12*(NextGame.velocity.y/10); NextGame.ball.y += NextGame.velocity.y; NextGame.velocity.x = 12*(NextGame.velocity.x/10); - } else { + } else { InitGame(); } - } + } } @@ -298,11 +346,16 @@ void SW_ISR (void) __interrupt 2 { void LCD_Set(__bit _CS1, __bit _CS2, unsigned char x, unsigned char y, unsigned char toSet) { CS1=_CS1; CS2=_CS2; - RS=0; RW=0; DB=0x40|y; - LCD_Send(); - DB=0xb8|x; - LCD_Send(); + if (y<0x40) { + RS=0; RW=0; DB=0x40|y; + LCD_Send(); + } + + if (x<0x08) { + DB=0xb8|x; + LCD_Send(); + } RS=1; DB=toSet; LCD_Send(); @@ -7,10 +7,10 @@ #define KB_F 0x18 #define KB_TEST(a, b) ((a & b & 0x0F) && (a & b & 0xF0)) -#define KUP1 KB_4 -#define KDOWN1 KB_A -#define KUP2 KB_F -#define KDOWN2 KB_D +#define KUP1 KB_A +#define KDOWN1 KB_4 +#define KUP2 KB_D +#define KDOWN2 KB_F // Vector typedef struct Vector @@ -42,8 +42,10 @@ typedef struct Game #define PLAYERHEIGHT 8 #define PLAYERWIDTH 3 +#define BALLWIDTH 1 #define SPLAYERHEIGHT ((unsigned int)(PLAYERHEIGHT << X_SHIFT) -1) -#define SPLAYERWIDTH ((unsigned int)(PLAYERWIDTH << X_SHIFT) -1) +#define SPLAYERWIDTH ((unsigned int)(PLAYERWIDTH << Y_SHIFT) -1) +#define SBALLWIDTH ((unsigned int)(BALLWIDTH << Y_SHIFT) -1) #define KEYINC (1 << 9) @@ -64,3 +66,6 @@ void hscanf(char* str, unsigned char* a, unsigned char* b, unsigned char* c); void LCD_Set(__bit _CS1, __bit _CS2, unsigned char x, unsigned char y, unsigned char toSet); void LCD_Clear(__bit _CS1, __bit _CS2); void LCD_DrawScreen(); + +void initRand(); +char rand(char seed); |