示例#1
0
int main(void) {
    BOARD_Init();

    // Configure Timer 1 using PBCLK as input. This default period will make the LEDs blink at a
    // pretty reasonable rate to start.
    OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_8, 0xFFFF);

    // Set up the timer interrupt with a priority of 4.
    INTClearFlag(INT_T1);
    INTSetVectorPriority(INT_TIMER_1_VECTOR, INT_PRIORITY_LEVEL_4);
    INTSetVectorSubPriority(INT_TIMER_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0);
    INTEnable(INT_T1, INT_ENABLED);

    /***************************************************************************************************
     * Your code goes in between this comment and the following one with asterisks.
     **************************************************************************************************/
    int x = 0x01;
    LEDS_INIT();
    //give the initial value
    checkItem.event = 0;
    checkItem.value = 0;
    int direction = RIGHT;
    while (1) {
        //set the value
        LEDS_SET(x);
        if (checkItem.event == 1) {
            checkItem.event = 0;
            //two directions
            if(direction == RIGHT){
                x = x << 1;
            }else{
                 x = x >> 1;
            }
        }
        //edge case of 0x100
        if((x == 0x100) && (direction == RIGHT)){
            x = 0x40;
            direction = LEFT;
        }
        //edge case of 0x00
        if((x == 0) && (direction == LEFT)){
            x = 0x02;
            direction = RIGHT;
        }
    }
void mainLoop()
{

    while(1) {



	//pid processing and output debug msg
	if (get_flag(FLAG_END_ADC_CONV)) {

	    temp_error = temp_adc[temp_lvl_real] - conv_result;

	    if ( get_flag(FLAG_PREHEAT) && (temp_error<5) ) {			//check if preheat phase completed
		temp_lvl_real = temp_lvl;
		reset_flag(FLAG_PREHEAT);
	    }

	    if ( get_flag(FLAG_BLINK_ON) && ((get_flag(FLAG_PREHEAT))==0) ) {	//BLINK_OFF if temp stabilized
		if ( (temp_error>-5) && (temp_error<5) ) {
		    LEDS_SET(temp_lvl);
		    reset_flag(FLAG_BLINK_ON);
		}
	    }

	    //calculate error for pid
	    temp_error = temp_adc[temp_lvl_real] - conv_result;
	    if (temp_error > 127) 
		temp_error=127;	// for int8_t in pid
	    else if (temp_error<-127)
		temp_error=-127;

	    heat_cycles = update_pid(&pid, (int8_t)(temp_error), conv_result);

	    reset_flag(FLAG_END_ADC_CONV);

//DBG
//	sprintf(txbuf,"%d,%d %d %d %x\r\n",conv_result,(uint16_t)(temp_lvl), (int16_t)heat_cycles, (int16_t)((int8_t)(temp_error)), (uint16_t)(flags));
//	UART_SendStr(txbuf);    //send result to uart
	}


	//zero-detection time-non-critical events processiong
	if (get_flag(FLAG_ZERO_REACHED)) {

	    reset_flag(FLAG_ZERO_REACHED);
	    phase_ticks++;


	    //leds blink
	    if (get_flag(FLAG_BLINK_ON)) {
		if ( (phase_ticks & 0b011111) == 0 ) {
		    LEDS_SET(temp_lvl);
		} else if ( (phase_ticks & 0b100000) == 0 ) {
		    LEDS_SET(0);
		}
	    }


	    //long switch-pressed: pwr-off
	    if (get_flag(FLAG_SWITCH_PRESSED)) {
		switch_pressed_timer++;
//		if ( switch_pressed_timer == 0 ) {
		if ( switch_pressed_timer == 0x90 ) {
		    switch_pressed_timer=0;
		    temp_lvl=0;
		    temp_lvl_real=0;
		    LEDS_SET(0);
		}
	    }

	    //if key is pressed
	    if ( ((SWITCH_PORT->IDR & SWITCH_PIN)==0) && ((get_flag(FLAG_SWITCH_PRESSED))==0) ) {
		set_flag(FLAG_SWITCH_PRESSED);
		switch_pressed_timer=0;
		temp_lvl = (temp_lvl+1) % 8;
		LEDS_SET(temp_lvl);
		set_flag(FLAG_BLINK_ON);
		set_flag(FLAG_PREHEAT);

		pid.pgain=pgains[temp_lvl];
		pid.igain=igains[temp_lvl];

		if ((temp_lvl<7) && (temp_lvl>0)) {
		    temp_lvl_real = temp_lvl+1;
		} else {
		    reset_flag(FLAG_PREHEAT);
		    reset_flag(FLAG_BLINK_ON);
		    temp_lvl_real = temp_lvl;
		}

	    //if key is released (pull-up)
	    } else if ( (SWITCH_PORT->IDR & SWITCH_PIN) && get_flag(FLAG_SWITCH_PRESSED) ) {
		reset_flag(FLAG_SWITCH_PRESSED);
	    }


	}


    } // end while(1)

}
示例#3
0
int main()
{
    // Configure the device for maximum performance but do not change the PBDIV
    // Given the options, this function will change the flash wait states, RAM
    // wait state and enable prefetch cache but will not change the PBDIV.
    // The PBDIV value is already set via the pragma FPBDIV option above..
    SYSTEMConfig(F_SYS, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    // Auto-configure the PIC32 for optimum performance at the specified operating frequency.
    SYSTEMConfigPerformance(F_SYS);

    // osc source, PLL multipler value, PLL postscaler , RC divisor
    OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1);

    // Configure the PB bus to run at 1/4th the CPU frequency, so 20MHz.
    OSCSetPBDIV(OSC_PB_DIV_4);

    // Enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();
    INTEnableInterrupts();

    // Configure Timer 2 using PBCLK as input. We configure it using a 1:16 prescalar, so each timer
    // tick is actually at F_PB / 16 Hz, so setting PR2 to F_PB / 16 / 100 yields a .01s timer.
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_16, F_PB / 16 / 100);

    // Set up the timer interrupt with a medium priority of 4.
    INTClearFlag(INT_T2);
    INTSetVectorPriority(INT_TIMER_2_VECTOR, INT_PRIORITY_LEVEL_4);
    INTSetVectorSubPriority(INT_TIMER_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0);
    INTEnable(INT_T2, INT_ENABLED);

