/* main function */ int main() { OSCSetPBDIV (OSC_PB_DIV_1); //configure PBDIV so PBCLK = SYSCLK initIntGlobal(); initPort(); initCN(); initPWM(); while (1); }
/* * Put bus in the idle state. This should be done as soon as possible to prevent * pins in unknown states from interfering with bus operations. The Master should probably * wait a few cycles upon power-up to allow all Slaves to release the bus before trying to * use it. */ void initBus() { /* Put everything in high-impedance state */ freeBus(); TRIS_RW = TRIS_IN; TRIS_REQ = TRIS_IN; TRIS_AKN = TRIS_IN; initCN(); }
int main() { first=1; start=0; int k=0; for (k=0;k<=7;k++) {ref_distance1[k]=0;ref_distance2[k]=0;cur_distance1[k]=0;cur_distance2[k]=0;} initIntGlobal(); initCN(); initTimer2(); initTimer4(); initPort(); LCD_init(); while(1) { while(start) { int i=0,j=0; for (i=0;i<=7;i++) { for (j=0;j<32;j++) { PORTDCLR = 0x0002; PORTDSET = 0x0010; DelayMotor(); PORTDCLR = 0x0010; PORTDSET = 0x0008; DelayMotor(); PORTDCLR = 0x0008; PORTDSET = 0x0004; DelayMotor(); PORTDCLR = 0x0004; PORTDSET = 0x0002; DelayMotor(); } TMR2=0; TMR4=0; PORTDSET=0x0021; DelayUsec(15); PORTDCLR=0x0021; DelayMsec(30); DisplayTMR2(); DisplayTMR4(); if (!first) {cur_distance1[i]=TMR2; cur_distance2[i]=TMR4;} else {ref_distance1[i]=TMR2;ref_distance2[i]=TMR4; cur_distance1[i]=TMR2; cur_distance2[i]=TMR4;} if (cur_distance1[i]-ref_distance1[i]>300 || ref_distance1[i]-cur_distance1[i]>300 ||cur_distance2[i]-ref_distance2[i]>300 || ref_distance2[i]-cur_distance2[i]>300) {PORTDbits.RD7=1;} DelayMsec(20); PORTDbits.RD7=0; } first=0; for (i=0;i<=7;i++) { for (j=0;j<32;j++) { PORTDCLR = 0x0010; PORTDSET = 0x0002; DelayMotor(); PORTDCLR = 0x0002; PORTDSET = 0x0004; DelayMotor(); PORTDCLR = 0x0004; PORTDSET = 0x0008; DelayMotor(); PORTDCLR = 0x0008; PORTDSET = 0x0010; DelayMotor(); } TMR2=0; TMR4=0; PORTDSET=0x0021; DelayUsec(15); PORTDCLR=0x0021; DelayMsec(30); DisplayTMR2(); DisplayTMR4(); cur_distance1[7-i]=TMR2; cur_distance2[7-i]=TMR4; if (cur_distance1[7-i]-ref_distance1[i]>300 || ref_distance1[i]-cur_distance1[7-i]>300 ||cur_distance2[7-i]-ref_distance2[i]>300 || ref_distance2[i]-cur_distance2[7-i]>300) {PORTDbits.RD7=1;} DelayMsec(20); PORTDbits.RD7=0; } } } }
int main() { initPWM(); a=0;b=0;c=0; a1=0;b1=0;c1=0; start=0; first=1; alm=0; motor_position=0; initIntGlobal(); initCN(); initTimer2(); initTimer4(); initPort(); LCD_init(); while(1) { while (start) { int i=0,j=0; for (i=1;i<=8;i++) { motor_position=i; for (j=0;j<32;j++) { PORTDCLR = 0x0002; PORTDSET = 0x0010; DelayMotor(); PORTDCLR = 0x0010; PORTDSET = 0x0008; DelayMotor(); PORTDCLR = 0x0008; PORTDSET = 0x0004; DelayMotor(); PORTDCLR = 0x0004; PORTDSET = 0x0002; DelayMotor(); } /////////////////////////////////////////////////////////////////////////////////////////// TMR2=0; TMR4=0; PORTDSET=0x0220; DelayUsec(15); PORTDCLR=0x0220; DelayMsec(16); a=TMR2; a1=TMR4; TMR2=0; TMR4=0; PORTDSET=0x0220; DelayUsec(15); PORTDCLR=0x0220; DelayMsec(16); b=TMR2; b1=TMR4; TMR2=0; TMR4=0; PORTDSET=0x0220; DelayUsec(15); PORTDCLR=0x0220; DelayMsec(16); c=TMR2; c1=TMR4; //DisplayTMR2(); //DisplayTMR4(); if ( (a<80000 || a1<80000 || b<80000 || b1<80000 || c<80000 || c1<80000)&& (b-c>800 || c-b>800 || b1-c1>800 || c1-b1>800 || a-b>800 || b-a>800 || a1-b1>800 || b1-a1>800 || a-c>800 || c-a>800 || a1-c1>800 || c1-a1>800 )) {alarm(); break;} //DelayMsec(20); /////////////////////////////////////////////////////////////////////////////////////////// } if (alm) {alm=0;break;} for (i=7;i>=0;i--) { motor_position=i; for (j=0;j<32;j++) { PORTDCLR = 0x0010; PORTDSET = 0x0002; DelayMotor(); PORTDCLR = 0x0002; PORTDSET = 0x0004; DelayMotor(); PORTDCLR = 0x0004; PORTDSET = 0x0008; DelayMotor(); PORTDCLR = 0x0008; PORTDSET = 0x0010; DelayMotor(); } /////////////////////////////////////////////////////////////////////////////////////////// TMR2=0; TMR4=0; PORTDSET=0x0220; DelayUsec(15); PORTDCLR=0x0220; DelayMsec(16); a=TMR2; a1=TMR4; TMR2=0; TMR4=0; PORTDSET=0x0220; DelayUsec(15); PORTDCLR=0x0220; DelayMsec(16); b=TMR2; b1=TMR4; TMR2=0; TMR4=0; PORTDSET=0x0220; DelayUsec(15); PORTDCLR=0x0220; DelayMsec(16); c=TMR2; c1=TMR4; //DisplayTMR2(); //DisplayTMR4(); if ( (a<80000 || a1<80000 || b<80000 || b1<80000 || c<80000 || c1<80000)&& (b-c>800 || c-b>800 || b1-c1>800 || c1-b1>800 || a-b>800 || b-a>800 || a1-b1>800 || b1-a1>800 || a-c>800 || c-a>800 || a1-c1>800 || c1-a1>800 )) {alarm(); break; } //DelayMsec(20); /////////////////////////////////////////////////////////////////////////////////////////// } //if (alm) {alm=0;break;} } } }
int main() { a=0;b=0;c=0; a1=0;b1=0;c1=0; first=1; start=0; int k=0; for (k=0;k<=7;k++) {ref_distance1[k]=0;ref_distance2[k]=0;cur_distance1[k]=0;cur_distance2[k]=0;} initIntGlobal(); initCN(); initTimer2(); initTimer4(); initPort(); LCD_init(); while(1) { while(start) { int i=0,j=0; for (i=1;i<=8;i++) { for (j=0;j<32;j++) { PORTDCLR = 0x0002; PORTDSET = 0x0010; DelayMotor(); PORTDCLR = 0x0010; PORTDSET = 0x0008; DelayMotor(); PORTDCLR = 0x0008; PORTDSET = 0x0004; DelayMotor(); PORTDCLR = 0x0004; PORTDSET = 0x0002; DelayMotor(); } /////////////////////////////////////////////////////////////////////////////////////////// TMR2=0; TMR4=0; PORTDSET=0x0021; DelayUsec(15); PORTDCLR=0x0021; DelayMsec(16); a=TMR2; a1=TMR4; TMR2=0; TMR4=0; PORTDSET=0x0021; DelayUsec(15); PORTDCLR=0x0021; DelayMsec(16); b=TMR2; b1=TMR4; TMR2=0; TMR4=0; PORTDSET=0x0021; DelayUsec(15); PORTDCLR=0x0021; DelayMsec(16); c=TMR2; c1=TMR4; //DisplayTMR2(); //DisplayTMR4(); if ( (a<80000 || a1<80000 || b<80000 || b1<80000 || c<80000 || c1<80000)&& (b-c>500 || c-b>500 || b1-c1>500 || c1-b1>500 || a-b>500 || b-a>500 || a1-b1>500 || b1-a1>500 || a-c>500 || c-a>500 || a1-c1>500 || c1-a1>500 )) {PORTDbits.RD7=1;} DelayMsec(20); PORTDbits.RD7=0; /////////////////////////////////////////////////////////////////////////////////////////// } first=0; for (i=7;i>=0;i--) { for (j=0;j<32;j++) { PORTDCLR = 0x0010; PORTDSET = 0x0002; DelayMotor(); PORTDCLR = 0x0002; PORTDSET = 0x0004; DelayMotor(); PORTDCLR = 0x0004; PORTDSET = 0x0008; DelayMotor(); PORTDCLR = 0x0008; PORTDSET = 0x0010; DelayMotor(); } /////////////////////////////////////////////////////////////////////////////////////////// TMR2=0; TMR4=0; PORTDSET=0x0021; DelayUsec(15); PORTDCLR=0x0021; DelayMsec(16); a=TMR2; a1=TMR4; TMR2=0; TMR4=0; PORTDSET=0x0021; DelayUsec(15); PORTDCLR=0x0021; DelayMsec(16); b=TMR2; b1=TMR4; TMR2=0; TMR4=0; PORTDSET=0x0021; DelayUsec(15); PORTDCLR=0x0021; DelayMsec(16); c=TMR2; c1=TMR4; //DisplayTMR2(); //DisplayTMR4(); if ( (a<80000 || a1<80000 || b<80000 || b1<80000 || c<80000 || c1<80000)&& (b-c>500 || c-b>500 || b1-c1>500 || c1-b1>500 || a-b>500 || b-a>500 || a1-b1>500 || b1-a1>500 || a-c>500 || c-a>500 || a1-c1>500 || c1-a1>500 )) {PORTDbits.RD7=1;} DelayMsec(20); PORTDbits.RD7=0; /////////////////////////////////////////////////////////////////////////////////////////// } } } }