Exemple #1
0
main(void) {

    //Configure cache, wait states and peripheral bus clock
    // 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(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    // Execute LCD initialization sequence
    initializeLCD();

    writeToLCD(CMDREG, 0x0F); // Turn on blinking cursor
    writeString("HELLO!"); // Write string to the LCD
    newLine(); // Set cursor to line two
    writeString("TESTING..."); // Print another string

    while (1) {
    }
}
main()
{
    // Disable JTAG (on RA0 and RA1 )
    mJTAGPortEnable( DEBUG_JTAGPORT_OFF );

    // 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(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    initializeUART();
    initializeADC();
    initializeLCD();
    initializeRPG();
    
    /* Initialize SD card */
    setup_SDSPI();
    SD_setStart();
    /* Fill tempBuffer[] with int 0 to 63
     * Write it to the current block.
     * Empty tempBuffer[] to all 0.
     * Read from the current block to make sure that it returns the right value.
     */
    fillTempBuffer();
    testSDReadWrite(tempBuffer);

    curr_read_block = curr_block;
    
    ConfigTimer1(); // Enable Timer1 for second counts
    configureInterrupts();

    // T2CON = 0x8030; // TMR1 on, prescale 1:256 PB

    mPORTASetPinsDigitalOut( LED_MASK ); // LEDs = output
    mPORTDSetPinsDigitalIn( PB_MASK_D ); // PBs on D = input

    curr_state = READY;
    // enable interrupts
    INTEnableInterrupts();
    int i = 0;
    while( 1 )
    {
        if (getPrintToUARTFlag() == 1){
            LCDMenuControl();
        
            //mPORTAToggleBits( LED_MASK );
            convertAndPrintIntegerToString("i => ", i++);
            convertAndPrintIntegerToString("timeElapse => ", timeElapsed);
            convertAndPrintIntegerToString("timeElapsedLEDSample => ", timeElapsedLEDSample);
            convertAndPrintIntegerToString("timeElapsedLEDTurnedOff => ", timeElapsedLEDTurnedOff);
            convertAndPrintIntegerToString("sampleLEDNow => ", sampleLEDNow);

            convertAndPrintIntegerToString(" ADC Value => ", getChannel5Value());
            printShadowDetect();
            printLightLevel();
            drawLightDetectedBar();
            controlPowerRelay();

            switch(curr_state) {
            case READY : WriteString("State => READY     ");
                        break;
            case SLEEP : WriteString("State => SLEEP    ");
                        break;
            case HIBERNATE : WriteString("State => HIBERNATE");
                        break;
            case BUSY : WriteString("State => BUSY     ");
                        break;
            }

            WriteString("\r");
            
            setPrintToUARTFlag(0);
        }
        if (NEW_BYTE_RECEIVED == 1){
            curr_state = READY;
            NEW_BYTE_RECEIVED = 0;
            //mPORTAToggleBits( LED_MASK );
            char tempArray[] = "g";
            tempArray[0] = characterByteReceived;
            WriteString(tempArray);
            if(curr_state = HIBERNATE) {
                addByteToBuffer(characterByteReceived);
            }
            else {
                PutCharacter(characterByteReceived);
            }
        }
        if(bufferIndex == 512) {
            SDWriteBlock(currBlock);
            currBlock++;
            bufferIndex = 0;
        }
         if((curr_state == READY) && (timeElapsed >= SLEEP_TIMEOUT) && (timeElapsed < HIBERNATE_TIMEOUT)) {
             curr_state = SLEEP;
         }
         else if((curr_state == SLEEP) && (timeElapsed >= HIBERNATE_TIMEOUT)) {
             curr_state = HIBERNATE;
             timeElapsed = 0;
         }
        if (transmitDataFromSDCard == 1) {
            transmitDataFromSDCard = 0;
            forwardDataToPrinter();
        }
    } // main (while) loop

    return 0;

} // main
Exemple #3
0
int main(void) {
	// initialize
	int rows[] = {ROW1, ROW2, ROW3, ROW4};
	int cols[] = {COL1, COL2, COL3};
	int count;											// counter variable used for loops
	int count_queue;									// Keep track of how many characters are
														// in the queue
	int code_is_correct;								 
	int lock_state = 2;									// initialized lock variable to intermediate status		
	
	// initialize interrupts
	PCICR |= (1 << PCIE0);								// set bit 0 of PCICR
	for(int i = 0; i < NUM_ROWS; i++){
		PCMSK0 |= (1 << rows[i]);						// allow row pins to trigger interrupts
	}
	sei();												// global interrupt enable
	
	// Initializing arrays 
	int enter_code[16] = {'E','N','T','E','R',' ','C','O','D','E',' ',' ',' ',' ',' ',' '};
	int incorrect_code[16] = {'I','N','C','O','R','R','E','C','T',' ','C','O','D','E',' ',' '};
	int unlocked_menu_1[16] = {'#',' ','-',' ','T','O',' ','L','O','C','K',' ',' ',' ',' ',' ',};
	int unlocked_menu_2[16] = {'*',' ','-',' ','S','E','T',' ','N','E','W',' ','C','O','D','E',};
	int new_code_menu[16] = {'S','E','T',' ','N','E','W',' ','C','O','D','E',' ',' ',' ',' ',};
	int cancel_code_change[16] = {' ',' ','-','-','C','A','N','C','E','L','E','D','-','-',' ',' ',};
	int current_code[9];
	
	initRows(rows);										// set keypad rows as inputs
	initColumns(cols);									// set keypad columns as outputs
	initializeLCD();									// set up LCD and initialize in 4 bit mode
	clearKeyQueue();
	count_queue = 0;


	// Ensure we're locked to start with.
	while (lock_state != 1) {
			lock_state = lock(2);
	}

	
	// wait loop
	while(1) {

		// Print greeting
		clearLCD();
		writeLCDline(enter_code,1);
		cursorPosition(2);

		// While box is in locked state
		while (lock_state == 1) {

				// Wait for a key press
				while(key_queue[count_queue] == '\0');
				
				// Now that we have a key press we need to look at what was pressed.
				// Is the first key in the queue a '#'...
				if(key_queue[0] == '#'){
					clearLCD();
					clearKeyQueue();
					count_queue = 0;
				
				// ...or is the first key in the queue a digit?...
				} else if(key_queue[0] >= '0' && key_queue[0] <= '9'){
					// QUESTION: DO WE NEED TO MANUALLY SHIFT THE QUEUE??
					writeLCDcharacter(key_queue[0]);
					++count_queue;
				
				// ...or if first key in the queue isn't a digit or '#' then
				// it must be the '*'
				} else {
					count = 0;
					code_is_correct = 1;
					// Checks the current queue code with the correct code
					while(key_queue[count] != '\0'){
						if(key_queue[count] != current_code[count])
							code_is_correct = 0;				
					}
					if(!code_is_correct){
						clearLCD();
						writeLCDline(incorrect_code,1);
						_delay_ms(5000);
						clearKeyQueue();
						count_queue = 0;
						clearLCD();
					}else{

						// Unlock the box
						lock_state = unlock(lock_state);
					}
				}		
			} // End while (lock_state == 1)

		// While box is in unlocked state
			while (lock_state == 0 ) {

				// Write unlocked menu
				clearLCD();
				writeLCDline(unlocked_menu_1, 1);
				writeLCDline(unlocked_menu_2, 2);

				clearKeyQueue();
				count_queue = 0;

				// Wait for keypress
				while(key_queue[count_queue] == '\0');

				// Now that we have a key press we need to look at what was pressed.
				// Is the first key in the queue a '#'...
				if(key_queue[0] == '#'){
					lock_state = lock(lock_state);
					clearKeyQueue();
					count_queue = 0;

				// ...or is the first key in the queue a '*'?
				} else if(key_queue[0] == '*'){

					clearLCD();
					clearKeyQueue();
					count_queue = 0;
					writeLCDline(new_code_menu, 1);
					cursorPosition(2);

					// Now we start entering the new code. An '*' indicates that we're
					// done entering the code.
					while(key_queue[0] != '*') {
						
						clearKeyQueue();

						// Wait for keypress
						while(key_queue[count_queue] == '\0');

						// Was the latest key pressed between 0 and 9, AND has the user
						// entered less than the max number (8) of digits allowed for the
						// code.
						if(key_queue[0] >= '0' && key_queue[0] <= '9' && count_queue < 9){
							// QUESTION: DO WE NEED TO MANUALLY SHIFT THE QUEUE??
							writeLCDcharacter(key_queue[0]);
							++count_queue;
						
						// If the latest key pressed is '*' then lets write the new
						// code to eeprom
						} else if (key_queue[0] == '*') {
							writeTOeeprom();


						// Otherwise the only key left is '#', so that's what must've been
						// pressed. :)
						} else {
							clearLCD();
							writeLCDline(cancel_code_change,1);
							_delay_ms(5000);
							clearKeyQueue();
							count_queue = 0;
							clearLCD();
						}
				
					}

					
				}		
			} // End while (lock_state == 0 )

	} // End while(1);
} // End main();
Exemple #4
0
 int main(void) {
	 // initialize
	// short int testcode[9] = {'3','2','1','\0','\0','\0','\0','\0','\0'};
	int rows[] = {ROW1, ROW2, ROW3, ROW4};
	int cols[] = {COL1, COL2, COL3};
	
	volatile int count_queue;							// Keep track of how many characters are
														// in the queue
	int code_is_correct;								 
			
	initializeMotorPins();
	initializeKeypadInterrupts(rows);	
	sei();												// global interrupt enable
	
	initRows(rows);										// set keypad rows as inputs
	initColumns(cols);									// set keypad columns as outputs
	initializeLCD();									// set up LCD and initialize in 4 bit mode
	BacklightLCD(1);
	clearKeyQueue();

	//writeLCDcharacter('x');

	
	// Ensure we're locked to start with.
	while (lock_state != 1) {
			lock_state = lock(2);
	}
	initializeTimeout();
	
	
	// wait loop
	while(1) {
		
		
		
		// Print greeting
		clearLCD();
		writeLCDline(enter_code,1);
		cursorPosition(2);
		readFROMeeprom(current_code);
		
		/*
		// TESTING MOTOR
		while(1){
			lock(1);
			_delay_ms(100);
			unlock(1);
			_delay_ms(100);	
		}		
		// END TESTING MOTOR
		*/
		
		// While box is in locked state
		while (lock_state == 1) {
			
			

					
			// Wait for a key press
			while(key_queue[count_queue] == '\0');
			
			// Now that we have a key press we need to look at what was pressed.
			// Is the first key in the queue a '#'...
			if(key_queue[0] == '#'){
				clearLCD();
				writeLCDline(enter_code,1);
				cursorPosition(2);
				clearKeyQueue();
				count_queue = 0;
				//testfun(1);
				
			// ...or is the first key in the queue a digit?...
			} 
			else if((key_queue[0] >= '0') && (key_queue[0] <= '9') && (count_queue < 8)){
				writeLCDcharacter(key_queue[0]);
				count_queue++;	
				
			// ...or if first key in the queue isn't a digit or '#' then
			// it must be the '*'
			} 
			else if(key_queue[0] == '*') {
				count = 0;
				code_is_correct = 1;
				popKey();
				
				clearLCD();
				cursorPosition(1);
				for(int i = 0; i < 10; i++){
					writeLCDcharacter(key_queue[i]);
				}
				cursorPosition(2);
				for(int i = 0; i < 9; i++){
					writeLCDcharacter(current_code[i]);
				}
				_delay_ms(1000);
									
					
				// Checks the current queue code with the correct code
				while(key_queue[count] != '\0'){
					if(key_queue[count] != current_code[count])
						code_is_correct = 0;
					++count;				
				}
				if(!code_is_correct && count > 0){
					clearLCD();
					writeLCDline(incorrect_code,1);
					_delay_ms(5000);
					clearKeyQueue();
					count_queue = 0;
					clearLCD();
					writeLCDline(enter_code,1);
					cursorPosition(2);
				}else if(code_is_correct && count > 0){

					// Unlock the box
					lock_state = unlock(lock_state);
				}
			}					
		} // End while (lock_state == 1)

		// While box is in unlocked state
			while (lock_state == 0 ) {

				// Write unlocked menu
				clearLCD();
				writeLCDline(unlocked_menu_1, 1);
				writeLCDline(unlocked_menu_2, 2);

				clearKeyQueue();
				count_queue = 0;

				// Wait for key press
				while(key_queue[count_queue] == '\0');

				// Now that we have a key press we need to look at what was pressed.
				// Is the first key in the queue a '#'...
				if(key_queue[0] == '#'){
					lock_state = lock(lock_state);
					clearKeyQueue();
					count_queue = 0;
					writeLCDline(enter_code,1);
					cursorPosition(2);

				// ...or is the first key in the queue a '*'?
				} else if(key_queue[0] == '*'){
					
					clearLCD();
					clearKeyQueue();
					count_queue = 0;
					writeLCDline(new_code_menu, 1);
					cursorPosition(2);

					// Now we start entering the new code. An '*' indicates that we're
					// done entering the code.
					while(key_queue[0] != '*') {
						
						// Wait for key press
						while(key_queue[count_queue] == '\0');

						// Was the latest key pressed between 0 and 9, AND has the user
						// entered less than the max number (8) of digits allowed for the
						// code.
						if(key_queue[0] >= '0' && key_queue[0] <= '9' && count_queue < 9){
							// QUESTION: DO WE NEED TO MANUALLY SHIFT THE QUEUE??
							writeLCDcharacter(key_queue[0]);
							++count_queue;
						
						// If the latest key pressed is '*' then lets write the new
						// code to eeprom
						} else if (key_queue[0] == '*') {
							popKey();
							writeTOeeprom(key_queue);
							break;

						// Otherwise the only key left is '#', so that's what must've been
						// pressed. :)
						} else if(key_queue[0] == '#') {
							clearLCD();
							writeLCDline(cancel_code_change,1);
							_delay_ms(5000);
							clearKeyQueue();
							count_queue = 0;
							clearLCD();
							break;
						}
				
					}

					
				}		
			} // End while (lock_state == 0 )

	} // End while(1);
} // End main();