Example #1
0
/********************************************************************
* Function: 	int main()
*
* Precondition: None.
*
* Input: 		None.
*
* Output:		None.
*
* Side Effects:	Enables 32-bit Timer2/3.  Enables UART.
*
* Overview: 	Initialization of program and main loop.
*			
* Note:		 	None.
********************************************************************/
int main()
{
	DWORD_VAL delay;

	//Setup bootloader entry delay
	sourceAddr.Val = DELAY_TIME_ADDR;	//bootloader timer address
	delay.Val = ReadLatch(sourceAddr.word.HW, sourceAddr.word.LW); //read BL timeout
	
	//Setup user reset vector
	sourceAddr.Val = USER_PROG_RESET;
	userReset.Val = ReadLatch(sourceAddr.word.HW, sourceAddr.word.LW);

	//Prevent bootloader lockout - if no user reset vector, reset to BL start
	if(userReset.Val == 0xFFFFFF){
		userReset.Val = BOOT_ADDR_LOW;	
	}
	userResetRead = 0;


	//If timeout is zero, check reset state.
	if(delay.v[0] == 0){

		//If device is returning from reset, BL is disabled call user code
		//otherwise assume the BL was called from use code and enter BL
		if(RCON & 0xFED3){
			//If bootloader disabled, go to user code
			ResetDevice(userReset.Val); 
		}else{
			delay.Val = 0xFF;
		}
	}


	T2CONbits.TON = 0;
	T2CONbits.T32 = 1; // Setup Timer 2/3 as 32 bit timer incrementing every clock
	IFS0bits.T3IF = 0; // Clear the Timer3 Interrupt Flag 
	IEC0bits.T3IE = 0; // Disable Timer3 Interrupt Service Routine 

	//Enable timer if not in always-BL mode
	if((delay.Val & 0x000000FF) != 0xFF){
		//Convert seconds into timer count value 
		delay.Val = ((DWORD)(FCY)) * ((DWORD)(delay.v[0])); 

		PR3 = delay.word.HW; //setup timer timeout value
		PR2 = delay.word.LW;

		TMR2 = 0;
		TMR3 = 0;
		T2CONbits.TON=1;  //enable timer
	}


	//If using a part with PPS, map the UART I/O
	#ifdef DEV_HAS_PPS
		ioMap();
	#endif

	
	//Configure UART pins to be digital I/O.
	#ifdef UTX_ANA
		UTX_ANA = 1;
	#endif
	#ifdef URX_ANA
		URX_ANA = 1;
	#endif


	// SETUP UART COMMS: No parity, one stop bit, autobaud, polled
	UxMODEbits.UARTEN = 1;		//enable uart
    #ifdef USE_AUTOBAUD
	    UxMODEbits.ABAUD = 1;		//use autobaud
    #else
        UxBRG = BAUDRATEREG;
    #endif
	#ifdef USE_HI_SPEED_BRG
		UxMODEbits.BRGH = 1;	//use high speed mode
	#endif
	UxSTA = 0x0400;  //Enable TX

	while(1){

		#ifdef USE_RUNAWAY_PROTECT
			writeKey1 = 0xFFFF;	// Modify keys to ensure proper program flow
			writeKey2 = 0x5555;
		#endif

		GetCommand();		//Get full AN851 command from UART

		#ifdef USE_RUNAWAY_PROTECT
			writeKey1 += 10;	// Modify keys to ensure proper program flow
			writeKey2 += 42;
		#endif

		HandleCommand();	//Handle the command

		PutResponse(responseBytes);		//Respond to sent command

	}//end while(1)

}//end main(void)
Example #2
0
int main(void)
{

    // Start from displaying of PIC24 banners
	_display_state = DISP_HELLO;

    // Setup PortA IOs as digital
    AD1PCFG = 0xffff;

	//IO Mapping for PIC24FJ64GA004 
	#ifdef __PIC24FJ64GA004__ //Defined by MPLAB when using 24FJ64GA004 device
		ioMap();
		lockIO();
	#endif

    // Setup SPI to communicate to EEPROM
    SPIMPolInit();

    // Setup EEPROM IOs
    EEPROMInit();

    // Setup the UART
    UART2Init();

	// Setup the timer
	TimerInit();
    
	// Setup the LCD
	mLCDInit();

	// Setup debounce processing
	BtnInit(); 

    // Setup the ADC
    ADCInit();

	// Setup the banner processing
	BannerStart();

	// Setup the RTCC
    RTCCInit();

	while (1) {
		LCDProcessEvents();
        ADCProcessEvents();

		if (TimerIsOverflowEvent()){

			// Button debounce processing
			BtnProcessEvents();
			// State dependent processing
			switch (_display_state) {
                // Show Microchip banners
				case DISP_HELLO: BannerProcessEvents(); break;
                // Show clock
				case DISP_CLOCK: TBannerProcessEvents(); break;
                // Show voltage and temperature
				case DISP_VOLTAGE: VBannerProcessEvents(); break;

                default: _display_state = DISP_HELLO;
			}// End of switch (_display_state)...

            // If S6 is pressed show the next example
			if (BtnIsPressed(4)) {

                // Change state and clear display 
                if(!TBannerIsSetup()){
       				_display_state++;
                    if(_display_state > DISP_MAX)
                         _display_state = 0;

                    // Initialize state
                    switch (_display_state) {
                        // Microchip banners
                 	    case DISP_HELLO: BannerInit(); break;
                        // Clock
    				    case DISP_CLOCK: TBannerInit(); break;
                        // Voltage and temperature
	        			case DISP_VOLTAGE: VBannerInit(); break;
                        default:
                             _display_state = 0;
        		    }// End of switch (_display_state)...
                    mLCDClear();
                }else
                    TBannerNext();

                // wait for button released
                while (BtnIsPressed(4)){
					BtnProcessEvents();
				}
			}// End of 	if (BtnIsPressed(4)){...

            if(_display_state == DISP_CLOCK){

        		if (BtnIsPressed(1)){
                        TBannerSetup();
                    // wait for button released
                    while (BtnIsPressed(1))	BtnProcessEvents();
                }// End of if (BtnIsPressed(1 ...

                if(TBannerIsSetup()){
	           		if (BtnIsPressed(2)) {
                        TBannerChangeField(1);
                        // wait for button released
                        while (BtnIsPressed(2))	BtnProcessEvents();
    		    	}// End of if (BtnIsPressed(2)){...
    
	    		    if (BtnIsPressed(3)) {
                        // wait for button released
                        TBannerChangeField(0);
                        while (BtnIsPressed(3))	BtnProcessEvents();
        			}// End of if (BtnIsPressed(3)){...
                }// End of if(TBannerIsSetup( ...

            }// End of if(_display_state == DISP_SET_CLOCK ...



            if(_display_state == DISP_VOLTAGE){

        		if (BtnIsPressed(2)){
                    ADCSetFromMemory();
                    // wait for button released
                    while (BtnIsPressed(2)){
						BtnProcessEvents();
					}
                }// End of if (BtnIsPressed(2 ...

           		if (BtnIsPressed(3)){
                    ADCStoreTemperature();
                    // wait for button released
                    while (BtnIsPressed(3)){
						BtnProcessEvents();
					}
  		    	}// End of if (BtnIsPressed(3)){...

            }// End of if(_display_state ...




    	}// End of if (TimerIsOverflowEvent()...
    }// End of while(1)...
}// End of main()...