Sei sulla pagina 1di 9

/******************************************************************************* ************ THIS PROGRAM GET THE DATA FROM THE SW1(UP FOR ON, DOWN FOR OFF)

& SW2(UP FOR FOR WARD, DOWN FOR REVERSE) AND RORATE THE TRAIN. ALSO GET THE DATA FROM THE SENSOR AND PUT THE SIG NALS. ******************************************************************************** ***********/ #include <16F877.H> #include <stdio.h> #use delay(clock=20000000) #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7) #use I2C(MASTER,sda=PIN_C4,scl=PIN_C3) unsigned char data,a,b,c; unsigned char ls0[]={0x55,0x54,0x50,0x40,0x00,0x00,0x01,0x05,0x15,0x55}; //led0-led3 selector for forward direction. unsigned char ls1[]={0x55,0x15,0x05,0x01,0x00,0x00,0x40,0x50,0x54,0x55}; //led0-led3 selector for reverse direction. unsigned char sel[]={0x0c0,0x0c2,0xc4,0xc6,0xc8,0xca,0xcc,0xce,0x0c0}; unsigned char sel1[]={0x0ce,0xcc,0xca,0xc8,0xc6,0xc4,0xc2,0x0c0,0xce}; unsigned char i,i1,j,k=0x15,senout=0x00,senout1=0x00,senout2,senout3,buzzon=0; void void void void void void void void void void void void void void crossingon(); crossingoff(); init(); initbuf(); sensor(); sensor1(); station1forward(); station2forward(); station1reverse(); station2reverse(); sw(); sw1(); reverse(); forward();

