void addChar(char c){ if (column == 8) { column = 0; row++; } if (row == 2) row = 0; moveCursorLCD(row, column); printCharLCD(c); column++; return; if(column != 8){ //end of the columns if(row != 2){ //end of the rows } else{ row = 0; column = 0; } } else{ ++row; column = 0; printCharLCD(c); ++column; } }
void printVoltageLCD(unsigned int adcValue){ printCharLCD(adcValue/100+48); // 1 place printCharLCD('.'); adcValue = adcValue % 100; printCharLCD(adcValue / 10 + 48); // 1/10 place printCharLCD(adcValue%10 + 48); // 1/100 place }
int main(void) { initLCD(); initKeypad(); enableInterrupts(); int counter = 1; char output = 'a'; while(1) { switch(state) { case Read_Keypad: if(ROW1 == PRESSED || ROW2 == PRESSED || ROW3 == PRESSED || ROW4 == PRESSED) { output = scanKeypad(); } if(ROW1 == RELEASED || ROW2 == RELEASED || ROW3 == RELEASED || ROW4 == RELEASED) { state = Print_LCD; } break; case Print_LCD: if( counter < 8) { printCharLCD(output); counter++; } else if (counter == 8) { printCharLCD(output); counter++; moveCursorLCD(1, 2); } else if (counter > 8 && counter < 16) { printCharLCD(output); counter++; } else if(counter == 16) { printCharLCD(output); counter = 0; moveCursorLCD(1, 1); } state = Read_Keypad; break; } } return 0; }
void analogPrintIR(int irData) { // gather data from ports // may need to swap port1-2-3-4 function inupts with global definitions... //////// printIRStateCode(lastTrackLineState); //////// printCharLCD(' '); //////// //////// printCharLCD(((irData & 128) >> 7) + '0'); //////// printCharLCD(((irData & 64) >> 6) + '0'); //////// printCharLCD(((irData & 32) >> 5) + '0'); //////// //////// moveCursorLCD(1, 1); //////// printStringLCD(" "); //////// printCharLCD(((irData & 16) >> 4) + '0'); //////// //////// printCharLCD(((irData & 4) >> 2) + '0'); //////// printCharLCD(((irData & 2) >> 1) + '0'); //////// printCharLCD(((irData & 1) >> 0) + '0'); //////// //////// printCharLCD(((irData & 8) >> 3) + '0'); clearLCD(); printIRStateCode(lastTrackLineState); printCharLCD(' '); //////// printCharLCD(((irData % 100000000) / 10000000) + '0'); // print eight digit //////// printCharLCD(((irData % 10000000) / 1000000) + '0'); // print seventh digit //////// printCharLCD(((irData % 1000000) / 100000) + '0'); // print sixth digit //////// moveCursorLCD(1, 1); //////// printStringLCD(" "); //////// //////// //////// printCharLCD(((irData % 100000) / 10000) + '0'); // print fifth digit //////// printCharLCD(((irData % 10000) / 1000) + '0'); // print fourth digit //////// printCharLCD(((irData % 100) / 10) + '0'); // print second digit //////// printCharLCD((irData % 10) + '0'); // print first digit //////// printCharLCD(((irData % 1000) / 100) + '0'); // print fourth digit //////// printCharLCD(((irData % 100) / 10) + '0'); // print second digit printCharLCD(((irData % 100000000) / 10000000) + '0'); // print eight digit printCharLCD(((irData % 100000) / 10000) + '0'); // print fifth digit moveCursorLCD(1, 1); printStringLCD(" "); printCharLCD(((irData % 10000) / 1000) + '0'); // print fourth digit printCharLCD(((irData % 1000000) / 100000) + '0'); // print sixth digit printCharLCD((irData % 10) + '0'); // print first digit printCharLCD(((irData % 10000000) / 1000000) + '0'); // print seventh digit printCharLCD(((irData % 1000) / 100) + '0'); // print fourth digit return; }
/* * The printTime() function takes a time value in milliseconds and a header value * and prints the time and the header to the LCD. */ void printTime(int totalTime, char header){ int mins = totalTime/60000; int mins_tens = mins/10; int secs = 0; int secs_tens = 0; int ms = 0; int ms_tens = 0; totalTime = totalTime - mins*60000; secs = totalTime/1000; secs_tens = secs/10; totalTime = totalTime - secs*1000; ms = totalTime/10; ms_tens = ms/10; if(header == RUNNING_CONST) { moveCursorLCD(0, 4); printStringLCD("Running:"); } else { moveCursorLCD(0, 4); printStringLCD("Stopped:"); } moveCursorLCD(1,4); printCharLCD(mins_tens|(0b00110000)); printCharLCD((mins%10)|(0b00110000)); printCharLCD(':'); printCharLCD(secs_tens|(0b00110000)); printCharLCD((secs%10)|(0b00110000)); printCharLCD(':'); printCharLCD(ms_tens|(0b00110000)); printCharLCD((ms%10)|(0b00110000)); }
int main() { enableInterrupts(); initTMR2(); char key = 'x'; while(1); { clearLCD(); InitKeyPad(); switch(state) { case findKey: ScanKeys(); //should i update the key value here? break; case debouncePress: delayUs(5000); //Proper Delay? break; case debounceRelease: delayUs(5000); break; case display: printCharLCD(key); break; } } }
/* * You can use printCharLCD here. Note that every time you write a character * the cursor increments its position automatically. * Since a string is just a character array, try to be clever with your use of pointers. */ void printStringLCD(const char* s) { int i; for(i=0; s[i]; i++) { //loops til s[i] is a null pointer printCharLCD(s[i]); } }
/* * The printStringLCD() function prints a string of characters to the LCD */ void printStringLCD(const char* s) { //TODO: BOGUS unsigned char i = 0; while(s[i] != '\0') { printCharLCD(s[i]); i = i + 1; } }
/* * You can use printCharLCD here. Note that every time you write a character * the cursor increments its position automatically. * Since a string is just a character array, try to be clever with your use of pointers. */ void printStringLCD(const char* s) { int i=0; //while we haven't reached the null char at the end of a string for (i = 0; s[i] != '\0'; i++){ printCharLCD(s[i]); } }
void printStringLCD(const char* s){ //if(s){ while(*s != '\0'){ printCharLCD(*s); s++; } //} }
void printIR() { // gather data from ports // may need to swap port1-2-3-4 function inupts with global definitions... int irData = readIR(); clearLCD(); // to test, without readIR(), use this code... printCharLCD(!IR1port + '0'); printCharLCD(!IR2port + '0'); printCharLCD(!IR3port + '0'); printCharLCD(!IR4port + '0'); // // printCharLCD((irData & 1) + '0'); // print first bit // printCharLCD(((irData & 2) >> 1) + '0'); // print second bit // printCharLCD(((irData & 4) >> 2) + '0'); // print third bit // printCharLCD(((irData & 8) >> 3) + '0'); // print fourth bit }
/* * This function is called in lab1p2.c for testing purposes. * If everything is working properly, you should get this to look like the video on D2L * However, it is suggested that you test more than just this one function. */ void testLCD(){ initLCD(); int i = 0; printCharLCD('c'); for(i = 0; i < 1000; i++) delayUs(1000); clearLCD(); printStringLCD("Hello!"); moveCursorLCD(1, 2); for(i = 0; i < 1000; i++) delayUs(1000); printStringLCD("Hello!"); for(i = 0; i < 1000; i++) delayUs(1000); }
int main() { SYSTEMConfigPerformance(10000000); //Does something with assembly to set clock speed enableInterrupts(); //Make interrupt work //initLEDs(); //initTimer1(); initTimer2(); //initSW1(); initLCD(); delay(500); //printCharLCD(0); testLCD(); moveCursorLCD(0,0); while (1) { printCharLCD('K'); } return 0; }
int main(void) { SYSTEMConfigPerformance(SYS_CLOCK); enableInterrupts(); initADC(); initTimer2(); //for PWM initPWM(); initLCD(); initTimer4(); //for pwm initSW(); int track = 1; int SpeedR = 0; int SpeedL = 0; int ones = 0; int dec = 0; float voltage = 0; //Actual main state machine loop while(1) { Speed = ADC1BUF0; moveCursorLCD(0,0); voltage = Speed * ((double)3.3/1023); ones = floor(voltage); dec = (voltage - ones) * 10; printCharLCD('0'+ones); printCharLCD('.'); printCharLCD('0'+dec); printCharLCD('V'); delayUs(10000); //10ms - Just to help the LCD not strobe too much switch(state) { case wait_Press: //handled by ISR break; case debouncePress: state = wait_Release; break; case wait_Release: //handled by ISR break; case debounceRelease: if (track == 1){ state = forward; } if (track == 2){ state = idle2; } if (track == 3){ state = reverse; } if (track == 4){ state = idle1; } break; case idle1: track = 1; motorsOff(); state = wait_Press; break; case forward: track = 2; if(Speed == 511){ SpeedR = 1023; SpeedL = 1023; } else if(Speed < 511){ SpeedR = 1023; SpeedL = (2*Speed); } else if(Speed > 511){ SpeedR = 2*(1023 - Speed); SpeedL = 1023; } else if (Speed == 1023){ SpeedR = 0; SpeedL = 1023; } else if (Speed == 0){ SpeedR = 1023; SpeedL = 0; } RightMotor_Write(SpeedR); LeftMotor_Write(SpeedL); break; case idle2: track = 3; motorsOff(); state = wait_Press; break; case reverse: track = 4; if(Speed == 511){ SpeedR = 1023; SpeedL = 1023; } else if(Speed < 511){ SpeedR = 1023; SpeedL = 2*Speed; } else if(Speed > 511){ SpeedR = 2*(1023 - Speed); SpeedL = 1023; } else if (Speed == 1023){ SpeedR = 1023; SpeedL = 0; } else if (Speed == 0){ SpeedR = 0; SpeedL = 1023; } RightMotor_Write(0-SpeedR); //maybe it needs the zero to be interpreted as negative? LeftMotor_Write(0-SpeedL); break; } } return 0; }
int trackLine(int i) { irStateType nextState = maintainSetting; //increased motor speed from 85 to 100 int motorSpeed = 90; // full speed ahead!! int secondMotorSpeed = 515; char str[6] = {0, 0, 0, 0, 0, 0}; int irData = 0; // possible irData values 0-15 switch (trackLineState) { // motor movement definitions are in pwm .h and .c files case findLine: // turn in circles until line is found motorFindLine(motorSpeed); lastTrackLineState = findLine; trackLineState = maintainSetting; break; case turnLeft: // turn left setMotorsLeft(motorSpeed); lastTrackLineState = turnLeft; trackLineState = maintainSetting; break; case turnRight: // turn right setMotorsRight(motorSpeed); lastTrackLineState = turnRight; trackLineState = maintainSetting; break; case goFwd: // go forward setMotorsForward(motorSpeed); lastTrackLineState = goFwd; trackLineState = maintainSetting; break; case goBck: // go backward setMotorsBackward(motorSpeed); lastTrackLineState = goBck; trackLineState = maintainSetting; break; case stop: // stop! setMotorsIdle(); lastTrackLineState = stop; trackLineState = maintainSetting; break; case maintainSetting: // check the ir data #ifdef use_digital_ir irData = readNewIR(); // changed from readIR() nextState = parseIRData(irData); #endif #ifdef use_analog_ir irData = analogReadIR(); // nextState = parseNewIRData(irData); if(i == 0){ nextState = turnLeft; delayUs(1000000); } else{ nextState = turnRight; delayUs(5000000); } #endif // only change states if necessary if (nextState != lastTrackLineState) { trackLineState = nextState; lastTrackLineState = maintainSetting; } else { trackLineState = maintainSetting; } break; } //****************REMOVE?*************** #ifdef debug_ir //clearLCD(); // the following print Char statments output the state name as a 3 char // code ### and code info ( as shown below ) // ### 000 // 00000 #ifdef use_digital_ir printIRStateCode(lastTrackLineState); printCharLCD(' '); printCharLCD(((irData & 128) >> 7) + '0'); printCharLCD(((irData & 64) >> 6) + '0'); printCharLCD(((irData & 32) >> 5) + '0'); moveCursorLCD(1, 1); printStringLCD(" "); printCharLCD(((irData & 16) >> 4) + '0'); printCharLCD(((irData & 4) >> 2) + '0'); printCharLCD(((irData & 2) >> 1) + '0'); printCharLCD(((irData & 1) >> 0) + '0'); printCharLCD(((irData & 8) >> 3) + '0'); // printCharLCD(((trackLineState & 8) >> 3) + '0'); // printCharLCD(((trackLineState & 4) >> 2) + '0'); // printCharLCD(((trackLineState & 2) >> 1) + '0'); // printCharLCD(((trackLineState & 1) >> 0) + '0'); // printIR(); // printCharLCD(((lastTrackLineState & 8) >> 3) + '0'); // printCharLCD(((lastTrackLineState & 4) >> 2) + '0'); // printCharLCD(((lastTrackLineState & 2) >> 1) + '0'); // printCharLCD(((lastTrackLineState & 1) >> 0) + '0'); #endif #endif }
int main(void) { char passWord[passwords][wordLen]; //[nump][lenp]; char temp[wordLen]; //string to hold characters as they are typed in int pwItt = 0; //index of the password being typed in by user int pwStoreIndex = 0; //index for string# in passWord[][] array //index is used to add pws to passWord[][] int numCharsPrinted = 0; // counter to keep things tidy on the display int modeStateEnable = 0; // enables the second state machine int i = 0, j = 0; int match = 0; int count = 0; ANSELE = 0; SYSTEMConfigPerformance(40000000); initLCD(); clearLCD(); writeLCD(0b00001111, 0, 50); initTimer1(); initKeypad(); enableInterrupts(); clearBuff(wordLen, temp); //initialize temp to NULL for (i = 0; i < wordLen; i++) { temp [i] = NULL; } //initialize passcode array to NULL for (i = 0; i < passwords; i++) { for (j = 0; j < wordLen; j++) { passWord[i][j] = NULL; } } #ifdef TEST2 int flag = 0; //Precode 2 passwords into password array and test if working for (i = 0; i < 2; i++) { for (j = 0; j < wordLen-3; j++) { if (flag == 0) { passWord[i][j] = '0'+j; } else { passWord[i][j] = '0'+j + 1; } } flag++; passWord[i][j] = '\0'; } #endif // initialize the state machine output printStringLCD("Enter"); #ifdef run while (1) { //<><><><> button de bounce stuff <><><><><><><><><><><><><><><><><><><><><><> switch (state) { case printKey: modeStateEnable = 1; temp[pwItt] = keyScanned; pwItt++; state = waitForRelease; break; case scanKey: keyScanned = scanKeypad(); state = printKey; break; case dbPress: delayUs(DBDelayTime); state = scanKey; break; case dbRelease: delayUs(DBDelayTime); state = waitForPress; break; case waitForPress: //enableInterrupts(); break; case waitForRelease: // enableInterrupts(); break; } //<><><><> END button de bounce stuff <><><><><><><><><><><><><><><><><><><><><><> //!!! TODO !!! every exit branch of the state machine WILL: // enable/disable interrupts // set the next state (even if it does not change) // set modeStateEnable to 1 or 0 // THIS will help with debugging //<><><><> Mode setting state machine <><><><><><><><><><><><><><><><><><><><><><> if (modeStateEnable == 1) { // need to ensure this is correct... disableInterrupts(); //messing with printing? modeStateEnable = 0; //wait for key input // the newKeyPressed variable gets changed to 1 everytime a key press is detected switch (modeState) { case firstStar: //interrupts are ENABLED DELETEME if (temp[1] == '*') { modeState = set; // the state that allows you to add pws modeStateEnable = 0; //wait for new key to be pressed clearLCD(); printStringLCD("Set Mode"); moveCursorLCD(2, 1); enableInterrupts(); } else { modeState = dispBad; modeStateEnable = 1; //goto state disableInterrupts(); } break; case dispGood: //interrupts are DISABLED DELETEME printOutput("Good"); clearLCD(); printStringLCD("Enter"); //prompt enter state clearBuff(wordLen, temp); // clear the temp string pwItt = 0; // reset the pw itterator modeState = dispEnter; //switch state modeStateEnable = 0; //wait for new key to be pressed enableInterrupts(); break; case dispBad: //interrupts are DISABLED DELETEME printOutput("Bad"); clearLCD(); printStringLCD("Enter"); // prompt enter state modeState = dispEnter; // switch state clearBuff(wordLen, temp); // clear the temp string pwItt = 0; // reset the pw itterator modeStateEnable = 0; // wait for new key to be pressed enableInterrupts(); break; case dispEnter: //interrupts are DISABLED DELETEME clearLCD(); printStringLCD("Enter"); moveCursorLCD(2, 1); printStringLCD(temp); // print the characters as they are entered if (pwItt == 4) { if (checkValid(temp, passWord) == 0) { //if( match == 0){ modeState = dispBad; // 0 means invalid pw modeStateEnable = 1; //goto state disableInterrupts(); } else { modeState = dispGood; //1 means valid pw modeStateEnable = 1; //goto state disableInterrupts(); printCharLCD('&'); } } else if (temp[0] == '*') { modeState = firstStar; modeStateEnable = 0; //wait for new key to be pressed enableInterrupts(); } else if (temp[0] == '#') { modeState = dispBad; modeStateEnable = 1; //continue disableInterrupts(); } else { modeState = dispEnter; modeStateEnable = 0; //wait for new key to be pressed enableInterrupts(); } break; case dispValid://- printOutput("Valid "); clearLCD(); printStringLCD("Enter"); if (addNewPw(temp, passWord) == 0) { // if password is not in list //if(match == 0){ // if password is not in list strcpy(passWord[pwStoreIndex], temp); // add it pwStoreIndex++; // increment itterator } modeState = dispEnter; clearBuff(wordLen, temp); // clear the temp string pwItt = 0; // reset the pw itterator modeStateEnable = 0; //wait for new key to be pressed enableInterrupts(); break; case dispInvalid://- printOutput("Invalid "); clearLCD(); printStringLCD("Enter"); modeState = dispEnter; clearBuff(wordLen, temp); // clear the temp string pwItt = 0; // reset the pw itterator modeStateEnable = 0; //wait for new key to be pressed enableInterrupts(); break; case set: printCharLCD(keyScanned); // might work better to press key pressed if (keyScanned == '*' || keyScanned == '#') { // if an invalid key was entered modeState = dispInvalid; // if new pw is not valid modeStateEnable = 1; //next state needs to be executed disableInterrupts(); } else if (pwItt == 6) { // pw == "**xxxx"... temp[0] = temp[2]; temp[1] = temp[3]; // remove leading "**" temp[2] = temp[4]; temp[3] = temp[5]; temp[4] = NULL; temp[5] = NULL; modeState = dispValid; modeStateEnable = 1; //next state needs to be executed disableInterrupts(); } else { modeStateEnable = 0; //next state needs to be executed enableInterrupts(); } break; } } //<><><><> END Mode setting state machine <><><><><><><><><><><><><><><><><><><><><><> } return 0; }
int main(void) { SYSTEMConfigPerformance(10000000); enableInterrupts(); initTimer2(); initKeypad(); initLCD(); char password1[5] = {'a','a','a','a','\0'}; //2D array of passwords char password2[5] = {'a','a','a','a','\0'}; char password3[5] = {'a','a','a','a','\0'}; char password4[5] = {'a','a','a','a','\0'}; char tempPassword[5] = {'\0','\0','\0','\0','\0'}; //password being entered char keyPressed = 'f'; int row = 1; int position = 0; int counter = 1; int numPasswords = 0; //number of passwords int i =0; int j =0; int set = 0; //TRISA.RA7 TRISAbits.TRISA7 = 0; //TRISCbits.TRISC4 = 0; while(1) { switch(state){ case Wait: if( set == 1){ // moveCursorLCD(1,1); //first line // printStringLCD("Set Mode"); } else { moveCursorLCD(1,1); //first line printStringLCD("Enter"); } break; case Wait2: if((PORTBbits.RB10 == 1) && (PORTBbits.RB12 == 1) && (PORTBbits.RB14 == 1) ) { //waits for button release state = DeBounceRelease; break; } else state = Wait2; break; case DeBounce: //moveCursorLCD(1,1); //first line //printStringLCD("DeBounce State"); delayUs(500); state = SearchKeypad; break; case DeBounceRelease: //moveCursorLCD(1,1); //first line //printStringLCD("DeBounce State"); delayUs(500); // moveCursorLCD(1,2); //first line // printStringLCD("Button Released"); state = EnterPassword; //IEC1bits.CNBIE = ON; break; case Set: moveCursorLCD(1,1); //first line printStringLCD("Set Mode"); moveCursorLCD(1,2); // clears second line clearLCD; set = 1; state = Wait; break; case EnterPassword: if(i == 4){ i = 0; } tempPassword[i] = keyPressed; i = i + 1; if( i == 2){ //if((passwords[0][numPasswords] == '*') && (passwords[1][numPasswords] == '*')) { if((tempPassword[0] == '*') && (tempPassword[1] == '*')) { i = 0; state = Set; } } state = WriteLCD; break; case CheckNewPassword: for(j = 0; j < 4; j++){ //checks all stored passwords if ((tempPassword[j] == '*') || (tempPassword[j] == '#')){ moveCursorLCD(1,1); //first line printStringLCD("Invalid"); //print valid delayUs(2000000); moveCursorLCD(1,2); // clears second line clearLCD; set = 0; state = Wait; } } moveCursorLCD(1,1); //first line printStringLCD("Valid"); //print invalid delayUs(2000000); moveCursorLCD(1,2); // clears second line clearLCD; numPasswords = numPasswords +1; if(numPasswords == 4){ numPasswords = 0; } if(numPasswords == 0){ for(j =0; j < 5; j++){ password1[j] = tempPassword[j]; } } else if(numPasswords == 1){ for(j =0; j < 5; j++){ password2[j] = tempPassword[j]; } } else if(numPasswords == 2){ for(j =0; j < 5; j++){ password3[j] = tempPassword[j]; } } else if(numPasswords = 3){ for(j =0; j < 5; j++){ password4[j] = tempPassword[j]; } } set = 0; state = Wait; break; case CheckPassword: if (strcmp(password1, tempPassword) == 0){ moveCursorLCD(1,1); //first line printStringLCD("Good"); //print valid delayUs(20000); moveCursorLCD(1,2); // clears second line clearLCD; state = Wait; } if (strcmp(password2, tempPassword) == 0){ moveCursorLCD(1,1); //first line printStringLCD("Good"); //print valid delayUs(20000); moveCursorLCD(1,2); // clears second line clearLCD; state = Wait; } if (strcmp(password3, tempPassword) == 0){ moveCursorLCD(1,1); //first line printStringLCD("Good"); //print valid delayUs(20000); moveCursorLCD(1,2); // clears second line clearLCD; state = Wait; } if (strcmp(password4, tempPassword) == 0){ moveCursorLCD(1,1); //first line printStringLCD("Good"); //print valid delayUs(20000); moveCursorLCD(1,2); // clears second line clearLCD; state = Wait; } moveCursorLCD(1,1); //first line printStringLCD("Bad"); //print invalid delayUs(20000); moveCursorLCD(1,2); // clears second line clearLCD; state = Wait; break; case SearchKeypad: //moveCursorLCD(1,1); //first line //printStringLCD("Search Keypad"); //delayUs(5000); // IEC1bits.CNBIE = OFF;// turn off ISR CNCONBbits.ON = 0; //turn change notifications off LATEbits.LATE0 = 1;// open drain collector for outputs LATEbits.LATE2 = 1; LATEbits.LATE4 = 1; LATEbits.LATE6 = 1; keyPressed = scanKeypad(); LATEbits.LATE0 = 0;// open drain collector for outputs LATEbits.LATE2 = 0; LATEbits.LATE4 = 0; LATEbits.LATE6 = 0; CNCONBbits.ON = 1; // CN on state = Wait2; //state = WriteLCD; break; case WriteLCD: if (counter == 9){ // row = ~row; counter = 1; } moveCursorLCD(counter,2); delayUs(2000); printCharLCD(keyPressed); delayUs(200); counter = counter + 1; if(i == 3){ if(set == 1){ state = CheckNewPassword; } state = CheckPassword; } state = Wait; //IEC1bits.CNBIE = ON; break; } } return 0; }
int main(void) { SYSTEMConfigPerformance(40000000); enableInterrupts(); initTimer2(); initLCD(); clearLCD(); initKeypad(); while (1) { switch (state) { case Start: initLCD(); clearLCD(); state = ROW1; break; case ROW1: Row=1; //printCharLCD('1'); LATGbits.LATG0 = DISABLED; LATFbits.LATF1 = DISABLED; LATDbits.LATD12 = DISABLED; LATGbits.LATG13 = ENABLED; //delayUs2(70); state = ROW2; break; case ROW2: Row=2; //printCharLCD('2'); LATDbits.LATD12 = DISABLED; LATFbits.LATF1 = DISABLED; LATGbits.LATG13 = DISABLED; LATGbits.LATG0 = ENABLED; //delayUs2(70); state = ROW3; break; case ROW3: Row=3; //printCharLCD('3'); LATDbits.LATD12 = DISABLED; LATGbits.LATG13 = DISABLED; LATGbits.LATG0 = DISABLED; LATFbits.LATF1 = ENABLED; //delayUs2(70); state = ROW4; break; case ROW4: Row=4; LATGbits.LATG13 = DISABLED; LATGbits.LATG0 = DISABLED; LATFbits.LATF1 = DISABLED; LATDbits.LATD12 = ENABLED; //delayUs2(70); state = ROW1; break; case DebouncePress: delayUs2(700); state = WaitRelease; break; case WaitRelease: state = WaitRelease; break; case DebounceRelease: delayUs2(700); state = WriteLCD; break; case WriteLCD: key=scanKeypad(Col, Row); printCharLCD(key); state = ROW1; break; } } return 0; }
int main(void) { SYSTEMConfigPerformance(10000000); enableInterrupts(); initTimerDebounce(); initTimerDelay(); initLCD(); initKeypad(); int runQATests = NTEST; //test keypad software functions if TEST, don't if NTEST if(runQATests == TEST) { test_clearString(); delayUs(2000000); clearLCD(); test_checkPass(); delayUs(2000000); clearLCD(); test_updatePass(); return -1; } char key = NULL; char passStringa[5] = ""; char passStringb[5] = ""; char passStringc[5] = ""; char passStringd[5] = ""; int PassCount = 0; int correct = NOTMATCH; char tempPass[5] = ""; char guessString[5] = ""; moveCursorLCD(0,0); while(1) { switch(state) { case enter: if(mode != ENTER || prevState != enter) { clearLCD(); printStringLCD("Enter"); moveCursorLCD(1,0); mode = ENTER; } prevState = enter; break; case debounce: break; case update: if(row != -1) { key = scanKeypad(row); } else { break; } if(key != -1) { printCharLCD(key); if(mode == ENTER) { guessString[keyPresses] = key;//append key to guessString state = enter; } else if(mode == SET) { tempPass[keyPresses] = key;//append key to passString state = set_mode; } keyPresses = keyPresses + 1; if(mode == ENTER) { correct = NOTMATCH; correct = checkPass(guessString, passStringa, passStringb, passStringc, passStringd); if(keyPresses == 4 && (correct == NOTMATCH)) { state = bad; } else if(key == '#') { state = bad; } else if((keyPresses > 1) && (keyPresses < 4) && (key == '*') && (strcmp(guessString, "**")!=0)) { state = bad; } else if((keyPresses==2)&&(strcmp(guessString,"**")==0)) { state = set_mode; } else if((keyPresses==2) && (key!='*')&&(guessString[0]=='*')) { state = bad; } else if((keyPresses==4)&&(correct == 0)) { state = good; } } else if((keyPresses<4)&&(mode==SET)&&(key!='#')&&(key!='*')) { state = set_mode; } else if(((key=='#')||(key=='*'))&&(mode==SET)) { state = invalid; } else if((mode==SET)&&(keyPresses==4)&&(key!='#')&&(key!='*')) { state = valid; } else if(keyPresses < 4 && mode == ENTER && key != '#') state = enter; } else { printCharLCD(' '); moveCursorLCD(1, keyPresses); if(mode == ENTER) state = enter; else if(mode == SET) state = set_mode; } key = NULL; break; case set_mode: if(mode != SET || prevState != set_mode) { clearLCD(); printStringLCD("Set Mode"); moveCursorLCD(1,0); keyPresses = 0; strcpy(guessString, ""); clearString(guessString); mode = SET; } prevState = set_mode; break; case good: clearLCD(); printStringLCD("Good"); keyPresses = 0; clearString(guessString); state = enter; prevState = good; delayUs(2000000); break; case bad: clearLCD(); printStringLCD("Bad"); keyPresses = 0; clearString(guessString); state = enter; prevState = bad; delayUs(2000000); break; case valid: clearLCD(); if(PassCount == 3) { PassCount = 0; } else PassCount = PassCount + 1; updatePass(tempPass, passStringa, passStringb, passStringc, passStringd, PassCount); printStringLCD("Valid"); keyPresses = 0; clearString(tempPass); mode = ENTER; state = enter; prevState = valid; delayUs(2000000); break; case invalid: clearLCD(); printStringLCD("Invalid"); keyPresses = 0; clearString(tempPass); mode = ENTER; state = enter; prevState = invalid; delayUs(2000000); break; } } return 0; }
int main(void) { initLCD(); initKeypad(); enableInterrupts(); int counter = 1; char output = 'a'; int count = 0; char p1[4]={'1','3','3','7'};//PRESET PASSWORD 1 char p2[4];//PASSWORD 2 char p3[4];//PASSWORD 3 char p4[4];//PASSWORD 4 char UserInput[4]; //USER INPUT ARRAY int i=0; //USER INPUT ARRAY ELEMENT int InSet = TURNOFF; int InEnter = TURNON; int GUESS1; int GUESS2; int GUESS3; int GUESS4; int StarCount=0; int PASSWORD2 = EMPTY; int PASSWORD3 = EMPTY; int PASSWORD4 = EMPTY; int USERINPUT= VALID; //for checking * or while(1) { switch(state) { case Enter_Mode: InSet = TURNOFF; InEnter = TURNON; clearLCD(); printStringLCD("Enter Code:"); moveCursorLCD(0,2); //COL ROW count=0; StarCount=0; if(count == 4) { count = 0; for(i=0;i<4;i++) { if(p1[i]!=UserInput[i]) { GUESS1=BAD; break; } else { GUESS1=GOOD; } } for(i=0;i<4;i++) { if(p2[i]!=UserInput[i]) { GUESS2=BAD; break; } else { GUESS2=GOOD; } } for(i=0;i<4;i++) { if(p3[i]!=UserInput[i]) { GUESS3=BAD; break; } else { GUESS3=GOOD; } } for(i=0;i<4;i++) { if(p4[i]!=UserInput[i]) { GUESS4=BAD; break; } else { GUESS4=GOOD; } } if(GUESS1==GOOD || GUESS2==GOOD || GUESS3==GOOD || GUESS4==GOOD) { clearLCD(); printStringLCD("GOOD"); for(i=0;i<500;i++) { delayUs(1000); } } else if(GUESS1==BAD && GUESS2==BAD && GUESS3==BAD && GUESS4==BAD) { clearLCD(); printStringLCD("BAD"); for(i=0;i<500;i++) { delayUs(1000); } } } if(StarCount==1) { USERINPUT=VALID; //ASSUME InSet = TURNOFF; InEnter = TURNON; StarCount=0; count=0; for(i=0;i<4;i++)//RESET USER INPUT { UserInput[i]='0'; } state = Set_Mode; } break; case Read_Keypad: if(ROW1 == PRESSED || ROW2 == PRESSED || ROW3 == PRESSED || ROW4 == PRESSED) { output = scanKeypad(); } if(ROW1 == RELEASED || ROW2 == RELEASED || ROW3 == RELEASED || ROW4 == RELEASED) { state = Print_LCD; } break; case Print_LCD: if( counter < 8) { printCharLCD(output); counter++; } else if (counter == 8) { printCharLCD(output); counter++; moveCursorLCD(1, 2); } else if (counter > 8 && counter < 16) { printCharLCD(output); counter++; } else if(counter == 16) { printCharLCD(output); counter = 0; moveCursorLCD(1, 1); } count++; break; case Set_Mode: InSet = TURNON; InEnter = TURNOFF; clearLCD(); printStringLCD("Set Mode:"); moveCursorLCD(0,2); //COL ROW USERINPUT=VALID; for(i=0;i<4;i++)//Check UserInput values UI[0]->UI[4] { if(UserInput[i]=='*' || UserInput[i]=='#')//Check for * or # { USERINPUT=NOTVALID; //SET USERINPUT to NOTVALID } } if(USERINPUT==VALID)// No * or # in USERINPUT VALUES { if(PASSWORD2==EMPTY) { for(i=0;i<4;i++) { p2[i]=UserInput[i]; } PASSWORD2 = FULL; clearLCD(); printStringLCD("PW2 STORED"); for(i=0;i<500;i++) { delayUs(1000); } } else if(PASSWORD2==FULL && PASSWORD3==EMPTY) { for(i=0;i<4;i++) { p3[i]=UserInput[i]; } PASSWORD3 = FULL; clearLCD(); printStringLCD("PW3 STORED"); for(i=0;i<500;i++) { delayUs(1000); } } else if(PASSWORD2==FULL && PASSWORD3==FULL && PASSWORD4==EMPTY) { for(i=0;i<4;i++) { p4[i]=UserInput[i]; } PASSWORD4 = FULL; clearLCD(); printStringLCD("PW4 STORED"); for(i=0;i<500;i++) { delayUs(1000); } clearLCD(); printStringLCD("VALID"); //PW4 IS THE LAST PASSWORD ENTERED for(i=0;i<500;i++) { delayUs(1000); } } if (PASSWORD2==FULL && PASSWORD3==FULL && PASSWORD4==FULL) //ALL PASSWORDS ARE FULL { clearLCD(); printStringLCD("STORAGE FULL"); for(i=0;i<500;i++) { delayUs(1000); } } else if (PASSWORD2==EMPTY || PASSWORD3==EMPTY || PASSWORD4==EMPTY) //At LEAST ONE PASSWORD IS EMPTY { clearLCD(); printStringLCD("VALID"); for(i=0;i<500;i++) { delayUs(1000); } } } else if(USERINPUT==NOTVALID)// * or # is in USERINPUT VALUES { clearLCD(); printStringLCD("NOT VALID"); for(i=0;i<500;i++) { delayUs(1000); } } break; } } return 0; }
int main(void) { SYSTEMConfigPerformance(40000000); initKeypad(); enableEnterruptKeypad(); initTimer2(); initLCD(); enableInterrupts(); moveCursorLCD(0,0); state = Wait; while (1) { switch (state) { case Wait: break; case Scan: key = scanKeypad(); state = MoveCursor; break; case MoveCursor: if(count == 0) moveCursorLCD(0,0); else if (count == 9) moveCursorLCD(1,0); state = Print; break; case debounce1: delayUs(500); state = Scan; break; case debounce2: delayUs(500); state = MoveCursor; break; case Print: delayUs(100); if(key == 0) printCharLCD('0'); else if(key == 1) printCharLCD('1'); else if(key == 2) printCharLCD('2'); else if(key == 3) printCharLCD('3'); else if(key == 4) printCharLCD('4'); else if(key == 5) printCharLCD('5'); else if(key == 6) printCharLCD('6'); else if(key == 7) printCharLCD('7'); else if(key == 8) printCharLCD('8'); else if(key == 9) printCharLCD('9'); else if(key == 10) printCharLCD('*'); else if(key == 11) printCharLCD('#'); state = Wait; break; } } return 0; }