#include volatile int shot=0; int refill_size; volatile int wait_count = 0; volatile int no_refill = 0; #define HOPPER_SIZE 180 #define REFILL_SIZE_0 100; #define REFILL_SIZE_1 110; #define REFILL_SIZE_2 140; #define REFILL_SIZE_3 150; #define HUNDREDS 64 #define TENS 32 #define ONES 16 //This table is a software fix for a hardware problem. I forgot to re-arange some data bus pins when laying out the PCB //So the Value for all the pins but A is wrong (so the values are WAY off) #define USEVALUEMAP 1 #ifdef USEVALUEMAP char value_mapping[10] = {0,1,4,5,8,9,12,13,2,3}; #endif void enable_timer(){ //stop the timer TR0 = 0; TH0 = 0; //clear time1 TL0 = 0; TMOD |= T0_M0; //set bit 0 TMOD &= (~T0_M1); //cleat bit 1 TF0 = 0; //and now start the timer TR0 = 1; } void waitsome(){ int i; for(i = 0;i<255;i++){ ; } } void selftest(){ int i=0,j=0; while(1){ if(j>9){ j=0; } P1 = value_mapping[j] | HUNDREDS | TENS | ONES; for(i=0;i<255;i++){ waitsome(); } j++; } } void testvalue(int v){ P1 = value_mapping[v] | HUNDREDS | TENS | ONES; PCON = PCON | PD; } void displayshot(){ int tmp = shot; int hundreds; int tens; int ones; hundreds = tmp/100; tmp -= hundreds*100; tens = tmp/10; ones = tmp%10; #ifdef USEVALUEMAP //this is a crappy hack to fix a problem I caused when laying out the board. hundreds = value_mapping[hundreds]; tens = value_mapping[tens]; ones = value_mapping[ones]; #endif P1= hundreds; waitsome(); P1_6 = 1; waitsome(); P1= hundreds; waitsome(); P1= tens; waitsome(); P1= tens|TENS; P1_5 = 1; waitsome(); P1= tens; waitsome(); P1= ones; waitsome(); P1= ones|ONES; P1_4 = 1; waitsome(); P1= ones; waitsome(); } volatile void decrement_counter() interrupt 0 { if(shot >0){ shot--; } displayshot(); } volatile void waitreload_fire() interrupt 1 { wait_count++; if(P3_3){ if(wait_count >15){ wait_count = 0; if(P3_3){ if( (shot+refill_size) > HOPPER_SIZE){ shot = HOPPER_SIZE; }else{ shot += refill_size; } displayshot(); } TR0 = 0; //disable the timer } }else{ //the button isn't pressed, disable the timer TR0 = 0; wait_count = 0; } } volatile void refill() interrupt 2 { enable_timer(); } int main(){ //setup the shot counter P0=0; shot=HOPPER_SIZE; displayshot(); //setup interrupt handling IT0 = 1; //edge trigger INT0 IT1 = 1; //edge trigger INT1 EX0 = 1; //Enable INT0 EX1 = 1; //Enable INT1 ET0 = 1; //Enable Timer0 EA = 1; //enable interrupts //TODO: set this based on the inputs for size. refill_size = REFILL_SIZE_0; //go to idle mode, wait for interrupts to do anything while(1){ PCON |= IDL; } //return 0; }