void main(void) { short mx, my, tmx, tmy, ignoreBtn; char stat[14]; // TODO initialise variables with the riht value //mx = -1; //my = -1; //ignoreBtn = 0; lcd_init(); initIO(); adc_init(); lcd_clear(0); drawField(); for (;;) { tmx = (short) (2 - adc_getValue(1) / 255.0 * 2 + 0.5); tmy = (short) (2 - adc_getValue(2) / 255.0 * 2 + 0.5); if (tmy != my || tmx != mx) { drawMarker(mx, my, 0); if (fields[mx][my] == 1) { placePlayerMark(mx, my, 1); } drawMarker(mx = tmx, my = tmy, 1); } if (!BUTTON_RIGHT && !ignoreBtn && !fields[mx][my]) { ignoreBtn = 1; pushes++; placePlayerMark(mx, my, player); if (checkStatus(mx, my)) { lcd_clear(0); sprintf(stat, "Player %u won!", player); lcd_drawString(27, 4, 0, 4, 0, 0, &stat[0], 1, 1); lcd_flush(); break; } if (pushes == 9) { lcd_clear(0); sprintf(stat, "Undecided!"); lcd_drawString(32, 4, 0, 4, 0, 0, &stat[0], 1, 1); lcd_flush(); break; } if (player == 1) { player = 2; } else if (player == 2) { player = 1; } } else if (BUTTON_RIGHT && ignoreBtn) { ignoreBtn = 0; } } }
int peripheralInit() { buzzer_init(); ledswi_initLedSwitch(1,3); display_7segments_initDisplays(); lcd_initLcd(); cooler_initForPwm(); heater_initForPwm(); adc_initAdc(); tach_init(); SIM_BASE_PTR->SCGC5 |= SIM_SCGC5_PORTB_MASK; PORTB_BASE_PTR->PCR[18] = PORTB_BASE_PTR->PCR[19] = PORT_PCR_MUX(3); PTB_BASE_PTR->PDDR = 0b11 << 19; tpm_config_t tpmConfig = { .eClock_source = McgPllFllClk, .uiPeriod_us = 1000, .ePrescaler_value = Prescaler128, .uiXtal_frequency = 40000000, .eAlignment = Edge }; /* Configure for 50% Duty Cycle */ channel_config_t channelConfig_cooler = { .eChannelOutput = NoInversion, .uiChannel = 1, .uiInterrupt_enable = 0, .uiPulse_width_us = 500 }; pwm_initPwm(TPM1, tpmConfig); pwm_channelInit (TPM1, tpmConfig, channelConfig_cooler); interpreter_init(); tc_installLptmr0(CYCLIC_EXECUTIVE_PERIOD, main_cyclicExecuteIsr); } int main(void) { boardInit(); peripheralInit(); /* Set Red LED for Status */ SIM_SCGC5 |= (SIM_SCGC5_PORTB_MASK); PORTB_PCR18 = PORT_PCR_MUX(1); PTB_BASE_PTR->PDDR = 1 << 18; /* Local variables */ char cTachReadout[15]; uint16_t uiTachValue; /* Main loop */ for (;;) { /* Blink Red LED for Status */ PTB_BASE_PTR->PTOR = 1 << 18; /* Serial CLI */ interpreter_readCommand(); /* Get temperature */ PRINTF("%d\r\n", adc_getValue()); /* Read and print tach value */ uiTachValue = tach_Hz(CYCLIC_EXECUTIVE_PERIOD/1000); lcd_itoa(uiTachValue, cTachReadout); lcd_clearLine(0); lcd_writeString(cTachReadout); lcd_writeString(" Hz"); /* Wait for next cycle */ while(!uiFlagNextPeriod); uiFlagNextPeriod = 0; } return 0; }
void main(void) { uint8_t genx = 0; uint8_t geny = 0; int8_t posx = 0; int8_t posy = 0; uint8_t bumpx[ANZ + 1]; uint8_t bumpy[ANZ + 1]; uint8_t i = 0; uint8_t mscount = 0; int8_t randx = 0; int8_t randy = 0; //char *schtring = "hello world"; PDR00 = 0xff; DDR00 = 0xff; // Set Port00 as output (right 7Segment display) PDR09 = 0xff; DDR09 = 0xff; // Set Port09 as output (left 7Segment display) PDR09 = DEC7SEG[1]; PDR00 = DEC7SEG[1]; lcd_init(); adc_init(); lcd_clear(0); random_init(); for(i = 0; i < ANZ + 1; i++) { do { genx = (random() % (128 / bumpsize)) * bumpsize; geny = (random() % (64 / bumpsize)) * bumpsize; } while((genx == 0 && geny == 0) || bumpcollision(bumpx, bumpy, i, genx, geny)); bumpx[i] = genx; bumpy[i] = geny; } for(;;) { for(i = 0; i < ANZ; i++) { lcd_drawRect(bumpx[i], bumpy[i], bumpsize, bumpsize, 1, 1); } lcd_drawRect(bumpx[ANZ], bumpy[ANZ], size + 2, size + 2, 0, 1); lcd_drawRect(posx, posy, size, size, 0, 1); posx = minmax((int) (((float) -(adc_getValue(1) - 255) / (float) 255) * (128 - size)) + randx, 128); posy = minmax((int) (((float) -(adc_getValue(2) - 255) / (float) 255) * (64 - size)) + randy, 64); lcd_drawRect(posx, posy, size, size, 1, 1); if(checkCollision(posx, posy) == 1) looser(); if(posx == bumpx[ANZ] + 1 && posy == bumpy[ANZ] + 1) winner(); lcd_drawRect(bumpx[ANZ], bumpy[ANZ], size + 2, size + 2, 1, 1); lcd_drawRect(bumpx[ANZ] + 1, bumpy[ANZ] + 1, size, size, 0, 1); mscount++; if(mscount == 50) { mscount = 0; if(random() % 2) { randx = minmax(randx + posorneg(), 5 + size); } else { randy = minmax(randy + posorneg(), 5 + size); } } delay_ms(10); } }