/* * File: ubirs_main.c * Author: Denns * * Created on December 23, 2018, 3:42 PM */ #include #include #include #include #define _XTAL_FREQ 20000000 // Setup the PIC #pragma config WDTE = OFF, PWRTE = ON, CP = OFF, BOREN = ON, #pragma DEBUG = OFF, LVP = OFF, CPD = OFF, WRT = HALF, FOSC = HS //functions unsigned int ADC_Read(unsigned char ); void LIGHTS1(void); void LIGHTS2(void); //definitions unsigned int ad_result0; unsigned int ad_result1; unsigned int x; /* * */ void main() { TRISB = 0xFF; TRISA = 0xFF; TRISC = 0x00; nRBPU = 0; //enable pull up resistors on portb ADCON0 = 0x81; ADCON1 = 0x80; //Configure porta all other pins as analog in. while(1) { //__delay_ms(0); // step1_forward(10); ad_result0 = ADC_Read(0); ad_result1 = ADC_Read(1); //PORTB = ad_result1; if(RB0==0) {RC0 = 0; RC1 = 1; x = 30; while(x>1) { LIGHTS1(); __delay_ms(100); LIGHTS2(); __delay_ms(100); x = x-1; } RC1 = 0; } if(RB1==0) {RC0 = 1; RC1 = 0; x = 30; while(x>1) { LIGHTS1(); __delay_ms(100); LIGHTS2(); __delay_ms(100); x = x-1; } RC0 = 0; } if(ad_result0 > ad_result1) {RC0 = 1; RC1 = 0; LIGHTS1(); } if(ad_result0 < ad_result1) {RC0 = 0; RC1 = 1; LIGHTS2(); } } } //a to d converter unsigned int ADC_Read(unsigned char channel) { ADCON0 &= 0xC5;; //Clearing the channel selection bits ADCON0 |= channel<<3; //Setting the channel Bits __delay_ms(2); //Wait time to charge hold capacitor GO_nDONE = 1; //Start A/D Conversion while(GO_nDONE); //Wait for A/D Conversion to complete return ((ADRESH<<8)+ADRESL); //Combine both bytes and returns Result } // LIGHTS void LIGHTS1() { RC4 = 1; RC5 = 0; RC6 = 1; RC7 = 0; } void LIGHTS2() { RC4 = 0; RC5 = 1; RC6 = 0; RC7 = 1; }