/************************************************************************************************ #pragma config OSC = HS //oscillator type is HS #pragma config PWRT = ON, BOR = OFF //power-up timer is on, brown-out detect is off #pragma config WDT = OFF //watchdog timer is off Clock is 4MHz Configuration Word all default: crystal oscillator (XT), power-up timer on, brown-out detect off, WDT off, LV Program disabled*/ //************************************************************************************************ //************************************************************************************************ // include header files #include #include //header file for delays #include //header file for Timers #include //header file for PWM //************************************************************************************************ //************************************************************************************************ /*function prototypes, reproduced from Header Files for information void OpenADC (unsigned char, unsigned char); void SetChanADC (unsigned char); void ConvertADC(void); char BusyADC(void); int ReadADC(void); void OpenTimer2 (unsigned char); void Delay10KTCYx (unsigned char); void Delay10TCYx (unsigned char); */ //************************************************************************************************ //************************************************************************************************ //User-defined function prototypes void init (void); //************************************************************************************************ //************************************************************************************************ //Declare Variables int light; //light sensor value int temp; //temp sensor value //************************************************************************************************ //************************************************************************************************ //Main Program void main (void) { init(); //call init function while (1) { SetChanADC (ADC_CH0); //Read and store light sensor value, channel 0 Delay10TCYx (2); //delay for 20us approx acquisition time ConvertADC(); while (BusyADC()); //wait for conversion to complete light = ReadADC()&0x03FF; // read it, AND out unwanted bits SetChanADC (ADC_CH1); //Read and store temp sensor value, channel 1 Delay10TCYx (2); //delay for 20us approx acquisition time ConvertADC(); while (BusyADC()); temp = ReadADC()&0x03FF; // read it, AND out unwanted bits if (PORTBbits.RB4 == 0) //Test right uswitch PORTBbits.RB5 = 0; if (PORTBbits.RB4 == 1) PORTBbits.RB5 = 1; Delay10KTCYx (10); }//end of while } //end of main //************************************************************************************************ //************************************************************************************************ //User Define functions void init (void) { /*Initialises SFRs, and sets initial outputs. All unused port bits set to output. Used bits are identified.*/ TRISA = 0b00000111; //All bits output,except 0 & 1 & 3 used for sensors inputs. TRISB = 0b00010000; //Bit 4 (microswitches) only input, //Switch all outputs off PORTA = 0; PORTB = 0; //Enable ADC. Port A bits 0,1,3 are analog input, internal reference,right justify result OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_3ANA_0REF,ADC_CH0 & ADC_INT_OFF); //************************************************************************************************ // OpenTimer2 (TIMER_INT_ON & T2_PS_1_1 & T2_POST_1_1); // Define the interrupt vector to be at 0004h // #pragma code int_vector=0x04 // void interrupt (void) // { // _asm GOTO timer2_isr _endasm //jump to ISR // } //************************************************************************************************