//****************************************************************************** // report hard error // void ERROR2(int16 error) { int i, j; // return if no error if (error == 0) return; __bic_SR_register(GIE); // disable interrupts RBX430_init(_1MHZ); // system reset @1 MHz while (1) { // pause LED_RED_OFF; BACKLIGHT_OFF; for (i=1; i<4; i++) for (j=1; j; j++); // flash LED's 10 times i = 10; while (i--) { LED_RED_TOGGLE; BACKLIGHT_TOGGLE; for (j=1; j<8000; j++); } // pause LED_RED_OFF; BACKLIGHT_OFF; for (i=1; i<2; i++) for (j=1; j; j++); // now blink error # for (i = 0; i < error; i++) { BACKLIGHT_ON; LED_RED_ON; for (j=1; j; j++); LED_RED_OFF; BACKLIGHT_OFF; for (j=1; j; j++); } } } // end ERROR2
//------------------------------------------------------------------------------ // void main(void) { ERROR2(RBX430_init(_12MHZ)); // init board to 12 MHz ERROR2(lcd_init()); // init LCD ERROR2(port1_init()); // init switches ERROR2(ADC_init()); // init ADC ERROR2(watchdog_init()); // init watchdog ERROR2(timerA_init()); // init TimerA ERROR2(timerB_init()); // init TimerB __bis_SR_register(GIE); // enable interrupts sys_event = NEW_GAME; // start w/new game //----------------------------------------------------------- // play forever while (1) { //----------------------------------------------------------- // event service routine loop //----------------------------------------------------------- while (1) { // disable interrupts while checking sys_event _disable_interrupts(); // if event pending, enable interrupts if (sys_event) _enable_interrupt(); // else enable interrupts and goto sleep else __bis_SR_register(LPM0_bits | GIE); //------------------------------------------------------- // I'm AWAKE!!! What needs service? if (sys_event == SWITCH_1) { sys_event = sys_event & ~SWITCH_1; SWITCH_1_event(); } else if (sys_event == NEW_GAME) // new game event { sys_event &= ~NEW_GAME; // clear new game event NEW_GAME_event(); // process new game } else if (sys_event == MOVE_BALL) // timer A event { sys_event &= ~MOVE_BALL; // clear TimerA event MOVE_BALL_event(ball); // update ball position } else if (sys_event == ADC_READ) // read ADC event { sys_event &= ~ADC_READ; // clear ADC event ADC_READ_event(rightPaddle); // process ADC read ADC_READ_event(leftPaddle); } else if (sys_event == LCD_UPDATE) { LCD_UPDATE_event(); } else if (sys_event == START_GAME) { sys_event = sys_event & ~START_GAME; START_GAME_event(); } else if (sys_event == NEW_RALLY) { sys_event = sys_event & ~ NEW_RALLY; NEW_RALLY_event(); } else if (sys_event == MISSED_BALL) { sys_event = sys_event & ~MISSED_BALL; MISSED_BALL_event(); } else if (sys_event == END_GAME) { sys_event = sys_event | END_GAME; END_GAME_event(); } else // ???? { ERROR2(SYS_ERR_EVENT); // unrecognized event } } } } // end main
//------------------------------------------------------------------------------ // main ------------------------------------------------------------------------ void main(void) { RBX430_init(_16MHZ); // init board ERROR2(lcd_init()); // init LCD // configure Watchdog WDTCTL = WDT_CTL; // Set Watchdog interval WDT_Sec_Cnt = WDT_1SEC_CNT; // set WD 1 second counter IE1 |= WDTIE; // enable WDT interrupt lcd_clear(); // clear LCD lcd_backlight(ON); // turn on LCD backlight lcd_rectangle(0, 0, NUM_COLS*2, NUM_ROWS*2, 1); // draw border __bis_SR_register(GIE); // enable interrupts // output splash screen & wait for switch lcd_wordImage(life_image, (159-126)/2, 50, 1); lcd_mode(LCD_PROPORTIONAL | LCD_2X_FONT); lcd_cursor(10, 20); printf("Press Any Key"); lcd_mode(0); while (1) if ((P1IN & 0x0f) ^ 0x0f) break; while (1) { uint16 row, col; // for each live row (78 down to 1) for (row = NUM_ROWS-2; row; --row) { // for each live column (78 down to 1) for (col = NUM_COLS-2; col; --col) { cell_death(row, col); } } char str[] = "x = 2, y = 2, rule = B3/S23\n2o$2o!"; char* ptr; int x_number = 0; int y_number = 0; const uint8 pttrn; for (ptr = str; *ptr; ptr++); { if (*ptr != 'x') continue; if ((*ptr < '0') && (*ptr > '9')) continue; while ((*ptr >= '0') && (*ptr <= '9')) { x_number = x_number * 10 + (*ptr++ - '0'); } if (*ptr != 'y') continue; if ((*ptr < '0') && (*ptr > '9')) continue; while ((*ptr >= '0') && (*ptr <= '9')) { y_number = y_number * 10 + (*ptr++ - '0'); } if (*ptr != 'n') continue; } } while (1) // new pattern seed { // load initial seed patterns uint16 generation = 0; // generation counter uint8 pen = BIRTH; // temp variable WDT_Sec_Cnt = WDT_1SEC_CNT; // set WD 1 second counter seconds = 0; // clear second counter while (1) { uint16 row, col; // for each live row (78 down to 1) for (row = NUM_ROWS-2; row; --row) { // for each live column (78 down to 1) for (col = NUM_COLS-2; col; --col) { if (pen == BIRTH) { cell_birth(row, col); } else { cell_death(row, col); } } } lcd_wordImage(life_image, (159-126)/2, 50, (BIRTH == pen) ? 3 : 1); lcd_cursor(4, 1); // output life generation printf("%d/%d", ++generation, seconds); if (seconds == 100) while (1); pen = (pen == BIRTH) ? DEATH : BIRTH; } } } // end main()
//------------------------------------------------------------------------------ // main ------------------------------------------------------------------------ void main(void) { RBX430_init(_16MHZ); // init board ERROR2(lcd_init()); // init LCD //lcd_volume(376); // increase LCD brightness watchdog_init(); // init watchdog port1_init(); // init P1.0-3 switches __bis_SR_register(GIE); // enable interrupts lcd_clear(); memset(life, 0, sizeof(life)); // clear life array lcd_backlight(ON); lcd_wordImage(life_image, (HD_X_MAX - 126) / 2, 50, 1); lcd_cursor(10, 20); printf("\b\tPress Any Key"); switches = 0; // clear switches flag life_pr = life_prev; life_cr = life_cur; life_nr = life_nex; while (!switches); // wait for any switch while (1) // new pattern seed { uint16 generation; // generation counter uint16 row, col; WDT_Sec_Cnt = WDT_1SEC_CNT; // reset WD 1 second counter seconds = 0; // clear second counter generation = 0; // start generation counter int loop; int neighbors; int left; int middle; int current; int right; loop = 1; memset(life, 0, sizeof(life)); // clear life array memset(life_pr, 0, 10 * sizeof(uint8)); // clear slider memset(life_cr, 0, 10 * sizeof(uint8)); // clear slider memset(life_nr, 0, 10 * sizeof(uint8)); // clear slider init_life(switches); // load seed based on switch switches = 0; // reset switches while (loop) // next generation { RED_TOGGLE; memcpy(life_pr, life[79], 10 * sizeof(uint8)); memcpy(life_cr, life[78], 10 * sizeof(uint8)); memcpy(life_nr, life[77], 10 * sizeof(uint8)); // for each life row (78 down to 1) for (row = NUM_ROWS-2; row > 0; row--) { left = 0; neighbors = 0; current = TEST_CELL(life_cr, 1); right = TEST_CELL(life_pr, 2) + TEST_CELL(life_cr, 2) + TEST_CELL(life_nr, 2); middle = TEST_CELL(life_pr, 1) + current + TEST_CELL(life_nr, 1); // for each life column (78 down to 1) for (col = 1; col < 79; col++) { neighbors = left + (middle - current) + right; //neighbors += (TEST_CELL(life_pr, (col - 1)) + TEST_CELL(life_pr, col) + TEST_CELL(life_pr, (col + 1))); // add number of neighbors on row above //neighbors += (TEST_CELL(life_cr, (col - 1)) + TEST_CELL(life_cr, (col + 1))); // add number of neighbors on current row //neighbors += (TEST_CELL(life_nr, (col - 1)) + TEST_CELL(life_nr, col) + TEST_CELL(life_nr, (col + 1))); // add number of neighbors on row below if(current == 1) // if the cell is currently alive { if(neighbors == 2 || neighbors == 3) // the cell has 2 or 3 neighbors { // do nothing, the cell remains alive } else // the cell doesn't have necessary neighbors { CELL_DEATH(row, col); // clear cell bit in life array CELL_DELETE(row, col); // clear LCD 2x2 pixel point } } else // the cell is currently dead { if(neighbors == 3) // the cell has 3 live neighbors { CELL_BIRTH(row, col); // set cell bit in life array CELL_DRAW(row, col); // set LCD 2x2 pixel point } } neighbors = 0; // reset neighbors left = middle; current = TEST_CELL(life_cr, col + 1); middle = right; right = TEST_CELL(life_pr, col + 2) + TEST_CELL(life_cr, col + 2) + TEST_CELL(life_nr, col + 2); } temp = life_pr; life_pr = life_cr; life_cr = life_nr; life_nr = temp; //memcpy(life_pr, life_cr, 10 * sizeof(uint8)); // sets next row //memcpy(life_cr, life_nr, 10 * sizeof(uint8)); // sets next row memcpy(life_nr, life[row - 2], 10 * sizeof(uint8)); // sets next row } // display life generation and generations/second on LCD if (display_results(++generation)) break; if(switches) { loop = 0; // when a switch is pressed, exit the while loop } } } } // end main()