//////////////////////////////////////////////////////////////////////////////////////////// // File: mainprogram.c // This code is the main program that is in the PIC. It is basically a Finite State Machine // that controls the different components of the Remote System Restarter. // // //////////////////////////////////////////////////////////////////////////////////////////// #include #include "config.h" #include "serial.c" #include "serio.c" #include "delay.h" #include "i2cmsu.c" #include "sysdefs.h" #include "isd5116.c" // State definitions #define onHookAnswNo 0 // This is the initial state and it monitors for the correct number of rings. #define offHook 1 // #define accessCodeVerify 2 // #define userOptions 3 #define resetForSecurity 4 #define programMenu 5 #define powerOptions 6 #define updateAccessCode 7 #define updateNumberOfRings 8 #define powerUp 9 #define powerDown 10 #define onHookAnswYes 11 #define answYesOrNo 12 #define restart 13 unsigned char state; unsigned int numberOfRings; unsigned char numRingsStored; unsigned int numRingsTemp; char transition = 0; int flag =1; char binaryOne=0; char binaryTwo=0; char binaryThree=0; char binaryFour=0; char digit; char special; unsigned char data; unsigned char passwordEntered[5]; unsigned char passwordEnteredFirst[5]; unsigned char passwordEnteredSecond[5]; unsigned char passwordStored[5]; unsigned char accessCode[5]={3,9,7,0,2}; int num=0; int i; int j; int k; int a; int j=55; int m=55; int n=55; int decision=0; int chances=0; char trys=0; char response; volatile unsigned char interrupt_flag; volatile unsigned int reach=0; void interrupt timer_isr(void){ if (TMR2IF){ reach+=1; TMR2IF = 0; if(reach==563){ interrupt_flag=1; reach=0; } } } char digitEntered( ); main(void){ serial_init(95,1); // 19200 in HSPLL mode, crystal = 7.3728 MHz pcrlf(); // this subroutine prints a newline to the terminal TRISB = 0xFD; // Set RB1 as output and all others as inputs PORTB = 0x00; // turn portb pins off TRISC = 0x02; PORTC = 0x00; state = answYesOrNo; // initialize state to numRings numberOfRings=0; transition=0; // enable I2C // i2c_init(17); // about 400Khz for HSPLL mode, crystal = 7.3728 MHz eeprom_write(0,2); DelayMsRinging(1000); for(a=0;a<5;a++){ data=accessCode[a]; eeprom_write(m,data); DelayMsRinging(1000); m++; } while(1){ switch(state){ case answYesOrNo: printf("State=answYesOrNo");pcrlf(); RB1=0; if(RC1==1){ state=onHookAnswYes; } else state=onHookAnswNo; break; case onHookAnswNo: printf("State=onHookAnswNo");pcrlf(); printf("This is the current one!!!!");pcrlf(); RB1=0; // reset relay numRingsStored = eeprom_read(0); // read number of rings stored at location 0 in EEPROM in PIC printf("Num of Rings stored is %d", numRingsStored);pcrlf(); if(numberOfRings==0){ printf("State=onHookAnswNo"); pcrlf(); } while(RB0){ if(RC1==1){ state=onHookAnswYes; break; } } do{ if(RC1==1){ break; } numberOfRings=numberOfRings+1; printf("Num of Rings %d", numberOfRings);pcrlf(); while(!RB0); // evaluates while low // RBO goes high here if(numberOfRings==numRingsStored) { printf("Cleared cuz it made it to five rings...should go to state offHook");pcrlf(); state = offHook; transition=1; numberOfRings=0; break; } // delay for 4 seconds if(numberOfRings!=numRingsStored){ printf("Delay is taken!!");pcrlf(); DelayMsRinging(4000); } if(RB0){ transition=1; numberOfRings=0; } }while(transition==0); break; case onHookAnswYes: printf("State=onHookAnswYes");pcrlf(); //RB1=0; // reset relay numRingsStored = eeprom_read(0); // read number of rings stored at location 0 in EEPROM in PIC printf("Num of Rings stored is %d", numRingsStored);pcrlf(); if(numberOfRings==0){ printf("State=onHookAnswYes"); pcrlf(); } while(RB0){ if(RC1==0){ state=onHookAnswNo; break; } } do{ if(RC1==0){ break; } numberOfRings=numberOfRings+1; printf("Num of Rings %d", numberOfRings);pcrlf(); while(!RB0); // evaluates while low // RBO goes high here if(numberOfRings==numRingsStored) { printf("Cleared cuz it made it to five rings...should go to state offHook");pcrlf(); //state = accessCodeVerify; DelayMsRinging(2000); RC0=1; transition=1; numberOfRings=0; break; } // delay for 4 seconds if(numberOfRings!=numRingsStored){ printf("Delay is taken!!");pcrlf(); DelayMsRinging(4000); } if(RB0){ transition=1; numberOfRings=0; } }while(transition==0); j=55; if(num==0){ for(i=0;i<5;i++){ passwordStored[i] = eeprom_read(j); //DelayMsRinging(1000); printf("The array's position %d has the value: %d and is stored in location: %d",i, passwordStored[i],j);pcrlf(); j++; } } // configure timer 2 // post scale of 16, prescale of 16, PR2 =255 TOUTPS3 = 1; TOUTPS2 = 1; TOUTPS1 = 1; TOUTPS0 = 1; T2CKPS1 = 1; // pre scale of 16 PR2 = 255; // enable interrupts IPEN = 0; TMR2IF = 0; TMR2IE = 1; PEIE = 1; GIE = 1; // start timer 2 TMR2ON = 1 ; interrupt_flag = 0; //clear semaphore while(num<5){ while(!RB6 && !interrupt_flag); if(interrupt_flag){ interrupt_flag=0; TMR2ON=0; printf("Five seconds is up!!!!!!!!!!!!!");pcrlf(); num=6; } TMR2ON=0; // turn off timer 2 printf("reach= %d", reach);pcrlf(); reach=0; // clear reach TMR2ON=1; // turn on timer 2 interrupt_flag=0; // clear interrupt flag //while(!RB6); // This delay of 100 mili seconds gives the PIC time to detect the new output of the dtmf decoder DelayMs(100); response=digitEntered( ); printf("You pressed: %d", response);pcrlf();pcrlf(); passwordEntered[num]=response; printf("The array's position %d has the value: %d",num, passwordEntered[num]);pcrlf(); num=num+1; } printf("You entered the following password:");pcrlf(); while(i<5){ printf("%d: You pressed: %d",i, passwordEntered[i]);pcrlf(); i++; } i=0; printf("The array's position 4 has the value: %d", passwordEntered[4]);pcrlf(); printf("You MADE IT!!");pcrlf(); TMR2ON=0; if(num==7){ state=answYesOrNo; printf("num=6");pcrlf(); } num=0; for(j=0;j<5;j++){ if(passwordEntered[j]!=passwordStored[j]){ decision=1; } } if(decision==1){ printf("access codes don't match");pcrlf(); num=0; decision=0; state=answYesOrNo; } else{ printf("access codes match!!!!!!");pcrlf(); chances=0; for(k=0;k<5;k++) { passwordEntered[k]=0; } response=0; num=0; decision=0; state=offHook; } RC0=0; transition=0; break; case offHook: printf("State=offHook");pcrlf(); RB1=1; // RB1 turn on and make relay put RSR in offHook position state = accessCodeVerify; break; case accessCodeVerify: printf("State=accessCodeVerify");pcrlf(); printf("I'm working!!!!"); pcrlf(); TMR2ON = 0; // make sure timer2 is off j=55; if(num==0){ for(i=0;i<5;i++){ passwordStored[i] = eeprom_read(j); //DelayMsRinging(1000); printf("The array's position %d has the value: %d and is stored in location: %d",i, passwordStored[i],j);pcrlf(); j++; } } isd_play_address(0); DelayMsRinging(3400); isd_stop(); // configure timer 2 // post scale of 16, prescale of 16, PR2 =255 TOUTPS3 = 1; TOUTPS2 = 1; TOUTPS1 = 1; TOUTPS0 = 1; T2CKPS1 = 1; // pre scale of 16 PR2 = 255; // enable interrupts IPEN = 0; TMR2IF = 0; TMR2IE = 1; PEIE = 1; GIE = 1; // start timer 2 TMR2ON = 1 ; interrupt_flag = 0; //clear semaphore while(num<5){ while(!RB6 && !interrupt_flag); if(interrupt_flag){ interrupt_flag=0; TMR2ON=0; printf("Five seconds is up!!!!!!!!!!!!!");pcrlf(); num=6; } TMR2ON=0; // turn off timer 2 printf("reach= %d", reach);pcrlf(); reach=0; // clear reach TMR2ON=1; // turn on timer 2 interrupt_flag=0; // clear interrupt flag //while(!RB6); // This delay of 100 mili seconds gives the PIC time to detect the new output of the dtmf decoder DelayMs(100); response=digitEntered( ); printf("You pressed: %d", response);pcrlf();pcrlf(); passwordEntered[num]=response; printf("The array's position %d has the value: %d",num, passwordEntered[num]);pcrlf(); num=num+1; } printf("You entered the following password:");pcrlf(); while(i<5){ printf("%d: You pressed: %d",i, passwordEntered[i]);pcrlf(); i++; } i=0; printf("The array's position 4 has the value: %d", passwordEntered[4]);pcrlf(); printf("You MADE IT!!");pcrlf(); TMR2ON=0; if(num==7){ state=answYesOrNo; printf("num=6");pcrlf(); } num=0; for(j=0;j<5;j++){ if(passwordEntered[j]!=passwordStored[j]){ decision=1; } } if(decision==1){ printf("access codes don't match");pcrlf(); num=0; decision=0; chances+=1; if(chances==3){ chances=0; state=resetForSecurity; } } else{ printf("access codes match!!!!!!");pcrlf(); chances=0; for(k=0;k<5;k++) { passwordEntered[k]=0; } response=0; num=0; decision=0; state=userOptions; } break; case userOptions: printf("State=userOptions");pcrlf(); isd_play_address(960); DelayMsRinging(6250); isd_stop(); response=0; // configure timer 2 // post scale of 16, prescale of 16, PR2 =255 TOUTPS3 = 1; TOUTPS2 = 1; TOUTPS1 = 1; TOUTPS0 = 1; T2CKPS1 = 1; // pre scale of 16 PR2 = 255; // enable interrupts IPEN = 0; TMR2IF = 0; TMR2IE = 1; PEIE = 1; GIE = 1; // start timer 2 TMR2ON = 1 ; interrupt_flag = 0; //clear semaphore while(!RB6 && !interrupt_flag); if(interrupt_flag){ interrupt_flag=0; TMR2ON=0; printf("Five seconds is up!!!!!!!!!!!!!");pcrlf(); num=6; } TMR2ON=0; printf("reach= %d", reach);pcrlf(); reach=0; TMR2ON=1; interrupt_flag=0; //while(!RB6); // This delay of 100 mili seconds gives the PIC time to detect the new output of the dtmf decoder DelayMs(100); if(num!=6){ response=0; // clear the response variable response=digitEntered( ); printf("You pressed: %d", response);pcrlf();pcrlf(); } if(num==6){ printf("Num equals %d", num);pcrlf();pcrlf(); num=0; state=answYesOrNo; } if(response==1){ response=0; state=programMenu; } if(response==2){ response=0; state=powerOptions; } break; case programMenu: printf("State=programMenu");pcrlf(); isd_play_address(2240); DelayMsRinging(6212); isd_stop(); // configure timer 2 // post scale of 16, prescale of 16, PR2 =255 TOUTPS3 = 1; TOUTPS2 = 1; TOUTPS1 = 1; TOUTPS0 = 1; T2CKPS1 = 1; // pre scale of 16 PR2 = 255; // enable interrupts IPEN = 0; TMR2IF = 0; TMR2IE = 1; PEIE = 1; GIE = 1; // start timer 2 TMR2ON = 1 ; interrupt_flag = 0; //clear semaphore while(!RB6 && !interrupt_flag); if(interrupt_flag){ interrupt_flag=0; TMR2ON=0; printf("Five seconds is up!!!!!!!!!!!!!");pcrlf(); num=6; } TMR2ON=0; printf("reach= %d", reach);pcrlf(); reach=0; TMR2ON=1; interrupt_flag=0; //while(!RB6); // This delay of 100 mili seconds gives the PIC time to detect the new output of the dtmf decoder DelayMs(100); if(num!=6){ response=0; // clear the response variable response=digitEntered( ); printf("You pressed: %d", response);pcrlf();pcrlf(); } if(num==6){ printf("Num equals %d", num);pcrlf();pcrlf(); num=0; state=answYesOrNo; } if(response==1){ response=0; num=0; TMR2ON=0; // turn timer2 off state=updateAccessCode; } if(response==2){ response=0; num=0; TMR2ON=0; // turn timer2 off state=updateNumberOfRings; } break; case updateAccessCode: printf("State=updateAccessCode");pcrlf(); TMR2ON=0; // make sure timer2 is off isd_play_address(4736); DelayMsRinging(2930); isd_stop(); // configure timer 2 // post scale of 16, prescale of 16, PR2 =255 TOUTPS3 = 1; TOUTPS2 = 1; TOUTPS1 = 1; TOUTPS0 = 1; T2CKPS1 = 1; // pre scale of 16 PR2 = 255; // enable interrupts IPEN = 0; TMR2IF = 0; TMR2IE = 1; PEIE = 1; GIE = 1; // start timer 2 TMR2ON = 1 ; interrupt_flag = 0; //clear semaphore while(num<5){ while(!RB6 && !interrupt_flag); if(interrupt_flag){ interrupt_flag=0; TMR2ON=0; printf("Five seconds is up!!!!!!!!!!!!!");pcrlf(); num=6; } TMR2ON=0; // turn off timer 2 printf("reach= %d", reach);pcrlf(); reach=0; // clear reach TMR2ON=1; // turn on timer 2 interrupt_flag=0; // clear interrupt flag //while(!RB6); // This delay of 100 mili seconds gives the PIC time to detect the new output of the dtmf decoder DelayMs(100); response=digitEntered( ); printf("You pressed: %d", response);pcrlf();pcrlf(); passwordEnteredFirst[num]=response; printf("The array's position %d has the value: %d",num, passwordEnteredFirst[num]);pcrlf(); num=num+1; } printf("You entered the following password:");pcrlf(); while(i<5){ printf("%d: You pressed: %d",i, passwordEnteredFirst[i]);pcrlf(); i++; } if(num==7){ state=answYesOrNo; printf("num=6");pcrlf(); } //num=0; else{ i=0; num=0; TMR2ON=0; isd_play_address(5600); DelayMsRinging(4186); isd_stop(); TMR2ON = 1 ; interrupt_flag = 0; //clear semaphore } while(num<5){ while(!RB6 && !interrupt_flag); if(interrupt_flag){ interrupt_flag=0; TMR2ON=0; printf("Five seconds is up!!!!!!!!!!!!!");pcrlf(); num=6; } TMR2ON=0; // turn off timer 2 printf("reach= %d", reach);pcrlf(); reach=0; // clear reach TMR2ON=1; // turn on timer 2 interrupt_flag=0; // clear interrupt flag //while(!RB6); // This delay of 100 mili seconds gives the PIC time to detect the new output of the dtmf decoder DelayMs(100); response=digitEntered( ); printf("You pressed: %d", response);pcrlf();pcrlf(); passwordEnteredSecond[num]=response; printf("The array's position %d has the value: %d",num, passwordEnteredSecond[num]);pcrlf(); num=num+1; } TMR2ON=0; printf("You entered the following password:");pcrlf(); while(i<5){ printf("%d: You pressed: %d",i, passwordEnteredSecond[i]);pcrlf(); i++; } i=0; if(num==7){ state=answYesOrNo; printf("num=6");pcrlf(); } num=0; for(j=0;j<5;j++){ if(passwordEnteredFirst[j]!=passwordEnteredSecond[j]){ decision=1; } } if(decision==1){ printf("access codes don't match");pcrlf(); num=0; decision=0; chances+=1; if(chances==3){ chances=0; state=userOptions; } } else{ printf("access codes match!!!!!!");pcrlf(); chances=0; for(a=0;a<5;a++){ data=passwordEnteredFirst[a]; eeprom_write(n,data); DelayMsRinging(500); n++; } n=0; for(k=0;k<5;k++) { passwordEnteredFirst[k]=0; passwordEnteredSecond[k]=0; } response=0; num=0; decision=0; state=userOptions; } break; case updateNumberOfRings: printf("State=updateNumberOfRings");pcrlf(); isd_play_address(6592); DelayMsRinging(3022); isd_stop(); // configure timer 2 // post scale of 16, prescale of 16, PR2 =255 TOUTPS3 = 1; TOUTPS2 = 1; TOUTPS1 = 1; TOUTPS0 = 1; T2CKPS1 = 1; // pre scale of 16 PR2 = 255; // enable interrupts IPEN = 0; TMR2IF = 0; TMR2IE = 1; PEIE = 1; GIE = 1; // start timer 2 TMR2ON = 1 ; interrupt_flag = 0; //clear semaphore while(!RB6 && !interrupt_flag); if(interrupt_flag){ interrupt_flag=0; TMR2ON=0; printf("Five seconds is up!!!!!!!!!!!!!");pcrlf(); num=6; } TMR2ON=0; printf("reach= %d", reach);pcrlf(); reach=0; TMR2ON=1; interrupt_flag=0; //while(!RB6); // This delay of 100 mili seconds gives the PIC time to detect the new output of the dtmf decoder DelayMs(100); if(num!=6){ // clear the response variable response=digitEntered( ); printf("You pressed: %d", response);pcrlf();pcrlf(); } if(num==6){ printf("Num equals %d", num);pcrlf();pcrlf(); num=0; state=answYesOrNo; } if(response!=0){ eeprom_write(0,response); } response=0; state=userOptions; break; case powerOptions: printf("State=powerOptions");pcrlf(); isd_play_address(3086); DelayMsRinging(6030); isd_stop(); // configure timer 2 // post scale of 16, prescale of 16, PR2 =255 TOUTPS3 = 1; TOUTPS2 = 1; TOUTPS1 = 1; TOUTPS0 = 1; T2CKPS1 = 1; // pre scale of 16 PR2 = 255; // enable interrupts IPEN = 0; TMR2IF = 0; TMR2IE = 1; PEIE = 1; GIE = 1; // start timer 2 TMR2ON = 1 ; interrupt_flag = 0; //clear semaphore while(!RB6 && !interrupt_flag); if(interrupt_flag){ interrupt_flag=0; TMR2ON=0; printf("Five seconds is up!!!!!!!!!!!!!");pcrlf(); num=6; } TMR2ON=0; printf("reach= %d", reach);pcrlf(); reach=0; TMR2ON=1; interrupt_flag=0; //while(!RB6); // This delay of 100 mili seconds gives the PIC time to detect the new output of the dtmf decoder DelayMs(100); if(num!=6){ response=0; // clear the response variable response=digitEntered( ); printf("You pressed: %d", response);pcrlf();pcrlf(); } if(num==6){ printf("Num equals %d", num);pcrlf();pcrlf(); num=0; state=answYesOrNo; } if(response==1){ response=0; num=0; state=restart; } if(response==2){ response=0; num=0; state=powerUp; } if(response==3){ response=0; num=0; state=powerDown; } //state=onHook; break; case powerUp: printf("State=powerUp");pcrlf(); RC2=1; DelayMsRinging(500); RC2=0; state=answYesOrNo; break; case powerDown: printf("State=powerDown");pcrlf(); RC2=1; DelayMsRinging(10000); RC2=0; state=answYesOrNo; break; case restart: printf("State=restart");pcrlf(); RC2=1; DelayMsRinging(10000); RC2=0; DelayMsRinging(2000); RC2=1; DelayMsRinging(500); RC2=0; state=answYesOrNo; break; case resetForSecurity: printf("State=resetForSecurity");pcrlf(); RB1=0; if(!RB7) state=answYesOrNo; break; default: state=answYesOrNo; break; }// end switch }// end while }// end main char digitEntered( ){ if(RB2){ binaryOne=1; } else{ binaryOne=0; } if(RB3){ binaryTwo=1; } else{ binaryTwo=0; } if(RB4){ binaryThree=1; } else{ binaryThree=0; } if(RB5){ binaryFour=1; } else{ binaryFour=0; } while(RB6); printf("binaryOne= %d", binaryOne);pcrlf(); printf("binaryTwo= %d", binaryTwo);pcrlf(); printf("binaryThree= %d", binaryThree);pcrlf(); printf("binaryFour= %d", binaryFour);pcrlf(); pcrlf(); if(binaryFour==1 && binaryThree==0 && binaryTwo==1 && binaryOne==0){ digit=0; } if(binaryFour==0 && binaryThree==0 && binaryTwo==0 && binaryOne==1){ digit=1; } if(binaryFour==0 && binaryThree==0 && binaryTwo==1 && binaryOne==0){ digit=2; } if(binaryFour==0 && binaryThree==0 && binaryTwo==1 && binaryOne==1){ digit=3; } if(binaryFour==0 && binaryThree==1 && binaryTwo==0 && binaryOne==0){ digit=4; } if(binaryFour==0 && binaryThree==1 && binaryTwo==0 && binaryOne==1){ digit=5; } if(binaryFour==0 && binaryThree==1 && binaryTwo==1 && binaryOne==0){ digit=6; } if(binaryFour==0 && binaryThree==1 && binaryTwo==1 && binaryOne==1){ digit=7; } if(binaryFour==1 && binaryThree==0 && binaryTwo==0 && binaryOne==0){ digit=8; } if(binaryFour==1 && binaryThree==0 && binaryTwo==0 && binaryOne==1){ digit=9; } if(binaryFour==1 && binaryThree==0 && binaryTwo==1 && binaryOne==1){ special='*'; digit=(int)special; } if(binaryFour==1 && binaryThree==1 && binaryTwo==0 && binaryOne==0){ special='#'; digit=(int)special; } return digit; }