/* C-language version of die tosser example Harware interface: PM0 = Pushbutton SW (Click to toss coin) PM1 = Head/Tail Outcome LED PTT = 8 LEDs displaying # tails PTAD= 8 LEDs displaying # heads */ #include /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc9s12c32" void delay20ms(void); void main(void) { unsigned char coin,nr_heads, nr_tails; DDRT = 0xff; //Make PTT and PTAD all outputs ATDDIEN = 0xff; DDRAD = 0xff; DDRM = 0x02; //Make Port M PM0 = input (SW) //and PM1 = output (LED). PERM = 0x3E; //Disable internal pullup on SW input PM0. nr_heads = 0; //Initialize nr_heads and nr_tails to zero. nr_tails = 0; PTT = 0; //Set all output pins to zero. PTAD = 0; PTM = 0; for(;;) { coin = 0; //Variable "coin" indicates outcome do { coin = coin ^ 1; //Toggle LSB of coin variable } while ((PTM & 1) == 1); //until SW pressed. delay20ms(); //Delay 20 ms to debounce SW press. while ((PTM & 1) == 0); //Wait here until SW released delay20ms(); //Delay 20 ms to debounce SW release. if ((coin & 1) == 1) { nr_heads++; //If heads outcome, PTM = PTM | 0b00000010; //increment nr_heads, PM1 = 1 (LED on) PTAD = nr_heads; //and update nr_heads on PTT output port. } else { nr_tails++; //If tails outcome, PTM = PTM & 0b11111101; //increment nr_tails, PM1 = 0 (LED off) PTT = nr_tails; //and update nr_tails on PTAD output port. } } } void delay20ms(void) { unsigned int i; for(i=0;i<40000;i++); }