void main() { init(); initbuf(); while(1) { start1: for(j=0x00;j<0x09;j++) { i1=0x05; for(i=0x00;i<0x09;i++) { start: if(i<=0x04) { i2c_start();

i2c_write(sel[j]); //write the address for the device selection. i2c_write(k); //write the register address. i2c_write(ls0[i]); //write the data for lso register. i2c_write(ls0[i1]); //write the data for ls1 register. i1++; printf("Rotate one movement here\n"); } if(j==0x08) //start the next rotation in the train. goto start1; stoptrain: sw(); //read t he data from the buffer. // if(c != 0x0) //check for sw1 & sw2 is high. // goto stoptrain; if(i>=0x05) { i2c_start(); i2c_write(sel[j]); i2c_write(k+1); i2c_write(ls0[i-0x04]); i2c_stop(); i2c_start(); i2c_write(sel[j+1]); i2c_write(k); i2c_write(ls0[i1-0x04]); i1++; printf("Rotate one movement here\n"); } i2c_stop(); stoptrain1: sw(); he data from the buffer. // & sw2 is high. // if((c != 0x0)) goto stoptrain1; sensor(); if(senout == 0x00) goto stop1; senout++; if(senout < 0x06) delay_ms(200); else if(senout == 0x06) the station1. { delay_ms(0x2000); //wait some delay in station1. initbuf(); output_d(0x10); //glow the yellow led for station1 in forward. //read t //check for sw1

//check

output_low(PIN_E2); output_high(PIN_E2); delay_ms(1000); initbuf(); output_d(0x08); //glow the green led for station1 in forward. output_low(PIN_E2); output_high(PIN_E2); delay_ms(100); } if((senout > 0x06) && (senout <=0x0a)) delay_ms(200); stop1: if(senout1 == 0x00) goto stop2; senout1++; if(senout1 < 0x06) delay_ms(200); else if(senout1 == 0x06) //check the station2. { delay_ms(0x2000); //wait some delay in station2. initbuf(); output_d(0x04); //glow the yellow led for station2 in forward. output_low(PIN_E1); output_high(PIN_E1); delay_ms(1000); initbuf(); output_d(0x02); //glow the green led for station2 in forward. output_low(PIN_E1); output_high(PIN_E1); delay_ms(100); } if((senout1 > 0x06) && (senout1 <=0x0a)) delay_ms(200); stop2: if(i != 0x08) delay_ms(200); /* if(buzzon == 0x01) { output_low(PIN_E0); delay_ms(10); output_high(PIN_E0); delay_ms(10); }*/ } } } } void init()

{ for(i=0;i<0x08;i++) { i2c_start(); i2c_write(sel[i]); i2c_write(0x15); i2c_write(0x00); i2c_write(0x00); i2c_stop(); printf("Clear the display here\n"); } } void sensor() { output_low(PIN_B4); a=input_d(); output_high(PIN_B4); b = a; b = b & 0x02; if(b == 0x02) for close the level crossing. { do{ output_low(PIN_B4); a=input_d(); rom the buffer. output_high(PIN_B4); c = a; c = (c & 0x80); }while(c != 0x0); crossingon(); } b = a; b = b & 0x04; if(b == 0x04) for open the level crossing. crossingoff(); b = a; b = b & 0x08; if(b == 0x08) station2forward(); b = a; b = b & 0x20; if(b == 0x20) station1forward(); } void crossingon() { output_d(0xF0); output_low(PIN_E1); output_high(PIN_E1); output_d(0xFF);

//get the data from the buffer.

//sensor3 is set to low(enable)

//get the data f

//sensor4 is set to low(enable)

//sensor5 is set to low(enable).

//sensor1 is set to low(enable).

output_low(PIN_E0); output_high(PIN_E0); output_d(0x00); output_low(PIN_E2); output_high(PIN_E2); buzzon = 0x01; } void crossingoff() { initbuf(); buzzon = 0x0; } void station1forward() { initbuf(); output_d(0x20); in forward. output_low(PIN_E2); output_high(PIN_E2); delay_ms(100); he train in station1. senout=0x01; } void station2forward() { initbuf(); output_d(0x08); n forward. output_low(PIN_E1); output_high(PIN_E1); delay_ms(100); he train in station1. senout1 = 0x01; } void sw() { output_low(PIN_B4); a=input_d(); output_high(PIN_B4); b=a; b= b & 0x40; c=a; c= (c & 0x80); if(b == 0x40) reverse(); } //glow the red led for station1

//decrement the speed for stop t

//glow the red led for statio2 i

//decrement the speed for stop t

//get the data from the buffer.

//check for switch1. //check for switch2.

void reverse() { senout2 = 0x00, senout3 = 0x00; init(); initbuf(); buzzon = 0x0; while(1) { start3: for(j=0x00;j<0x09;j++) { i1=0x05; for(i=0x00;i<0x09;i++) { start4: if(i<=0x04) { i2c_start(); i2c_write(sel1[j]); //write the address for the device selection. i2c_write(0x06); //write the register address. i2c_write(ls1[i]); //write the data for lso register. i2c_start(); i2c_write(sel1[j]); //write the address for the device selection. i2c_write(0x05); //write the register address. i2c_write(ls1[i1]); //write the data for ls1 register. i1++; printf("Rotate one movement here\n"); } if(j==0x08) //start the next rotation in the train. goto start3; stoptrain2: sw1(); //read t he data from the buffer. // if(c != 0x0) //check for sw1 & sw2 is high. // goto stoptrain2; if(i>=0x05) { i2c_start(); i2c_write(sel1[j]); i2c_write(0x05); i2c_write(ls1[i-0x04]); i2c_stop(); i2c_start(); i2c_write(sel1[j+1]); i2c_write(0x06); i2c_write(ls1[i1-0x04]); i1++; printf("Rotate one movement here\n"); }

stoptrain3: sw1(); he data from the buffer. // & sw2 is high. // if((c != 0x0)) goto stoptrain3; sensor1(); if(senout2 == 0x00) goto stop3; senout2++; if(senout2 < 0x06) delay_ms(200); else if(senout2 == 0x06) the station2 in reverse dircetion. { delay_ms(0x2000); //wait some delay in station2 in reverse dirction. initbuf(); output_d(0x80); //glow the yellow led for station2 in reverse. output_low(PIN_E2); output_high(PIN_E2); delay_ms(1000); initbuf(); output_d(0x40); //glow the green led for station2 in reverse. output_low(PIN_E2); output_high(PIN_E2); delay_ms(100); } if((senout2 > 0x06) && (senout2 <=0x0a)) delay_ms(200); stop3: if(senout3 == 0x00) goto stop4; senout3++; if(senout3 < 0x06) delay_ms(200); else if(senout3 == 0x06) //check the station1. { delay_ms(0x2000); //wait some delay in station1. initbuf(); output_d(0x02); //glow the yellow led for station1 in reverse. output_low(PIN_E2); output_high(PIN_E2); delay_ms(1000); //read t //check for sw1

//check

initbuf(); output_d(0x01); //glow the green led for station1 in reverse. output_low(PIN_E2); output_high(PIN_E2); delay_ms(100); } if((senout3 > 0x06) && (senout3 <=0x0a)) delay_ms(200); stop4: if(i != 0x08) delay_ms(200); /* if(buzzon == 0x01) { output_low(PIN_E0); delay_ms(10); output_high(PIN_E0); delay_ms(10); }*/ } } } } void sensor1() { output_low(PIN_B4); a=input_d(); output_high(PIN_B4); b = a; b = b & 0x10; if(b == 0x10) station2reverse(); b = a; b = b & 0x04; if(b == 0x04) for close the level crossing. { do{ output_low(PIN_B4); a=input_d(); rom the buffer. output_high(PIN_B4); c = a; c = (c & 0x80); }while(c != 0x0); crossingon(); } b = a; b = b & 0x02; if(b == 0x02) for open the level crossing. crossingoff(); b = a; b = b & 0x01;

//get the data from the buffer.

//sensor6 is set to low(enable).

//sensor3 is set to low(enable)

//get the data f

//sensor4 is set to low(enable)

if(b == 0x01) station1reverse(); } void station1reverse() { initbuf(); output_d(0x04); in forward. output_low(PIN_E2); output_high(PIN_E2); delay_ms(100); he train in station1. senout3=0x01; } void station2reverse() { initbuf(); output_d(0x01); n forward. output_low(PIN_E1); output_high(PIN_E1); delay_ms(100); he train in station1. senout2=0x01; } void sw1() { output_low(PIN_B4); a=input_d(); output_high(PIN_B4); b=a; b= b & 0x40; c=a; c= c & 0x80; } void initbuf() { output_d(0x00); output_low(PIN_E1); output_high(PIN_E1); output_d(0x00); output_low(PIN_E2); output_high(PIN_E2); output_d(0x00); output_low(PIN_E0); output_high(PIN_E0); }

//sensor2 is set to low(enable).

//glow the red led for station1

//decrement the speed for stop t

//glow the red led for statio2 i

//decrement the speed for stop t

//get the data from the buffer.

//check for switch1. //check for switch2.

//noy glow the station2 signal.

//noy glow the station2 signal.

//noy glow the station2 signal.