You are on page 1of 7

//LOGIC FOR PIC 1 #include <16F877A.

h> #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) //#byte RCREG = 0x1A //#USE RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7) //used to set up receiver

//(range is 00-FF) (make it 16-bits) (previous was 0x0A00) #define THRESHOLD 200

//function prototypes int ADC_threshold(int);

int main(){

unsigned char rchannel; //receiver channel variable // int dig_input_value; //digital input value variable int thresholdval = 1; //int input_value;

// setup_uart(9600); setup_adc_ports(ALL_ANALOG); //RA0 port is set to analog w/ Vcc as ref voltage to receive source tank sensor data setup_adc(ADC_CLOCK_DIV_32); //use the external 20mhz clock set_adc_channel(0); //sets it to read the analog value of the source tank at pin AN0

delay_ms(20); //allow some delay time in order for the PIC to set up the ADC after initializing the ports/clock

while(1){ // delay_ms(50); // insert a time for however long you want to wait between reading all channels and starting the entire process for(rchannel = 0; rchannel < 8; rchannel++){ // dig_input_value = ADC_threshold(THRESHOLD); //result of the analog to digital conversion compared with the middle threshold value //input_value = input(PIN_B5); if(!ADC_threshold(THRESHOLD)){ //check to see if source tank is low. if it is, don't even poll receiver channels and turn ALL motors off

output_low(PIN_D2); //turn motor 0 off output_low(PIN_D3); //turn motor 1 off output_low(PIN_C4); //turn motor 2 off output_low(PIN_C5); //turn motor 3 off output_low(PIN_D4); //turn motor 4 off output_low(PIN_D5); //turn motor 5 off output_low(PIN_D6); //turn motor 6 off output_low(PIN_D7); //turn motor 7 off

else{ switch(rchannel){ case 0: //set PIC to stay on channel 0 of receiver that has frequency 903.37 Hz using parallel selection output_low(PIN_B0); //PIN CS0 output_low(PIN_B1); //PIN CS1 output_low(PIN_B2); //PIN CS2 delay_ms(22); //delay between reading each receiver channel //fputc(dig_input_value); //thresholdval = RCREG; if(input(PIN_C7) == 0) //right now, this would be thresholdval == 0 //for rs-232 use: UART_REG == 0x00 output_high(PIN_D2); //turn motor 0 on else if(input(PIN_C7) == 1) //UART_REG == 0x01 output_low(PIN_D2); //turn motor 0 off break; case 1: //set PIC to stay on channel 1 of receiver that has frequency 906.37 Hz using parallel selection output_high(PIN_B0); //PIN CS0 output_low(PIN_B1); //PIN CS1 output_low(PIN_B2); //PIN CS2 delay_ms(22); //delay between reading each receiver channel //fputc(dig_input_value);

if(input(PIN_C7) == 0) //right now, this would be thresholdval == 0 //for rs-232 use: UART_REG == 0x00 output_high(PIN_D3); //turn motor 0 on else if(input(PIN_C7) == 1) //UART_REG == 0x01 output_low(PIN_D3); //turn motor 0 off break; case 2: //set PIC to stay on channel 2 of receiver that has frequency 907.37 Hz using parallel selection output_low(PIN_B0); //PIN CS0 output_high(PIN_B1); //PIN CS1 output_low(PIN_B2); //PIN CS2 delay_ms(22); //delay between reading each receiver channel //fputc(dig_input_value); if(input(PIN_C7) == 0) output_high(PIN_C4); //turn motor 2 on else if(input(PIN_C7) == 0) output_low(PIN_C4); //turn motor 2 off break; case 3: //set PIC to stay on channel 3 of receiver that has frequency 909.37 Hz using parallel selection output_high(PIN_B0); //PIN CS0 output_high(PIN_B1); //PIN CS1 output_low(PIN_B2); //PIN CS2 delay_ms(22); //delay between reading each receiver channel // fputc(dig_input_value);

if(input(PIN_C7) == 0) output_high(PIN_C5); //turn motor 3 on else if(input(PIN_C7) == 1) output_low(PIN_C5); //turn motor 3 off break; case 4: //set PIC to stay on channel 4 of receiver that has frequency 912.37 Hz using parallel selection output_low(PIN_B0); //PIN CS0 output_low(PIN_B1); //PIN CS1 output_high(PIN_B2); //PIN CS2 delay_ms(22); //delay between reading each receiver channel //fputc(dig_input_value); if(input(PIN_C7) == 0) output_high(PIN_D4); //turn motor 4 on else if(input(PIN_C7) == 1) output_low(PIN_D4); //turn motor 4 off break; case 5: //set PIC to stay on channel 5 of receiver that has frequency 915.37 Hz using parallel selection output_high(PIN_B0); //PIN CS0 output_low(PIN_B1); //PIN CS1 output_high(PIN_B2); //PIN CS2 delay_ms(22); //delay between reading each receiver channel // fputc(dig_input_value);

if(input(PIN_C7) == 0) output_high(PIN_D5); //turn motor 5 on else if(input(PIN_C7) == 1) output_low(PIN_D5); //turn motor 5 off break; case 6: //set PIC to stay on channel 6 of receiver that has frequency 919.37 Hz using parallel selection output_low(PIN_B0); //PIN CS0 output_high(PIN_B1); //PIN CS1 output_high(PIN_B2); //PIN CS2 delay_ms(22); //delay between reading each receiver channel // fputc(dig_input_value); if(input(PIN_C7) == 0) output_high(PIN_D6); //turn motor 6 on else if(input(PIN_C7) == 1) output_low(PIN_D6); //turn motor 6 off break; case 7: //set PIC to stay on channel 7 of receiver that has frequency 921.37 Hz using parallel selection output_high(PIN_B0); //PIN CS0 output_high(PIN_B1); //PIN CS1 output_high(PIN_B2); //PIN CS2 delay_ms(22); //delay between reading each receiver channel // fputc(dig_input_value);

if(input(PIN_C7) == 0) output_high(PIN_D7); //turn motor 7 on else if(input(PIN_C7) == 1) output_low(PIN_D7); //turn motor 7 off break; } //end of switch statements } //end of else statement

} //end of for loop

} //end of while loop

return 0; }//end of main loop

//Returns 0 if the current value is below the parameter theshold value. 1 otherwise int ADC_threshold(int thresh){ //unsigned char makes it an 16-bit value, int makes the threshold variable a 16-bit value if(read_adc() < thresh) //compare the converted analog value to the middle value in hex between 0v and 5v return 0; //sets the variable to which it returns to a digital low else return 1; //sets the variable to which it returns to a digital high }

You might also like