/******************************** Your custom code goes below here ********************************/
    int check;
    OledInit();
    AdcInit();
    LEDS_INIT();
    check = GameInit();

    if(check == STANDARD_ERROR) {
        FATAL_ERROR();
    }
    float currPage;
    float binSize;
    float titleSize;
    float descSize;
    float numPages;
    uint8_t roomExit;
    uint16_t adcValue = 0;

    while(1) {
        roomExit = GameGetCurrentRoomExits();
        LEDS_SET(roomExit);
        while(buttonEvents == 0) {
            descSize = GameGetCurrentRoomDescription(roomData.description);
            titleSize = GameGetCurrentRoomTitle(roomData.title);

            numPages = ((titleSize + descSize) / MAX_OLED_PIXELS);
            binSize = (ADC_MAX_VALUE / numPages);

            if(AdcChanged()) {
                adcValue = AdcRead();
            }

            currPage = (adcValue / binSize);
            if(currPage < 1) {
                char titleArray[TITLE_OLED_SPACE] = {0};
                char descriptionBuffer[FIRST_PG_DESCRIPTION_OLED_SPACE] = {0};

                strncpy(descriptionBuffer, roomData.description, DESCRIPTION_COPY);
                sprintf(titleArray, "%s\n%s", roomData.title, descriptionBuffer);

                OledClear(OLED_COLOR_BLACK);
                OledDrawString(titleArray);
            } else {
                char buffer[MAX_OLED_PIXELS] = {0};
                int buffIndex;
                buffIndex = (int)currPage * MAX_OLED_PIXELS;
                strncpy(buffer, (roomData.description + buffIndex - OFFSET), MAX_OLED_PIXELS);

                OledClear(OLED_COLOR_BLACK);
                OledDrawString(buffer);
            }
            OledUpdate();
        }

        if((buttonEvents & BUTTON_EVENT_4UP) && (roomExit & GAME_ROOM_EXIT_NORTH_EXISTS)) {
            GameGoNorth();
        } else if((buttonEvents & BUTTON_EVENT_3UP) && (roomExit & GAME_ROOM_EXIT_EAST_EXISTS)) {
            GameGoEast();
        } else if((buttonEvents & BUTTON_EVENT_2UP) && (roomExit & GAME_ROOM_EXIT_SOUTH_EXISTS)) {
            GameGoSouth();
        } else if((buttonEvents & BUTTON_EVENT_1UP) && (roomExit & GAME_ROOM_EXIT_WEST_EXISTS)) {
            GameGoWest();
        }
        buttonEvents = BUTTON_EVENT_NONE;
    }



/**************************************************************************************************/
    while (1);
}