int main(void) { int counter = 0; int delay=1000000; //Set the initial delay to 1 second int LEDIndex; int winVal; //Initialize all the modules LEDInit(); dipInit(); switchInit(); rngInit(); timerInit(); for(;;) { clearAll(); LEDIndex=0; winVal=getRandom(); //Get a random value to be the winning value for this round LEDPutVal(winVal,1); //Display that winning value timerDelay(delay); if(!getDipState()) { //Random Game while(!getSwitchState(0)) { //While the button is not pressed clearAll(); LEDIndex=getRandom(); LEDPutVal(LEDIndex,1); timerDelay(delay); } } else { //Sequence Game while(!getSwitchState(0)) { //While the button is not pressed clearAll(); LEDPutVal(LEDIndex++,1); //Increment the LED index so that it displays sequentially LEDIndex %= 4; //Keep the LED index within our domain timerDelay(delay); } //The following conditional statement is needed to bring the index back to the value that was clicked //We need this to compare to the winning value to see if the game has been won if(LEDIndex==0) { LEDIndex=3; } else { LEDIndex--; } } //If the chosen value is equal to the winning value if(LEDIndex==winVal) { winAnimation(); //If so, display the winning animation delay *= 0.85; //Decrease the delay } else { loseAnimation(); //Else, display the losing animation delay=1000000; //Reset the delay } } #if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT) printf("Hello World in C++ from MCF52259 derivative on TWR-MCF5225X board\n\r"); #endif for(;;) { counter++; } }
void setup(void) { //unsigned long t3,servoSOF; fruitInit(); switchInit(); //i2cm_init(I2C_MASTER, I2C_SLEW_ON, FOSC/400000/4-1) ; //400kHz : clock = FOSC/(4 * (SSPADD + 1)) : SSPADD=FOSC/clock/4-1 //HMC5883_init(); // --------- init motors PWM on TMR2 ----------- // now done by core & dcmotor // PWM on Timer2 : fPWM = fOSC / (TMR2prescale * 4 * (PR2+1)) // = 64000000Hz/(4 * 4 * 256) = 15625Hz /*CCP2CON=CCP3CON=0b00001100 ; // single PWM active high T2CONbits.T2CKPS0=1; //prescaler 4 PR2=255; T2CONbits.TMR2ON=1;*/ // ------- init PWM1 to pulse MOTC_IN1 //PSTR1CON=0; //PSTR1CONbits.STR1D=1; // ------- init PWM2 to pulse MOTD_EN //PSTR2CON=0; //PSTR2CONbits.STR2B=1; #if 1 // ---------- capture timer : TMR1 ------------ T1CON=0b00110011;//src=fosc/4,ps=8,16bit r/w,on. PIE1bits.TMR1IE=1; //1; IPR1bits.TMR1IP=1; // ---------- Servos ------------ //Servo_Init(); //SERVO_SET_PORT(0,K3); dcmotorInit(X); DCMOTOR(X).Setting.PosWindow = 1; DCMOTOR(X).Setting.PwmMin = 50; DCMOTOR(X).Setting.PosErrorGain = 6; DCMOTOR(X).Setting.onlyPositive = 0; DCMOTOR(X).PosRamp.maxSpeed = 40; DCMOTOR(X).PosRamp.maxAccel = 50; DCMOTOR(X).PosRamp.maxDecel = 50; rampSetPos(&DCMOTOR(X).PosRamp, 0); DCMOTOR(X).PosPID.GainP = 120; //90 DCMOTOR(X).PosPID.GainI = 10; DCMOTOR(X).PosPID.GainD = 0; DCMOTOR(X).PosPID.MaxOut = 1023; DCMOTOR(X).VolVars.homed = 1; pinModeDigitalOut(MXEN2); digitalSet(MXEN2); dcmotorInit(Y); DCMOTOR(Y).Setting.PosWindow = 100; DCMOTOR(Y).Setting.PwmMin = 50; DCMOTOR(Y).Setting.PosErrorGain = 1; DCMOTOR(Y).PosRamp.maxSpeed = 2000; DCMOTOR(Y).PosRamp.maxAccel = 5000; DCMOTOR(Y).PosRamp.maxDecel = 5000; rampSetPos(&DCMOTOR(Y).PosRamp, 16384); DCMOTOR(Y).PosPID.GainP = 200; //90 DCMOTOR(Y).PosPID.GainI = 1; DCMOTOR(Y).PosPID.GainD = 0; DCMOTOR(Y).PosPID.MaxOut = 1023; DCMOTOR(Y).VolVars.homed = 1; pinModeDigitalOut(MYEN2); digitalSet(MYEN2); #endif analogInit(); //ANALOG_SELECT(MOTX_POSCHAN, MOTX_POS); analogSelect(MOTX_CSCHAN, MXCS); //ANALOG_SELECT(MOTY_POSCHAN, MOTY_POS); analogSelect(MOTY_CSCHAN, MYCS); analogSelect(ACCX_CHAN, ACCX); analogSelect(ACCZ_CHAN, ACCZ); switchSelect(0,K7); //EEreadMain(); delayStart(mainDelay, 5000); // init the mainDelay to 5 ms }